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
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.
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++ ?
(C++ programming) Assignment *Circle Class -Radius r (private) as an attribute variable -Member function -Get(): Function...
(C++ programming) Assignment *Circle Class -Radius r (private) as an attribute variable -Member function -Get(): Function that returns r value of property variable -Put(int d): A function that stores d in attribute variable r *Declare a one-dimensional array of type Circle and in each array element Read and store integers from standard input device *Declare the swap() function to swap two elements with each other *Write a program that sorts the elements of a one-dimensional array of circle type in...
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|)...
Suppose that Andrew has the utility function: U(C,M)= C0.5+M0.5 , where C represents cupcakes and M...
Suppose that Andrew has the utility function: U(C,M)= C0.5+M0.5 , where C represents cupcakes and M represents muffins. Andrew has income of $150. Suppose that the initial price per cupcake is $2 and the price per muffin is $1. At these prices, Andrew spends $100 on muffins. When cupcakes are on sale, each cupcake costs $1. When cupcakes are on sale, Andrew spends $75 on muffins. The price of muffins and Andrew’s income is unchanged throughout. 1. Draw a diagram...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT