Question

In: Computer Science

4. Write a function called mean that will return the average value of an integer array....


4. Write a function called mean that will return the average value of an integer array. The integer array and its length must be passed to the function as parameters.

5.Write a function called max that will return the index of the max value of an integer array. The integer array and its length must be passed to the function as parameters.
E.g. array = {1,2,3,4,5,6,7,8,9,0}
max = 9
index = 8

6.Write a function called variance that calculates and returns the variance of an array that is passed to it as a parameter. Also, pass the length of the array as a parameter.
variance = mean(array^2) - (mean(array))^2
array^2 = square of each element of the array
You must use the mean function above in your calculation.

c language

Solutions

Expert Solution

/*****************************************MaxIndex.c**********************************************/

#include<stdio.h>
#include<math.h>
int max(int array[], int size){
  
   int max = 0,i,index;
   for(i=0;i<size;i++){
      
       if(max<array[i]){
          
           max = array[i];
           index = i;
       }
   }
  
   return index;
}
int main(){
  
   int array[] = {1,2,3,4,5,6,7,8,9,0};
   int size = sizeof(array)/sizeof(array[0]);
   int maxIndex = max(array,size);
   printf("max index is: %d\n",maxIndex);
   return 0;
}

/*********************output******************/

max index is: 8

--------------------------------

/********************************ArrayMean.c***************************/

#include<stdio.h>
#include<math.h>
float mean(int array[],int size){
  
   float total = 0;
   int i;
   for(i=0;i<size;i++){
      
       total+=array[i];
   }

   return total/size;
}
int main(){
  
   int array[] = {1,2,3,4,5,6,7,8,9,0};
   int size = sizeof(array)/sizeof(array[0]);
   float meanOfArray = mean(array,size);
   printf("Mean is: %f\n",meanOfArray);
   return 0;
}

/********************output*******************/

Mean is: 4.500000

--------------------------------

/*************************Variance.c***********************/

#include<stdio.h>
#include<math.h>
float mean(int array[],int size){
  
   float total = 0;
   int i;
   for(i=0;i<size;i++){
      
       total+=array[i];
   }

   return total/size;
}
float variance(int array[],int size){
  
   int total = 0,i;
   for(i=0;i<size;i++){
      
       total+=pow(array[i],2);
   }
   float meanOfArraySquare = (float)total/size;
   float squareOfMean = pow(mean(array,size),2);
   return meanOfArraySquare - squareOfMean;
}
int main(){
  
   int array[] = {1,2,3,4,5,6,7,8,9,0};
   int size = sizeof(array)/sizeof(array[0]);
   float var = variance(array,size);
   printf("Variance is: %f\n",var);
  
   return 0;
}

/******************output*******************/

Variance is: 8.250000

--------------------------------

Please let me know if you have any doubt or modify the answer,Thanks:)


Related Solutions

Challenge 4 – Random Integer in Range Write a function to return a random integer between...
Challenge 4 – Random Integer in Range Write a function to return a random integer between a minimum value and maximum value. var ival = IntRandomRange(, );
Write a function called fillList that takes three parameters, an integer array, input file, and size....
Write a function called fillList that takes three parameters, an integer array, input file, and size. The function should fill the integer array with randomly generated values between two numbers lowLim and highLim read from the input file. in C++
Write a function called draw_card. It takes no arguments and returns an integer representing the value...
Write a function called draw_card. It takes no arguments and returns an integer representing the value of a blackjack card drawn from a deck. Get a random integer in the range 1 to 13, inclusive. If the integer is a 1, print "Ace is drawn" and return 1. If the integer is between 2 and 10, call it x, print "<x> is drawn" and return x (print the number, not the string literal "<x>"). If the number is 11, 12,...
Write a function maxFun() that returns the maximum value in any given integer array. Determine the...
Write a function maxFun() that returns the maximum value in any given integer array. Determine the function parameters (complete the area shown in ___________. ___________maxFun(____________________________) { }
Write a value returning function called isPrime. This function accepts integer number as parameter and checks...
Write a value returning function called isPrime. This function accepts integer number as parameter and checks whether it is prime or not. If the number is prime the function returns true. Otherwise, function returns false. A prime number is the number that can be divided by itself and 1 without any reminder, i.e. divisible by itself and 1 only. DO THIS USING C++ LANGUAGE .WITH UPTO CHAPTERS 5 (LOOP).
Write a C function to calculate and return the factorial value of any positive integer as...
Write a C function to calculate and return the factorial value of any positive integer as an argument. Then call this function from main() and print the results for the following input values: 2, 3,4, 5, 10, 15 What is the maximum value of an integer for which factorial can be calculated correctly on a machine using your algorithm?
Write a java script function that accepts integer array as input, and display a new array...
Write a java script function that accepts integer array as input, and display a new array by performing fol lowing modifications, • The values in odd index positions must be incremented by 1 • The values in even index positions must be decremented by 1. • Assume the array index starts from 0. Input 1: arr = [1,2,3,4] Output 1: arr = [0,3,2,5 it done html and javascript and plz do it in simple way
Write a method with the following header to return an array of integer values which are...
Write a method with the following header to return an array of integer values which are the largest values from each row of a 2D array of integer values public static int[] largestValues(int[][] num) For example, if the 2D array passed to the method is: 8 5 5 6 6 3 6 5 4 5 2 5 4 5 2 8 8 5 1 6 The method returns an array of integers which are the largest values from each row...
Write a function that will take in an array (of type double), and will return the...
Write a function that will take in an array (of type double), and will return the array with all of the elements doubled. You must use pass-by-reference and addressing and/or pointers to accomplish this task. C++
Software Decode: Write a function that accepts an in-order array of unsigned integer values. The function...
Software Decode: Write a function that accepts an in-order array of unsigned integer values. The function shall then scan the array for a specific pattern: Three values contained within the array equally spaced 20 units apart. The function shall return the index position within the original array where the pattern begins or -1 if not present. Given the input array: data[] = {10,20,31,40,55,60,65525} The function shall return: 1 IN JAVA PLEASE
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT