Hello everyone, i am new in this forum and also in C++ programming. I just started learning C++ few months ago from my university then i stuck from some assignment they give me yesterday. I need to count number's occurrences from input data. This is the assignment :

Enter 20 input numbers below :
30, 10, 20, 20, 30, 60, 90, 70, 60, 40, 30,90, 20, 30, 90, 30, 50, 60, 80, 20
a. Sum all the input number.
b. Count the occurrences of each number ! Example :
Number "10" occurrences : 1 time
Number "20" occurrences : 4 times
and so on.

I already reading some reference for counting words but somehow i can't use it to solve this problem. I need your help >.<

Dani AI

Generated

— breaking the work into two steps (sum first, then count) is the right move. was correct to suggest starting with summing to confirm input handling, and 's hint to store inputs in an array makes both tasks straightforward. Two practical options follow: a beginner-friendly array + nested-loops method (only basic control flow) and a cleaner approach using std::map (shorter, scales better).

A simple array approach (easy to read and understand):

#include <iostream>

int main() {
    const int N = 20;
    int a[N];
    for (int i = 0; i < N; ++i)
        std::cin >> a[i];

    int sum = 0;
    for (int i = 0; i < N; ++i)
        sum += a[i];

    bool seen[N] = { false };
    for (int i = 0; i < N; ++i) {
        if (seen[i]) continue;
        int count = 1;
        for (int j = i + 1; j < N; ++j)
            if (a[j] == a[i]) { ++count; seen[j] = true; }
        std::cout << "Number " << a[i] << " occurs " << count << " times\n";
    }
    std::cout << "Sum = " << sum << '\n';
    return 0;
}

A compact, idiomatic solution using std::map:

#include <iostream>
#include <map>

int main() {
    const int N = 20;
    std::map<int,int> freq;
    int x, sum = 0;
    for (int i = 0; i < N; ++i) { std::cin >> x; ++freq[x]; sum += x; }

    for (std::map<int,int>::const_iterator it = freq.begin(); it != freq.end(); ++it)
        std::cout << "Number " << it->first << " occurs " << it->second << " times\n";

    std::cout << "Sum = " << sum << '\n';
    return 0;
}

Notes and pitfalls: the array method is O(N^2) but is easiest for beginners to understand; the std::map method is O(N log K) and prints keys sorted. If input is comma-separated, the extraction operator >> will not skip commas—either enter numbers separated by whitespace or read a whole line and parse it (for example with std::getline + std::stringstream) before counting. Practicing loops and arrays first will make the std::map approach much clearer.

Recommended Answers

All 3 Replies

Try posting an attempt. You may want to store the input in an array and then sum the elements of the array with a loop. counting the numbers will be the next step. Post the sumation code first.

hmm an attempt....honestly, i really don't know how to write the code because functions that I know only conditional IF and increment and decrement for now. It really help me if you can show me some example of it.

hmm an attempt....honestly, i really don't know how to write the code because functions that I know only conditional IF and increment and decrement for now. It really help me if you can show me some example of it.

Focus on the easier part first, the summing of the numbers. Don't worry about the counting yet, which is the harder part. Whether you can do the summing part will help tell us where you are. And regarding the counting part, how familiar are you with arrays?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.