This is homework so I am not looking for a better way to do this. I am only asking for a second pair of eyes to spot my mistake. I have a function that takes a two-dimensional array and fills the first row with the elements from a one-dimensional array. It is then supposed to fill the remaining row with three times that of the previous row. The first part works but the second part does nothing. Near as I can figure it out it looks as if the second part doesn’t even run. Is it because I need to reset the array after the first loop?

const int NUM_FOUR = 4;
void copyGamma(int matrix[][NUM_FOUR], int src, int list2[]);

int main()
{
    int inStock[NUM_TEN][NUM_FOUR];
    int gamma[NUM_FOUR] = {11, 13, 15, 17};

    // Copy gamma elements to inStock and fill the other elements
    // by multiplying by three to each remaining successive row
    // print again to show changes
    copyGamma(inStock, 0, gamma);
    cout << "inStock has the following elements: " << endl;
    printMatrix(inStock, NUM_TEN);

    system("pause");

    return 0;
}

// (d) Function that sets the elements of the first row of inStock to gamma and the remaining
//     rows to three times the previous row without modifying the elements of gamma
void copyGamma(int matrix[][NUM_FOUR], int numRows, int list[])
{
    int row, col;
    int nextRow = 1;

    for (col = 0; col < NUM_FOUR; col++)
        matrix[0][col] = list[col];

// something here to rest array perhaps?

    for (row = 0; row < numRows; row++)
    {
        for (col = 0; col < NUM_FOUR; col++)
            matrix[(nextRow)][col] = matrix[(row)][col] * 3;
            nextRow++;
    }
}

This is due tomorrow so I am not expecting to have this working correctly by then but I sure would like to know where I went wrong. Any help would be appreciated. Not looking for someone to do my homework just need advice on what I have wrong. Thanks

The way you've indented your code suggests the error. If nextRow++ needs to be inside the inner loop, the loop body must be surrounded with braces:

for (col = 0; col < NUM_FOUR; col++)
{
    matrix[(nextRow)][col] = matrix[(row)][col] * 3;
    nextRow++;
}

Thank you for the surprisingly quick response. I was not expecting anything today. I edited the code but the results remain the same. I put a breakpoint at the start of the second loop and it appears the function drops out after the first loop.

// (d) Function that sets the elements of the first row of inStock to gamma and the remaining
//     rows to three times the previous row without modifying the elements of gamma
void copyGamma(int matrix[][NUM_FOUR], int numRows, int list[])
{
    int row, col;
    int nextRow = 1;

    for (col = 0; col < NUM_FOUR; col++)
        matrix[0][col] = list[col];

    //breakpoint set on line below
    for (row = 0; row < numRows; row++)
    {
        for (col = 0; col < NUM_FOUR; col++)
            {
                matrix[(nextRow)][col] = matrix[(row)][col] * 3;
                nextRow++;
            }
    }
}

84382590ff9e26a3569e756ba76db8e0

copyGamma(inStock, 0, gamma);

You're passing 0 into numRows and never change that before the loop. Is 0 less than 0? Because that's the first condition for entering the outer loop, which means it will not be executed because the condition fails.

Thank you so much. I was expecting to hand this in uncompleted. It was a typo, the 0 was meant to be a 10. It still is not working fully however. I will mess with it a bit and see where I went wrong. It seems to skip every three columns. Now that you pointed out my error I should be able to pinpoint this new one that was exposed. Thanks again.

Worked that out to experience another. I moved the nextrow++ to the outside loop and now it updates all the elements but I get a stack corrupt error. I suspect my code is not stopping at the end of the array??

Got it. The code was not writing past the array but reading past the boudry with nextRow++. I lowered the row to 9 and it works.

// (d) Function that sets the elements of the first row of inStock to gamma and the remaining
//     rows to three times the previous row without modifying the elements of gamma
void copyGamma(int matrix[][NUM_FOUR], int numRows, int list[])
{
    int row, col;
    int nextRow = 1;

    for (col = 0; col < NUM_FOUR; col++)
        matrix[0][col] = list[col];

    for (row = 0; row < 9; row++)
    {
        for (col = 0; col < NUM_FOUR; col++)
            {
                matrix[(nextRow)][col] = matrix[(row)][col] * 3;
            }
        nextRow++;
    }
}

Thanks again for your help decptikon.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.