C++ array using multidimensional

How to make a c++ program that will output like this 1 2 3 4 5
.................................................................................6 7 8 9 10
.................................................................................11 12 13 14 15
using multidimensional in array.I am a beginner of c++ and I don`t have any idea how to start it.
would you help me.......
Last edited on
Hi there Nikkiglec. I am not sure exactly what you are requesting, but given the information, I am going to say you are asking for how to make a three dimensional array, where the first array holds the numbers 1-5, the second 6-10 and the third 11-15. If that is the case, here is some code on that matter:

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
// to display some text
#include <iostream>
using namespace std;

int main()
{
     // create an array, which will be a 3x5 grid (total of 15 spaces)
     // you can access each element by typing multiarray[x][y]
     // remember, while it is 15 spaces, the first element is [0][0], while the last is [2][4]
     int multiarray[3][5];

     // this loop just sets each element to have the corresponding number
     for(int x = 1; x <= 15; x++)
     {
          // some if statements to choose which array to use
          if(x <= 5)
               multiarray[0][x] = x;
          else if(x <= 10)
               multiarray[1][x-5] = x;
          else if(x <= 15)
               multiarray[2][x-10] = x;
     }

     // lets display some of the array
     // remember to subtract 1 from the array when accessing it
     cout << "Element in row 2 collum 3: " << multiarray[1][3] << "\n";
     cout << "Element in row 3 collum 1: " << multiarray[2][0] << "\n";
     
     while(1); return 1;
}
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
// 2D array
#include <iostream>
using namespace std;

int main () {
int numRows = 3;
int numCols = 5;

int anArray[numRows][numCols] = 
{
{ 1, 2, 3, 4, 5, },
{ 6, 7, 8, 9, 10, },
{ 11, 12, 13, 14, 15 }
};

	for (int i=0;i<3;i++)
	{
		for (int j=0;j<5;j++)
		{
			cout << anArray[i][j] << "\t" ;
		}
		cout << endl;
	}

 return 0;
}
Here is a fix
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
// 2D array
#include <iostream>
using namespace std;

int main ()
{
    const int numRows = 3; // when making arrays, you need to have consts
    const int numCols = 5;

    int anArray[numRows][numCols] = 
    {
        { 1, 2, 3, 4, 5 }, // you had "4, 5, }," when it should have been without the comma after the 5
        { 6, 7, 8, 9, 10 }, // same error as above
        { 11, 12, 13, 14, 15 }
    };

	for (int i=0;i<3;i++)
	{
		for (int j=0;j<5;j++)
		{
			cout << anArray[i][j] << "\t" ;
		}
		cout << endl;
	}

    return 0;
}
Topic archived. No new replies allowed.