# Thread: Nested loops and 2D Variable Length Arrays

1. ## Nested loops and 2D Variable Length Arrays

Greetings. I am attempting to perform scalar multiplication with a 3x3 variable length array. Normally this is a simple task for me. Scalar multiplication is done by multiplying each element of a matrix by the same integer.The problem is that the 2nd and 3rd rows of my result matrix have garbage values. It's as if these elements are never assigned values. I have been backwards and forwards through the code but I don't see anything wrong. This is either a brain cramp on my part or a bug in the system. Below is the code and a sample run of the program. I'm hoping a fresh set of eyes will spot a mistake.
Code:
`#include // functions for the input and display of 2D matricesvoid input( int rows, int cols, int a[rows][cols] );void display( int rows, int cols, int a[rows][cols] );#define MAX 10int main(void){    int a[MAX][MAX], b[MAX][MAX];    int rows, cols;    int m;    printf("Enter # of rows: ");    scanf("%d", &rows);    printf("Enter # of cols: ");    scanf("%d", &cols);    printf("\n");    printf("Enter data for input matrix:\n");    input( rows, cols, a);    printf("\n");    printf("Enter scalar multiplier: ");    scanf("%d", &m);    printf("\n");    // Here is the nested loop where the multiplication is done    for(int i = 0; i < rows; i++)    {        for(int j = 0; j < cols; j++)        {            b[i][j] = m * a[i][j];        }    }    printf("input matrix:\n");    display(rows, cols, a);    printf("\n");    printf("result matrix:\n");    display(rows, cols, b);    printf("\n");    return 0;}void input( int rows, int cols, int a[rows][cols] ){    for(int i = 0; i < rows; i++)    {        for(int j = 0; j < cols; j++)        {            printf("a[%d][%d]: ", i, j);            scanf("%d", &a[i][j]);        }    }}void display( int rows, int cols, int a[rows][cols] ){    for(int i = 0; i < rows; i++)    {        for(int j = 0; j < cols; j++)        {            printf("%-3d", a[i][j]);        }        printf("\n");    }}`
Sample input and output:Enter # of rows: 3Enter # of cols: 3Enter data for input matrix:a: 1a: 1a: 1a: 1a: 1a: 1a: 1a: 1a: 1Enter scalar multiplier: 2input matrix:1 1 1 1 1 1 1 1 1 result matrix:2 2 2 3260041952890 -1444937728-4275510 2. Well posting code that isn't all on one line would be a help. 3. I used code tags, but they obviously didn't work. It wasn't just the code that was messed up. Whatever the problem was, it affected the entire post. It was as if the board ignored all my carriage returns. Perhaps a mod has an explanation. 4. Originally Posted by fguy I used code tags, but they obviously didn't work. It wasn't just the code that was messed up. Whatever the problem was, it affected the entire post. It was as if the board ignored all my carriage returns. Perhaps a mod has an explanation.
Perhaps you didn't paste it as plain text. You haven't mentioned your system/IDE/editor or anything so we can only guess. If there is an option to "copy (or paste) as plain text" or something like that, use it. Alternatively, if you're in Windows (the usual sad case), you could try pasting it into Notepad first, then Ctrl-A, Ctrl-C to copy it, and Ctrl-V it here. 5. I am using Debian Linux. Also my explanation of the problem was typed directly into the message window as two paragraphs, but it too lost the carriage returns and was presented as one big paragraph. I don't have this problem with the only other board I have used recently. I'll do a little experimenting to see if I can solve this. 6. This part of the post was entered as only one sentence.This second part of the post was entered as a separate sentence in its own paragraph. If this post shows up as one paragraph you will know that the problem affects text that was typed in and not pasted. 7. I am using Ubuntu.

This is a second paragraph.

Code:
```#include <stdio.h>

#define MAX 10

void input(int rows, int cols, int a[rows][cols]) {
for(int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++) {
printf("a[%d][%d]: ", i, j);
scanf("%d", &a[i][j]);
}
}

void display(int rows, int cols, int a[rows][cols]) {
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++)
printf("%-3d", a[i][j]);
printf("\n");
}
}

int main(void) {
int a[MAX][MAX], b[MAX][MAX];
int rows, cols;
int m;

printf("Enter # of rows: ");
scanf("%d", &rows);
printf("Enter # of cols: ");
scanf("%d", &cols);

printf("\nEnter data for input matrix:\n");
input(rows, cols, a);

printf("\nEnter scalar multiplier: ");
scanf("%d", &m);

for(int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
b[i][j] = m * a[i][j];

printf("\ninput matrix:\n");
display(rows, cols, a);

printf("\nresult matrix:\n");
display(rows, cols, b);
printf("\n");

return 0;
}```
Your problem is that you're mixing fixed-length arrays with variable-length arrays. You need to define your arrays like this (after reading in row and col):
Code:
`    int a[rows][cols], b[rows][cols];` 8. That solved it. This is the sort of mistake that they don't write about in the book, so I don't mind making it. Thanks for your help. 9. Originally Posted by fguy That solved it. This is the sort of mistake that they don't write about in the book, so I don't mind making it. Thanks for your help.
You changed to using variable-length arrays; you should know that NOT all standard C Compilers support variable-length arrays.

Tim S. 10. Duly noted, stahta01. I believe VLAs are optional with C11. It's a pity though, VLAs are convenient in some cases. On another issue, this board software doesn't like me very much. In addition to the disappearing carriage returns discussed earlier, it seems the only way I can reply in a thread is by doing a Quick Reply. Popular pages Recent additions cols, input, int, printfn;, rows 