C++ Reverse Array

Reverse an integer array in C

The problem is, you swap every element twice (unless it is the center one) which ultimately does not swap anything. At the first loop, you technically swap first and last item. At the last loop, you did the same. So, you reverse the first action, returning the item back to original position.

Another problem is you try to access arr[5] which is undefined as your array is of size 5 and thus the index should be from 0 to 4 only.

Following is a function reversing the array:

void ReverseArray(int arr[], int size)
{
for (int i = 0; i < size/2; i++)
{
int temp = arr[i];
arr[i] = arr[size - 1 - i];
arr[size - 1 - i] = temp;
}
}

Reverse Array C

What you are doing in your reverseAr1D function is to copy the already reversed elements in temp in the reversed order into ar. So you will end up with the original order of elements.

In your reverseAr1D function you have to change the second for loop to this:

for(int i = 0; i < size; i++){
*(ar + j) = *(temp + i);
j++;
}

How to reverse an array and store the values of the reversed array in a new array

Because you use new array as the argument of koko function, so you do not need to return it. If you want to return it, you should change the type of returning from int to int *.

In koko function, d has to always begin at 0, so you do not need to declare it as the argument of this function. Let's declare it as the local variable in this function.

c is one of iterator of for loop, so let's declare it in this function with the initial value equals to size - 1 (size is the size of array or the number of elements in this array).

BTW, this function become as below:

void koko(int *array,int *array2,int size)
{
int d = 0;
int c;
for(c = size - 1;c>-1;c--,d++)
{
array2[d]=array[c];
}
}

For using this function in main, you just need to give two arrays and size of the array (7 in this case):

 koko(niz,niz2,7);

The complete code for testing:

#include <stdio.h>

void koko(int *array,int *array2,int size)
{
int d = 0;
int c;
for(c = size - 1;c>-1;c--,d++)
{
array2[d]=array[c];
}
}

int main()
{
int niz[]={2, 4, 5, 7, 4, 8, 3};
int niz2[7];
koko(niz,niz2,7);
for(int i = 0; i <7; i++)
printf("%d ",niz2[i]);
}

The output:

3 8 4 7 5 4 2 

Reverse an array in c

You are reversing the array twice; that's why you have the original array.

How? Take for an example a 10-element array. As your first step, you'll swap 0th and 9th element. As your last step, you'll swap 9th and 0th element. Net result: no change.

You don't want to run i up to n. You want to run i up to j, so that you never swap elements back (i.e. change the condition to while (i < j) { ... })

How to reverse every string in an array of strings through a function in C?

For starters this code snippet

char **arr;
arr[0]="John";
arr[1]="Doe";
arr[2]="Programmer";

invokes undefined behavior because the pointer arr is uninitialized and has an indeterminate value.

Moreover this approach in any case is wrong because you may not change string literals.

What you need is to declare a two-dimensional array as for example

enum { N = 11 };

//...

char arr[3][N] =
{
"John", "Doe", "Programmer"
};

In this case the function declaration will look like

void invert( char arr[][N], int n );

The enumeration must be declared before the function declaration.

Instead of the two-dimensional array you could declare an array of pointers like

char s1[] = "John";
char s2[] = "Doe";
char s3[] = "Programmer";
char * arr[3] = { s1, s2, s3 };

In this case the function declaration may be as shown in your question

void invert(char** arr, int n)

So what you need to do with minimal changes is to substitute this code snippet

char **arr;
arr[0]="John";
arr[1]="Doe";
arr[2]="Programmer";

for this code snippet

char s1[] = "John";
char s2[] = "Doe";
char s3[] = "Programmer";
char * arr[3] = { s1, s2, s3 };

Can we reverse the digits of a number using Array in c?

do{
n=n/10;
count++;
}while(n>0);

print n after this loop and see if it's value is what you expect. n is 0 after this loop.

I'd suggest creating a temporary variable like this:

int temp;

and assign the value of n to temp(temp=n;).

Now when the do while loop ends, assign temp back to n so that n contains it's initial value.

Also, in this loop,

for(i=0;i<count;i++){
a[i]=n%10;
}

you should do n=n/10 (like you did while counting the number of digits) after a[i]=n%10.

So your final code may look like this:

#include<stdio.h>
int main(){
int n,count=0;
int temp;
scanf("%d",&n);
temp=n;
do{
n=n/10;
count++;
}while(n>0);
n=temp;
int i,a[100];

for(i=0;i<count;i++){
a[i]=n%10;
n/=10;
}

for(i=0;i<count;i++){
printf("%d",a[i]);
}
return 0;
}

Also, you didn't really need the do while loop to count the number of digits, you could've done this in the first for loop like this:

#include<stdio.h>
int main(){
int n,count=0;
scanf("%d",&n);
int i,a[100];
for(i=0;n>0;i++){
a[i]=n%10;
n/=10;
count++; //counting the number of digits
}

for(i=0;i<count;i++){
printf("%d",a[i]);
}
return 0;

}

Reversing a structed array with a recursive function

In these statements

(a->items[0])=(a->items[a->size]);
(a->items[a->size])=tmp;

you access memory beyond the allocated array because the valid upper index is a->size - 1.

Instead of decreasing the data member size by one

a->size=a->size-1;

you have to decrease it by two.

a->size=a->size-2;

Moreover the function changes the values of data members size and items of the original object passed as an argument. So after exiting the function the state of the original object will be changed.

And this expression

a+1

does not make sense because you passed to the function a pointer to a single object of the type Tarray.

The function can look the following way as it is shown in the demonstrative program below.

#include <stdio.h>
#include <stdlib.h>

typedef struct Sarray
{
int *items;
size_t size;
} Tarray;

void ArrayReverse ( Tarray *t )
{
if ( ! ( t->size < 2 ) )
{
int tmp = t->items[0];
t->items[0] = t->items[t->size - 1];
t->items[t->size - 1] = tmp;

t->size -= 2;
++t->items;

ArrayReverse( t );

t->size += 2;
--t->items;
}
}

int main(void)
{
Tarray t = { 0 };
size_t n = 5;

t.items = malloc( n * sizeof( int ) );

if ( t.items != NULL ) t.size = n;

for ( size_t i = 0; i < t.size; i++ )
{
t.items[i] = ( int )( i + 1 );
}

ArrayReverse( &t );

for ( size_t i = 0; i < t.size; i++ )
{
printf( "%d ", t.items[i] );
}

putchar( '\n' );

free( t.items );

return 0;
}

The program output is

5 4 3 2 1

Reversing array in C

Start by initializing the array:

char inputString[1001] = {0};

Reverse array in C language

Already there are great answers provided by the experts..

I am trying to answer in my way.

Basically when reverseArray(ar, start, end); is called
start=1,end=3

Recursively calling reverseArray(ar, start, end);

reversed your array and returned control when start=2 end=2

But your first while loop hasn’t finished yet and undoing what you wanted to achieve.

Function call by value , by reference is playing the game here.

dryrun

You are passing reference of arr but for start and end you are passing value.



Related Topics



Leave a reply



Submit