-
Number of occurances
I am working on a program for class and could use a bit of guidance. The program is supposed to read in a number, declare and array of that size, have the user fill the array with numbers from 0-100. I have that part down just fine. Next it needs to order the numbers and display the number of times each number appeared. Now you can see I tried to do this by incrementing a[i]++ but i am not sure that will work, or how i would go about outputting that. any advice?
Code:
#include <iostream>
using namespace std;
int main()
{
int sizeOfList=0;
int a[101];
int i = 0;
int j = 0;
cout << "Enter an integer: " << endl;
cin >> sizeOfList;
cout << "Enter an integer in the range 0-100:" << endl;
while (j < sizeOfList) {
cin >> i;
a[i]++;
j++;
}
if (a[i] != 0){
cout << i << endl;
}
}
-
I think the first step would be to read something on dynamic arrays, in the current program if the size of list is greater then 101 then you will get a memory overflow.
-
thanks for pointing that out. let me explain my thinking and maybe i can be pointed in the right direction. Since the user is allowed to input numbers between 0-100. I figured i could use the array to keep track of the occurrences. would this work? also i believe i would need to set all a[i] to 0 for all numbers first. If this is needed, how would i go about doing that?
-
I think you not quite understanding something here...
this code sets HOW MANY numbers are avaliable, not the MAX VALUE of the number being input,
This Code here...
Code:
while (j < sizeOfList) {
cin >> i;
a[i]++;
j++;
}
dosn't actually input any numbers into the array; it just selects the array and goes to the next one without doing anything, you would want something more like
Code:
while (j < sizeOfList) {
cin >> a[j];
j++;
}
or even better...
Code:
for(j =0; j < sizeOfList; j++)
{
cin >> a[j];
}
notice how i is redendent in both cases.
and i'd recomend checking your output at the end of the program so you can see whats going on, add something like
Code:
for(j=0;j < sizeOfList; j++)
{
cout<< a[j] << std::endl ;
}
while(1){}
just to debug and make sure whats going in is whats going out.
in this case you dont need to preset anything to zero.
Hopefully that will help.
-
i understand that a[i] makes an array of size a. I think i am just being unclear of my approach. I was going to use the array a[i] to keep track of the occurrences, not store the input. ie, if the input is 50, 10, 30, 50... a[50]=2 a[10]=1 a[30]=1. Is this not a reasonable way to do that?
thanks for you time matty
-
lol oops I miss read your first post and went running off in the wrong direction,
so if iv'e got it right this time you want to find out how many times the value x occured in an array a[y]?
sorta like this?
Code:
cin >> value;
for(j = 0; j < sizeOfList; j++)
{
if(a[j] == value)
{i ++;}
}
cout << "The value " << value << " occured " << i << " times."
-
haha i am failing at explaining this i guess.. i'll take it slowly. there is two inputs, the first is a int which declares the number of second inputs, the second input is a list of numbers that fall in the range 0-100
ie. input 1 = 5... input 2 = 5,6,25,34,5
the ouput is two columns, the first is the numbers that were entered in the second input and the second is the numbers of times each number appeared.
ie
5 2
6 1
25 1
34 1
-
ok so basicly this is just slightly modifyed from the other code, hopefully iv'e got it this time lol, but this is how I would go about it...
Code:
#include <iostream>
using namespace std;
int main()
{
int sizeOfList=0;
int a[101];
int i = 0;
int j = 0;
int count;
int x;
cout << "Enter an integer: " << endl;
cin >> sizeOfList;
cout << "Enter an integer in the range 0-100:" << endl;
for(x =0; x < sizeOfList; x++)
{
count = 0;
cin >> i;
for(j = 0; j < 100; j++)
{
if(a[j] == i)
{count ++;}
}
cout << i << " ";
cout << count << std::endl;
}
}
-
once again, thanks matty. There is one more aspect I guess i should have made clear. I need to be able to have the user input all the numbers at once(as in uninterupted) and have it perform all the ouput at once at the end. Also, the calculations are wrong.. but i think i can figure that part out with a bit of time...
ie this codes ran
Code:
Enter an integer:
5
Enter an integer in the range 0-100:
5
5was input 0times
4
4was input 1times
3
3was input 0times
2
2was input 0times
1
1was input 8times
I need it to looks like this when run
Code:
Enter an integer:
5
Enter an integer in the range 0-100:
5
4
3
2
1
5was input 0times
4was input 1times
3was input 0times
2was input 0times
1was input 8times
-
to do that i'd change i into and array and have a for loop with the cin part abouve the other loops
like...
Code:
for(x = 0; x < sizeOfList;x++)
{
cin >> i[x];
}
ofcourse you would have to set i as a dynamic array after the first inputing section of the program,
then chage 'i' into 'i[x]' on the rest of the program
-
thanks, i think i can take it from here. you have been of great help!