Thread: Nested loops and 2D Variable Length Arrays

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    12

    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[0][0]: 1a[0][1]: 1a[0][2]: 1a[1][0]: 1a[1][1]: 1a[1][2]: 1a[2][0]: 1a[2][1]: 1a[2][2]: 1Enter scalar multiplier: 2input matrix:1 1 1 1 1 1 1 1 1 result matrix:2 2 2 3260041952890 -1444937728-4275510

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,208
    Well posting code that isn't all on one line would be a help.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2017
    Posts
    12
    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. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Quote Originally Posted by fguy View Post
    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. #5
    Registered User
    Join Date
    Apr 2017
    Posts
    12
    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. #6
    Registered User
    Join Date
    Apr 2017
    Posts
    12
    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. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    I am using Ubuntu.

    This is a second paragraph.

    Here is your code:
    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. #8
    Registered User
    Join Date
    Apr 2017
    Posts
    12
    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. #9
    Registered User
    Join Date
    May 2009
    Posts
    3,903
    Quote Originally Posted by fguy View Post
    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.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  10. #10
    Registered User
    Join Date
    Apr 2017
    Posts
    12
    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 subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 12-07-2016, 06:41 AM
  2. Variable-Length Arrays
    By Quant89 in forum C Programming
    Replies: 1
    Last Post: 04-13-2013, 09:57 AM
  3. Variable length arrays
    By nik2 in forum C Programming
    Replies: 2
    Last Post: 03-18-2010, 09:48 AM
  4. Variable length arrays
    By tsantana in forum C Programming
    Replies: 8
    Last Post: 06-13-2009, 12:17 AM

Tags for this Thread