Question

In: Computer Science

C++ and there has to be a FUNCTION to get all the integers. Suppose there is...

C++ and there has to be a FUNCTION to get all the integers.

Suppose there is a file called MidtermExam.txt that is full of integers. Write a program that complies with the following instructions:

Write a function that you will later call in main. Your function is to read from the file all the integers and print to the screen the total of all the numbers in the file, and the average with 2 significant digits after the decimal point.
If your program is unable to access the file, you should tell the user that (show a message) and then shut down the program.
When you are done reading from the file, give the commands to close the file.
Note: I did not forget to attach an input file. You can use the one in lecture 6's module if you want, or create dummy file yourself. I am interested in your code.

Solutions

Expert Solution

Hello!

I have used the function so that

  • Reads all the integers from file
  • Checks whether it can be accessed or not
  • If can access then reads all the integers to an array
  • Calculates the total of all integers and prints the total
  • Calculates the average of all integers and prints the average

Some method used in the code

ifstream is used to read files as our code requires only reading operation we used ifstream

ifstream file; // file is a variable name

file.open("fileName.ext"); // this opens the file mentioned

file.is_open() returns true if file is opend, or else false if we cannot able to open or access it.

file.is_eof() // returns true when the cursor reaches at the end of the file ,

file >> num1 // moves the data integer to num1 from the file

So finally at the end of the we should close the file

file.close() // closes the file

CODE:

#include <iostream>
#include <fstream>
#include <stdio.h>

using namespace std;

void readFileCalTotalAvg()
{
  int data[1000], num = 0;
  double total = 0.0, avg;
  // ifstream for reading file
  ifstream file;
  // open method to open file
  file.open("MidtermExam.txt");
  // loop conftinues till it reaches the end of the file
  if (!file.is_open())
  {
    cout << "File cannot be accesed!" << endl;
  }
  else
  {
    while (!file.eof())
    {
      // data from file copies to data[num]
      file >> data[num];
      // stores num of integers in file
      num++;
    }
    // this is because there's an extra line in the text file
    num--;
    // closing a file
    file.close();

    // printing the data stored from file in data array
    //for (int i = 0; i < num; i++)
    //  cout << data[i] << endl;

    // iterarting over each integer and add to total
    for (int i = 0; i < num; i++)
      total += data[i];

    // display the total of all integers in the file
    cout << "Total of all the marks: " << total << endl;
    // calculates the average of all integers
    avg = total / num;
    // displays the average of all the integres
    cout << "Average marks: ";
    // printing only 2 digits after decimal point
    printf("%.2f\n", avg);
  }
}

int main()
{
  // calls the function which read the file and calculates the total and average
  readFileCalTotalAvg();
  return 0;
}

OUTPUT:

When Data is in horizontal

When data is in vertical order

When file cannot be accesed

Hope this helps and clear.

I strive to provide the best of my knowledge so please upvote if you like the content.

Thank you!


Related Solutions

In C++, create a function exchangesl that takes an argument of an array of integers (...
In C++, create a function exchangesl that takes an argument of an array of integers ( for C++ use implement void exchangesl(vector<int>& a) . Those integers need to be changed so that the smallest and largest values in the array are exchanged. Assume that there is at least one element, if the largest value occurs more than once then exchange the first instance, if the smallest value happens more than once then exchange the last instance.
In C++, create a function exchangesl that takes an argument of an array of integers (...
In C++, create a function exchangesl that takes an argument of an array of integers ( for C++ use implement void exchangesl(vector<int>& a) . Those integers need to be changed so that the smallest and largest values in the array are exchanged. Assume that there is at least one element, if the largest value occurs more than once then exchange the first instance, if the smallest value happens more than once then exchange the last instance. Use the following file:...
Develop a C++ function to find the number of even and odd integers in a given...
Develop a C++ function to find the number of even and odd integers in a given array of integers Pass in an array of integers Pass in the size of the array of integers Pass in a reference parameter for the number of even values Pass in a reference parameter for the number of odd values The function doesn't return anything Develop a C++ program to test your function
Prove by contraposition and again by contradiction: For all integers a,b, and c, if a divides...
Prove by contraposition and again by contradiction: For all integers a,b, and c, if a divides b and a does not divide c then a does not divide b + c Elaboration with definitions / properties used would be appreciated! Thanks in advance!!
Suppose that David has the following utility function over consumption level c: U(c) = √c (that’s...
Suppose that David has the following utility function over consumption level c: U(c) = √c (that’s the square root of c) Also, suppose David has wealth $1000, but faces the risk of a financial loss $500 with probability 0.2 (20% chance). He does not save anything (he consumes all the wealth he can). Now suppose that David has a choice of insuring his potential losses. The insurance policy pays him $0 if he does not have financial loss and pays...
Suppose that David has the following utility function over consumption level c: U(c) = √c (that’s...
Suppose that David has the following utility function over consumption level c: U(c) = √c (that’s the square root of c) Also, suppose David has wealth $1000, but faces the risk of a financial loss $500 with probability 0.2 (20% chance). He does not save anything (he consumes all the wealth he can). What is David’s consumption in the event of no loss? What is his utility in this case? What is David’s consumption in the event of financial loss?...
C++ programing Write a main function that  reads a list of integers from a user, adds to...
C++ programing Write a main function that  reads a list of integers from a user, adds to an array using dynamic memory allocation, and then displays the array. The program also displays the the largest element in the integer array. Requirement: Using pointer notation.
Write C++ void function called averageTemperature() that asks the user to enter integers with a sentinel...
Write C++ void function called averageTemperature() that asks the user to enter integers with a sentinel value of -999 to stop entering numbers. Then average the numbers and print the average. You will need an int acculumator for the temperatures and an int acculumator for the number of temperatures entered. Use the accumulators to calculate the average which will need to be a decimal number. Call the function from the main() function.
how do you get the sum of all elements in a stack in c++ ?
how do you get the sum of all elements in a stack in c++ ?
Suppose f is a function defined for all real numbers which has a maximum value of...
Suppose f is a function defined for all real numbers which has a maximum value of 5 and a minimum value of −7. Label each of the following as MUST, MIGHT, or NEVER true. Explain, and if you say might, give an example of yes and an example of no. A The maximum value of f(|x|) is 7. B The maximum value of f(|x|) is 5. C The maximum value of f(|x|) is 0. D The minimum value of f(|x|)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT