random numbers

Write a function called nRandomNumbers( ) to performing the following:
• Generates N random integers between -250 and 250 (inclusive) and save them in a file called number.txt, (Use the function srand()).
• Prints all the generated integers.
• Finds the average of all the negative integers, and the maximum of all the positive integers.
• Returns the the average and maximum values.
hey everyone i really need help in writing this program.thnx!!
At least post the code you have so far so we know what kind of help you need...
#include<iostream>

#include<ctime>
using namespace std;
int main()
{
int N,aveN,maxP,a=-250,b=250;

srand((unsigned)time(0));
cout<<"enter the value of N:";
cin>>N;
int nrandomnumbers(N);
cout<<"the 5 random integers are:"<<N<<endl;
cout<<"the average of all negative integers is"<<aveN<<endl;
cout<<"the maximum of all positive integers is"<<maxP<<endl;
for(a=1;a<=5;a++)
{
N=a+rand()%b-a+1;
cout<<N<<endl;

}
return 0;

}
closed account (S6k9GNh0)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<ctime>

int main()
{
	int N, aveN, maxP;
	const int a = -250;
	const int b = 250;

	std::cout << "Enter the value of N: ";
	std::cin >> N;
	int nrandomnumbers(N);

	std::cout << "the 5 random integers are:" << N << std::endl;
	std::cout << "the average of all negative integers is" << aveN <<std::endl;
	std::cout << "the maximum of all positive integers is" << maxP << std::endl;
	 
	srand((unsigned)time(0));
	for(a = 1; a <= 5; a++)
	{
		N = a + rand() % b - a + 1;
		std::cout << N << std::endl;
	}
}


1) You haven't implemented nrandomnumbers(); and I suggest you make the name nRandomNumbers(); like the instructions say, however, some teachers may not be picky.

2) I suggest that you not use using namespace std; like I've suggested so many times before for good habits.

3) You might want to review the return value of rand(). I remember reading something about how using modulo is rather bad.
Last edited on
a + rand() % b - a + 1

reduces to

rand() % b + 1

due to precedence of operations, and this
clearly generates random numbers in the range [1...b].

3) is not a problem if this is just a homework assignment. No instructor will be that picky unless this is for
a statistics class (in which case you'd be told not to use rand() at all).

Last edited on
thnx alot u guys...just one more thing how do i get the average of all negative numbers and the max of all positive numbers?is ther a specific equation?
If i were you I'd declare two variables, avg_neg and max_pos. I would initialize both to zero and then I would add some lines inside the loop where random numbers are generated. If the current number was negative I'd add him to avg_neg. If the current number was positive I would check if it was more than max_pos and if it was I'd set max_pos to the current number. After the loop I'd divide avg_neg with the number of negative numbers... mmm seems to me we need another variable here, let's say count_neg to hold the number of the negative entries... Try to figure out how to do it, it's not very difficult. Also think carefully about the type you are going to use for each one of these variables (int, double etc...)
N = a + rand() % b - a + 1; doesn't give negative value i think.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
    int N, aveN, maxP;
    const int a = -250;
    const int b = 250;
    
    int neg=0, zero=0, pos=0;
    srand((unsigned)time(0));
    for(int i = 1; i <= 100000; i++)
    {
        N = a + rand() % b - a + 1;
        if( N<0 )  neg++;
        if( N>0 )  pos++;
        if( N==0 ) zero++;
    }
    std::cout << "Frequency\n"
              << "Negative: " << neg
              << "\nZero:     " << zero
              << "\nPositive: " << pos
              << '\n';
}
Last edited on
N = a + rand() % b - a + 1;

The 'a's cancel each other out here. This is the same as N = rand() % b + 1

Maybe you meant N = a + rand() % (b - a) + 1; ??
if you're talking to me, yes i already know that. that's what i want to point out to someone who posted that.

anyway, do you have a solution to generating that negative range?
here's mine
1
2
//where a is the negative range
N = rand() % (abs(a)+abs(b)+1) + a;
Yeah I see now that jsmith already said the same thing XD. Sorry, that's what I get for only reading the last post in the thread.

