C programming Write a program to declare an array of size 10

C programming.

Write a program to declare an array of size 10 and initialize it with random values in the range 1 to 54. Pass this array as well as the size to a function called reverse. The function reverse should display the values in reverse order.

Example if the random values are the following:

34

53

6

2

46

22

1

2

30

29

The function should display the values as:

29 30 2 1 22 46 2 6 53 34

Write a different function that finds the median of these values. In other words you should sort this array and find the middle value. Again in main you should pass this array to this function as well as the size.

In main call reverse again—now reverse should display the values in sorted form but in reverse order of how you sored them.

Display the largest value and smallest value. Now that the values are sorted this should be just one line.

34

53

6

2

46

22

1

2

30

29

Solution

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;

void reverse(int arr[], int size);
int * median(int arr[], int size);


int main() {
int numbers[10]; // declared an array of size 10
int max= 54;
int min = 1;
int size=10;
   int *sortedArray;
printf(\"\ Numbers before reversal : \");
for (int i = 0; i < size; i++) {
numbers[i]=(rand() % (max + 1 - min)) + min; // generating random number between 1 to 54
printf(\"%d \\t\", numbers[i]);
}
  
reverse(numbers,size);
sortedArray=median(numbers,size);

reverse(sortedArray,size);
printf(\"\ smallest value : %d \", sortedArray[size-1]);
printf(\"\ largest value : %d \", sortedArray[0]);
  
  
   return 0;
}

void reverse(int arr[], int size) {
int x,y,tempNum;
y = size - 1; // y will Point to last Element
x = 0; // i will be pointing to first element
// swapping method
while (x < y) {
tempNum = arr[x];
arr[x] = arr[y];
arr[y] = tempNum;
y--; // decrement y
x++; // increment x
}

//Print out the Result of Insertion
printf(\"\ Numbers after reversal : \");
for (x = 0; x < size; x++) {
printf(\"%d \\t\", arr[x]);
}



}

int * median(int arr[], int size){
printf(\"\ Median : \");
float tempNum;
int i, j;
// the following two loops sort the array x in ascending order
for(i=0; i< size-1; i++) {
for(j=i+1; j<size; j++) {
if(arr[j] < arr[i]) {
// swap elements
tempNum = arr[i];
arr[i] = arr[j];
arr[j] = tempNum;
  
}
}
   }
if(size%2==0) {
// if there is an even number of elements, return mean of the two elements in the middle
printf(\"%d \\t\",((arr[size/2] + arr[size/2 - 1]) / 2));
} else {
// else return the element in the middle
printf(\"%d \\t\",arr[size/2]);
}
  
return arr;
}

output:


Numbers before reversal : 20    17    46    26    42    26    47    13    34    38   
Numbers after reversal : 38    34    13    47    26    42    26    46    17    20   
Median : 30   
Numbers after reversal : 47    46    42    38    34    26    26    20    17    13   
smallest value : 13
largest value : 47

C programming. Write a program to declare an array of size 10 and initialize it with random values in the range 1 to 54. Pass this array as well as the size to
C programming. Write a program to declare an array of size 10 and initialize it with random values in the range 1 to 54. Pass this array as well as the size to
C programming. Write a program to declare an array of size 10 and initialize it with random values in the range 1 to 54. Pass this array as well as the size to

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site