Click here to Skip to main content
15,903,671 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I want to write a C program by using FUNCTION. This program should make the elements of list reverse...
I found a program but it looks very hard for me as a beginner. Is there is any simple way I can do this program by using function....

What I have tried:

C
#include <stdio.h> 
 
void reverse_list(int arr[], int size) { 
   int temp, i; 
   for (i = 0; i < size/2; i++) { 
      temp = arr[i]; 
      arr[i] = arr[size-i-1]; 
      arr[size-i-1] = temp; 
   } 
} 
 
int main() { 
   int arr[] = {1, 2, 3, 4, 5,6}; 
   int size = sizeof(arr)/sizeof(arr[0]); 
   int i; 
 
   printf("Original list: "); 
   for (i = 0; i < size; i++) { 
      printf("%d ", arr[i]); 
   } 
 
   reverse_list(arr, size); 
 
   printf("\nReversed list: "); 
   for (i = 0; i < size; i++) { 
      printf("%d ", arr[i]); 
   } 
 
   return 0; 
}
Posted
Updated 1-Apr-23 22:52pm
v3
Comments
Richard MacCutchan 31-Mar-23 7:00am    
You need to start by defining what type of list you want to reverse. It is then just a matter of iterating the list from end to beginning, copying each element to a new list. What you do with the result is up to you.
Rick York 31-Mar-23 11:05am    
Yes, this program already uses a function to do it.

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So start by thinking about how you would do it manually: write a list on paper and manually reverse it. Think about how you did that, and then consider what you might need to do to do that with a computer.

Since your assignment wants a function think about what you need to p[ass to it, and what it should return; about what a list is (because there are several different collections that can be considered a list, from an array to a linked list of pointers) then think about writing a function prototype. With that, you can start planning what to do inside the function!

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
The present program reverses the order in an array. An array is the simplest form of a list. There is also already a reverse_list() function that reverses the order. To make it easier to read you could add a swap function.
C
void swap(int* a, int* b)
{  /* TODO */ }

The function reverse_list() would then be simple:
C
void reverse_list(int arr[], int size) {
  // int temp;
  for (int i = 0; i < size / 2; i++) {
      swap(&arr[i], &arr[size - i - 1]);
  }
}
 
Share this answer
 
v2
Imagine that an array is Ice cube tray that you divided up into 5 slots .
int arr[] = {1, 2, 3, 4, 5};
It is up to you on what you fill in your ice cube tray and the number of slots you want in it.

Now Imagine that you indexed /numbered each slot with numbers 0,1,2,3 and 4 .

So now you have a Ice cube tray all divided and indexed and ready to be filled up.
Lets fill it up with chocolates in the shape of numbers
In slot 0 we place chocolate 1.
In slot 1 we place chocolate 2.
In slot 2 we place chocolate 3.
In slot 3 we place chocolate 4.
In slot 4 we place chocolate 5.


So you want to know which chocolate is first in the ice cube tray ?
That's easy because in an array the Index keeps track of what's first.
So Slot 0 is always first , Slot 1 is second and so on ...

Okay so far so good but now your friend comes over and decides that he wants to unpack all the ice cubes
you have in the tray and to repack them back but in descending order.

So the first thing your friend does is he first figures out which Ice cube tray (array) and how many ice cubes are in it (Size)

int size = sizeof(arr)/sizeof(arr[0]); 


The main reason your friend wants to know the size of the array is to understand how many slots are in it aka whats the last slot since he already knows:

1) That the 1st slot is 0
2) and that all the other slots are in numerical order aka 0,1,2,3

So once he figures out the size of the array (ice cube tray) he can than attempt to unpack it and keep track of things during swap.

So in technical terms :

The line int arr[] = {1, 2, 3, 4, 5}; initializes an integer array arr with 5 elements containing the values 1, 2, 3, 4, 5.

The line int size = sizeof(arr)/sizeof(arr[0]); calculates the size of the array arr by dividing the total number of bytes occupied by the array (which is given by sizeof(arr)) by the number of bytes occupied by a single element of the array (which is given by sizeof(arr[0])).

Okay so now that you understand all that it's finally time to do the reverse .
Note now I am giving you an example for array arr of integers: {1, 2, 3, 4, 5} that is to be reversed.

First :
The function reverse_list takes an integer array arr and its size size as inputs

Than it declares two local variables temp and i.

Than it initializes the loop with i = 0 and continues until i < size/2.
Inside the loop, it stores the value of arr[i] in the temporary variable temp.


Now for the Actual Swap :
It assigns the value of arr[size-i-1] to arr[i] to swap the values.

It assigns the value of temp to arr[size-i-1] to complete the swap.


Finally The loop continues to the next iteration until it has swapped all the necessary elements.


So this is how it Works for the swap :


Iteration 1:

i=0
arr[i]=1
arr[size-1-i]=5
temp=1
arr[i]=5
arr[size-1-i]=1

Iteration 2:

i=1
arr[i]=2
arr[size-1-i]=4
temp=2
arr[i]=4
arr[size-1-i]=2

And so on ...



Explanation :


The loop starts with i=0.
The value of the current element arr[i] is 1.
The corresponding element to swap with is arr[size-1-i], which is arr[4] or 5.
The current element arr[i] is assigned the value of arr[4] or 5.
The corresponding element arr[size-1-i] is assigned the value of arr[i], which is 5.
The temporary variable temp is assigned the value of the original arr[i], which was 1.
The loop continues with i=1.
The value of the current element arr[i] is 2.
The corresponding element to swap with is arr[size-1-i], which is arr[3] or 4.
The current element arr[i] is assigned the value of arr[3] or 4.
The corresponding element arr[size-1-i] is assigned the value of arr[i], which is 4.
The temporary variable temp is assigned the value of the original arr[i], which was 2.
The loop continues with i=2.
The value of the current element arr[i] is 3.
The corresponding element to swap with is arr[size-1-i], which is also arr[2] or 3.
The current element arr[i] is assigned the value of arr[2] or 3.
The corresponding element arr[size-1-i] is assigned the value of arr[i], which is also arr[2] or 3.
The temporary variable temp is assigned the value of the original arr[i], which was 3.
The loop exits because i=3 and size/2=2.
After the loop, the array arr has been reversed in place, and its values are now {5, 4, 3, 2, 1}.
 
Share this answer
 
v6

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