Question

In c++, build a program that does simple calculation function(s) such as summing, averaging, etc. using...

In c++, build a program that does simple calculation function(s) such as summing, averaging, etc. using separately complied files for the implementation and interface. Points will be rewarded based on the separate files working tongether successfully.
0 0
Add a comment Improve this question Transcribed image text
Answer #1


Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you

MyFunctions.h
----

#ifndef MyFunctions_h
#define MyFunctions_h

int sum(int arr[], int n);
int max(int arr[], int n);
int min(int arr[], int n);
double avg(int arr[], int n);

#endif /* MyFunctions_h */


MyFunctions.cpp
---------------

#include "MyFunctions.h"
int sum(int arr[], int n){
   int s = 0;
   for(int i = 0;i < n; i++)
       s += arr[i];
   return s;
}

int max(int arr[], int n){
   int largest = arr[0];
   for(int i = 1; i < n; i++){
       if(arr[i] > largest)
           largest = arr[i];
   }
   return largest;
}

int min(int arr[], int n){
   int smallest = arr[0];
   for(int i = 1; i < n; i++){
       if(arr[i] < smallest)
           smallest = arr[i];
   }
   return smallest;
}

double avg(int arr[], int n){
   return ((double)sum(arr, n)) / n;
}

main.cpp
-----
#include <iostream>
#include "MyFunctions.h"
using namespace std;

int main(){
   int arr[20];
   int n;
  
   cout << "How many number do you want to enter? ";
   cin >> n;
   cout << "Enter the numbers separated by space: ";
   for(int i = 0; i < n; i++)
       cin >> arr[i];
  
   cout << "Sum: " << sum(arr, n) << endl;
   cout << "Max: " << max(arr, n) << endl;
   cout << "Min: " << min(arr, n) << endl;
   cout << "Avg: " << avg(arr, n) << endl;
  
   return 0;
}


output
---
How many number do you want to enter? 5
Enter the numbers separated by space: 4 2 8 1 6
Sum: 21
Max: 8
Min: 1
Avg: 4.2

Add a comment
Know the answer?
Add Answer to:
In c++, build a program that does simple calculation function(s) such as summing, averaging, etc. using...
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
  • C++ programining exercise 2. Do Chapter 10's Programming Exercise 8, using the name Lab12ClockType Enhanced. (Your...

    C++ programining exercise 2. Do Chapter 10's Programming Exercise 8, using the name Lab12ClockType Enhanced. (Your files will build upon the files that you created for the previous program.) Your program should consist of three files: clockTypeEnhanced h, a header file that declares your class. clockTypeEnhanced.cpp, an implementation file that defines your class's member functions. Lab12ClockTypeEnhanced.cpp, a file that tests your class by creating a clockTypeEnhanced object and demonstrating the member functions. Show me your working program

  • C++ Use C++ functions and build a program that does the most basic job all students...

    C++ Use C++ functions and build a program that does the most basic job all students have to contend with, process the grades on a test and produce a summary of the results. The big wrinkle is, it should be a multi-file program. -Requires three simple things: Figure out the best score of all scores produced Figure out the worst score of all scores produced Assign a letter grade for each score produced Complete this lab by writing three functions....

  • Please help I am confused to where start. It C++ program thank you The stoi function...

    Please help I am confused to where start. It C++ program thank you The stoi function converts a string to an integer, but does not check whether or not the value of the string is a good integer. If the stoi function fails, an exception is thrown and the program terminates. Write a program that asks the user to enter two strings, one called sNumerator and the other sDenominator. Using the stoi function, convert the two strings to numbers but...

  • Please help me to write a program in C++ with comments and to keep it simple...

    Please help me to write a program in C++ with comments and to keep it simple as possible. Thank you! Here is my program I have done before and that need to be change: #include <iostream> #include <iomanip> using namespace std; // function prototype – returning one value double cylvol(double, double); // Note: two data types inside brackets double surfarea(double, double); // Note: two data types inside brackets int main() { double r, l, V, S; cout << setprecision(2) <<...

  • Writing Unix Utilities in C (not C++ or C#) my-cat The program my-cat is a simple...

    Writing Unix Utilities in C (not C++ or C#) my-cat The program my-cat is a simple program. Generally, it reads a file as specified by the user and prints its contents. A typical usage is as follows, in which the user wants to see the contents of my-cat.c, and thus types: prompt> ./my-cat my-cat.c #include <stdio.h> ... As shown, my-cat reads the file my-cat.c and prints out its contents. The "./" before the my-cat above is a UNIX thing; it...

  • WRITE A PROGRAM IN C THAT DOES THE FOLLOWING Read a floating point number without using...

    WRITE A PROGRAM IN C THAT DOES THE FOLLOWING Read a floating point number without using scanf or conversion functions from the C library. You would need to write a utility function that processes console input using only getc and/or getchar and flow of control logic to process character-based input from the console to process the input.

  • (c programming): write a program that contains a function named getName, that does not get any...

    (c programming): write a program that contains a function named getName, that does not get any parameter and returns a character array of a random name from a constant list of 30 names, in a global array. the function returns that random name. each name in the list is a character string that consists of not more than 20 characters(not including finishing 0). the name consists of only characters from the american alphabet (a-zA-Z) and does not contain any "white"...

  • Student ID: 123 Write a C+ program with the following specifications: a. Define a C++ function (name it function_Student...

    Student ID: 123 Write a C+ program with the following specifications: a. Define a C++ function (name it function_StudentlD where StudentID is your actual student ID number) that has one integer input (N) and one double input (x) and returns a double output S, where N S = n 0 and X2 is given by 0 xeVn n 0,1 Хл —{2. nx 2 n 2 2 m2 x2 3 (Note: in the actual quiz, do not expect a always, practice...

  • Overview: file you have to complete is WordTree.h, WordTree.cpp, main.cpp Write a program in C++ that...

    Overview: file you have to complete is WordTree.h, WordTree.cpp, main.cpp Write a program in C++ that reads an input text file and counts the occurrence of individual words in the file. You will see a binary tree to keep track of words and their counts. Project description: The program should open and read an input file (named input.txt) in turn, and build a binary search tree of the words and their counts. The words will be stored in alphabetical order...

  • Develop a functional flowchart and then write a C++ program to solve the following problem. 1....

    Develop a functional flowchart and then write a C++ program to solve the following problem. 1. Create a text file named c1.txt and write your brand of computer (like Dell, HP, etc) in the file. You will be reading the name of the file from the keyboard as a string, using the string class. Your program will also read the brand of your computer from the keyboard. The process of the file creation (name of the file, mode for opening...

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
ADVERTISEMENT