logo elektroda
logo elektroda
X
logo elektroda

[C++] Zero Appears Before Inverted Array Elements - Debugging Code

bobik89 19632 7
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 7865814
    bobik89
    Level 10  
    Hello

    I have to write a program that, after entering ten elements of an array, will display the array "backwards".
    I already have it written, but there is a small problem. In this inverted array, before all these elements, as you can see, I get "0" why?

    here is the code:
    
    #include <cstdlib>
    #include <stdio.h>
    
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {   
        int tab[9];
        int *wskaznik;
        wskaznik=tab;
      
     printf("Wprowadz elementy tablicy 10-cio elementowej: \n\n");
     for(int i=0; i<=9; i++)
     {
     printf("Podaj %d element tablicy: ", i);
     scanf("%d", &wskaznik[i]);
     }
     
     
     printf("\nNastepujaca tablica wyswietla  elementy wprowadzonej tablicy: \n");
     for(int i=0; i<=9; i++,wskaznik++)
     {      
     printf("%d", *wskaznik);
     printf("\n");
     }
    
    *wskaznik=0;
     printf("\nWprowadzone elementy w odwrotnej kolejnosci: \n"); 
     for (int i=10; i>=0; i--)
     {
     printf("%d", *wskaznik--);
     printf("\n");
     }
    
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    


    I corrected the title. - arnoldziq
  • ADVERTISEMENT
  • #2 7865954
    arnoldziq
    VIP Meritorious for electroda.pl
    Because you enter 10 (from 0 to 9) and you read 11 (from 10 to 0).
  • ADVERTISEMENT
  • #3 7865996
    Dżyszla
    Level 42  
    You did not correctly point to the array element in the last loop. Generally, I would recommend using array notation by moving the pointer itself.
  • ADVERTISEMENT
  • #4 7866457
    bobik89
    Level 10  
    I don`t understand... in the last loop I tried changing the 9 to 8 or 10 and nothing helps, it`s even worse. And we have to write the program "only on pointers"
  • ADVERTISEMENT
  • #5 7866459
    akaz_
    Level 13  
    As arnoldziq wrote.

    Generally, you don`t need pointers at all to get the job done.
    It is true that the whole thing will work faster (the memory locations of the array elements will be calculated faster), but as you can see, it also takes more code,
    and it`s very easy to get lost.

    So alternatively you could do something like this:
    
    for (int i=9; 0<=i; i--)
       cout << tablica[i];
    



    EDIT:
    I didn`t have time to read the post that it must be on the indicators.
    The problem is not the loop, but what is before the loop, namely: *index = 0;
  • #6 7868020
    bobik89
    Level 10  
    when I delete this line *pointer=0; this throws some number out of my mind
  • Helpful post
    #7 7868042
    Dżyszla
    Level 42  
    Let`s get back to the theory...
    for(exp_1;cond;exp_2)

    The order of execution is:
    exp_1
    cond
    exp_2
    cond
    ...

    So, before the condition prohibiting loop execution occurs (or more precisely: before the loop execution condition is not met), the pointer is moved outside the array (10th element). So, to return to the correct one in the next loop, you must either initialize it with the position of the last element of the array or simply "rewind" it by this one position. You can do this either before the loop, in the loop initialization code, or by pre-decrementing the loop.

    Also, an important mistake! The array has 9 elements (elements with indexes from 0 to 8), and you save 10 elements! What`s worse, you`re already reading 11!
  • #8 7868393
    bobik89
    Level 10  
    ok, it`s working now. Thank you very much!

Topic summary

The discussion revolves around a C++ program that is intended to display an array of ten elements in reverse order. The user encounters an issue where a "0" appears before the inverted array elements. Responses highlight that the problem arises from incorrect array indexing and pointer manipulation. Specifically, the user mistakenly attempts to read 11 elements instead of 10, leading to accessing an out-of-bounds memory location. Suggestions include correcting the loop to properly reference the last element of the array and avoiding unnecessary pointer usage. Ultimately, the user resolves the issue by adjusting the pointer initialization and loop conditions.
Summary generated by the language model.
ADVERTISEMENT