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

I am new to programming in C++ and trying to learn. Can someone help me work thr

ID: 3884144 • Letter: I

Question

I am new to programming in C++ and trying to learn. Can someone help me work through this example problem?

Part A
You are to read the input data into an appropriate vector and then dump
the data using an appropriate function.


Part B (I believe I would be able to help my self from here, I just really need to know how to begin Part A)
You are given an input file comprising 20 (HARD-CODED 20) floating point
numbers.
You are to read these 20 numbers into a vector of double values.
The real task is this: If you take three values at a time and compute the
distance between the largest and smallest values of the three, what are the
minimum and the maximum values you find among all triples in the vector?
For example, if you had five values that were
3,1,10,7,2
then the maximum distance would be 13 (for the 10 and the 3 and any
number in between) and the minimum distance would be 2, for the triple
1,2,3.
You can do this any way you choose. You can use BFI (Brute Force and
Ignorance) and run a triple loop. You could sort, in which case the max
is the absolute value of the difference between the smallest and the largest.
Doesn’t matter.
Your program will have a header file main.h and a program file main.cc
and will be compiled with a simple makefile.
You should read from standard input and write to standard output.

The input file (file.txt) reads:

15.952
-42.021
63.987
11.909
545.178
89.522
1.023
-16.611
7.741
89.275
233.689
68.753
-75.763
2.512
598.290
-12.784
-902.620
123.971
78.632
51.132

Explanation / Answer

#include<iostream>
#include<vector>
#include<fstream>


using namespace std;

int main(){

ifstream fin;
vector<float> data;
float b;
float temp;

fin.open("file.txt");
while (fin >> b){
       data.push_back(b);
      
}
for (int i = 0; i<data.size(); i++){
      for (int j = i; j<data.size(); j++){
          if (data[j] < data[i]){
             temp = data[i];
             data[i] = data[j];
             data[j] = temp;
          }
      }
}
int max = data[data.size()-1] - data[0];
int min = 10000;
int index = 0;
for(int i = 0; i<data.size()-2; i++){
     if ((data[i+2] - data[i]) < min){
         min = data[i+2] - data[i];
         index = i;
     }
}
cout << "Maximum : " << max << endl;
cout << "Minimum : " << min << endl;
    
return 0;
}