Hi All,

I am new to C++, I am tring to generate the random number from 0-99, and save it to a matrix array with the size of 10x10.
Below is my code, the resule comes out funny. Can you help to see what goes wrong?

#include<stdio.h>		//Header file
#include<stdlib.h>
#include<time.h>
#include<iostream>

using namespace std;

#define MAXROWS 10		//Define max rows =20		
#define MAXCOLS 10		//Define max cols =30

int main()
{
		int random_matrix[MAXROWS][MAXCOLS];
		int i;
		int j;

		/* initialize random seed: */
		srand(static_cast<unsigned>(time(0)));

		  /* generate  number: */
		  for (i=0;i<MAXROWS;i++)
		  {
			  for (j=0;j<MAXCOLS;j++)
			  random_matrix[i][j]= rand() % 100;
			  cout << random_matrix[i][j] << endl; //display table
		  }
			system("pause");
}

Edited 5 Years Ago by WaltP: Added CODE Tags -- please use them

First, when posting code, please use [code] ...code tags... [/code]. They preserve formatting and add Line Numbers making your code easier to read. Observe (with minor formatting corrections for readability and discussion):

#include<stdio.h> //Header file
#include<stdlib.h>
#include<time.h>
#include<iostream>

using namespace std;

#define MAXROWS 10 //Define max rows =20 
#define MAXCOLS 10 //Define max cols =30

int main()
{
  int random_matrix[MAXROWS][MAXCOLS];
  int i;
  int j;

  /* initialize random seed: */
  srand(static_cast<unsigned>(time(0)));

  /* generate number: */
  for (i=0;i<MAXROWS;i++)
  {
    for (j=0;j<MAXCOLS;j++)
      random_matrix[i][j]= rand() % 100;
    cout << random_matrix[i][j] << endl; //display table
  }
  system("pause");
}

Second, you need to decide if you're writing C++ or C. To write proper C++, you need to use the modern headers <cstdio>, <cstdlib>, and <ctime> instead of the antiquated <stdio.h>, <stdlib.h> and <time.h> versions.

Third, I would replace those #defines with actual constant definitions:

const int ROWS = 25;
const int COLS = 15;

Fourth, Line 25 doesn't do what you seem to think it does. In your output, you're only outputting a single element, not an entire row. Unfortunately, that element is outside of the legal boundaries of your array. This is why you are only getting a limited number of uninitialized values. Your inner for loop only goes to Line 24; it needs to include Line 25. You'll have to rework this loop to adjust the limits of its scope.

Edited 5 Years Ago by Fbody: n/a

Thank you, I have fixed the code, and now it's working..
Now I understand more of this forum and as well as my problem,

First, when posting code, please use [code] ...code tags... [/code]. They preserve formatting and add Line Numbers making your code easier to read. Observe (with minor formatting corrections for readability and discussion):

#include<stdio.h> //Header file
#include<stdlib.h>
#include<time.h>
#include<iostream>

using namespace std;

#define MAXROWS 10 //Define max rows =20 
#define MAXCOLS 10 //Define max cols =30

int main()
{
  int random_matrix[MAXROWS][MAXCOLS];
  int i;
  int j;

  /* initialize random seed: */
  srand(static_cast<unsigned>(time(0)));

  /* generate number: */
  for (i=0;i<MAXROWS;i++)
  {
    for (j=0;j<MAXCOLS;j++)
      random_matrix[i][j]= rand() % 100;
    cout << random_matrix[i][j] << endl; //display table
  }
  system("pause");
}

Second, you need to decide if you're writing C++ or C. To write proper C++, you need to use the modern headers <cstdio>, <cstdlib>, and <ctime> instead of the antiquated <stdio.h>, <stdlib.h> and <time.h> versions.

Third, I would replace those #defines with actual constant definitions:

const int ROWS = 25;
const int COLS = 15;

Fourth, Line 25 doesn't do what you seem to think it does. In your output, you're only outputting a single element, not an entire row. Unfortunately, that element is outside of the legal boundaries of your array. This is why you are only getting a limited number of uninitialized values. Your inner for loop only goes to Line 24; it needs to include Line 25. You'll have to rework this loop to adjust the limits of its scope.

This question has already been answered. Start a new discussion instead.