Write a program that can read a list of numbers, find their average, sort the nu
ID: 3857940 • Letter: W
Question
Write a program that can read a list of numbers, find their average, sort the numbers, and search the average. The detailed tasks are described below. 1) Write a function named EvenOrOdd, which inspects the argument and returns an integer value. If the argument is even, the function returns 0; if odd, returns 1. 2) Write a function named ArrayAverage, which returns the average of an array. 3) Read 6 integers from the user’s input and store them in an array named numbers. Display the array elements (in the original order). 4) Declare an integer variable named avg. Call the function ArrayAverage to compute the average of the elements in the array numbers, and assign it to avg. 5) If avg is even, do the bubble sorting; if odd, do the selection sorting. In both cases, sort the array to an ascending order. Display the sorted array elements on the screen. 6) Apply the binary searching to find whether avg is in the array numbers. Display the searching result on the screen.
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
int EvenOrOdd(int n)
{
if(n % 2 == 0)
return 0;
else
return 1;
}
int ArrayAverage(int numbers[], int size)
{
int sum = 0, avg = 0;
for(int i=0;i<size;i++)
sum += numbers[i];
avg = sum/size;
return avg;
}
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void bubbleSort(int numbers[], int size)
{
for (int i = 0; i < size-1; i++)
{
for (int j = 0; j < size-i-1; j++)
{
if (numbers[j] > numbers[j+1])
swap(&numbers[j], &numbers[j+1]);
}
}
}
void selectionSort(int numbers[], int size)
{
int min;
for (int i = 0; i < size-1; i++)
{
min = i;
for (int j = i+1; j < size; j++)
{
if (numbers[j] < numbers[min])
min = j;
}
swap(&numbers[min], &numbers[i]);
}
}
int binarySearch(int numbers[], int start, int end, int search)
{
if (end >= start)
{
int mid = (start + end)/2;
if (numbers[mid] == search)
return mid;
if (numbers[mid] > search)
return binarySearch(numbers, start, mid-1, search);
return binarySearch(numbers, mid+1, end, search);
}
return -1;
}
int main(int argc, char *argv[])
{
int x = EvenOrOdd(atoi(argv[1])); //Even odd
if(x == 0)
printf("Even ");
else
printf("Odd ");
int size = 6;
int numbers[size];
printf("Enter array elements : ");
for(int i=0;i<size;i++)
scanf("%d",&numbers[i]); //get inputs from user for array
for(int i=0;i<size;i++)
printf("%d ",numbers[i]); //printing array elements
printf(" ");
int avg = ArrayAverage(numbers, size); //getting average from array
if(avg % 2 == 0)
bubbleSort(numbers, size); //bubble sort
else
selectionSort(numbers, size); //selection sort
for(int i=0;i<size;i++)
printf("%d ",numbers[i]); //printing array elements
printf(" ");
int result = binarySearch(numbers, 0, size-1, avg); //binary search
if(result != -1)
printf("Avg is found in array numbers ");
else
printf("Avg is not found in array numbers ");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.