In: Computer Science
The goal of this project is to practice (Write a C Program) with a function that one of its parameter is a function.The prototype of this function is:
void func ( float (*f)(float*, int), float* a, int length);
This means the function: func has three parameters:
We then define three functions with the following prototypes:
float sum(float* a, int length): Returns the sum of all the elements of the array: a.
float product(float* a, int length): Returns the product of all the elements of the array: a.
float average(float* a, int length): Returns the average of all the elements of the array: a.
For the function: main write code for the following comments.
int main (){
//Declare (define) these variables.
float* a;
int n, …; //You need to define more variables.
//Ask the user how numbers
//The user enters a positive integer. Give it to variable: n
// Use the built-in function: malloc to allocate n consecutive location of
// type float for variable: a
// Ask the user to enter n decimal numbers and give them to array: a. Do not
// forget, you need to use: & to read to an element of an array.
// Prompte the user the following message:
printf("Enter 1,2,or 3 to calculate the sum, product, or average of the numbers respectively:");
// Code based on the number the user enters. If the number entered is:
// 1 then call func with the argument: sum and two more arguments a and n.
// 2 then call func with the argument: product and two more arguments a and n.
// 3 then call func with the argument: average and two more arguments a and n.
// An integer other than 1, 2, or 3 then output the use that only numbers 1,
// 2, or 3 should be entered.
return 0;
}
The following is four sample dialogs of my program. Note that the boldfaced lines are not from the execution of the program.
Sample dialog 1: The output is the addition of numbers.
How many numbers? 3
Enter 3 number: 2.2 3.3 4.4
Enter 1, 2, or 3 to calculate the sum, product, or average of the numbers respectively: 1
The result is: 9.90
Sample dialog 2: The output is the product of numbers.
How many numbers? 3
Enter 3 number: 2.2 3.3 4.4
Enter 1, 2, or 3 to calculate the sum, product, or average of the numbers respectively: 2
The result is: 31.94
Sample dialog 3: The output is the avarage of numbers.
How many numbers? 3
Enter 3 number: 345.2 -345.5 22.4
Enter 1, 2, or 3 to calculate the sum, product, or average of the numbers respectively: 3
The result is: 7.37
Sample dialog 4: The output is an error message because the user entered 4.
How many numbers? 3
Enter 3 number: 1 2 3
Enter 1, 2, or 3 to calculate the sum, product, or average of the numbers respectively: 4
Only enter 1, 2, or 3
Answer:
#include <stdio.h>
#include <stdlib.h>
void func ( float (*f)(float*, int), float* a, int length);
float sum(float* a, int length);
float product(float* a, int length);
float average(float* a, int length);
C code
#include <stdio.h>
#include <stdlib.h>
void func ( float (*f)(float*, int), float* a, int
length){
printf("The result is: %.2f\n",(*f)(a,length));
}
float sum(float* a, int length){
float total = 0;
int i;
for(i=0;i<length;i++){
total += a[i];
}
return total;
}
float product(float* a, int length){
float prod = 1;
int i;
for(i=0;i<length;i++){
prod *= a[i];
}
return prod;
}
float average(float* a, int length){
return sum(a,length)/length;
}
int main(){
int length,i,choice;
printf("How many numbers? ");
scanf("%d",&length);
printf("Enter %d number: ",length);
float* a = (float*)
malloc(length*(sizeof(float)));
for(i=0;i<length;i++){
scanf("%f",&a[i]);
}
printf("Enter 1, 2, or 3 to calculate the sum,
product, or average of the numbers respectively: ");
scanf("%d",&choice);
if(choice == 1){
func(&sum , a , length);
}
else if(choice == 2){
func(&product , a ,
length);
}
else if(choice == 3){
func(&average , a ,
length);
}
else{
printf("Error: Invalid
choice");
}
free(a);
return 0;
}