In: Computer Science
use repil.it edit my code please i already did all part but need to edit more its run for some not shwing all
intro to C-programin be sure to edit on my 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);
**{note} from teacher need to edit
Why are you creating the const array
If you make it const you can't change the elaments.
For size calc use the function sizeof()
you have declare search_for as global variable
in the below statement "void search_array(const my_array[],int size){" you have to give datatype of array.
variable temp scope is main function, you need to declare the temp variable in sort_array()
_____________________________________________\
/
//libraries
//files that we need to include
#include
#include
#include
//Function prototype:
double get_mean(const int [], int);
void search_array(const int [], int);
void sort_array(int [], int);
int main(void){
int my_array[5];
//input requesting from user
printf("Please Enter 5 integers, hit enter after each input:\n");
//for loop to get input
for(int i=0; i<5; i++){
scanf("%d",&my_array[i]);
}
//function call for mean of the integer
double m=get_mean(my_array,5);
//print mean
printf("The mean of array is:%.2f\n",m);
search_array(my_array,5);
sort_array(my_array,5);
printf("Array sorted in order: ");
return 0;
}
//function definition to get_mean
double get_mean(const int my_array[],int size){
double sum=0.0;
for(int i=0; i
//calculate to get mean
sum+=my_array[i];
}
double get_mean = sum/size;
return get_mean;
}
//function definition to search_array
void search_array(const int my_array[], int size){
int search_for = 42;
for(int i=0;i
if(my_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");
}
// Function to print an array
void printArray(int my_array[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", my_array[i]);
printf("\n");
}
// Function to Sort array
void sort_array(int my_array[], int size){
int temp;
for(int i=0;i
for (int j=i+1;j
if (my_array[i] > my_array[j]){
temp=my_array[i];
my_array[i]=my_array[j];
my_array[j]=temp;
}
}
}
}
//libraries file that we need to include
#include<stdio.h>
//Function prototype:
double get_mean(const int [], int);
void search_array(const int [], int);
void sort_array(int [], int);
void printArray(int [], int );
int search_for;
int main(void)
{
int my_array[5],size;
double m;
//input requesting from user
printf("Please Enter 5 integers, hit enter after each input:\n");
for(int i=0; i<5; i++) //for loop to get input
{
scanf("%d",&my_array[i]);
}
size=sizeof(my_array)/sizeof(my_array[0]); //Using sizeof() function to calculate the size of the array
m=get_mean(my_array,size); //Calling get_mean() function to get the mean of the array
printf("The mean of the array is:%lf\n\n",m); //printing mean
search_array(my_array,size); //Calling search_array() function to search for the global variable
sort_array(my_array,size); //Calling sort_array() function to sort the array
printf("Array sorted in order: "); //Printing the sorted array
printArray(my_array,size); //Calling printArray() function to print the array
return 0;
}
//function to calculate mean
double get_mean(const int my_array[],int size)
{
int sum=0;
for(int i=0; i<size;i++)
sum+=my_array[i]; //calculate to get mean
double get_mean = (double)sum/size;
return get_mean;
}
//function to search for 42
void search_array(const int my_array[], int size)
{
search_for = 42; //Setting the global variable search_for to 42
int flag=0;
for(int i=0;i<size;i++)
{
if(my_array[i]==search_for)
{
//found
flag=1;
break;
}
}
if(flag==1)
printf("You wrote The Answer to the Great Question!\n\n"); // found
else
printf("You might not know the answer to Life, the Universe, and Everything\n\n"); //not found
}
// Function to print array
void printArray(int my_array[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", my_array[i]);
printf("\n");
}
// Function to Sort array
void sort_array(int my_array[], int size)
{
int temp;
//Sorting in descending order
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size;j++)
{
if (my_array[i] < my_array[j])
{
temp=my_array[i];
my_array[i]=my_array[j]; //Swapping my_array[i] and my_array[j]
my_array[j]=temp;
}
}
}
}
The ouput :
PLEASE LIKE THE ANSWER IF YOU FIND IT HELPFUL OR YOU CAN COMMENT IF YOU NEED CLARITY / EXPLANATION ON ANY POINT.