Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Arrays and reading from a file USE C++ to write a program that will read a file

ID: 3930972 • Letter: A

Question

Arrays and reading from a file

USE C++ to write a program that will read a file containing floating point numbers, store the numbers into an array, count how many numbers were in the file, then output the numbers in reverse order.

The program should only use one array and that array should not be changed after the numbers have been read.

You should assume that the file does not contain more than 100 floating point numbers. The file name is 4-7Array.txt.

the number example like

108.2444
67.8807
108.5296
107.8583
84.2688
100.0140
67.0943

.........

Explanation / Answer

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

int main()
{
  
    //open file for reading
   fstream myfile("4-7Array.txt");
   //declare variables
    float tmp;
    int size=-1;
    //array to hold nos
    float nos[100];
    //read through EOF
    while (myfile >> tmp)
    {
        //increment the count and assign value to array
        size++;
        nos[size]=tmp;
    }
    //close file
   myfile.close();

   //loop through array and display
   for(int i=size;i>=0;i--)
   {
       cout<<nos[i]<<endl;
   }
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote