c++

closed account (4y79216C)
hello everyone can someone help me about array plsss i really need help right now

using one-dimensional array. create a program that will count the number of the same intries in a series number based on the user specific no. to count
EXPECTED output
Enter no.1:2
Enter no.2:1
Enter no.3:1
Enter no4:2
what number to v=count?1
total number 1 is 2

should use a counter
i appretiate those who will answer

this is my code
#include<iostream>
using namespace std;
int main()
{
int n,hn,nc;
int i;
int count;
float totaln;
count=0;

cout<<"ENTER MANY NUMBERS TO INPUT?:"<<endl;
cin>>hn;

for(i=1;i<=hn;i++)
{

cout<<"ENTER NUMBER :"<<i<<":";
cin>>n;}



cout<<"WHAT NUMBER TO COUNT:"<<endl;
cin>>nc ;
count=0;
totaln+=n;

cout<<"THE TOTAL NUMBER IS:"<<totaln;


system("PAUSE");
}
First off, please put your code in code <> brackets. The button for this can be found under the Format box to the right of the text window when creating/editing a thread.

Secondly, you haven't even attempted to implement your array. :( As it stands, your user will enter in "n", "hn" times in a row, but "n" will just be overwritten each time. It is stored nowhere. Also, variables such as "i" that are to be used within a for loop can be completely defined within the for loop itself, unless you need to reference the last value of "i" later on or something. Therefore, your for loop becomes:

1
2
3
4
for(int i=1;i<=hn;i++)
{

}


Within your first for loop you should be using "i" to also place the values within your array (which you haven't declared yet). Technically you'll be using i-1, since you'll want to start at 0, and not 1, since someArray[0] will coincide with the first spot in the array.
susied this is not codding for arrays u should declare first array.......like A[10];..........etc then u use for loop for it
learn tutorial of arrays......
int a[5];
int count=0;
int sum=0;
for(int i=1;i<=5;i++)
{
cout<<"enter number";
cin>>a[i];
sum=sum+a[i];
count++;
}
cout<<count<<endl;
cout<<"array sum="<<a[i];
Last edited on
closed account (4y79216C)
yayks,,, like this

#include<iostream>
using namespace std;
int main()
{

string name[50]={};
int s[]={};
int i;
int hn;
int l;
int k;
int n;
int largest;
int smallest;

float average;
cout<<"ENTER MANY many students?:"<<endl;
cin>>hn;
for(i=1;i<hn;i++)
{
cout<<"ENTER name of students :"<<i<<":" ;
cin>>n;{
cout<<"ENTER SCORE:";
cin>>l;}

for(i=0;i<hn;i++){
if(s[i]>largest)
largest=s[i];

for(i=0;i<hn-1;i++){
if(s[i]<smallest)
smallest=s[i];}

}}

cout<<"THE HIGHEST IS"<<s[i];



system("PAUSE");
}

i don't know how to calculate
string name[50]={};
Code tags please. Also arrays are fixed-size. use std::vector instead.
closed account (4y79216C)
here's my new code right now
#include<iostream>
using namespace std;
int main()
{

int a[]={};
int count=0;
int sum=0;
int i,hn,w;

cout<<"ENTER MANY NUMBERS TO INPUT?:"<<endl;
cin>>hn;
for(i=1;i<hn;i++)
cout<<"ENTER NUMBER :#"<<i-1;
cin>>a[i];
cout<<"WHAT NUMBER TO CUONT:"<<endl;
cin>>w;
{
sum=sum+a[i];
count++;
}
cout<<count<<endl;
cout<<"TOTAL:"<<a[i];
system("PAUSE");


}

but i think closer
EssGeEich wrote:
Code tags please. Also arrays are fixed-size. use std::vector instead.

Follow, Please.

http://www.cplusplus.com/articles/z13hAqkS/
http://www.cplusplus.com/reference/vector/vector/
closed account (4y79216C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream>
using namespace std;
int main()
{

int a[]={};
int count=0;
int sum=0;
int i,hn,w;

cout<<"ENTER MANY NUMBERS TO INPUT?:"<<endl;
cin>>hn;

for(i=1;i<hn;i++){

cout<<"ENTER NUMBER :#"<<i-1;
cin>>a[i];

cout<<"WHAT NUMBER TO CUONT:"<<endl;
cin>>w;
{
sum=sum+a[i];
count++;
}
cout<<count<<endl;
cout<<"TOTAL:"<<a[i];
system("PAUSE");


}

That program does not need any array or std::vector, it works without it.

How, it is left as an exercise for the OP.
closed account (4y79216C)
but its say using one dimensional
modoran, read it again and try to translate the phrase "Enter many numbers to input?". There is a way not-to-use std::vector I know, but I won't say which one because...
modoran wrote:
...it is left as an exercise for the OP.
Last edited on
I was thinking of something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

int main()
{
    int Count = 0;
    int Numbers = 0;
    int Num = 0;

    cout << "Enter many numbers to input ?" << endl;
    cin >> Numbers;

    for (int i = 0; i < Numbers; i++) {
        cout << "Enter #" << (i+1) << endl;
        cin >> Num;
        Count += Num;
    }

    cout << "The sum of total " << Numbers << " is " << Count << endl;
    return 0;
}
closed account (4y79216C)
ok thanks for the codes.. i change in the last part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

using namespace std;

int main()
{
    int Count = 0;
    int Numbers = 0;
    int Num = 0;
   int w;
    cout << "Enter many numbers to input ?" << endl;
    cin >> Numbers;

    for (int i = 0; i<Numbers; i++) {
        cout << "Enter #" << (i+1) << endl;
        cin >> Num;}
        
    cout<<"WHAT NUMBER TO CUONT:"<<endl;
    cin>>w;
    Count += Num;
    

    cout << "The sum of total " << w << " is " << Count << endl;
    system("PAUSE");

}



but it doesn't work :(
when i enter

Enter many numbers to input ? :2
Enter #1:2
Enter #:2 3
WHAT NUMBER TO CUONT: 5

total number 5 is 6

instead of like this
total number 5 is 0
beacuse i 5 has not belong to what i enter


Last edited on
Sorry, I misunderstood your question. If you want to count a occurence of a number, the code could be this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

using namespace std;

int main()
{
    int Count = 0;
    int Numbers = 0;
    int Num = 0;
    int Search = 0;

    cout << "How many numbers do you want to enter ?" << endl;
    cin >> Numbers;

    cout << "What number occurence to search for ?" << endl;
    cin >> Search;

    for (int i = 0; i < Numbers; i++) {
        cout << "Enter #" << (i+1) << endl;
        cin >> Num;
        if (Num == Search)
            Count++;
    }

    cout << "Total occurences of number '" << Search << "' is " << Count << endl;
    return 0;
}
closed account (4y79216C)
ahhh .. sorry for disturb but its always answer 0. Im trying to understand this but i dont how to be like this(correct answer)
how many want you to enter?: 4
enter #1: 2
enter #2: 2
enter #3: 2
enter #4: 2
What number to be count?: 2
the total number of 2 is "4"
hmmmm
Last edited on
It seems all right to my eyes. Copy-pasted the code and did what you wrote, worked a charm.
closed account (4y79216C)
near to finish
but my problem is the number i will enter will be add instead of searching how many numbers??
Be more detailed - and specific.
closed account (4y79216C)
Thanks for everything and for codes it helps me a lot thank for all your concerned
Topic archived. No new replies allowed.