C++ Lab Data file for the lab: CIS 22A Lab 2 Scientific Notation Write a program
ID: 3822164 • Letter: C
Question
C++ Lab
Data file for the lab:
CIS 22A Lab 2 Scientific Notation Write a program to analyze scientific data. Input: The first line of the file contains the number of data readings. The following lines contain the data presented as floating-point numbers Compute: The mean mean The Standard Deviation. SN i-1 output: Output is to be to a file Output the standard deviation first. Output the data in the original order 8 values per line in tabular format to single column: data from low to high, and the words ofrange" if the value mean 1.5 SN or if the value mean 1.5 SN. Code requirements: Absolutely no square brackets anywhere in the program except when implementing new and delete. Function to input number of data readings, allocate memory and input actual data. Function to allocated memory for an array of pointers to the actual data and set these pointers pointing to the actual data. Function to sort the data using the array of pointers to the data so that original data is not altered. Use the selection sort algorithm. Function to output.Explanation / Answer
Here is the code for you:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;
void SelectionSort(double *A, int SIZE)
{
int i, j;
for(i = 0; i < SIZE-1; i++) //For each element.
{
int min = i; //Assumes the first element in the remaining array as min.
for(j = i; j < SIZE; j++) //Find the position of least element in remaining array.
if(*(A+j) < *(A+min))
min = j;
int temp = *(A+i); //Exchange the first element in the array, with minimum element.
*(A+i) = *(A+min);
*(A+min) = temp;
}
}
double StandardDeviation(double *numbers, int count, double average)
{
double stdDev = 0;
for(int i = 0; i < count; i++)
stdDev += pow(*(numbers+i) - average, 2); //Sum of (number - average) whole square.
stdDev = sqrt(1/(double)count * stdDev);
return stdDev;
}
double Mean(double *numbers, int count)
{
double mean = 0.0;
for(int i = 0; i < count; i++)
mean += *(numbers + i);
mean /= count;
return mean;
}
double *readValues(ifstream& fin, int numOfValues)
{
double *values = new double[numOfValues];
for(int i = 0; i < numOfValues; i++)
fin >> *(values + i);
return values;
}
void writeOutputToFile(ofstream& fout, double* values, int numOfValues)
{
double average = Mean(values, numOfValues);
double stdDev = StandardDeviation(values, numOfValues, average);
fout << "The standard deviation of the values in file is: " << stdDev << endl;
for(int i = 0; i < numOfValues; i++)
{
fout << *(values + i) << " ";
if((i+1) % 8 == 0)
fout << endl;
}
SelectionSort(values, numOfValues);
for(int i = 0; i < numOfValues; i++)
{
if((*(values + i) < average - 1.5 * stdDev) || *(values + i) > average + 1.5 * stdDev)
fout << "Out of range" << endl;
else
fout << *(values + i) << endl;
}
}
int main()
{
ifstream fin;
string fileName;
cout << "Enter the name of the input file: ";
cin >> fileName;
fin.open(fileName);
while(!fin.is_open())
{
cout << "Unable to open the input file." << endl;
return 0;
}
int numOfValues;
fin >> numOfValues;
double *values = readValues(fin, numOfValues);
ofstream fout;
cout << "Enter the name of the output file: ";
cin >> fileName;
fout.open(fileName);
writeOutputToFile(fout, values, numOfValues);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.