2 by 2 matrix using for loops

Any advice?? Remember I'm a beginner and don't know all the lingo just yet.

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
#include<iostream>
#include<string> // Allows the comiler to receive strings.

using namespace std;


int main(){
	
	int num[4];
	int i;
	
	cout << "Please enter in four numbers, no decimals please: ";
	
	for(i=0; i < 4; i++){
				cin >> num[i];
		}
		for(i=0; i < 5; i++){
					cout << endl;
			}
			for(i=0; i < 2; i++){
					cout << num[i] << " ";
		}
				for(i=0; i < 1;i++){
						cout << endl;
			}
					for(i=2; i < 4; i++){
							cout << num[i] << " ";
				}
	
	cout << endl;
	return 0;
	}
what do you want to do now?
advice... book brackets save printing costs in books. If you are not printing, do not use them; they do not align and are hard to read. (Book brackets are of the form statement { instead of
statement
{

your indententation is killing me, and I am notoriously unpicky about such things. The rolling indents imply the for loops are inside each other, but they are not.
This is not perfect either, but a 10 second reformat of your code to explain a little what I mean:

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
#include<iostream>
#include<string> // Allows the comiler to receive strings.

using namespace std;

int main()
{
 int num[4];
 int i;
 cout << "Please enter in four numbers, no decimals please: ";
 for(i=0; i < 4; i++)
  { //see how this
    cin >> num[i];
  }  //aligns with this?
 for(i=0; i < 5; i++)
  {
    cout << endl;
  }
 for(i=0; i < 2; i++)
  {
    cout << num[i] << " ";
  }
for(i=0; i < 1;i++)
  {
   cout << endl;
  }
for(i=2; i < 4; i++)
  {
    cout << num[i] << " ";
  }
cout << endl;
return 0;
}
Last edited on
2 by 2 matrix

Easy peasy, a 2 dimensional array. int num[2][2];

So why create a 1 dimensional array with int num[4]; and write code to use the array in 1 dimension.

*hint* You can simulate a 2D array with a 1D array. How the elements are accessed differs.

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
#include <iostream>

int main()
{
   const unsigned rows = 2;
   const unsigned cols = 2;
   int matrixA[rows][cols];

   for (size_t i = 0; i < rows; i++)
   {
      for (size_t j = 0; j < cols; j++)
      {
         std::cout << "matrix[" << i << "][" << j << "]: ";
         std::cin >> matrixA[i][j];
      }
   }
   std::cout << '\n';

   for (size_t i = 0; i < rows; i++)
   {
      for (size_t j = 0; j < cols; j++)
      {
         std::cout << matrixA[i][j] << ' ';
      }
      std::cout << '\n';
   }
   std::cout << '\n';

   // you can simulate a 2D array
   int matrixB[rows * cols];

   for (size_t i = 0; i < rows; i++)
   {
      for (size_t j = 0; j < cols; j++)
      {
         std::cout << "matrix[" << i << "][" << j << "]: ";
         std::cin >> matrixB[2 * i + j];
      }
   }
   std::cout << '\n';

   for (size_t i = 0; i < rows; i++)
   {
      for (size_t j = 0; j < cols; j++)
      {
         std::cout << matrixB[2 * i + j] << ' ';
      }
      std::cout << '\n';
   }
}
matrix[0][0]: 1
matrix[0][1]: 2
matrix[1][0]: 3
matrix[1][1]: 4

1 2
3 4

matrix[0][0]: 5
matrix[0][1]: 9
matrix[1][0]: 12
matrix[1][1]: 7

5 9
12 7

I repeat what jonin said, your formatting is seriously whacked. Trying to understand your code without reformatting is really hard.

Your loop at 23-25 is unneeded. It loops one time.
Thanks for the advice but why do you come off as an a**hole?
1
2
//matrixB[2 * i + j]
matrixB[cols * i + j];
to avoid magic numbers
to avoid magic numbers

I was swiftly mashing code together, as quickly as I could get it to work. I know I let some smelly code behind.

JErdmann, don't ask for advice if you don't want to hear it.
Topic archived. No new replies allowed.