- #1
- 12
- 0
Homework Statement
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[j] (where 0 ( i < R and 0 ( j < C) with
random numbers ranging in [0, 200]. I will describe how to use the pseudorandom
number generator.
4. Using nested loops, print out every element A[j] (where 0 ( i < R and 0 ( j < C)
Homework Equations
Also notice that your program needs this number to be in the specified range of [0, 200].
Would mod again be useful here?
The Attempt at a Solution
Code:
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
srand(time(NULL)); //Seed Random Number Generator
int A[10][10];
int R;
int C;
cout<<"Please Enter an integer between 1 and 10: \n"; // Ask for Values of R and C, ensuring both within specified range
cin>>R;
while (R<1 || R>10)
{
cout<<"Please Re-enter an integer between 1 and 10: \n";
cin>>R;
}
cout<<"Please Enter another integer between 1 and 10: \n";
cin>>C;
while (C<1 || C>10)
{
cout<<"Please Re-enter an integer between 1 and 10: \n";
cin>>C;
}
for(int i=0; i<=R; i++) //Nested for loops to fill array with random numbers
{
for(int j=0; j<=C; j++)
{
A[i][j]=rand();
}
}
return 0;
}
So, I'm pretty confident that I have the array filled properly, to the extent of inputing random numbers in each element of the array "A". However, the range is waaaay off, I think the pseudo-random number generator's range is from 0 to 2147483647. I need to cut that back to [0,200]. Any tips on how to approach that using what I'm assuming is being referred to as mod (%)?
Secondly, to output every element of A in the specified range, would I use a linear search, if so, how would I go about taking the found value and outputting it rather than just returning that it was found?
Thanks for any help!