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

Discussion Overview

The discussion revolves around implementing a 2D array in C++ that is filled with random integers within a specified range. Participants explore how to correctly generate random numbers, fill the array, and subsequently search for values within it. The scope includes programming concepts and techniques related to arrays, random number generation, and searching algorithms.

Discussion Character

  • Homework-related
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant describes the task of creating a 2D array and filling it with random integers, noting the need for the numbers to be in the range of [0, 200].
  • Another participant suggests using the expression rand()%201 to generate numbers within the desired range.
  • A participant expresses confusion about implementing a linear search in the array and shares an incorrect code snippet that leads to compilation errors.
  • Another participant points out the need to loop through the array to check for values instead of using undeclared variables, suggesting a more appropriate structure for the search logic.

Areas of Agreement / Disagreement

Participants generally agree on the method to generate random numbers within the specified range, but there is no consensus on the correct implementation of the linear search, as different approaches are suggested and some confusion remains.

Contextual Notes

There are unresolved issues regarding the correct implementation of the linear search, including variable scope and the logic for checking values within the array.

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
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K