Akshat kant
Akshat kant

Reputation: 49

I want Print an array in reverse but it is not printing all the elements of the array

 #include <stdio.h>

 int main()

 {
 int a,n,i;
 int arr[n];
// int arr[]={1,4,3,2};
scanf("%d",&n);
for(int i=0;i<n;i++)
{
    scanf("%d",&arr[i]);
}
for(int i=n;i>=0;i--)
{
    printf("%d ",arr[i]);
}
}

Thanks guys for helping in that but now what could be the error its showing garbage value.

Upvotes: 1

Views: 167

Answers (5)

HiEd
HiEd

Reputation: 160

You are getting garbage values because your second for loop tries to print arr[n] which is out of bounds. Array indexing goes from 0 to n-1 where n is the size of the array. Ref

Here is the corrected code.

#include <stdio.h>

int main()
{
 int a,n,i;
 int arr[n];
 // int arr[]={1,4,3,2};
 scanf("%d",&n);
 for(int i=0;i<n;i++)
 {
    scanf("%d",&arr[i]);
 }
 for(int i=n;i>0;i--)
 {
    printf("%d ",arr[i-1]);
 }
 return 0;
}

Upvotes: 0

ShahzadIftikhar
ShahzadIftikhar

Reputation: 523

Need to tweak condition in for loop to i = n -1 while printing reverse array.

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 310990

For the defined array

int arr[]={1,4,3,2};

in this for loop

for(int i=3;i>=arr[0];i--)

i equal to 0 is not greater than or equal to arr[0] equal to 1. So the condition of the loop i>=arr[0] evaluates to logical false when i is equal to 0.

Maybe actually you mean the following loop

for(int i=3;i >=0;i--)

Pay attention to that it is not a good idea to use magic numbers like 3.

You could define the loop the following way

for ( size_t i = sizeof( arr ) / sizeof( *arr ); i != 0; --i )
{
    printf( "%d ", arr[i - 1] );
}

or

for ( size_t i = sizeof( arr ) / sizeof( *arr ); i != 0; )
{
    printf( "%d ", arr[--i] );
}

Also in your commented statements are placed in a wrong order

int a,n;
//int arr[n];
//scanf("%d",&n);

At first you need to read a positive value in the variable n and only after that to declare the variable length array arr

int a,n;
scanf("%d",&n);
int arr[n];

Here is a demonstrative program.

#include <stdio.h>

int main(void) 
{
    size_t n;

    if ( scanf( "%zu", &n ) == 1 && n != 0 )
    {
        int arr[n];

        for ( size_t i = 0; i < n; i++ ) 
        {
            scanf( "%d", arr + i );
        }

        for ( size_t i = 0; i < n; i++ )
        {
            printf( "%d ", arr[i] );
        }

        putchar( '\n' );

        for ( size_t i = n; i != 0; i-- )
        {
            printf( "%d ", arr[i-1] );
        }

        putchar( '\n' );
    }

    return 0;
}

If to enter the following values

5
1 2 3 4 5

then the program output will be

1 2 3 4 5 
5 4 3 2 1

Upvotes: 2

stiven solanki
stiven solanki

Reputation: 1

You can do something like:

int main ()
{
  int a, n;
//int arr[n];
  int arr[] = { 1, 4, 3, 2 };
//scanf("%d",&n);
  for (int i = sizeof (arr) / sizeof (arr[0]) - 1; i >= 0; i--)
    {
      printf ("%d ", arr[i]);
    }
}

sizeof gives size of type (for int in c size is 4) so you can divide it by any one element of the array then your iterator variable i's value is also dynamic.

Upvotes: 0

Henrik Carlqvist
Henrik Carlqvist

Reputation: 1168

As arr[0] contains 1 your code will only print arr[3], arr[2] and arr[1].

Upvotes: 0

Related Questions