Thread: Homework help

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    4

    Homework help

    This is my last question to answer on my homework assignment and I'm borderline about to rip my hair out If someone could please assist me

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    
    {
    int n;
    int a = 2;
    int b[2];
    int c = 3;
    int i;
    scanf("%d", &n);
    
    for(i = 0; i < n; i++){
    b[i] = n * i;
    }
    
    
    printf("value of a=%d, c=%d\n", a, c);
    
    for(i = 0; i < n; i++){
    
    printf("b[i] = %d\n", b[i]);
    
    }
    return 0;
    }
    1.What is the problem with the above code snippet?
    2.Why does it exhibit the behavior it does? Explain. How do you fix it?

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    There are several problems. Which one in particular are you having trouble with?

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Since b is an array with dimension two, we should check the input to be sure that n is not more than two AND not less than zero. You can easily do this with an if statement.
    With the code you provided, if you input 10 for example, you will that the values of a and c may not be the ones you expected, because of the array overflow!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    You just gave him the answer flat out ...

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    what is the size of the array?
    What will happen if user enters value bigger than array size?
    How should you fix the problem?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by nonpuz View Post
    You just gave him the answer flat out ...
    Partially true. Sorry. :/
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  7. #7
    Registered User
    Join Date
    Dec 2011
    Location
    Namib desert
    Posts
    94
    int n;
    int b[2];

    scanf("%d", &n);

    for(i = 0; i < n; i++)

    b[i] = n * i;


    What will happen if a user, during scanf(), for example enters 4 and your for-loop counter 'i' gets a value greater than 1 ?
    Does for example b[3] exists, what will happen if you assign a value to an undeclared memory location ?

  8. #8
    Registered User
    Join Date
    Sep 2013
    Posts
    4
    Quote Originally Posted by std10093 View Post
    Since b is an array with dimension two, we should check the input to be sure that n is not more than two AND not less than zero. You can easily do this with an if statement.
    With the code you provided, if you input 10 for example, you will that the values of a and c may not be the ones you expected, because of the array overflow!
    Thanks alot! Haha although the answer was given flat out I would've understood if someone just mentioned array overflow. I'm so used to java where it would through out of bounds exceptions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework :)
    By boksanns94 in forum C Programming
    Replies: 4
    Last Post: 06-10-2011, 06:55 AM
  2. Homework Help
    By andrei1599 in forum C# Programming
    Replies: 1
    Last Post: 02-06-2011, 09:38 PM
  3. C homework help
    By camel-man in forum C Programming
    Replies: 6
    Last Post: 01-25-2011, 09:19 AM
  4. Help me with my c++ homework please
    By dmeyers81 in forum C++ Programming
    Replies: 5
    Last Post: 12-05-2010, 09:26 PM
  5. Homework =)
    By Raeliean in forum C++ Programming
    Replies: 9
    Last Post: 07-16-2005, 10:27 PM