|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread |
Rating:
|
Display Modes |
|
|
#1 |
|
Member (4 bit)
Join Date: Dec 2004
Posts: 8
|
C++ arrays help
for this program i need to have a user enter a certain amount of numbers, and have the program print how many times each number was entered. i have to use an array......how do i get it to print how many times each number was entered? it should work like this.....say i enter 2,1,6,3,7,7,2,4,5,.....the program should print
1 was entered 1 time, 2 was entered 3 times, and so on. heres what i have done so far. #include int main() { int number[ 100 ]; int x; int num; while ( num != 0 ); { for ( x = 1; x <= 100; x++ ) { std::cout << "Enter a number\n"; std::cin >> num; number[ x ] = num; } } return 0; } |
|
|
|
|
|
#2 |
|
Member (10 bit)
Join Date: Jul 2002
Location: University of California, Santa Barbara
Posts: 800
|
Is there a bound on the number that can be input, ie 0-100? If so, make an array of a size that will encompass the range of numbers taht can be input, say int foo[100], and use a for loop to go through the array the user input, and each time number 'i' comes up you increment foo[i] (this all means you will need to initialize foo with all zeros).
So you go through user input array and come to a 3, that means you increment foo[3]. One more thing... it looks like you'll get an error with that program because the user can theoretically input numbers until x == 100, but when that happens you will be accessing number[100], and an array of size 100 has elements 0-99. So change it to "for(x = 0; x < 100; x++)". Last edited by mattg2k4; 12-05-2004 at 04:53 PM. |
|
|
|
|
|
#3 |
|
Member (7 bit)
|
try this
Try This, I've made it so you can tell the amount of times larger numbers repeate like 333 or 23 or error.
Code:
#include |
|
|
|
|
|
#4 |
|
Member (4 bit)
Join Date: Dec 2004
Posts: 8
|
ok im not sure how to make the program print how many times a certain number was entered...how do u make the program check the array to see how many time each number was entered.....makes no sense to me.
|
|
|
|
|
|
#5 |
|
Member (7 bit)
|
its in the findNumber function
The array that I made is a 2 dimensional array. When the person enters a number it goes into the following code.
Back to the 2 dimensional array.This number[a][0] is a variable for the array the box where the letter "a" is for the row number to store the information the second box is for the column. now the for loop replaces the "a" with an incremented number this determines where the number was stored. The if statement checks weather or not their are more than one of a number and if their isnt more than one then it assigns it a number one in the second column of the array. Which brings me back to the array really quickly the first column of the array stores the number and the second column stores the amount of times it appears. Code:
for(int a =0; a < 99; a++)
{
if(number[a][0] == InputNumber)
{
number[a][1] = number[a][1] + 1;
foundit = true;
return number[a][1];
} else if(number[a][0] == NULL)
{
number[a][0] = InputNumber;
number[a][1] = 1;
foundit = true;
return number[a][1];
}
}
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|