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

Recommended Answers

All 3 Replies

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

You have done two main things wrong:

  1. 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.

  2. 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.

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).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.