Question

In: Computer Science

The goal of this project is to practice (Write a C Program) with a function that...

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:

  • float (*f)(float*, int): This parameter itself is a function: f that has two parameters and returns a floating-point number. In the body of the function: func we call the function: f with its arguments to be the second and third parameters of the function: func (i.e. a, and length). The parameters:
    • float*: This represents an array of type: float.
    • int: A parameter of a simple type to be used for the size of the array.
  • float* a: This represents an array of type: float.
  • int length: A parameter of a simple type to be used for the size of the array.

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);

Solutions

Expert Solution

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;
}


Related Solutions

Goal: Write a program that provides practice in class development and implementation of programs in multiple...
Goal: Write a program that provides practice in class development and implementation of programs in multiple separate files. Make sure you thoroughly read and understand the directions before you begin PART 1: Write a program that displays a temperature conversion chart. There should be 6 functions defined as part of a class. • print_introduction_message • get_conversion_table_specifications • print_message_echoing_input • fahrenheit_to_celsius • fahrenheit_to_kelvin • print_table A portion of the code is defined below but not using classes. Add the 4 missing...
This is c++. The goal is to test each method function by writing a simple program...
This is c++. The goal is to test each method function by writing a simple program displaying how they work in int main . #ifndef ARRAY_FUNCTIONS_H #define ARRAY_FUNCTIONS_H #include <iostream> #include <iomanip> using namespace std; template <typename T> class Array_functions { public: int size; int cap; int* ptr; //default ctor Array_functions(); // ctor with one arg Array_functions(int size); //allactes a dynamic array T* allocate(int n); //resizes array onto another T* resize_arr(T *dest,int old_cap, int new_cap); //deletes array void delete_arr(T& a);...
Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for...
Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for Bubble Sort b. Use a dynamic array of integers in a variable size of n. c. Display the following information: 1) Total counts of comparisons 2) Total counts of shifts / moves / swaps, whichever applies d. Write a main() function to test a best, and an average cases in terms of time efficiency i. Fill out the array with random numbers for an...
write a program in C Write a function that is passed an array of characters containing...
write a program in C Write a function that is passed an array of characters containing letter grades of A, B, C, D, and F, and returns the total number of occurrences of each letter grade. Your function should accept both lower and upper case grades, for example, both 'b' and 'B' should be bucketed into your running total for B grades. Any grade that is invalid should be bucketed as a grade of 'I' for Incomplete. You must use...
Can you solve this C program by using Function? Q1. Write a C program to ring...
Can you solve this C program by using Function? Q1. Write a C program to ring the computer bell at any number of times you specify. Use the system clock as a delay, you need to include the time header file.
write C++ program using functions (separate function for each bottom) Write a program to find if...
write C++ program using functions (separate function for each bottom) Write a program to find if a number is large word for two given bottom base - bottom1 and bottom2. You can predict that a number, when converted to any given base shall not exceed 10 digits. . the program should ask from user to enter a number that it should ask to enter the base ranging from 2 to 16 after that it should check if the number is...
Write a C++ PROGRAM: Add the function min as an abstract function to the class arrayListType...
Write a C++ PROGRAM: Add the function min as an abstract function to the class arrayListType to return the smallest element of the list. Also, write the definition of the function min in the class unorderedArrayListType and write a program to test this function.
C++ Write a program that has two functions. The 1st function is the main function. The...
C++ Write a program that has two functions. The 1st function is the main function. The main function should prompt the user for three inputs: number 1, number 2, and an operator. The main function should call a 2nd function called calculate. The 2nd function should offer the choices of calculating addition, subtraction, multiplication, and division. Use a switch statement to evaluate the operator, then choose the appropriate calculation and return the result to the main function.
Program in C Write a function that takes a string as an argument and removes the...
Program in C Write a function that takes a string as an argument and removes the spaces from the string.
1- Write it with C++ program §Write a function Rotate that rotates an array of size...
1- Write it with C++ program §Write a function Rotate that rotates an array of size n by d elements to the left §Use array as argument §In the main function, call the function Rotate and show the rotated array §Test your code For example: Input: [1 2 3 4 5 6 7], n = 7, d = 2 Output: [3 4 5 6 7 1 2] 2- Write it in C++ §Search Insert Position •Given a sorted array in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT