Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
Old 12-04-2004, 08:54 PM   #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;
}
tominator is offline   Reply With Quote
Old 12-05-2004, 04:51 PM   #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.
mattg2k4 is offline   Reply With Quote
Old 12-05-2004, 05:46 PM   #3
Member (7 bit)
 
Join Date: Nov 2003
Location: LA
Posts: 101
Send a message via AIM to HNPFL
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 
using namespace std;

int findNumber(int); // prototype function
int listNumbers();

int number[100][2]; // declare global array

int main()
{
	int InputNumber;
	
	cout << "To exit loop please enter 999999 (six 9's)" << endl;

	do {
	cout << "Enter Number: ";
	cin >> InputNumber;

	findNumber(InputNumber);

	} while(InputNumber != 999999);

	cout << endl;
	listNumbers();

	system("pause");
return 0;
}

int findNumber(int InputNumber)
{
	int foundit = false; 

	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];
		}


	}

}


int listNumbers()
{

	for(int a=0; a < 99; a++)
	{
		if(number[a][0] == NULL)
		{
			return 0;
		
		} else {

			cout << "number " << number[a][0] << " has been used " << number[a][1] << " times." << endl;
		}
	
	}


}
HNPFL is offline   Reply With Quote
Old 12-05-2004, 08:05 PM   #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.
tominator is offline   Reply With Quote
Old 12-05-2004, 11:28 PM   #5
Member (7 bit)
 
Join Date: Nov 2003
Location: LA
Posts: 101
Send a message via AIM to HNPFL
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];
		}


	}
HNPFL is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 07:13 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2