How to Correctly Implement a 2D Array with Random Values in C++?

  • Comp Sci
  • Thread starter mkienbau
  • Start date
  • Tags
    Arrays C++
In summary: I'll leave it to you to fill in the details. But remember, like you did with the rand call, you need to loop through the array and check the range for EACH element. In summary, the program creates a 2D array of 100 integers and asks the user for two integers within a specified range. Using nested loops, the array is filled with random numbers within a specified range. The pseudorandom number generator is used to generate the numbers. To output every element within the specified range, a linear search is implemented by looping through the array and checking the range for each element. The found values are then displayed using cout.
  • #1
mkienbau
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!
 
Physics news on Phys.org
  • #2
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.
 
  • #3
Thanks!

So, let's 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?
 
  • #4
mkienbau said:
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]
 

Related to How to Correctly Implement a 2D Array with Random Values in C++?

1. How do I declare a 2-D array in C++?

To declare a 2-D array in C++, you can use the following syntax:
type arrayName[rowSize][colSize];
For example, to declare a 2-D integer array with 3 rows and 4 columns, you can use:
int myArray[3][4];

2. How do I access elements in a 2-D array?

To access elements in a 2-D array, you can use the following syntax:
arrayName[rowIndex][colIndex];
For example, to access the element at the second row and third column in the array declared above, you can use:
myArray[1][2]; //since indexing starts at 0

3. How do I initialize a 2-D array in C++?

To initialize a 2-D array in C++, you can use nested for loops or brace initialization.
For nested for loops, you can use the following syntax:
for(int i = 0; i < rowSize; i++){
 for(int j = 0; j < colSize; j++){
  arrayName[i][j] = value;
 }
}
For brace initialization, you can use the following syntax:
type arrayName[rowSize][colSize] = {{value1, value2, ...}, {value3, value4, ...}, ...};
For example, to initialize the 2-D array declared above with values 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, and 12, you can use:
int myArray[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};

4. Can I pass a 2-D array as a function parameter in C++?

Yes, you can pass a 2-D array as a function parameter in C++.
To do so, you need to specify the size of the second dimension in the function parameter.
For example, to pass the 2-D array declared above to a function, you can use:
void myFunction(int arr[][4]){
 //function code goes here
}
myFunction(myArray);

5. How can I dynamically allocate a 2-D array in C++?

To dynamically allocate a 2-D array in C++, you can use the new operator.
You will need to specify the row and column sizes and use a double pointer to store the address of the array.
For example, to dynamically allocate a 2-D array with 3 rows and 4 columns, you can use:
int** myArray = new int*[3];
for(int i = 0; i < 3; i++){
 myArray[i] = new int[4];
}
//don't forget to free the memory after use
for(int i = 0; i < 3; i++){
 delete [] myArray[i];
}
delete [] myArray;

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
960
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
Back
Top