Thread: For loop

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    11
    Rep Power
    0

    For loop


    Hey ! Here is my code - So, my for loop is working however, at the end I am trying to print out my array but its not working.

    For example, if I do an array of size 3, my output is

    enter the size of your array: 3
    enter number0: 1
    enter number1: 4
    enter number 2: 5

    here is the problem, I want it to printout:

    145

    at the end but, its giving me a bunch of garbage. Dont understand what is going on !


    help !
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    11
    Rep Power
    0
    I dont know what is it that I am doing wrong ??
  4. #3
  5. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    > printf ("%d", array[i]);
    You could put this inside your loop.

    Or you could put it in another loop.

    It needs to be in one loop or another, because it doesn't print the whole array.
    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
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    67
    Rep Power
    0
    yes only for loop for printf was missing ..
    and what is use of getchar here?
    without it it works fine ...

    btw how come compiler didnt warn or give error if we declare
    int array[size]; after pritnf...
    Last edited by swapy; January 28th, 2013 at 11:59 PM.
  8. #5
  9. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    Originally Posted by swapy
    btw how come compiler didnt warn or give error if we declare
    int array[size]; after pritnf...
    That is legal code per C++ standards and C99 standards. Only older C compilers will complain about it.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    68
    Rep Power
    4
    You can also simplify your code a little bit.

    Code:
    sizeof(array)/sizeof(array[0])
    You're using this to determine the number of elements in your array, but you already have a variable in your program where you stored that.
    Doing this might not be necessary, but it could help you when you are rereading or debugging old code.

    btw how come compiler didnt warn or give error if we declare int array[size]; after pritnf...
    In C, you can use variables before they have been initialized. If you do this, the variable's value is most likely some garbage that had been stored on the stack earlier. That's why your compiler might compile your code, but it should at least give you a warning.

    yes only for loop for printf was missing .. and what is use of getchar here? without it it works fine ...
    The getchar function returns a char from stdin. This means it's blocking your program since its waiting for some input.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    67
    Rep Power
    0
    but scanf is doing the same thing as getchar ....
    if scanf is used why next statement is getchar ?? i didnt get the purpose
  14. #8
  15. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    In this instance, the getchar() serves no purpose.

    If the user types in 123 456 789 or
    123
    456
    789

    it makes no difference to the %d format, since it will automatically skip any whitespace (newline, spaces, tabs) before trying to convert the next number.

    All the conversion formats of scanf, except %c and %[ have the property of skipping leading whitespace.

    If however, you switch from scanf() to fgets(), then the semantics change, and you have to do some cleanup of the input stream - which is typically done using getchar() to read characters until a \n is seen (or EOF is seen).
    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
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Location
    Hyderabad
    Posts
    10
    Rep Power
    0

    Change condition part of for loop


    change the condition of for loop and no need of two conditions at a time either one or double value would be inserted by arrays.

IMN logo majestic logo threadwatch logo seochat tools logo