# Need help creating a simple array program.

[b][red]This message was edited by devfer at 2002-10-1 14:8:4[/red][/b][hr]
I am going through my C++ book and I am looking at programming projects to help me get ready for the test. Could someone help me write a basic console application program. Here is what the question asks me. "Write a program to read into an array of positive numbers entered by the user. Use the centennial value -1 to terminate input. You may assume that the user enters fewer than 50 numbers. Your program must determine the number of repetitions of each entry. Then display the output in the tabular form with one column the distinct numbers entered and second column the number of occurences of the corresponding entries. A sample run of the program is:

Enter your numbers terminated by -1: 4 5 2 9 13 4 12 9 13 8 4 -1

4 3
5 1
2 1
9 2
13 2
12 1
8 1
If anyone knows how to do this could you help me out. Thank You

«1

• Since the maximum number of numbers is fifty, declare an array for that:
[code]
int numbers[50];
[/code]

Then read in the numbers until you encouter -1:
[code]
char done = 0;
int index = 0;

while( !done )
{
// READ IN NUMBER
// STORE IN numbers[index++];
// IF NUMBER == -1 DONE = 1;
}
[/code]

Now count all the instances of the numbers. Since only fifty numbers are allowed, only fifty different numbers can occur. Count the numbers. Initialize to zero.
[code]
int occurrences[50];

for ( i = 0; i < 50; i++ )
occurrences[i] = 0;

i = 0;
done = 0;
while( !done )
{
occurrences[number[i++]]++;

// CHECK THE done CONDITION HERE
}
[/code]
• [b][red]This message was edited by devfer at 2002-10-1 15:39:43[/red][/b][hr]
I really appreciate your help. This is just one part of c++ i am lost in. When the user enters the numbers how do you store them in a array. I am really confused. Also when it is equal to -1 you do this right:
if(number = -1)
{
????Then what would you put here????
}

Sorry Im not a very smart programmer i guess.

• [b][red]This message was edited by stober at 2002-10-1 15:49:24[/red][/b][hr]
Here is one way to do it This does not do everything you want it to, but it shows you how to get keyboard entries:
[code]
#include
using namespace std;

#define MAX_COUNT 50

int main(int argc, char**argv)
{
int numbers[MAX_COUNT];
int nEntries = 0;
// initialize numbers with zeros
for(int i = 0; i < MAX_COUNT; i++)
numbers[i] = 0;

int Number;
bool bDone = false;
while(bDone == false){
cout << "enter number: " << nEntries+1 << ": ";
cin >> Number;
if(Number < 0) {
bDone = true;
}
else{
numbers[nEntries] = Number;
nEntries++;
}
}
cout << endl;
for(i = 0; i < nEntries; i++)
cout << numbers[i] << endl;

return 0;

}
[/code]

• In the if brackets you would put the work "break". This stops the iteration of the loop and continues executing the rest of the program.