do you have a solution to generating that negative range?


? Why not just use the normal N = a + rand() % (b - a); approach? That works with any range as long as b > a.


b = 251 <- to make it inclusive on the upper bound (or you can do rand % (b-a+1))
a = -250

b-a = 501
rand = [0..500]
+a = [-250..250]
Last edited on
i just realize that my code has the same concept with N = a + rand() % (b - a); there are just some arrangement difference and simplifying the expression :p
i tried to write the code again but it got all mixed up..idk wats wrong??heres my code till now..plz help me its due sunday n im freakin out..
#include<iostream>
#include<iomanip>
#include<ctime>
using namespace std;
int main()
{
int N, aveN, maxP,neg=0,pos=0;
int a = -250;
int b = 251;

std::cout << "Enter the value of N: ";
std::cin >> N;
int nRandomNumbers(N);
srand((unsigned)time(0));

std::cout << "the 5 random integers are:"<<N;

for(a = 1; a <= 5; a++)
{
N =N = rand() % (abs(a)+abs(b)+1) + 1;

cout<<setw(10)<<left<<N;

}

std::cout << "the average of all negative integers is" << aveN <<std::endl;
std::cout << "the maximum of all positive integers is" << maxP << std::endl;



return 0;
}
Let's look at the problem differently.

You need to generate a random number in the range [ -250... 250].

That is exactly 501 different numbers, right?

So if you generate a random number in the range [0...500], that is also exactly
501 different numbers.

You can translate the range [0...500] to [ -250...250] by subtracting 250.
how about now?i feel like thers something missing..plz reply asap...
#include<iostream>
#include<iomanip>
#include<ctime>
using namespace std;
nRandomNumbers(int&x);
void aveN(int&y);
void max(int&h);
int main()
{
int N,maxP;
int a = -250;
int b = 250;

cout << "Enter the value of N: ";
cin >> N;
int nRandomNumbers(N);
cout << "the 5 random integers are:"<<N<<endl;
cout << "the average of all negative integers is" <<aveN <<endl;
cout << "the maximum of all positive integers is" << maxP << endl;

return 0;
}
void nRandomNumbers(int x,int&y,int&h)
{
int N,b,a=0,aveN,maxP;
int neg=0, zero=0, pos=0;
srand((unsigned)time(0));
for(a=N;a<=501;a++)
{
N = a + rand() % (a-b) + 1;

if( N<0 )
{

aveN+=1;
neg++;}

else if( N>0 )
{
max=(aveN/a++);

pos++;

}
}
hey guys,,lsn thnx for ur help in the previous program.but now i need help in writing this program..i really really dont get anything in it...


Write a program that prompts the user to enter three positive integers representing the number of rows, the number of columns of a grid, and the number of mines hidden in the grid, the program then calls the following function:
void Minesweeper(int nRows, int nColumns, int nMines )
The function Minesweeper( ) prints a grid of nRows x nColumns of 0's and 1's. A 0 represents a square that has no mine and a 1 represents a square that has a mine. The parameter nMines represents the number of mines.

Hint: Store the 0's and 1's in a two dimensional array first then print that array.
Hint: Nested for loop
this the code i wrote so far...
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void minesweeper(int nRows, int nColumns, int nMines);
int main ()
{
int nRows,nColumns[]={0},nMines[]={1};
cout<<"Enter the number of rows: ";
cin>>nRows;
cout<<"Enter the number of columns: ";
cin>>nColumns;
cout<<"Enter the number of mines: ";
cin>>nMines;
minesweeper(nRows,nColumns,nMines);
return 0;
}

void minesweeper(int nRows, int nColumns, int nMines)
{

char not_mines;
if (nMines>0)
not_mines=(nRows*nColumns)-nMines;

int mineArray[nRows][nColumns];
for (int i=0; i<nRows; i++)
{
for(int j=0; j<nColumns; j++)
mineArray[i][j]= not_mines;
cout<<setw(5)<<mineArray[i][j];
}
}
i just keep getting errors
closed account (S6k9GNh0)
Use code tags.
Topic archived. No new replies allowed.