November 19, 2024

CurledMark

curled up on the sofa

Introduction of reverse the contents of an array program

In this article, the program is explained in a sequential way. First, we approach the program by understanding the swapping concept. Then we think of the series printing which is the previous step in the swapping program. Then we write the program for swapping the numbers and use the same logic to swap using the array which will output us as reverse the contents of an array program. So if we show this simply, we used:

  • Printing the number series logic
  • The numbers swapping logic
  • The array swapping logic

Reverse an array program

The snippet of printing the reverse the contents of an array program

y=0;

While(y<=9)

{

Printf(“%d”,x[y]);

y++;

}

Explanation

If we interchange the 0th place value with the 9th one,1st place with the 8th one,2nd place with the 7th one, and so on, the reverse of the contents of the array is completed which is the requirement of the program. So let us perform “Swapping”.

What is Swapping in C?

Swapping two numbers in the C programming language means exchanging the values of two variables. Suppose you have two variables var1 & var2. Value of var1 is 20 & value of var2 is 40. So, after swapping the value of var1 will become 40 & value of var 2 will become 20.

Printing the below series logic

0    9

1    8

2    7

3    6

4    5

e=0;

f=9;

While(e<f)

{

Printf(“%d%d\n”,e,f);

e++;

f–;

}

Write a program to print the series

0    9

1    8

2    7

3    6

4    5

#include<stdio.h>

int main()

{

int e,f;

e=0;

f=9;

While(e<f)

{

Printf("%d%d\n",e,f);

e++;

f--;

}

return 0;

}

Explanation

Now we have known how to print the above series. We can derive the swapping program from this as we want to swap the first place with the last and so on.

Swapping logic

e=10;

f=3;

We will declare a new variable g and assign the value of e to g. Then f to e and then g to f. If we work out this, the value will be swapped.

g=e;

e=f;

f=g;

Working

g=10

e=3

f=10

You can compare this with the above initialization, they are swapped.

Write a program to swap two numbers

#include<stdio.h>

int main()

{

int e,f,g;

e=10,f=3;

g=e;

e=f;

f=g;

Printf("Value of e is %d\n",e);

Printf("The value of f is %d\n",f);

return 0;

}

Output:

Value of e is 3

The value of f is 10

The logic for the reverse the contents of an array

g=x[e];

x[e]=x[f];

x[f]=g;

Accept 10 numbers in an array and reverse the contents of the array

#include<stdio.h>
int main()
{
int x[10],y,e,f,g;
y=0;
while(y<=9)
{
printf(“Enter a number:”);
scanf(“%d”,&x[y]);
y++;
}
e=0;
f=9;
while(e<f)
{
g=x[e];
x[e]=x[f];
x[f]=g;
e++;
f–;
}
y=0;
while(y<=9)
{
printf(“%d\n”,x[y]);
y++;
}
return 0;
}

The output of reverse the contents of an array program

Enter a number: 10

Enter a number: 20

Enter a number: 30

Enter a number: 40

Enter a number: 50

Enter a number: 60

Enter a number: 70

Enter a number: 80

Enter a number: 90

Enter a number: 100


Enter a number: 100

Enter a number: 90

Enter a number: 80

Enter a number: 70

Enter a number: 60

Enter a number: 50

Enter a number: 40

Enter a number: 30

Enter a number: 20

Enter a number: 10

Reference

http://www.freebookcentre.net/Language/Free-C-Books-Download.html

http://www.freebookcentre.net/programming-books-download/Computer-Programming-Fundamentals-Using-C-Part1.html

Conclusion of reverse the contents of an array program

The program is successful as you can see. Please try to write the program and run it. We have tried our best to explain the logic and program so we hope you find this implementation alternative. Stay tuned for the upcoming blogs.

Also, read

https://curledmark.in/the-largest-number-program-in-an-array-in-c-programming/

https://curledmark.in/armstrong-number-and-arrays-in-c-programming/

https://curledmark.in/prime-reversepower-programs-in-c-programming/

The Book Of Five Rings Summary

The Blue Umbrella

How to Talk to Anyone by Leil Lowndes

Jonathan Livingston Seagull Book Summary

Your Brain on PORN Book Summary by Gary Wilson

THE IMMORTALS OF MELUHA By Amish Tripathi full summary

Eleven Minutes by Paulo Coelho Summary

Hyperfocus by Chris Bailey Summary

Black Beauty by Anna Sewell Summary

You Can Heal Your Life by Louise Hay Summary

ATTITUDE IS EVERYTHING BY JEFF KELLER SUMMARY

THE BIOGRAPHY OF SWAMI VIVEKANANDA

THE SHAWSHANK REDEMPTION EXPLAINED

Turtles All the Way Down by John Green Story Explained