Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C++
#include <iostream.h>
#include <conio.h>

void main()
{
    clrscr();
    int *p1,*p2,n,i;
    cout<<"Enter the size of the array ";
    cin>>n;
    p1=new int[n];
    p2=p1;
    for(i=0;i<n;i++)
        cin>>*p2++;
    p2=p1;
    for(i=n;i>0;i--)
        cout<<*p2--;
    getch();
    }

}


i input values into array created using new operator and want to print these values in reverse order but
program give wrong output

i enter
1
2
resut is 13262 instead of 2 1
Posted
Updated 24-Apr-13 4:12am
v4
Comments
CHill60 24-Apr-13 9:35am    
It might be worth posting some sample data that you've input, what the "wrong output" was and what you were expecting to see

Look at what you are doing:
You allocate "n" elements, and then load "n" of them. Where is the pointer after the first loop?
Answer: pointing at the element after the last one.

So when you try to cout the values in the second loop, the first access is to an element that doesn't exist...

Try it on paper with a value of n=2 and you will see what I mean.
 
Share this answer
 
Comments
CHill60 24-Apr-13 9:41am    
My +5. Only spotted that after I'd reformatted the code - amazing what whitespace can do for the eyes
Ian A Davidson 24-Apr-13 10:44am    
+4. Correct result but wrong reason. He's resetting p2=p1, so he's pointing at the beginning of the array, but then in the second loop he's doing p2-- so it's (presumably) outputting the first integer (1) and then pointing to random information before the array.
Regards,
Ian.
OriginalGriff 24-Apr-13 10:58am    
Not in the original version, which was the one I answered:
"#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int *p1,n,i;
cout<<"Enter the size of the array ";
cin>>n;
p1=new int[n];
for(i=0;i<n;i++)
cin>>*p1++;
for(i=n;i>0;i--)
cout<<*p1--; // problem is here
getch();
}

i input values into array created using new operator and print these values in reverse order but
program give wrong out put"
Ian A Davidson 24-Apr-13 11:06am    
Oh yep. Sorry I "checked" that by doing a comparison, but obviously I did something wrong there. Oh dear. Changed it to a 5 :D
OriginalGriff 24-Apr-13 11:28am    
Don't worry about it - I'm not going to! :laugh:
Without changing too much, try this instead:
C++
#include <iostream.h>
#include <conio.h>

void main()
{
    clrscr();
    int *p1,n,i;
    cout<<"Enter the size of the array ";
    cin>>n;
    p1=new int[n];
    for(i=0;i<n;i++)
        cin>>p1[i];
    for(i=n;i>0;i--)
        cout<<p1[i-1];
    delete [] p1;
    getch();
}
Regards,
Ian.
 
Share this answer
 
v4
Comments
nv3 24-Apr-13 11:43am    
Particularly for a beginner that is way better to read and understand than OP's original code. My 5.
Ian A Davidson 24-Apr-13 13:12pm    
Thank you. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900