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

  • Context: Comp Sci 
  • Thread starter Thread starter mkienbau
  • Start date Start date
  • Tags Tags
    Arrays C++
Click For Summary
SUMMARY

This discussion focuses on implementing a 2D array in C++ that is filled with random integers within a specified range. The user is guided to create a 10x10 array, request dimensions R and C from the user, and fill the array with random values using the rand() function. To constrain the random values to the range [0, 200], the correct approach is to use rand() % 201. Additionally, the discussion addresses how to print the array elements and suggests using nested loops for both filling and displaying the array contents.

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Familiarity with random number generation using rand()
  • Knowledge of nested loops for array manipulation
  • Basic concepts of input validation in C++
NEXT STEPS
  • Learn how to implement input validation for user inputs in C++
  • Explore the use of rand() % 201 for generating random numbers within a specific range
  • Study the implementation of linear search algorithms in C++
  • Investigate best practices for array handling and memory management in C++
USEFUL FOR

C++ programmers, computer science students, and anyone interested in learning about array manipulation and random number generation in C++.

mkienbau
Messages
12
Reaction score
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
Using rand()%201 would give you numbers from 0-200, inclusive.

When you find an appropriate i and j, you can use count << A[j]; to display the value held in that position.
 
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?
 
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]
 

Similar threads

  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K