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

    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    -858993460 Value when use prinft, please help


    Hi, i'm starting with my course of Structured programming and i'm programming in C, so, here i am in Microsoft Visual C++ 2010 making this program of print an array of 20 numbers in order and backwards, but when the backward array is printed it shows "-858993460" in the first value and i don't know why the program do this, i would like you to help me to solve this and a little explanation of why and what can i do for avoid it in the future.

    Here is the code and the result:
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
    	
    	int Items[20];
    	int i;
    	puts("introduce 20 numbers, followed by return");
    
    	for (i=0; i<20; i++)
    		scanf("%d", &Items[i]);
    	
    	for (i=0; i<20; i++)
    	printf("%d ",Items[i]);
    	printf("\n");
    	
    	for (i=20; i>0; i--)
    	printf("%d ",Items[i]);
    	
    	getchar();
    	getchar();
    	return 0;
    }
    and here is the result

    Photo Result

    EDIT: I don't know why the photo of the results is not showing but it's something like

    Code:
    Introduce 20 numbers followed by return
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
    -858993460 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5  4 3 2
    debugged in cmd
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,366
    Rep Power
    1870
    > for (i=20; i>0; i--)
    This should be
    for (i=19; i>=0; i--)
    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0
    Originally Posted by salem
    > for (i=20; i>0; i--)
    This should be
    for (i=19; i>=0; i--)
    Thanks so much, that work perfectly!
    Now if it is not too much to ask i would to know, the reason why is that serie of number showing and why is diferent
    Code:
    (i=19; i>=0; i--)
    from
    Code:
    (i=20; i>0; i--)
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,366
    Rep Power
    1870
    Rather than printing Items[i], why not just print i instead.
    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

IMN logo majestic logo threadwatch logo seochat tools logo