In C++, create a program (and include comments detailing your design decisions)
ID: 3723633 • 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 ( he 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 (The top number (5 in the case of this example) should be read in as the size of the arrays):
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
/* Please select the path of file before running the program on your system */
// FileTestPro.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string fileName = "";
while(1)
{
cout << "Enter File Name:" << endl;
getline(cin, fileName);
if (!fileName.compare(""))
{
cout << "FileName is not entered!" << endl;
}
else
break;
}
string filePath("D:\Self Practice\Chegg\FileTestPro\FileTestPro\");
string lcFullFileName = filePath + fileName;
ofstream myfile;
myfile.open(lcFullFileName);
if (!myfile.is_open())
{
cout << "File Name not opened!" << endl;
}
int lnSizeOfArray = 0;
//cout << "Enter the number of double values need to be as input" << endl;
//cin >> lnSizeOfArray;
int lnValue[5] = {0};
int lnAnotherValue[5] = { 0 };
int minVal = 0;
int maxVal = 0;
for (int lnCnt = 0; lnCnt < 5; ++lnCnt)
{
int lnVal = 0;
cout << "Enter the double value::";
cin >> lnVal;
lnValue[lnCnt] = lnVal;
lnAnotherValue[lnCnt] = lnVal;
myfile << lnVal << endl;
}
//
ofstream normalisedfile;
normalisedfile.open("D:\Self Practice\Chegg\FileTestPro\FileTestPro ormalisedFile.txt");
if (!normalisedfile.is_open())
{
cout << "File Name not opened!" << endl;
}
normalisedfile << "Original Array:[";
for (int lnCnt = 0; lnCnt <= 4; ++lnCnt)
{
normalisedfile << lnValue[lnCnt] << " ";
}
normalisedfile << "]";
normalisedfile << "Normalised Array:[";
for (int lnCnt = 0; lnCnt <= 4; ++lnCnt)
{
// considering array[0] as minvalue & array[4] as maxvalue
float normalisedVal = ((lnValue[lnCnt] - lnValue[0]) / (lnValue[4] - lnValue[0]));
normalisedfile << normalisedVal << " ";
}
normalisedfile << "]";
//
ofstream reversedfile;
reversedfile.open("D:\Self Practice\Chegg\FileTestPro\FileTestPro eversedFile.txt");
if (!reversedfile.is_open())
{
cout << "File Name not opened!" << endl;
}
reversedfile << "Original Array:[";
for (int lnCnt = 0; lnCnt <= 4; ++lnCnt)
{
reversedfile << lnValue[lnCnt] << " ";
}
reversedfile << "]";
reversedfile << "Reversed Array:[";
for (int lnCnt = 4; lnCnt >= 0; --lnCnt)
{
reversedfile << lnValue[lnCnt] << " ";
}
reversedfile << "]";
return 0;
}