In C++, create a program (and include comments detailing your design decisions)
ID: 3723417 • Letter: I
Question
In C++, create a program (and include comments detailing your design decisions) that performs the following:- Prompts the user for a file name and stores it - Prompt the user until they give the name of a file that can be opened - Read in and store the double in the file then... - Create a copy of the values in another array of the same size - Then, normalize the values in the copy (yes this will change values) - The largest value is always normalized to 1.0 and the smallest to 0.0 - The values in between are adjusted to be between 0 and 1 but represent their former ratios to the original number Hint: (value - min) / (max - min) - Create a copy of the original array then reverse the order of value (e.g. the first value in the original will be the last value in the new array) - Once you have the normalized and reversed arrays, store them in two seperate files along with the original values - Store the normalized array in a file called "normalized.txt" - Store the reversed array in a file called "reversed.txt" - Assume the user types in "input.txt" as the file name and the file contains the following: 5 10.0 40.0 20.0 30.0 50.0
Example normalized.txt: Original array: [10.0, 40.0, 20.0, 30.0, 50.0] Normalized array: [0, .75, .25, .5, 1]
Example of reserved.txt: Original array: [10.0, 40.0, 20.0, 30.0, 50.0] Reversed array: [50.0, 30.0, 20.0, 40.0, 10.0]
You can assume the file will be properly formatted and contain good data.
All code (excluding headers and namespace) must be done inside of the main function.
In C++, create a program (and include comments detailing your design decisions) that performs the following:
- Prompts the user for a file name and stores it - Prompt the user until they give the name of a file that can be opened - Read in and store the double in the file then... - Create a copy of the values in another array of the same size - Then, normalize the values in the copy (yes this will change values) - The largest value is always normalized to 1.0 and the smallest to 0.0 - The values in between are adjusted to be between 0 and 1 but represent their former ratios to the original number Hint: (value - min) / (max - min) - Create a copy of the original array then reverse the order of value (e.g. the first value in the original will be the last value in the new array) - Once you have the normalized and reversed arrays, store them in two seperate files along with the original values - Store the normalized array in a file called "normalized.txt" - Store the reversed array in a file called "reversed.txt" - Assume the user types in "input.txt" as the file name and the file contains the following: 5 10.0 40.0 20.0 30.0 50.0
Example normalized.txt: Original array: [10.0, 40.0, 20.0, 30.0, 50.0] Normalized array: [0, .75, .25, .5, 1]
Example of reserved.txt: Original array: [10.0, 40.0, 20.0, 30.0, 50.0] Reversed array: [50.0, 30.0, 20.0, 40.0, 10.0]
You can assume the file will be properly formatted and contain good data.
All code (excluding headers and namespace) must be done inside of the main function.
In C++, create a program (and include comments detailing your design decisions) that performs the following:
- Prompts the user for a file name and stores it - Prompt the user until they give the name of a file that can be opened - Read in and store the double in the file then... - Create a copy of the values in another array of the same size - Then, normalize the values in the copy (yes this will change values) - The largest value is always normalized to 1.0 and the smallest to 0.0 - The values in between are adjusted to be between 0 and 1 but represent their former ratios to the original number Hint: (value - min) / (max - min) - Create a copy of the original array then reverse the order of value (e.g. the first value in the original will be the last value in the new array) - Once you have the normalized and reversed arrays, store them in two seperate files along with the original values - Store the normalized array in a file called "normalized.txt" - Store the reversed array in a file called "reversed.txt" - Assume the user types in "input.txt" as the file name and the file contains the following: 5 10.0 40.0 20.0 30.0 50.0
Example normalized.txt: Original array: [10.0, 40.0, 20.0, 30.0, 50.0] Normalized array: [0, .75, .25, .5, 1]
Example of reserved.txt: Original array: [10.0, 40.0, 20.0, 30.0, 50.0] Reversed array: [50.0, 30.0, 20.0, 40.0, 10.0]
You can assume the file will be properly formatted and contain good data.
All code (excluding headers and namespace) must be done inside of the main function.
Explanation / Answer
#include <fstream>
#include <iostream>
using namespace std;
int main () {
fstream infile;
string fname;
cout<<"Enter file name";
cin>>fname;
infile.open(fname.c_str(), ios::out | ios::in );
if (!infile.is_open())
{
cout << "Opening file " << infile << " failed." << "Enter another file name"<<endl;
cin>>fname;
infile.open(fname.c_str(), ios::out | ios::in );
}
// Read first line containing number of doubles
int size = getline(infile, line);
double orig[size],norm[size],rev[size];
// Save doubles in original array & reverse array
for(int i=0; i<size; i++){
orig[i] = getline(infile, line);
rev[size-i-1]=orig[i];
}
int max=orig[0], min=orig[0];
// find max & min
for(int i=1; i<size; i++){
if(orig[i]<min)
min = orig[i];
if(orig[i]>max)
max = orig[i];
}
//normalize array elements and save in norm array
for(i=0; i<size; i++){
norm[i] = (orig[i]-min)/(max-min);
}
fstream outfile1,outfile2;
outfile1.open("normalized.txt", ios::out );
outfile2.open("reserved.txt", ios::out );
outfile1<<"Original array: [";
outfile2<<"Original array: [";
//write original array to files
for(i=0; i<size; i++){
outfile1<<orig[i];
outfile2<<orig[i];
if(i==size-1){
outfile1<<"]"<<endl;
outfile2<<"]"<<endl;
}
else{
outfile1<<", ";
outfile2<<", ";
}
}
outfile1<<"Normalized array: [";
outfile2<<"Reversed array: [";
//write normalized and reversed array
for(i=0; i<size; i++){
outfile1<<norm[i];
outfile2<<rev[i];
if(i==size-1){
outfile1<<"]"<<endl;
outfile2<<"]"<<endl;
}
else{
outfile1<<", ";
outfile2<<", ";
}
}
outfile1.close();
outfile2.close();
infile.close();
}