Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

reversing the order of array

i am really fed up. please help me where i am getting wrong. i just cant get my mistake..
for input 1,2,3,4,5 its giving 5,2,3,4,1....

#include
#include

void reverse(int a[], int k)
{


int j,i,t;
for (i=1; i<=k; i++)
{ cin>>a[i]; }

for (j=k; j>=k/2; j--)
{ t=a[j];

a[j]=a[k-j+1];

a[k-j+1]=t;
}
for (j=1; j<=k; j++)
{cout<<a[j]<<"
"; }
}
void main()
{
int a[5];


reverse(a, 5);
}

Comments

  • WaltPWaltP Posts: 335Member
    : #include
    : #include
    Why are you using [b]iostream.h[/b] AND [b]stdio.h[/b]? And iostream.h has been replaced ages ago. It's just [b]iostream [/b]now.

    : for (i=1; i<=k; i++)
    : { cin>>a[i]; }
    If your function is supposed to reverse the data, why does it input the data? A function should do one thing, and do it well.

    : for (j=1; j<=k; j++)
    Arrays in C++ start at 0, not 1

    : void main()
    [b]main()[/b] is not and never has had a [italic]void[/italic] definition. It is [italic]int[/italic].

    Why don't you post your code in CODE tags so we can read it properly? Assuming of course you formatted it properly in the first place.

    Last, you can easily debug your own code by outputting variable and equation values at key places so you can see if the values are correct.
Sign In or Register to comment.