# C++ Help, 2-D Arrays

• Comp Sci

## 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!

## Answers and Replies

Using rand()%201 would give you numbers from 0-200, inclusive.

When you find an appropriate i and j, you can use cout << A[j]; to display the value held in that position.

Thanks!

So, lets say I have this array filled with random integers ranging from [0,200]. How do I implement a linear search?

From what I can find I understand that you'd have to set it up something like this:

Code:
int m;
(m>=0 && m<=R)

int n;
(n>=0 &&  n<=C)

if (A[i]==m)
{
cout<<A[i];
}

else
{
cout<<"No values found.";
}

if (A[j]==n)
{
cout<<A[j];
}

else
{
cout<<"No values found.";
}

This is obviously not right though because when I compile it says i and j are undeclared, and if I pull them out it gives 2 errors that are the same:

error C2440: '==' : cannot convert from 'int [10][10]' to 'int'

Any thoughts, or suggestions?

From what I can find I understand that you'd have to set it up something like this:

Code:
int m;
(m>=0 && m<=R)

int n;
(n>=0 &&  n<=C)

You can't assign range values to n and m like you're doing there. What you'll want to do is loop through the array, the same way you did to add the values, except this time the body of the loop will check for the range conditions on i and j. (You don't really need new variables n and m)

For example,

Code:
for i...
for j...
if ( i >=0 && i <= R && j >=0 && j <=C )
cout << A[i][j]