Thread: Code error

  1. #1
    Registered User
    Join Date
    Feb 2021
    Posts
    17

    Code error

    Hello,
    Can someone tell me what is wrong with the following code and how should i modify it to make it work. What should i write instead of 'rest of the program'

    insert
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    int main()
    {
    
    
        long** a = malloc(15*sizeof(long*));
        for ( int i = 0 ; i <= 15; i++ ) {
            a[i] = malloc(15*sizeof(long));
            for ( int j = 0 ; j <= 15 ; j++ ) {
                a[i][j] = 0;
            }
        }
        for ( int i = 0 ; i <= 15; i++ ) {
            for ( int j = 0 ; j <= 15 ; j++ ) {
                printf("a[%d][%d] = %d\n", i, j, a[i][j]);
            }
        }
        
        //rest of the program
        
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    210
    The same thing we said on discord. "<=15" should be "< 15"

  3. #3
    Registered User
    Join Date
    Feb 2021
    Posts
    17
    I did it but i didn't work
    In the excercise I supposed to write some code after the second for loop to make it work

  4. #4
    Registered User
    Join Date
    Feb 2021
    Posts
    17
    Can you help please!

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,590
    How are we supposed to 'make it work' when you haven't said what it's supposed to do to begin with.

    There's this
    Code:
    $ gcc -Wall foo.c
    foo.c: In function ‘main’:
    foo.c:17:20: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘long int’ [-Wformat=]
                 printf("a[%d][%d] = %d\n", i, j, a[i][j]);
                        ^
    Did you change ALL the <= 15?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    int main()
    {
    
    
        long** a = malloc(15*sizeof(long*));
        for ( int i = 0 ; i < 15; i++ ) {
            a[i] = malloc(15*sizeof(long));
            for ( int j = 0 ; j < 15 ; j++ ) {
                a[i][j] = 0;
            }
        }
        for ( int i = 0 ; i < 15; i++ ) {
            for ( int j = 0 ; j < 15 ; j++ ) {
                printf("a[%d][%d] = %ld\n", i, j, a[i][j]);
            }
        }
    
        //rest of the program
    
        for ( int i = 0 ; i < 15; i++ )
          free(a[i]);
        free(a);
        return 0;
    }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code Blocks showing error in code! Don't know what's it.
    By kdushyant297 in forum C Programming
    Replies: 2
    Last Post: 09-08-2017, 09:59 AM
  2. Code error
    By DeepFyre in forum C++ Programming
    Replies: 9
    Last Post: 12-27-2004, 01:32 AM
  3. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 09:45 PM
  4. Need help with one error in my code
    By RJstuff1 in forum C++ Programming
    Replies: 6
    Last Post: 09-23-2001, 04:13 AM

Tags for this Thread