In: Computer Science
use repil.it
intro to C-programin be sure Make code very basic and add good comments thank you
1-Write a program that requests 5 integers from the user and
stores them in an array. You
may do this with either a for loop OR by getting a string from
stdin and using sscanf to store formatted input in each spot in the
array.
2-Add a function to the program, called get_mean that determines the mean, which is the average of the values. The function should be passed the array and the size of the array, but use const so that it cannot be changed, only accessed. It should return a double, which is the calculated mean. The main function will print the value.
//Function prototype: double get_mean(const int [], int);
3-Create a function called search_array that searches the array for the answer to life, the universe, and everything - the integer 42. If 42 is found, print You wrote The Answer to the Great Question!. If it is not found, print You might not know the answer to Life, the Universe, and Everything. Set a global variable called search_for to store the number to search for (42)
//Function prototype: void search_array(const int [], int);
4-Create a function called sort_array. The array should be sorted in descending order. The function should be passed the array and the size of the array. The function does not return a value; the array is changed by the function. The main function will print the sorted array (you may create a print array function if desired).
//Function prototype: void sort_array(int [], int);
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
#include<stdio.h>
//global variable to store the number to search for (42)
int search_for = 42;
//method to calculate and return the mean of values
double get_mean(const int array[], int size){
double sum=0;
//finding sum of integers on the array
for(int i=0;i<size;i++){
sum+=array[i];
}
//finding mean
double mean=sum/size;
return mean;
}
//method to search the array for global variable value search_for
void search_array(const int array[], int size){
for(int i=0;i<size;i++){
if(array[i]==search_for){
//found
printf("You wrote The Answer to the Great Question!\n");
return;
}
}
//not found
printf("You might not know the answer to Life, the Universe, and Everything\n");
}
//method to print contents of an array
void print_array(const int array[], int size){
for(int i=0;i<size;i++){
printf("%d ",array[i]);
}
printf("\n");
}
//method to sort the array in descending order
void sort_array(int array[], int size){
//using bubble sort algorithm
for(int i=0;i<size;i++){
for(int j=0;j<size-i-1;j++){
if(array[j]<array[j+1]){
//swapping elements at j and j+1
int temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
}
int main(){
const int size=5;
//creating an array of size 5
int array[size];
//asking and reading values into the array
printf("Enter %d integers: ",size);
for(int i=0;i<size;i++){
scanf("%d",&array[i]);
}
//finding, displaying mean
double mean=get_mean(array,size);
printf("Mean value: %.2f\n",mean);
//searching for 42
search_array(array,size);
//sorting and displaying sorted array
sort_array(array,size);
printf("Array sorted in descending order: ");
print_array(array,size);
return 0;
}
/*OUTPUT*/
Enter 5 integers: 23 42 88 34 99
Mean value: 57.20
You wrote The Answer to the Great Question!
Array sorted in descending order: 99 88 42 34 23