954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Random Array Program, finds maximum,minimum,average, and mode

hey, this program is supposed to display 50 random numbers, which is does already, average those numbers, find the maximum,minimum, and mode of the numbers. I haven't started on the mode yet, but i dont see why the other things don't work. Did I initalize it wrong?

#include<string>
#include<ctime>
#include<iostream>

using namespace std;


int main()
{
	int random[50],numcount[60],counter=0,counter2=0,max=0,min=0,average,total=0,mode=0,range=0;

	srand((unsigned)time(0));
	min=random[0];

	for(counter; counter<50; counter++)
	{
		random[counter]=rand()%50 + 1;
		cout<<random[counter]<<endl;
		
	}
	
	for(counter; counter<50; counter++)
	{
		if(max<random[50]);
			max=random[50];

	}

	for(counter; counter<50; counter++)
	{
		if(min>random[50]);
			min=random[50];

	}


	
	
	

	
	
	for(counter; counter<50; counter++)
	
	{
		numcount[random[50]]++;

	}
	
	for(counter; counter<50; counter++)
	{
		total=total+random[50];
	
	}
		average=total%50;
		range=max-min;
	
	
	
	cout<<"Average= "<<average<<endl;
	cout<<"Maximum= "<<max<<endl;
	cout<<"Minimum= "<<min<<endl;
	cout<<"Range= "<<range<<endl;
	cout<<"Mode= "<<mode<<endl;
	
	
	
	system("pause");
	return(0);
}
Topherman
Newbie Poster
1 post since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

You need to remember that an array holds values in its elements from 0 to size-1
in the case of random[50] the values of the numbers can be stored from random[0] to random[49]

for(counter; counter<50; counter++)//did not use counter as the array index
{
if(min>random[50]);//the 50th element is reserved for \0
min=random[50];

}

In your loops you did not try to access all the elements of the array

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

You have done two main things wrong:As Zeroliken said, you're loops are not trying to access all the elements, so something like

for ( counter; counter < 50; counter++ )
{
   if( max < random[50] );
      max=random[50];
}

doesn't do what you want it to. You should use this instead:

for ( counter; counter < 50; counter++)
{
   if ( max < random[counter] );
      max = random[counter];
}

Note that counter is used in the loop, not 50.
You don't reset your counter variable between loops, you should put counter = 0; before each loop for this to work. It's often preferable to declare the loop variable in the for statement itself:

for ( int i = 0; i < iMax; ++i ) {
   // Do things
}

This way you know that there is no interference between your loops :)
I'd also avoid declaring your variables on one line as you have. No-one will thank you for it. Just put one variable on each line, especially when you're initialising them with values or declaring a mixture of single values and arrays.

ravenous
Posting Pro
516 posts since Jul 2005
Reputation Points: 269
Solved Threads: 92
 

At line 13, you're also initializing min to the first (zero'th) element of the array before you've initialized the array. Instead, do the same thing, but immediately before the loop over the elements of the array where you're updating min (lines 29-34 above).

raptr_dflo
Practically a Master Poster
602 posts since Aug 2010
Reputation Points: 76
Solved Threads: 82
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: