Question: Need to fix the code (C++) this is my text file days index 2 54 5 50 7 45 10 37 14 35 19 25
Need to fix the code (C++)
this is my text file
days index
2 54
5 50
7 45
10 37
14 35
19 25
26 20
31 16
34 18
38 13
45 8
52 11
53 8
60 4
65 6
so I am reading the given data and printing both columns and I am not able to find mean median mode standard devation and range
of INDEX column
#include
using namespace std;
/*File name to read*/ char* filename; /*variable year*/ double* year; /*variable for area */ double* AreaValue; /*variable for displaying the data from the file*/ int Data; /*variable for finding the Exponent of both the formulas*/ double Exp;
void displayMenu(); void loadFile(); void releaseMemory(); void power(); void exponential();
int main() {
char input = '0'; while (input != '2') { /*displaymenu() method is call*/ displayMenu(); cin >> input;
switch (input) { case '1': /*calling the loadfile method use for reading the file */ loadFile(); break; case '2': /*quit option when selcted and release memory method is called */ releaseMemory(); break; default: /*if any other option is selected */ cout << "Invalid inputs "; continue; } } }
float CalcMean( int values[], int num) { if (num <= 0) return 0; int count = 0; for (int i = 0; i < num; i++) count += values[i]; return (float)count / num; }
float CalcMedian( int values[], int num) { if (num <= 0) return 0; if (num % 2) return (float)values[(num + 1) / 2]; else { int pos = num / 2; return (float)(values[pos] + values[pos + 1]) / 2; } } int CalcMode( int values[], int num) { if (num <= 0)return 0; int max_count = 0, pos = 0 ,max_nums = 0; int count = 1; for (int i = 1; i < num; i++) { if (values[i] != values[pos]) { if (count > max_count) { max_count = count; max_nums = 0; } else if (count == max_count) max_nums++; pos = i; count = 0; } else count++; } if (max_nums) return 0; else return values[pos]; }
int CalcRange( int values[], int num) { if (num <= 0) return -1; int max, min; max = min = values[0]; for (int i = 1; i < num; i++) { if (values[i] > max)max = values[i]; else if (values[i] < min) min = values[i]; } return max - min; }
void displayMenu() { /*display the main menu when called */ //cout << "******************************************************* "; cout << " LEAST_SQUARES LINEAR REGRESSION "; cout << "1. Exponential Fit "; cout << "2. Quit "; //cout << "******************************************************* "; cout << "Select an option: "; }
void loadFile() {
ifstream infile; //char filename[200]; int days = 0; int index = 0; int recordCount = 0; int recordToSkip = 0; double total = 0.0; double mean = 0.0; double median = 0.0;
char buffer[256]; int ch; cout << endl << "Please enter the name of the file to open: "; /*clears the error flag on cin*/ cin.clear(); /*skips to the next newline */ cin.ignore(256, ' '); /*It assumes that buffer is an array of at least size characters*/ cin.getline(buffer, 256);
fstream file(buffer); /*check if the filename exist or not */ if (!file.is_open()) { cout << "FAILED TO LOAD FILE" << endl; return; } std::string str; std::getline(file, str); // skip the first line filename = new char[strlen(buffer) + 1]; strcpy_s(filename, strlen(buffer) + 1, buffer); Data = 0; string line; /*redaing and counting the lines */ while (getline(file, line)) { ++Data; //cout << line << endl; } file.clear(); /* get the length of the file */ file.seekg(0, ios::beg);
/*displaying the data from the file */ string getcontent; while (getline(file, getcontent)) { cout << getcontent << endl; } /*display the total line in the file */ cout << "There are " << Data << " records." << endl << endl;
{ float mean = 0, mean1 = 0, meanf = 0, meanf1 = 0, median, sd, range, norm, sum = 0, deviation = 0, lmn = 0; float c[10]; for (int i = 0; i < Data; i++) { c[i] = buffer[i]; } for (int j = 0; j < Data; j++) { mean = mean + c[j]; } meanf = mean / Data; cout << "Mean = " << meanf << endl; //printf("Mean= %f", meanf);
}
/*displaying the second menu */ while (1) { cout << "******************************************************* "; cout << "1. Exponential fit to data "; cout << "2. Power Law fit to data "; cout << "3. Quit "; cout << "******************************************************* "; cout << "Select an option: "; cin >> ch; cout << " "; if (ch == 1)exponential(); if (ch == 2)power(); if (ch == 3) { releaseMemory(); exit(0); } } exit(0); }
void releaseMemory() { /*releasing the memory used by the variable */ delete filename; delete AreaValue; delete year;
Data = 0; Exp = 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
