Question:
How can one Implement a function in C that takes an array of integers,& and reverses the order of items in the
hisluv
2007-09-13 04:31:55 UTC
The function you implement must have the following form:
void reverse_order(int array[], int n)
{
/* your code goes here: re-order the n items in the array so that the
first one becomes the last
* * and the last one first.
*/
}
You should then implement a main() function which reads in a set of integers into an array,
reverses the order of the number (using the function you have just defined) and prints them out
again.
For example, when complete and compiled, you should be able to run your program, and enter
the numbers:
2 4 5 11 23 1 4
then program will then print out:
4 1 23 11 5 4 2
I have tried writing one but am failing to get the part dat enters the numbers
Three answers:
Lala
2007-09-13 04:52:43 UTC
well lets say...



ping[10] and pong[10]



for 0 to 10 using i

pong[10-i] = ping[0+i]



for 0 to 10 again using i

ping[i]=pong[i]



i can put it in real c or c++ but you got it you need an other array if not well use a single integer and do swaping...
verdie
2016-05-18 08:35:46 UTC
Hi I think that this does the job you may need to mess around with the input and output and consider input and range checking. void reverse_order(int array[], int n) { /* your code goes here: re-order the n items in the array so that the first one becomes the last * * and the last one first. */ int l; // left pointer int temp; // temp storage for (l = 0; l <--n; l++) { temp = array[l]; array[l] = array[n] array[n] = temp; } int main() { int array[100]; int n; int i; revere printf("How many numbers do you have? "); scanf("%i", &n); for(i=0;i
anonymous
2007-09-13 04:50:41 UTC
void reverse_order(int array[], int n)

{

int returnedArray[n];

int index = 0;

while( n > 0 )

{

n= n-1;

returnedArray[index] = array[n];

index++;

}

array = returnedArray;

}



you can change the function to return The new Array, instead of "void" use "int[]".



BY!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...