(Mean and Median Program) With the “response_method1” array, calculate mean and
ID: 3883412 • Letter: #
Question
(Mean and Median Program)
With the “response_method1” array, calculate mean and medium values. The supplied code checks your results and prints messages indicating if your values are correct. When creating a medium function, check that the two middle elements are averaged in an array with an even number of elements. Using Ubuntu
1] Do not change function names etc that has been given to you.
[2] Do not add additional libraries (i.e. for strings, and sorting), you need to do this yourself.
[3] Problems 1,2,3, must be done recursively. Thus, there shouldn't be any for/while loops.
[4] To clarify the purpose of low and high in recursiveMinimum read the below:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
// Project Includes
#include
#define SIZE_PROBLEM1 10
#define SIZE_PROBLEM2 30
#define SIZE_PROBLEM3 10
#define MAXRANGE_PROBLEM3 1000
#define SIZE_PROBLEM4 100
//
// Functions
////////////////////////////////////////////////////////////////////////////////
//
// Function : main
// Description : The main function for the CMPSC assignment #1
//
// Inputs : argc - the number of command line parameters
// argv - the parameters
// Outputs : 0 if successful test, -1 if failure
int main(int argc, char *argv[]) {
/////// Problem 4 Start //////////////////////////////////////
// array of responses
printf(" -------------------------------------------------------");
printf(" Problem 4 Start");
//Method 1
int response_method1[SIZE_PROBLEM4] = {6, 7, 8, 9, 8, 7, 8, 9, 8, 9,
7, 8, 9, 5, 9, 8, 7, 8, 7, 1,
6, 7, 8, 9, 3, 9, 8, 7, 1, 7,
7, 8, 9, 8, 9, 8, 9, 7, 1, 9,
6, 7, 8, 7, 8, 7, 9, 8, 9, 2,
7, 8, 9, 8, 9, 8, 9, 7, 5, 3,
5, 6, 7, 2, 5, 3, 9, 4, 6, 4,
7, 8, 9, 6, 8, 7, 8, 9, 7, 1,
7, 4, 4, 2, 5, 3, 8, 7, 5, 6,
4, 5, 6, 1, 6, 5, 7, 8, 7, 9};
double mean_result = mean(? ? ? ? ); // process mean
double median_result = median(? ? ? ? ); // process median
if (mean_result == 6.6200)
{
if (median_result == 7.0)
{
printf(" Method1 Results for Problem 4 Correct ");
}
else
{
printf(" Method1 Check your median result for Problem 4 ");
}
}
else
{
printf(" Method1 Check your mean result for Problem 4 ");
}
//Method 2
if (0) //Change to 1 to complete this section
{
//Load problem4_data.txt
double mean_result = mean(? ? ? ? ); // process mean
double median_result = median(? ? ? ? ); // process median
if (mean_result == 6.6200)
{
if (median_result == 7.0)
{
printf(" Method2 Results for Problem 4 Correct ");
return(0);
}
else
{
printf(" Method2 Check your median result for Problem 4 ");
return(1);
}
}
else
{
printf(" Method2 Check your mean result for Problem 4 ");
return(1);
}
}
printf(" Problem 4 End");
printf(" -------------------------------------------------------");
Explanation / Answer
double mean_result = mean( response_method1,100); // process mean
//mean function
double mean(int array[],int n){
double m=0;
for(int i=0;i<n;i++){
m = m+array[i];
}
m= m/n;
return m;
}
double median_result = median(response_method1,100 ); // process median
//median function
double median(int array[],int n){
int i, j;
for (i = 0; i < n-1; i++){
for (j = 0; j < n-i-1; j++){
if (array[j] > array[j+1]){
int temp = array[j];
array[j] = array[j+1];
array[j+1]= temp;
}
}
}
double med;
int middle;
middle = n/2.0;
if (n % 2 ==0 )
{
med = (array[middle] + array[middle + 1]) / 2.0;
}
else
{
med = array[middle + 1];
}
return med;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.