Display Array in Rows and Columns using For loop

Hello everyone! I'm a beginner with C++, having started coding a few months back. Recently, I'm trying to experiment with For loops, and this time I'm trying to display two different arrays in two separate grids. I am using the old Borland Turbo C++ compiler.
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<iostream.h>
#include<conio.h>

main()

{

clrscr();

int rows,cols,grid;
int elem1[1],elem2[1];

cout<<" [ Input the number of rows ] : ";
cin>>rows;
cout<<" [ Input the number of columns ] : ";
cin>>cols;
grid = rows * cols;
elem1[grid],elem2[grid];

// Input of array elements

int x,y;

for(x=0;x<grid;x++)
{
cout<<"Enter element "<<x<<" of table 1: ";
cin>>elem1[x];
}
cout<<endl<<endl;
for(y=0;y<grid;y++)
{
cout<<"Enter element "<<y<<" of table 2: ";
cin>>elem2[y];
}
//display both tables

int q,w,a,s;
a=rows+1;
for(q=0;q<grid;q+=a)
{
  s=q+cols;
  for(w=q;w<s;w++){
  cout<<elem1[w]<<"\t";
cout<<endl;
}

int e,r,d,f;
d = rows+1;
for(e=0;e<grid;e+=d)
{
	f = e+cols;
	for(r=e;r<f;r++)
	cout<<elem2[r]<<"\t";
cout<<endl;
}

getch();
return 0;

}


As an example, if rows = 3, cols = 4, grid = 12, and both arrays can hold 12, the output should be:
1 2 3 4
5 6 7 8
9 10 11 12

But with the code above I'm getting:

2 3 4
1 2 3

I would greatly appreciate any help in this matter, thanks!
To display the array in a matrix form..

1
2
3
4
5
6
7
8
for(int i = 0; i < rows; i++)
{
    for(int j = 0; j < columns; j++)
    {
        cout<<Array[i][j]<<"\t";
    }
    cout<<endl;
}


Thanks! Just another question, where shall I put those lines of code? After I populate both arrays?
So I tried doing this:

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
 
#include<iostream.h>
#include<conio.h>

main()
{
clrscr();

int i,j,rows,columns,z;
int Array[1];

cout<<"Input rows: ";
cin>>rows;
cout<<"Input columns: ";
cin>>columns;
z=rows*columns;
Array[z];

for(int x=0;x<z;x++)
{
cout<<"Input element number "<<(x+1)<<" :";
cin>>Array[x];
}

for(i=0;i<rows;i++);
{
for (j=0;j<columns;j++)
	{
	cout<<Array[i][j]<<"\t";
	}
	cout<<endl;
}


getch();
return 0;
}


It gives me an `Invalid indirection` error on line 28.
check line 10 and 17
Thanks! I tried researching more about matrices, and I stumbled upon this matrix addition program:

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<iostream.h>
#include<conio.h>

main()
{
clrscr();


const int rows=2,columns=2;

cout<<"Size of matrix is "<<rows<<" x "<<columns<<endl;

cout<<"Enter value for first matrix: "<<endl;

int first[rows][columns], second[rows][columns];

int x,y;

for(x=0;x<rows;x++)
{
	cout<<"Enter value for row # :"<<x+1<<endl;

	for(y=0;y<columns;y++)
	{
	cin>>first[x][y];
	}
}

cout<<"\n\n\n Enter value for second matrix: "<<endl;
for(x=0;x<rows;x++)
{
	cout<<"Enter value for row #: "<<x+1<<endl;
	for(y=0;y<columns;y++)
	{
	cin>>second[x][y];
	}
}

for(x=0;x<rows;x++)
{
	for(y=0;y<columns;y++)
	{
	first[x][y]=first[x][y]+second[x][y];
	}
}

cout<<"\n\n\n Results: "<<endl;
for(x=0;x<rows;x++)
{
	cout<<endl;
	for(y=0;y<columns;y++)
	{
	cout<<"\t\t"<<first[x][y]<<"    ";
	}
}
getch();
return 0;
}


It works fine, however the rows and columns needs to be constant. Is there any way to make them variable?
just remove const and give int rows,columns; and then accept rows and columns by user
Removing the const gives me a "constant expression required" error.
just remove const and give int rows,columns; and then accept rows and columns by user

Sorry but this is wrong

An array defined at compile time must have const size.

The following example shows how to define an array at run time:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// arrays.cpp
// compile with: /EHsc
#include <iostream>
int main()
{
   using namespace std;
   int size = 0;
   cout << "how big should the array be? ";
   cin >> size;
   int* myarr = new int[size];
   for (int i = 0 ; i < size ; i++)
      myarr[i] = 10;

   for (i = 0 ; i < size ; i++)
      printf("myarr[%d] = %d\n", i, myarr[i]);
}

https://msdn.microsoft.com/en-us/library/7wkxxx2e(v=vs.71).aspx
Last edited on
can u show me what u did???
1
2
3
4
5
6
7
8
9
10
int rows,columns;

cout<<"Enter number of rows: ";
cin>>rows;

cout<<"Enter number of columns: ";
cin>>columns;

int* first = new int[rows][columns];
int* second =  new[rows][columns];
@shadowCODE
we could have completely ignored the const and go somewhat like this:

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include<iostream>

void read(int[10][10],int,int);
void write(int[10][10],int,int);
void sum(int[10][10],int[10][10],int,int,int[10][10]);
void diff(int[10][10],int[10][10],int,int,int[10][10]);

main()


{
cout<<"enter the row and column of matrix a: ";
int r,c;
cin>>r>>c;
cout<<"Enter the matrix:\n";
int a[10][10];
read(a,r,c);
cout<<"\nThe matrix is:\n";
write(a,r,c);



cout<<"\nEnter the second matrix:\n";
int b[10][10];
read(b,r,c);
cout<<"\nThe matrix is:\n";
write(b,r,c);



cout<<"the sum of the matrix is:\n";
int s[10][10];
sum(a,b,r,c,s);
cout<<"\nthe difference of the matrix is:\n";
int d[10][10];
diff(a,b,r,c,d);

}
void read(int a[10][10],int r,int c)
{
for(int i=0;i<r;i++)
	{
		for(int j=0;j<c;j++)
			{
				cin>>a[i][j];
			}
	}
}
void write(int a[10][10],int r,int c)
{
for(int i=0;i<r;i++)
	{
	cout<<"\n\n";
		for(int j=0;j<c;j++)
			{
				cout<<a[i][j];
				cout<<"\t";
			}
	}

}
void sum(int a[10][10],int b[10][10],int r,int c,int e[10][10])
{
for(int i=0;i<r;i++)
	{
		for(int j=0;j<c;j++)
			{
				e[i][j]=a[i][j]+b[i][j];
			}
	}
	write(e,r,c);
}
void diff(int a[10][10],int b[10][10],int r,int c,int e[10][10])
{
for(int i=0;i<r;i++)
	{
		for(int j=0;j<c;j++)
			{
				e[i][j]=a[i][j]-b[i][j];
			}
	}
	write(e,r,c);
}

Last edited on
how does this ignore const?? int a[10][10];. 10 is a const and still imposes restriction to know information of max size of the matrix before runtime.

The idea here is not to remove the keyword const from the code but to eliminate the fact that we have to know the size of the array before time.
This is where dynamic memory allocation comes in handy with new
Last edited on
d'oh.........

didn't knew that but int a[10][10]; works fine for small runs :D

thanks @shadowCODE I'll definitely now learn about dynamic memory allocation. Any useful links?
Topic archived. No new replies allowed.