Question

In: Computer Science

Write two functions in C++ called readData() and findAverage(). Please answer it in C++ The first...

  1. Write two functions in C++ called readData() and findAverage().

Please answer it in C++

The first function should take a reference to an ifstream object and a reference to an array of double, read data into the array from the file and return the number of values read. It should stop reading when the end of the file is reached. The data file contains prices of an unspecified item, one per line.

The second function takes a reference to an array of double, and an int indicating the number of elements in the array and then calculates and returns the average of the values in the array.

(b) The program below is designed to use the functions readData() and findAverage()defined in part a above to read the data from a file called prices.txt to the array and then calculate and display the average. Currently, the program has missing sections indicated by the empty text boxes. Complete the program by inserting the missing statements into the spaces provided.

Solutions

Expert Solution

Code for the following problem:-

#include <iostream>
#include<fstream>
using namespace std;

int readData(ifstream &ptr,double* values)// function to read data from prices.txt
{double x;
int count=0;
if (!ptr) { //to check if our file is read correctly.
cout << "Unable to open file datafile.txt";
exit(1); // call system to stop
}
while (ptr >> x) {
values[count]=x;
count++;
}

return(count);
}


double findAverage(double* values,int count)// function to fid average
{double sum=0;
for(int i=0;i<count;i++)
sum+=values[i];
  
return(sum/count);
}

int main()
{
ifstream inFile;
inFile.open("prices.txt");
double values[1000];
int count=readData(inFile,values);
double average=findAverage(values,count);
cout<<"Average= "<<average;

return 0;
}

Sample input file used :-

10.54
20
45.98
23.55
45.8

Screenshots of IDE:-

SCREENSHOT OF INPUT FILE USED:-

SCREENSHOT OF OUTPUT:-

************************IN CASE OF ANY DOUBT FEEL FREE TO LEAVE A COMMENT***************************


Related Solutions

JavaScript Write a function called "first" that takes in two arguments - the first is an...
JavaScript Write a function called "first" that takes in two arguments - the first is an argument called arr that is an array of numbers, the second is an optional number argument called num(hint: you'll need a default value - look back at the slides). If "num" was not passed in (since it's optional), the "first" function will return an array containing the first item of the array. If a value for "num" was given, the "first" function will return...
Please answer it in C++ (a) Declare a class called Book representing a textbook.      A...
Please answer it in C++ (a) Declare a class called Book representing a textbook.      A textbook has a title (eg. “Programming in C++”) A textbook has an author (eg. “Helen Johnstone”) A textbook has a size in number of pages (eg. 550) A textbook has a price (eg. $135.99). These attributes should be represented as the data members of the class and be hidden from the outside of the class. The class should also have a constructor used to...
PLEASE ANSWER I WILL RATE YOUR ANSWER AND THUMBS UP For the following C functions: int...
PLEASE ANSWER I WILL RATE YOUR ANSWER AND THUMBS UP For the following C functions: int q7(int x) {     return x & (~x+1); } int q8(int x, int m, int n) {     int a = ~m+1;     int b = ~x +1;     a = x + a;     b = b + n;     return !((a|b) >> 31); } int q9(int x, int n) {    /* assume x and n are not a negative integer */...
Please write code for C language Problem: Write a couple of functions to process arrays. Note...
Please write code for C language Problem: Write a couple of functions to process arrays. Note that from the description of the function you have to identify what would be the return type and what would be part of the parameter. display(): The function takes an int array and it’s size and prints the data in the array. sumArray(): It takes an int array and size, and returns the sum of the elements of the array. findMax(): It takes an...
Please solve questions in C++ ASAP!! thank you (a) Write a function in C++ called readNumbers()...
Please solve questions in C++ ASAP!! thank you (a) Write a function in C++ called readNumbers() to read data into an array from a file. Function should have the following parameters: (1) a reference to an ifstream object (2) the number of rows in the file (3) a pointer to an array of integers The function returns the number of values read into the array. It stops reading if it encounters a negative number or if the number of rows...
Write a C program, called reverse, using standard I/O functions, to take a file as input...
Write a C program, called reverse, using standard I/O functions, to take a file as input then copies it to another file in reverse order. That is, the last byte becomes the first, the byte just before the last one becomes the second, etc. The program call should look like: reverse fileIn fileOut
In C++ Create two functions called TheNumber. One version of TheNumber should accept a string and...
In C++ Create two functions called TheNumber. One version of TheNumber should accept a string and output the accepted string 10 times. The other version of TheNumber should accept a double and output the accepted double 10 times. This uses function overloading.
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.
C++ Please Fill in for the functions for the code below. The functions will be implemented...
C++ Please Fill in for the functions for the code below. The functions will be implemented using vectors ONLY. Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public: // Default Constructor Stack() {// ... } // Push integer n onto top of...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer list using dynamic array ONLY (an array that can grow and shrink as needed, uses a pointer an size of array). Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: class List { public: // Default Constructor List() {// ... } // Push integer n onto...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT