Just got this new assignment and really don't know where to start. Any help would be great. I'm not a Computer Science major. I have a Bachelors in Graphic Design and am taking this as a pre-requisite for an HTML course over the summer I'm planning to take. Heres the assignemnt:
Write a Program that reads the integers between 1 and 100 and counts the occurrence of each number. Assume the input ends with 0.
**Note** if a number occurs more than one time, the plural word "times" is used in the output.
It's a homework assignment, so I'm not going to show you code, but that said, I can point you in the right direction.
Try using a loop to parse all of the integers entered into a list, use an iterator to page through the list, and count the integers that are in there, then display any values that occur.
#include <iostream>
#include <ctime>
#include "InsertionSort.h"
usingnamespace std;
int main()
{
double list[100]; // Don't use doubles to store int
double integers[100]; // Don't use doubles to store int
int numberOfIntegers = 1;
cout<<"Enter some integers between 1 and 100: "; //Prompts the user to enter integers between 1 and 100
for (int i = 0; i < numberOfIntegers; i++) // This loop will never, ever end.
{
cin>>list[i]; //gets integers from user
numberOfIntegers++;
for (int j = 0; j<100; j++)
{
if (list[i] = j)
integers[j]++;
else
integers[j] = 0; // You just reset your count. The program will only return the very last number counted.
for (int j = 0; j<100; j++)
{
// the following code displays the results
if (integers[j] != 1 && integers[j] != 0){
cout<< integers[i] << " occurs " << integers[j] << " times"<<endl;
break;}
elseif (integers[j] == 1 && integers[j] != 0){ // By definition, if something == 1, then the same thing != 0. This is a redundancy.
cout<< integers[i] << " occurs 1 time"<<endl;
break;}
}
}
}
int pause;
cin>>pause;// Use cin.get();
return 0;
}
He's trying to parse a single line, which means the end of the for loop has to be defined at runtime, and unless he's willing to take argc and argv[] under consideration, there's no way for the compiler (that I know of) to predefine a variable for the max iterations of a for loop.
@ ciphermagi: Do you mean at compile time? You are contridicting yourself right now. That's irrelevant though since a for loop will indeed allow you to use a variable that isn't defined until the program is executed (run time) for the end condition. I do this constantly to iterate through vectors like this for(int i = 0; i < MyVector.size(); i++).
EDIT: Also, as long as you keep in mind that you are comparing chars then his for loop could work.
@ OP: Do you know how to use structures yet? I just did something like this as a personal project and they help keep things straight.