Modukan
• : In the if brackets you would put the work "break". This stops the iteration of the loop and continues executing the rest of the program.
:
: Modukan
:
So is this right:
while( !done )
{
cout<< "Enter your numbers terminated by:
";
cin>> numbers[index++]
if (numbers = -1 && done = 1)
{
break;
}

• : In the if brackets you would put the work "break". This stops the iteration of the loop and continues executing the rest of the program.
:
: Modukan
:

There are several ways to do it; You suggestion will work too:

[code]
while(1){
cout << "enter number: " << nEntries+1 << ": ";
cin >> Number;
if(Number < 0) {
break;
}
else{
numbers[nEntries] = Number;
nEntries++;
}
}
[/code]
• Yes, exept don't place your ++ incriment in the array [ ]. Place it after the data is already input or before it.

Modukan
• [b][red]This message was edited by devfer at 2002-10-1 17:46:42[/red][/b][hr]

This is what i have so far. This is what the program says to do:

a program to read into an array of positive numbers entered by the user. Use the centennial value -1 to terminate input. You may assume that the user enters fewer than 50 numbers. Your program must determine the number of repetitions of each entry. Then display the output in the tabular form with one column the distinct numbers entered and second column the number of occurences of the corresponding entries. A sample run of the program is:

Enter your numbers terminated by -1: 4 5 2 9 13 4 12 9 13 8 4 -1

4 3
5 1
2 1
9 2
13 2
12 1
8 1

I know I am missing the last part displaying the numbers but am I missing anything in the other parts i can seem to find anything but I don't know:

#include
int main ()
{
int numbers[50];
char done = 0;
int index = 0;

while( !done )
{
cout<< "Enter your numbers terminated by:
"; // READ IN NUMBER
cin>> numbers; // STORE IN numbers[index++];
if (numbers = -1 && done = 1) // IF NUMBER == -1 DONE = 1;
{
break;
}

}
int occurrences[50];

for ( i = 0; i < 50; i++ )
occurrences[i] = 0;

i = 0;
done = 0;
while( !done )
{
occurrences[number[i++]]++;

// CHECK THE done CONDITION HERE
}

• : : In the if brackets you would put the work "break". This stops the iteration of the loop and continues executing the rest of the program.
: :
: : Modukan
: :
: So is this right:
: while( !done )
: {
: cout<< "Enter your numbers terminated by:
";
: cin>> numbers[index++]
: if (numbers = -1 && done = 1)
: {
: break;
: }
:
:

You're close

1. When do you set the value of "done" to 1? (hint: you don't)
2. In the IF statement, what is the value of numbers?
3. Also in the IF statement, you need to use the boolean operator '==' instead of the assignment operator '='.
4. Also in the IF statement, use the logical OR operator '||' instead of the AND operator '&&'

[code]
int done = 0;
int index = 0;
int number = 0;
while( !done )
{
cout<< "Enter your numbers terminated by:
";
cin>> number;
if (number == -1 )
done = 1;
else
{
numbers[index++] = number;
}
[/code]
• is their anything else wrong in the whole program i have here:

#include
int main ()
{
int numbers[50];
char done = 0;
int index = 0;

while( !done )
{
cout << "Enter your numbers terminated by:
";
cin >> numbers;
if (numbers == -1)
{
done =1;

else
{
numbers[index++] = number;
}
}

}
int occurrences[50];

for ( i = 0; i < 50; i++ )
occurrences[i] = 0;

i = 0;
done = 0;
while( !done )
{
occurrences[number[i++]]++;

// CHECK THE done CONDITION HERE
}

• : is their anything else wrong in the whole program i have here:
:
: #include
: int main ()
: {
: int numbers[50];
: char done = 0;
: int index = 0;
:
: while( !done )
: {
: cout << "Enter your numbers terminated by:
";
: cin >> numbers;
: if (numbers == -1)
: {
: done =1;
:
: else
: {
: numbers[index++] = number;
: }
: }
:
:
: }
: int occurrences[50];
:
: for ( i = 0; i < 50; i++ )
: occurrences[i] = 0;
:
: i = 0;
: done = 0;
: while( !done )
: {
: occurrences[number[i++]]++;
:
: // CHECK THE done CONDITION HERE
: }
:
:

Have you tried to compile it yet? I still see one or two mistakes.

• : : is their anything else wrong in the whole program i have here:
: :
: : #include
: : int main ()
: : {
: : int numbers[50];
: : char done = 0;
: : int index = 0;
: :
: : while( !done )
: : {
: : cout << "Enter your numbers terminated by:
";
: : cin >> numbers;
: : if (numbers == -1)
: : {
: : done =1;
: :
: : else
: : {
: : numbers[index++] = number;
: : }
: : }
: :
: :
: : }
: : int occurrences[50];
: :
: : for ( i = 0; i < 50; i++ )
: : occurrences[i] = 0;
: :
: : i = 0;
: : done = 0;
: : while( !done )
: : {
: : occurrences[number[i++]]++;
: :
: : // CHECK THE done CONDITION HERE
: : }
: :
: :
:
:
: Have you tried to compile it yet? I still see one or two mistakes.
:
: i tried compiling it but it gives me all kinds of errors, i know some of them is b/c the program isn't finished but it still doesn't look good. what kind of mistakes are they.

• : : i tried compiling it but it gives me all kinds of errors, i know some of them is b/c the program isn't finished but it still doesn't look good. what kind of mistakes are they.
:
:

Hint: compare your version with the version I posted earlier. Pay close attention to variable names. I know my version compiles with VC6 without errors.

• figured out a couple things still many errors. Is there anything wrong with the second while statement:

int occurrences[50];

for (int i = 0; i < 50; i++ )
occurrences[i] = 0;

i = 0;
done = 0;
while( !done )
{
occurrences[number[i++]]++;

// CHECK THE done CONDITION HERE
}

• : [b][red]This message was edited by devfer at 2002-10-1 17:46:42[/red][/b][hr]
:
: This is what i have so far. This is what the program says to do:
:
: a program to read into an array of positive numbers entered by the user. Use the centennial value -1 to terminate input. You may assume that the user enters fewer than 50 numbers. Your program must determine the number of repetitions of each entry. Then display the output in the tabular form with one column the distinct numbers entered and second column the number of occurences of the corresponding entries. A sample run of the program is:
:
: Enter your numbers terminated by -1: 4 5 2 9 13 4 12 9 13 8 4 -1
:
: 4 3
: 5 1
: 2 1
: 9 2
: 13 2
: 12 1
: 8 1
:
:
: I know I am missing the last part displaying the numbers but am I missing anything in the other parts i can seem to find anything but I don't know:
:
: #include
: int main ()
: {
: int numbers[50];
: char done = 0;
: int index = 0;
:
: while( !done )
: {
: cout<< "Enter your numbers terminated by:
"; // READ IN NUMBER
: cin>> numbers; // STORE IN numbers[index++];
: if (numbers = -1 && done = 1) // IF NUMBER == -1 DONE = 1;
: {
: break;
: }
:
: }
: int occurrences[50];
:
: for ( i = 0; i < 50; i++ )
: occurrences[i] = 0;
:
: i = 0;
: done = 0;
: while( !done )
: {
: occurrences[number[i++]]++;
:
: // CHECK THE done CONDITION HERE
: }
:
:
:
:
:
:
:
:
:
:

Well you still didn't replace the assignment '=' with a comparison '==' like several guys told you to. Second, if you want to store the read in number in the array, you must supply an index. So use cin >> numbers[index] instead of cin >> numbers.

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!