Question

Exercise 3.11. Write a function that computes the integer mean of an array of integers. For example, the integer mean of -1, 4,2 is (-1 4 2)/3 5/3 1, where denotes integer division. The function should implement the following specification 1 Computes the integer me an of an array and stores it 2 where mn references. Returns -1 for erroneous input 3 (len 0 or NULL array); otherwise returns 0. 5 int mean (int a, int len, int mn. Implement a unit test of mean in a main function.if it is possible help me with the code in C language and I could learn it, thanks !

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is code:

int mean(int *a,int len,int *mn)
{
if(len <= 0 || a == NULL) // len is zero of a is null
{
return -1;
}
else
{
int sum = 0;
int i;
//calculate the sum of array
for(i = 0 ; i < len ; i++)
{
sum += *(a + i);
}
// calculate the mean
*mn = sum / len;
return 0;
}
}

Sample program to test:

#include <stdio.h>

int main()
{
int array[3] = {-1, 4, 2};
int *p;
p = array;
int var = 0; /* actual variable declaration */
int *m; /* pointer variable declaration */
m = &var; /* store address of var in pointer variable*/

int result = mean(p,3,m);
if(result == -1)
printf("Invlid input");
else
printf("result is %d\n",*m);

return 0;
}
int mean(int *a,int len,int *mn)
{
if(len <= 0 || a == NULL) // len is zero of a is null
{
return -1;
}
else
{
int sum = 0;
int i;
//calculate the sum of array
for(i = 0 ; i < len ; i++)
{
sum += *(a + i);
}
// calculate the mean
*mn = sum / len;
return 0;
}
}

Output:

result is 1

Add a comment
Know the answer?
Add Answer to:
if it is possible help me with the code in C language and I could learn...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • please explain [5 pts] What is the outcome of compiling and executing the following code. Assume...

    please explain [5 pts] What is the outcome of compiling and executing the following code. Assume it is embedded in an otherwise correct and complete C++ program void printBackwards(int *arr, int len){ if (len <=0) return; cout< } int main(){ int arr[4] = {1, 2, 3, 4}; printBackwards(arr, 4); } [10 pts] Implement a function that returns the minimum value in an integer array ​‘arr’​ of length ‘len’​. You MUST use a RECURSIVE solution. //Precondition: An integer array with length...

  • 2b Please code in language: Ocaml TODO: Implement a recursive OCaml function ascending : int ->...

    2b Please code in language: Ocaml TODO: Implement a recursive OCaml function ascending : int -> int list, which accepts an integer n as input (again, assume n ≥ 0), and returns a list of integers from 0 to n in ascending order. starter code: let ascending (n : int) : int list = (* your work here *) [ ]

  • c++ show code thanks, only use library <iostream> Question 1 Write the following two functions The...

    c++ show code thanks, only use library <iostream> Question 1 Write the following two functions The first function accepts as input a two-dimensional array of integers. It returns two results: the sum of the elements of the array and the average The second function accepts as input a two-dimensional array of integers and integer value V. It returns true if the value V is found in the array, false otherwise. Write a main program that declares and initializes the following...

  • help me solving this both please in c++ language 6 Write function max(int al Lint n)...

    help me solving this both please in c++ language 6 Write function max(int al Lint n) that receives an array of integer and its length, it returns the maximum number in that array. Test your function in main. 7) Write functin reverse (char x[ .int n) that reieves an array of characters and reverse the order of its elements. Test this function in main.

  • Write Java code to implement a FSM machine that recognizes the language for the alphabet {a,b,c} ...

    Write Java code to implement a FSM machine that recognizes the language for the alphabet {a,b,c} consisting of all strings that contain two consecutive c's and end with b.                   Your FSM program should include the following three static methods (Java) or functions (C):                                           a.    int nextState(int state, char symbol)                                  A state-transition function that returns the next state based on the                                  current state and an input symbol. This function should also return                                  -1 when an invalid input character is detected.                                  State...

  • PLEASE HELP!!! C PROGRAMMING CODE!!! Please solve these functions using the prototypes provided. At the end...

    PLEASE HELP!!! C PROGRAMMING CODE!!! Please solve these functions using the prototypes provided. At the end please include a main function that tests the functions that will go into a separate driver file. Prototypes int R_get_int (void); int R_pow void R Jarvis int start); void R_fill_array(int arrayll, int len); void R_prt_array (int arrayl, int len) void R-copy-back (int from[], int to [], int len); int R_count_num (int num, int arrayll, int len) (int base, int ex) 3. Build and run...

  • I need help solving this problem using the ML programming language Write a function sqsum of...

    I need help solving this problem using the ML programming language Write a function sqsum of type int -> int that takes a non-negative integer n and returns the sum of the squares of all the integers 0 through n. Your function need not behave well on inputs less than zero

  • use c++ language, keep it simple i am using code block Exercise #2: Digitise a number...

    use c++ language, keep it simple i am using code block Exercise #2: Digitise a number Write the function digitiselint, int[]) of type int, which takes an integer N and finds all the digits of that integer and save them in an array. The function then returns the number of digits in N. Write the main() program that reads an integer, calls the function digitisel ), and prints the digits in reverse order. Sample input/output: Enter an integer: 2309456 The...

  • write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...

  • Code in C language ADT: typedef struct{ int ID; float salary; int age; }Employee; ...

    code in C language ADT: typedef struct{ int ID; float salary; int age; }Employee; Specification: In this lab, five functions need to be implemented using the given ADT. 1. Employee* readRecord(FILE*) This function receives a FILE pointer created before. It reads a line from the provided csv file, and creates an Employee struct pointer with the information from that line then returns the pointer back to the calling function. Each line in the provided csv file contains the id, salary,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT