Sample of Multidimensional Array

Hello Guyz,

I just want to share this code that I made. Please feel free to comment. Thanks :D

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
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
	const int numRows = 5; // set the size of rows of our multi Array
	const int numCols = 5; // set the size of column of our multi Array
	
	int ctrRows; // counter for our row loops
	int ctrCols; // counter for our column loops
	
	int multiArray[numRows][numCols] = {0}; // declaration of Multidimensional Arrays
	
	int num = 1; // the value that we need to store in our array
	
	for(ctrRows = 0; ctrRows < 5; ctrRows++) // first loop..intended for the  rows of the array
	{
		for(ctrCols = 0; ctrCols < 5; ctrCols++)  // first loop..intended for the column of the array
		{
			multiArray[ctrRows][ctrCols] = num; // assigning a value
			num++;
		}
		num = num - 4;
	}
	
	for(ctrRows = 0; ctrRows < 5; ctrRows++) // first loop..intended for the  rows of the array
	{
		for(ctrCols = 0; ctrCols < 5; ctrCols++) // first loop..intended for the column of the array
		{
			cout << multiArray[ctrRows][ctrCols] << " "; //output the value
		}
		cout << endl;
	}
	
	
	system("pause");
	return 0;
}



1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
Regarding the use of system("pause") http://cplusplus.com/forum/beginner/1988/

Maybe try adding something new to your code, like writing the output to a text file?

How about reading values into the multi-dimensional array from a text file and then printing them to the console?

Just some food for thought if you want to expand your code a bit (;
Wow!!! that's a great idea.. however I am still studying the very basics of C++. I will try to make a code on what you just suggested.... Thanks a lot :)
Thanks for the link sir... i have removed the

system("pause");

and replaced it with this...

1
2
cout << "Press ENTER to continue...";
  cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );


Is that good way to exit the program?
Last edited on
Topic archived. No new replies allowed.