Answer to Question #265261 in C++ for Pranali

Question #265261

Write a program, maxmin.c, which finds the maximum and minimum of a set of 

floats stored in the file samplesin.txt. You can assume that the number of values in the input file is less than 100.

Your main must use the following steps:

ˆ open the two files samplesin.txt and maxminout.txt.

ˆ read the values from samplesin.txt into the array x.

ˆ compute the maximum and minimum values found in the array x.

ˆ write the maximum and minimum values to the le maxminout.txt.

Your output file should look like :

There are 80 values in the array

The maximum value in the array is 63.605999

The minimum value in the array is 4.808900


1
Expert's answer
2021-11-12T17:27:32-0500
#include<iostream>
#include<fstream>
#include<string>
using namespace std;


int main()
{    
    int n = 0; 
    int num;
    float arr[100];


    ifstream File;
    File.open("samplesin.txt");
    ofstream File2;
    File.open("maxminout.txt");


    while(!File.eof())
    {
        File >> arr[n];
        n++;
    }


    File.close();
    int max=arr[0];
    int min=arr[0];
    for(int i=0;i<n;n++)
    {
        if(arr[i]>max){
            max=arr[i];
        }
        if(arr[i]<min){
            min=arr[i];
        }
    }
    
    File2<<"\nThere are "<<n<<" values in the array";
    File2<<"\nThe maximum value in the array is "<<max;
    File2<<"\nThe minimum value in the array is "<<min;
    File2.close();
    cout << "done\n";
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS