Friday, July 31, 2009

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

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

Implement a function in C that takes an array of integers,%26amp; and reverses the order of items in the array?
Wouldn't it be better if you wrote it yourself? I'm sure your instructor would think so.





Oh well - it's your loss...








#include %26lt;stdio.h%26gt;





void printArray(int array[], int n)


{


int x=0;


for (x=0; x%26lt;n; x++) {


printf("%d ",array[x]);


}


printf("\n");


}





void reverse_order(int array[], int n)


{


int tmpInt, x;


/* Assume n is the number of elements */


for (x=1; x%26lt;n/2; x++) {


tmpInt=array[x-1];


array[x-1]=array[n-x];


array[n-x]=tmpInt ;


}


}





#define MAX 10





int main() {


int array[MAX]={1,2,3,4,5,6,7,8,9,10};


printArray(array, MAX);


reverse_order(array,MAX);


printArray(array, MAX);


return 0;


}
Reply: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 %26lt;--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", %26amp;n);


for(i=0;i%26lt;n;i++)


scanf("%i", %26amp;(array[i]));


reverse_order(array, n);


for(i=0;i%26lt;n;i++)


printf("%i ", array[i]);


printf("\n");


}
Reply:im not sure about C, but i can write it in C++, so hope this helps. here it goes:





#include %26lt;iostream%26gt;





// since u want this function specifically to be


// void, i will assume that you you want this function to


// print it automatically from here.





void reverseOrderAndPrint(int arrayIn[], int n)


{





using namespace std;





cout %26lt;%26lt; "\n\nyour entry in reverse is: \n";


for(int i=0, j=n; i %26lt; n; i++, j--)


{





cout %26lt;%26lt; "itm#" %26lt;%26lt; j %26lt;%26lt; " : " %26lt;%26lt; arrayIn[j-1] %26lt;%26lt; "\t";





}





}





void main()


{





using namespace std;





const int count = 10;


int intVals[count];





for(int i=0; i %26lt; count; i++)


{





cout %26lt;%26lt; "Please give a value for Item #" %26lt;%26lt; i+1 %26lt;%26lt; ": ";


cin %26gt;%26gt; ((int)intVals[i]);





}





reverseOrderAndPrint(intVals, count);





}


No comments:

Post a Comment