Tuesday, July 28, 2009

I need help using arrays for c++, in a program.?

So Im supposed to write a program that saves 100 random numbers between 10-50 into an array, and then finds their maximum, minimum, and mean.





So far i have this, and it outputs a number, but I'm stuck on the max,min and mean, I have no ideas.





#include %26lt;iostream%26gt;


#include %26lt;cstdlib%26gt;


using namespace std;





int main()


{ int num1[100];


int i,max,min,mean;


for(i=10;i%26lt;50;i++)


num1[i]=num1[i]+rand();


cout%26lt;%26lt;num1[i]%26lt;%26lt;endl;


return 0;


}





It returns -1 for an integer, Any suggestion would really help, aslo if you spot anything wrong with my program please tell me.


Thanks.

I need help using arrays for c++, in a program.?
There is a simple/inefficient way, and a complex/efficient way.





I'm guessing that you have not studied sorting algorithms, or algorithm analysis, so we will approach the simple/inefficient way.





#include %26lt;iostream%26gt;


#include %26lt;cstdlib%26gt;


#include %26lt;ctime%26gt;





int main() {


int i;


int min;


int max;


float mean;


int num[100];





max = 0;


min = 100;


mean = 0;





srand((unsigned)time(0));





for(i = 0; i %26lt; 100; i++) {


num[i] = (rand() % 40) + 10;


if(num[i] %26gt; max) {


max = num[i];


}


if(num[i] %26lt; min) {


min = num[i];


}


mean += num[i];


}





mean /= 100;





cout %26lt;%26lt; min %26lt;%26lt; endl %26lt;%26lt; max %26lt;%26lt; endl %26lt;%26lt; mean %26lt;%26lt; endl;





return 0;





}





====================


Notes:





1) You are picking 100 random numbers, not 50 - 10 = 40 random numbers





2) rand() %40 + 10 will generate a random number between 10 and 50. rand() generates a number between 0 and N. N modulus(%) 40 returns the remainder of N / 40, so a number between 0 and 40. Add 10 to that and you get a number between 10 and 50.





3) min and max are calculated everytime you generate a new number. if the new number is less than the existing minimum, or the new number is greater than the existing maximum, you update min/max





4) mean is just the average of all numbers generated, so you actually just add them all up then divide by the total amount of numbers (in this case 100) at the end. mean also needs to be a float (floating point number instead of integer) since you will most likely have fractions as in 23.4534





5) return 0; needs to go outside of the for loop and should only return 0 from the main() function once you have completed everything.
Reply:You need to do what is called "seeding" the random number generator. Think of it like you're planting the seed to grow a list of quasi-random numbers. It's good to use the current time for this since it's different every time. Like this:





int nums[50];


srand( (unsigned)time( NULL ) );


for(int i = 0; i %26lt; 50;i++ )


{


//generate number and add to array//


}





To establish a min and max, you could do a little math. Use the remainder function to get results only within your range.





int nThisNum = nMin + rand() % (nMax - nMin);


No comments:

Post a Comment