2 dimensional array

How to generate random numbers in a 2 dimensional array. Searched everywhere to try to teach myself, but found nothing. My teacher kind of sucks =/
yea, I'm way off. don't embarrass me..

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <time.h>
using namespace std;

int main()
{
int r, c;


cout << ("how many rows are there? : ) " << endl;
cin >> r;

cout << ("how many columns are there?: ) " << endl;
cin >> c;

int grade[r][c];

srand (clock());

for (r = 0; r < r; r++)

{
for (c = 0; c < c; c++)
{
grade[r][c] = rand() % r*c;
}

Last edited on
I'm also a beginner at C++ so I might not answer you correctly but I might be able to give some guidance forward.

First, in your for-loops the condition r < r and c < c will cause the loops to exit immediately since 0 is never less than 0. You should use a different variable as the iterator:

for (int iii = 0; iii < r; iii++)

{
for (kkk = 0; kkk < c; kkk++)
{
grade[iii][kkk] = rand() % r*c;
}

Also, I think you need to initialize the rand-function with a seed using the srand-function. This is typically done using a call to time(0) that exist in the ctime-lib. See below

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime> //NEW

using namespace std;

int main()
{
int r, c;


cout << ("how many rows are there? : ) " << endl;
cin >> r;

cout << ("how many columns are there?: ) " << endl;
cin >> c;

int grade[r][c];

srand(time(0)); //NEW - sets the seed used by rand() to system clock

for (int iii = 0; iii < r; iii++)
{
for (kkk = 0; kkk < c; kkk++)
{
grade[iii][kkk] = rand() % r*c;
}
}

I hope this helps
1
2
3
4
5
6
7
cout << ("how many rows are there? : ) " << endl;
cin >> r;

cout << ("how many columns are there?: ) " << endl;
cin >> c;

int grade[r][c];


You can't do this. Arrays must be declared with a size known at compile time.

What you ask for is different from this anyway. I assume you don't know about new, and therefor your teacher expects you to make an array large enough for your needs.

How to generate random numbers in a 2 dimensional array.

You need two things, a 2D array and a random number generator.

A 2D array is created like so:
1
2
3
4
5
6
7
8
9
10
const int WIDTH = 10;
const int DEPTH = 10;

int arr[WIDTH][DEPTH];

// To traverse
for (int i = 0; i < WIDTH; i++)
  for (int j = 0; j < HEIGHT; j++)
    // do something with arr[i][j]


To create random numbers:
1
2
3
4
5
6
7
8
#include <ctime>

int main(void)
{
  srand(time(NULL));  // Do this only once in your program

  int i = rand();   // i is random
  i = rand() % 10;  // i is random between 0-9 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <ctime>
#include <cstdlib>
#include <iostream>
using std::cout; using std::endl;

int main()
{
    srand(time(NULL)); // Seed the random number generator;

    int r, c;
    cout << "How many rows are there?: " << endl;
    cin >> r;
    cout << "How many columns are there?: " << endl;
    cin >> c;

    int** grade = new int*[r];
    for (int i = 0; i < r; ++i)
    {
        grade[i] = new int[c];
        for (int j = 0; j < c; ++j)
            grade[i][j] = rand() % (r*c);
    }


    // grade[][] is now a 2d array with random numbers between 0 and r*c.

}
Last edited on
Oh ok, thanks! Everyone's answer helped. Just have to re-familiarize myself with pointers and dynamic variables
Topic archived. No new replies allowed.