RaduQ99 RaduQ99 - 1 year ago 97
C++ Question

C++ Matrix values of

I want to create a matrix of 5 lines and 5 columns which contains values from 1 to 9. The following programs displays numbers from 1 to 25 instead, when I input

to the program..

#include <iostream>

using namespace std;

int a[20][20], n, x = 0;

int main()
cout << "n=";
cin >> n;

for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
a[i][j] = x+1;
x = x+1;

for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
cout << a[i][j] << " ";

cout << endl;

I'm a c++ beginner, so maybe it's simple to do but i don't know how to make it show values from 1 to 9. This is the matrix I except:

1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7

Asu Asu
Answer Source

There are some issues in your code.

C arrays or STL containers?

First off, a your matrix may only hold matrices as big as 20x20. Your program will silently fail if you enter n bigger than 20, because you will access memory out of bounds; which causes an undefined behavior (see other common UB cases here).

You may want to use a dynamic size array - A good way to achieve this is to use std::vector. Unfortunately, this isn't as comfortable to use as a C-style array with two dimensions - you can achieve the same access syntax using a std::vector<std::vector<int>>, but this is not very efficient (see this answer if you are interested why) nor quite comfortable.

The Boost C++ library provides a multidimensional array library. Once you get experienced enough, you may find an use in it.

<= or <? 0-index or 1-indexed?

Many programming languages today uses 0-indexing for arrays. This means the first element of the array is located at index 0, not 1. Some languages, such as Lua, doesn't do this.

This means you should iterate i and j from 0 to n. This also means n is excluded, so you should use <, not <=.

Filling the matrix with numbers from 1 to 9

Your code doesn't do anything so you get numbers from 1 to 9 - it only fills the matrix with numbers from 1 to n * n. You could change this using an if clause to set x every time it goes above 9:

if (x > 9) { x = 0; } // x = 0 because it will be 1 on the next iteration

That being said, there is more convenient, as @PMar's answer says. The modulo operator % will do the task as well.

a[i][j] = (x % 9) + 1;
x = (x % 9) + 1;

This way, you will get every number from 1 to 9.

Now, you can also do another cleanup: Why are you calculating x's next value, and only then setting it? You could assign the new x value before the assignment to your matrix's cell. This allows having clearer code, with less copy pasting, which implies better maintainability.

x = (x % 9) + 1;
a[i][j] = x;

Another code quality consideration

I cannot say if your original code source was indented like your question (before it was edited), but you should really indent your code, for the future you and other people that will have to read your code. It allows for much better readability.

Same goes for different parts of your code : Add some space! It only can get more readable if you make a clear distinction between even just expressions.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download