I need help with the random number generator- my range is -250 to 250, and i think i got the right format on that, but when i run the program, i get some garbage number, can someone help explain to me why this is, or what i have done wrong in my program?
also, can someone give me an idea as how to find the max value of the positive random integers- i think i might need a loop to do that but im not quit sure how to go about that. any help is greatly appreciated, thanks in advance!!
Code://************************************************************************* // This program prompts the user to enter 'N', the number of random numbers // to generate and then prints these numbers to a file and returns them to // the main program. It also prints out the with the average of all the // negative integers and the maxiumum of the positive random integers. //******************************************************************** #include <iostream> #include <fstream> #include <cstdlib> using namespace std; ifstream inData("randNumbers.txt", ios::in); ofstream outData("randNumbers.txt", ios::out); int nRandomNumbers(int N); //declare all prototypes int main () { int N, average, max, neg_avg, pos_max; cout<<"Enter the value of N: "; cin>>N; average= nRandomNumbers(neg_avg); max= nRandomNumbers(pos_max); cout<<"The average of all negative integers is: "<<average<<endl; cout<<"The maximum of all positive integers is: "<<max<<endl; return 0; } int nRandomNumbers(int N) { int i, randm; int neg_sum=0; int avg_neg, max_pos; for (i=0; i<N; i++) randm= (-250)+ rand()%451; outData<<randm; cout<<"The "<< N <<" random integers are: "<<randm<<" , "<<endl; for (i=0; i<N; i++) { neg_sum++; } if (randm<0) { avg_neg= neg_sum/N; return avg_neg; } //if (randm>0) //{ // for( }



LinkBack URL
About LinkBacks



I used to be an adventurer like you... then I took an arrow to the knee.