-1

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 5 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
  • 1,875
  • 17
  • 27
RaduQ99
  • 13
  • 4
  • 2
    The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should \[edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Oct 05 '16 at 17:34
  • That code you show is nearly unreadable. Please apply indentation evenly throughout the code, don't just use it for certain pieces. – Some programmer dude Oct 05 '16 at 17:36
  • 1
    Also, you do know that array indexes are zero-based? Why are you starting your loops at index `1`? Doing that now just gives you a bad habit that might be hard to break later when it matters. – Some programmer dude Oct 05 '16 at 17:38
  • Lastly, and regarding your problem. You do know that your initialization of the matrix will run `n * n` times? You might want to reset `x` sometime and somewhere. – Some programmer dude Oct 05 '16 at 17:39
  • I don't know how to make the matrix to show values from 1 to 9. When i run the program and i enter 5 it show me a matrix with values from 1 to 25. – RaduQ99 Oct 05 '16 at 17:41
  • Program is just a way of explaining things to computer in a very detailed way. Try reading your program in some natural language you use, like English, and find the problem in that explanation. Hint: where did ypur program says that you can only use numbers from 1 to 9? – Gasper Oct 05 '16 at 17:43

3 Answers3

0

You need to use the modulus operator (%). If you want the values in the matrix to be as you have computed them, but only need to change the display, you would output the matrix values as follows:

cout << (a[i][j] % 10) << " ";

If you want the one-digit values to be in the matrix itself, you would instead change the increment on 'x' to the following:

x = (x+1) % 10;}
PMar
  • 11
  • 1
0

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.

Sebastian Brosch
  • 42,106
  • 15
  • 72
  • 87
asu
  • 1,875
  • 17
  • 27
0
#include <iostream>

int main()
{

    int a[5][5];
    int x = 1;

    for(int i = 0; i < 5; i++)
    {
        for(int j = 0; j < 5; j++)
        {
            if(x > 9)
                x = 1;
            a[i][j] = x;
            x++;
        }
    }

    for(int i = 0; i < 5; i++)
    {
        for(int j = 0; j < 5; j++)
        {
            if(a[i][j] < 10)
                std::cout << " ";
            std::cout << a[i][j] << " ";
        }

        std::cout << std::endl;
    }

    std::cout << std::endl;
    return 0;
}
Raindrop7
  • 3,889
  • 3
  • 16
  • 27