CSE202Lab5 - you must “seed” the random number...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 202 Lab Assignment 5 Name your code lab5.cpp. 1. Create a 2D array, A[10][10], of 100 integers. 2. Ask the user for two integers R and C where 0 < R ≤ 10 and 0 < C ≤ 10. 3. Using nested loops, fill every element A[i][j] (where 0 ≤ i < R and 0 ≤ j < C) with random numbers ranging in [0, 200]. I will describe how to use the pseudo- random number generator. 4. Using nested loops, print out every element A[i][j] (where 0 ≤ i < R and 0 ≤ j < C) Pseudo-Random Number Generator To use, first add the following directives to your code: #include <iostream> #include <ctime> #include <cstdlib> using namespace std; Notice the latter two are new files we had not seen before. At the start of your program,
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: you must “seed” the random number generator with an arbitrary integer: int main() { srand(time(NULL)); //seed random number generator Now every time that you call rand(), it will typically return a value in between 0 and 2147483647: cout << rand() << endl; //a random value in [0, 2147483647] cout << rand() << endl; //another random value in [0, 2147483647] return 0; } The above program will output two random numbers. So in your program, treat rand() as a variable or expression (just keep in mind that every time it is called, it gives you a random number. Also notice that your program needs this number to be in the specified range of [0, 200]. Would mod again be useful here?...
View Full Document

This note was uploaded on 10/18/2009 for the course CSE 201 taught by Professor All during the Spring '09 term at Ohio State.

Ask a homework question - tutors are online