Thursday, July 30, 2009

How can one Implement a function in C that takes an array of integers,& and reverses the order of items in the

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

How can one Implement a function in C that takes an array of integers,%26amp; and reverses the order of items in the
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...
Reply:void reverse_order(int array[], int n)


{


int returnedArray[n];


int index = 0;


while( n %26gt; 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!


No comments:

Post a Comment