2

I have to create a program that allows a user to fill in a (partial) Latin Square of order 4. You can use 0's to represent empty cells. The user will give the number to place, the row and column. The number should only be placed if it does not violate the properties of a partial Latin square and it shouldn't rewrite numbers that have already been placed.

I have an matrix that is outputting all zeroes now. So next I have to replace each of these values by what the user is inputting. The problem is I don't know how to do this.

Here is my code:

#include <iostream>
using namespace std;

const int ORDER = 4;
void fill (int m[], int order);
void outputMatrix (int m[], int order);
void replaceValue (int m[], int order, int n, int row, int column);

int main(){
    int matrix[ORDER];
    int row;
    int column;
    int n;
    fill (matrix, ORDER);
    outputMatrix (matrix, ORDER);
    do {
    cout << "Enter the number to place, the row and the column, each seperated by a space: ";
    cin >> n;
    cin >> row;
    cin >> column;

    }while (n > 0 || n <= ORDER);
    if (n <= 0 || n >= ORDER){
        cout << "Thank you";
        cout << endl;
    }

    return 0;
}


void fill (int m[], int order){
   for (int i = 0; i < order*order; i++){
        m[i] = 0;
   }
}

void outputMatrix (int m[], int order){
    int c = 0;
    for (int i = 0; i < order*order; i++){
         c++;
            cout << m[i] << ' ';
            if (c == order){
                cout << endl;
                c = 0;
            }
        }
        cout << endl;   
}

void replaceValue (int m[], int order, int n, int row, int column){
    for (int i = 0; i < order; i++){
        m[order] = m[row][column];
        m[row][column] = n;
    }
}

How do I replace values in a Matrix in C++?

2
  • Something like arr[i][j] = 5; ? Commented Nov 6, 2013 at 19:35
  • DO you want to set all matrix values to 0? Commented Nov 6, 2013 at 19:50

3 Answers 3

2

If you have a matrix, matrix[row][col] = value; would do the trick. However, I see that you allocate a single array. Make sure you look at this.

EDIT:

I looked closer at you code and you are doing some things wrong.

First: matrix[ORDER] will create a single array of ORDER values. If you want and ORDER by ORDER matrix try: matrix[ORDER][ORDER]

Second: You are calling:

void fill (int m[], int order){
    for (int i = 0; i < order*order; i++){
        m[i] = 0;
    }
}

with an of size 4 and order == 4. This will loop outside the array and give you problems.

Try something like:

matrix[ORDER][ORDER];
for (int row = 0; row != ORDER; ++row)
{
    for (int col = 0; col != ORDER; ++col)
    {
        matrix[row][col] = 0;
    }
}

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

1

You can't really write arr[i][j] if arr is defined as arr[]. There's no information about the length of the row (how many columns there are).

You could use arrays of type arr[][4], and write your functions like so:

// The & is to pass by reference.
void print(int (&arr)[][4], int length)
{
    for(int i = 0; i < length; i++) {
        for(int j = 0; j < 4; j++) {
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }
}

But in my opinion for a low-order multidimensional array like this one, using a typedef for a vector of vectors is the better option:

typedef vector<vector<int> > Matrix;
void print(Matrix& arr)
{
    for(int i = 0; i < arr.size(); i++) {
        for(int j = 0; j < arr[i].size(); j++) {
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }
}

In either case, writing arr[i][j] = k will behave as you expect.

2 Comments

I'm not allowed to use vector unfortunately
Then use my first suggestion.
1

The easiest way to clear/zero your matrix is that:

memset( &matrix, 0, sizeof(matrix));

;-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.