I'm not getting out put what should I do
I have 3 text files which is in same folder with main program.
I have attached program with it too.
please help me with this.
------------------------------------------------------------------------------------
This program will read a group of positive numbers from three files ( not all necessarily the same size), and then calculate the average and median values for each file. Each file should have at least 10 scores. The program will then print all the scores in the order that they were input, showing each number and what percentage it is above or below the average for each file. Note: Finding the average doesn’t require an array. Finding the median and the percentages does require an array.
Finding the Median
To find the median of a group of n numbers, sort them into order. If n is odd, the median is the middle entry. In an array named@data with elements starting at index zero, this will be element $data[(n-1)/2]. (Of course, you will have to put a scalar variable in place of n)
If n is even, the median is the average of the numbers “surrounding” the middle. For an array named @data with elements starting at index zero, this is ($data[(n/2)]+$data[(n/2)-1])/2.
Here is sample output from three separate runs of the program. User input in bold and italics.
Scores Report
Enter fie name for first file : class_A.txt
Enter fie name for first file : class_B.txt
Enter fie name for first file : class _C.txt
For class A The average is 8 The median value is 6 Value % above/below average 4 50% below 6 25% below 14 75% above For class B The average is 9 The median value is 10 Value % above/below average 3 50% below 6 25% below 12 75% above For class C The average is 7 The median value is 6 Value % above/below average 4 50% below 8 25% below 9 75% above File A had the highest average of 35.5 File B had the highest median of 34 File C had the highest average percent of the scores below the average with a value of 47.25.
In this example, the percentages all happened to come out to be integers. For an arbitrary set of numbers, this won’t be the case. Do not truncate your results to integers use two decima places for values that our output.
Sorting an Array Numerically
When you sort an array, the default is to sort in alphabetical (ASCII/Unicode) order. This isn’t what you want for an array containing numbers. Otherwise, the number "47" (as a string) will sort before the number "5" (as a string).
-------------------------------------------------------------------------------------------------------------
//
#include<iostream>
#include<string>
#include <fstream>
#include <stdlib.h>
using namespace std;
//reading class from filename
double* readClass(string fileName,double& avg,double&
countArray){
//defining local variables
double *arrayName = NULL;
double temp=0,sum=0;
int count=0;
string line;
ifstream f (fileName);
//opening file
if (!f.is_open())
{
//printing error if file not found
perror("Error while opening file");
}else{
while(getline(f, line)) {
//converting string to double
temp=stod(line);
//checking if value is less than equal to zero
if(temp<=0){
//printing message and ignoring file
cout<<"Value cannot be :"<<temp<<"
in:"<<fileName<<endl;
}else{
//adding sum and in this loop only counting valid entries
sum+=temp;
count++;
}
//cout<<stod(line)<<endl;
}
//closing file
f.close();
ifstream f (fileName);
int index=0;
if(count>0)
arrayName = new double [count]; //creating dynamic array
//adding file data to new array
while(getline(f, line)) {
temp=stod(line);
if(temp>0){
arrayName[index]=temp;
index++;
}
}
}
//checking if file has some valid data
if(sum!=0)
{
avg=sum/count;
countArray=count;
}else{//returning default
avg=0;
countArray=0;
}
return arrayName;
}
//function to swap two numbers
void swap(double *p,double *q) {
int t;
t=*p;
*p=*q;
*q=t;
}
//sort function
void sort(double a[],int n) {
int i,j;
for(i = 0;i < n-1;i++) {
for(j = 0;j < n-i-1;j++) {
if(a[j] > a[j+1])
swap(&a[j],&a[j+1]);
}
}
}
//median function
double getMedian(double arrayName[],int n){
sort(arrayName,n);
if (n % 2 != 0)
return (double)arrayName[n/2];
return (double)(arrayName[(n-1)/2] + arrayName[n/2])/2.0;
}
//printing results and getting average below value
void printResults(double average,int count,double *className,double
&averageBelow){
int sumBelow=0;
if(count>0)
{
cout<<"The Average Value Is :
"<<average<<endl;
cout<<"The Median Value Is :
"<<getMedian(className,count)<<endl;
cout<<"Value % above/below average ";
for(int i=0;i<count;i++)
{
double percentBelowAbove=0;
percentBelowAbove=((className[i]-average)/average) * 100;
if(percentBelowAbove<0)
{
cout<<className[i]<<"
"<<percentBelowAbove*-1<<"% below ";
sumBelow-=percentBelowAbove;
}else if(percentBelowAbove>0)
{
cout<<className[i]<<"
"<<percentBelowAbove<<"% above ";
}else{
cout<<className[i]<<" % is same ";
}
}
averageBelow=sumBelow/count;
}else{
cout<<"No valid data found";
averageBelow=100;
}
}
//Main class to call functions
int main()
{
double *classA = NULL;
double *classB = NULL;
double *classC = NULL;
double
classAAvergage,countA,classBAvergage,countB,classCAvergage,countC;
double
classAAvergageBelow,classBAvergageBelow,classCAvergageBelow;
double classAMedian,classBMedian,classCMedian;
string classAFileName,classCFileName,classBFileName;
cout<<"Enter Name for first file: ";
//classAFileName="c:\users\faraz\desktop\a.txt";
//classBFileName="c:\users\faraz\desktop\b.txt";
//classCFileName="c:\users\faraz\desktop\c.txt";
cin>>classAFileName;
cout<<"Enter Name for second file: ";
cin>>classBFileName;
cout<<"Enter Name for third file: ";
cin>>classCFileName;
classA=readClass(classAFileName,classAAvergage,countA);
classB=readClass(classBFileName,classBAvergage,countB);
classC=readClass(classCFileName,classCAvergage,countC);
cout<<" For class A ";
printResults(classAAvergage,countA,classA,classAAvergageBelow);
cout<<" For class B ";
printResults(classBAvergage,countB,classB,classBAvergageBelow);
cout<<" For class C ";
printResults(classCAvergage,countC,classC,classCAvergageBelow);
classAMedian=getMedian(classA,countA);
classBMedian=getMedian(classB,countB);
classCMedian=getMedian(classC,countC);
if(classAAvergage>classBAvergage &&
classAAvergage>classCAvergage)
{
cout<<"File A had the highest average of
"<<classAAvergage<<endl;
}
else if(classBAvergage>classAAvergage &&
classBAvergage>classCAvergage)
{
cout<<"File B had the highest average of
"<<classBAvergage<<endl;
}
else if(classCAvergage>=classAAvergage &&
classCAvergage>classBAvergage)
{
cout<<"File C had the highest average of
"<<classCAvergage<<endl;
}else
{
cout<<"All average are same having value of
"<<classAAvergage<<endl;
}
if(classAMedian>classBMedian &&
classAMedian>classCMedian)
{
cout<<"File A had the highest median of
"<<classAMedian<<endl;
}
else if(classBMedian>classAMedian &&
classBMedian>classCMedian)
{
cout<<"File B had the highest median of
"<<classBMedian<<endl;
}
else if(classCMedian>=classAMedian &&
classCMedian>classBMedian)
{
cout<<"File C had the highest median of
"<<classCMedian<<endl;
}else
{
cout<<"All median are same having value of
"<<classAMedian<<endl;
}
if(classAAvergageBelow>classBAvergageBelow &&
classAAvergageBelow>classCAvergageBelow)
{
cout<<"File A had the highest average percent of the scores
below the average with a value of
"<<classAAvergageBelow<<endl;
}
else if(classBAvergageBelow>classAAvergageBelow &&
classBAvergageBelow>classCAvergageBelow)
{
cout<<"File B had the highest average percent of the scores
below the average with a value of
"<<classBAvergageBelow<<endl;
}
else if(classCAvergageBelow>=classAAvergageBelow &&
classCMedian>classBAvergageBelow)
{
cout<<"File C had the highest average percent of the scores
below the average with a value of
"<<classCAvergageBelow<<endl;
}else
{
cout<<"All median are same having value of
"<<classAAvergageBelow<<endl;
}
system("Pause");
}
I have gone through the entire code It seems to be correct It is running completely fine on my compiler Only 1 change needs to be done - instead of using stod() (on line 25 and 44) for converting string to double, use stringstream as used by me as you have not provided the files I have used some random numbers as shown -
Below is the C++ code I hope that i have provided sufficient comments for your better understanding Note that I have done proper indentation but this code is automatically left alligned on this interface
#include<iostream>
#include<string>
#include <fstream>
#include <stdlib.h>
#include<sstream>
using namespace std;
//reading class from filename
double* readClass(string fileName,double& avg,double&
countArray){
//defining local variables
double *arrayName = NULL;
double temp=0,sum=0;
int count=0;
string line;
stringstream ss;
ifstream f (fileName);
//opening file
if (!f.is_open())
{
//printing error if file not found
perror("Error while opening file");
}else{
while(getline(f, line)) {
//converting string to double
ss<<line;
ss>>temp;
ss.clear();
//checking if value is less than equal to zero
if(temp<=0){
//printing message and ignoring file
cout<<"Value cannot be :"<<temp<<"
in:"<<fileName<<endl;
}else{
//adding sum and in this loop only counting valid entries
sum+=temp;
count++;
}
//cout<<stod(line)<<endl;
}
//closing file
f.close();
ifstream f (fileName);
int index=0;
if(count>0)
arrayName = new double [count]; //creating dynamic array
//adding file data to new array
while(getline(f, line)) {
ss<<line;
ss>>temp;
ss.clear();
if(temp>0){
arrayName[index]=temp;
index++;
}
}
}
//checking if file has some valid data
if(sum!=0)
{
avg=sum/count;
countArray=count;
}else{//returning default
avg=0;
countArray=0;
}
return arrayName;
}
//function to swap two numbers
void swap(double *p,double *q) {
int t;
t=*p;
*p=*q;
*q=t;
}
//sort function
void sort(double a[],int n) {
int i,j;
for(i = 0;i < n-1;i++) {
for(j = 0;j < n-i-1;j++) {
if(a[j] > a[j+1])
swap(&a[j],&a[j+1]);
}
}
}
//median function
double getMedian(double arrayName[],int n){
sort(arrayName,n);
if (n % 2 != 0)
return (double)arrayName[n/2];
return (double)(arrayName[(n-1)/2] + arrayName[n/2])/2.0;
}
//printing results and getting average below value
void printResults(double average,int count,double *className,double
&averageBelow){
int sumBelow=0;
if(count>0)
{
cout<<"The Average Value Is :
"<<average<<endl;
cout<<"The Median Value Is :
"<<getMedian(className,count)<<endl;
cout<<"Value % above/below average ";
for(int i=0;i<count;i++)
{
double percentBelowAbove=0;
percentBelowAbove=((className[i]-average)/average) * 100;
if(percentBelowAbove<0)
{
cout<<className[i]<<"
"<<percentBelowAbove*-1<<"% below ";
sumBelow-=percentBelowAbove;
}else if(percentBelowAbove>0)
{
cout<<className[i]<<"
"<<percentBelowAbove<<"% above ";
}else{
cout<<className[i]<<" % is same ";
}
}
averageBelow=sumBelow/count;
}else{
cout<<"No valid data found";
averageBelow=100;
}
}
//Main class to call functions
int main()
{
double *classA = NULL;
double *classB = NULL;
double *classC = NULL;
double
classAAvergage,countA,classBAvergage,countB,classCAvergage,countC;
double
classAAvergageBelow,classBAvergageBelow,classCAvergageBelow;
double classAMedian,classBMedian,classCMedian;
string classAFileName,classCFileName,classBFileName;
cout<<"Enter Name for first file: ";
//classAFileName="c:\users\faraz\desktop\a.txt";
//classBFileName="c:\users\faraz\desktop\b.txt";
//classCFileName="c:\users\faraz\desktop\c.txt";
cin>>classAFileName;
cout<<"Enter Name for second file: ";
cin>>classBFileName;
cout<<"Enter Name for third file: ";
cin>>classCFileName;
classA=readClass(classAFileName,classAAvergage,countA);
classB=readClass(classBFileName,classBAvergage,countB);
classC=readClass(classCFileName,classCAvergage,countC);
cout<<" For class A ";
printResults(classAAvergage,countA,classA,classAAvergageBelow);
cout<<" For class B ";
printResults(classBAvergage,countB,classB,classBAvergageBelow);
cout<<" For class C ";
printResults(classCAvergage,countC,classC,classCAvergageBelow);
classAMedian=getMedian(classA,countA);
classBMedian=getMedian(classB,countB);
classCMedian=getMedian(classC,countC);
if(classAAvergage>classBAvergage &&
classAAvergage>classCAvergage)
{
cout<<"File A had the highest average of
"<<classAAvergage<<endl;
}
else if(classBAvergage>classAAvergage &&
classBAvergage>classCAvergage)
{
cout<<"File B had the highest average of
"<<classBAvergage<<endl;
}
else if(classCAvergage>=classAAvergage &&
classCAvergage>classBAvergage)
{
cout<<"File C had the highest average of
"<<classCAvergage<<endl;
}else
{
cout<<"All average are same having value of
"<<classAAvergage<<endl;
}
if(classAMedian>classBMedian &&
classAMedian>classCMedian)
{
cout<<"File A had the highest median of
"<<classAMedian<<endl;
}
else if(classBMedian>classAMedian &&
classBMedian>classCMedian)
{
cout<<"File B had the highest median of
"<<classBMedian<<endl;
}
else if(classCMedian>=classAMedian &&
classCMedian>classBMedian)
{
cout<<"File C had the highest median of
"<<classCMedian<<endl;
}else
{
cout<<"All median are same having value of
"<<classAMedian<<endl;
}
if(classAAvergageBelow>classBAvergageBelow &&
classAAvergageBelow>classCAvergageBelow)
{
cout<<"File A had the highest average percent of the scores
below the average with a value of
"<<classAAvergageBelow<<endl;
}
else if(classBAvergageBelow>classAAvergageBelow &&
classBAvergageBelow>classCAvergageBelow)
{
cout<<"File B had the highest average percent of the scores
below the average with a value of
"<<classBAvergageBelow<<endl;
}
else if(classCAvergageBelow>=classAAvergageBelow &&
classCMedian>classBAvergageBelow)
{
cout<<"File C had the highest average percent of the scores
below the average with a value of
"<<classCAvergageBelow<<endl;
}else
{
cout<<"All median are same having value of
"<<classAAvergageBelow<<endl;
}
system("Pause");
}
Below is the screenshot of output
I have tried to explain it in very simple language and I hope that i have answered your question satisfactorily.Leave doubts in comment section if any.
I'm not getting out put what should I do I have 3 text files which is...
Am I getting this error because i declared 'n' as an int, and then asking it to make it a double? This is the coude: #include "stdafx.h" #include <iostream> #include <fstream> #include <string> #include <algorithm> #include <vector> using namespace std; void sort(double grades[], int size); char calGrade(double); int main() { int n; double avg, sum = 0;; string in_file, out_file; cout << "Please enter the name of the input file: "; cin >> in_file; ...
I have 2 issues with the C++ code below. The biggest concern is that the wrong file name input does not give out the "file cannot be opened!!" message that it is coded to do. What am I missing for this to happen correctly? The second is that the range outputs are right except the last one is bigger than the rest so it will not output 175-200, it outputs 175-199. What can be done about it? #include <iostream> #include...
I want to change this code and need help. I want the code to not use parallel arrays, but instead use one array of struct containing the data elements, String for first name, String for last name,Array of integers for five (5) test scores, Character for grade. If you have any idea help would be great. #include #include #include #include using namespace std; const int NUMBER_OF_ROWS = 10; //number of students const int NUMBER_OF_COLUMNS = 5; //number of scores void...
C++ problem where should I do overflow part? in this code do not write a new code for me please /////////////////// // this program read two number from the user // and display the sum of the number #include <iostream> #include <string> using namespace std; const int MAX_DIGITS = 10; //10 digits void input_number(char num[MAX_DIGITS]); void output_number(char num[MAX_DIGITS]); void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base); int main() { // declare the array = {'0'} char num1[MAX_DIGITS] ={'0'}; char...
First create the two text file given below. Then complete the main that is given. There are comments to help you. An output is also given You can assume that the file has numbers in it Create this text file: data.txt (remember blank line at end) Mickey 90 Minnie 85 Goofy 70 Pluto 75 Daisy 63 Donald 80 Create this text file: data0.txt (remember blank line at end) PeterPan 18 Wendy 32 Michael 28 John 21 Nana 12 Main #include...
I'm getting an Error: LNK1561: entry point must be defined. Can someone edit it with using the basic iostream and string libraries, only basic lib. In a population, the birth rate and death rate are calculated as follows: Birth Rate = Number of Births ÷ Population Death Rate = Number of Deaths ÷ Population For example, in a population of 100,000 that has 8,000 births and 6,000 deaths per year, Birth Rate = 8,000 ÷ 100,000 = 0.08 Death Rate...
Need help with a C++ program. I have been getting the error "this function or variable may be unsafe" as well as one that says I must "return a value" any help we be greatly appreciated. I have been working on this project for about 2 hours. #include <iostream> #include <string> using namespace std; int average(int a[]) { // average function , declaring variable int i; char str[40]; float avg = 0; // iterating in...
When running the program at the destructor an exception is being thrown. Can someone help me out? vararray.h: #ifndef VARARRAY_H_ #define VARARRAY_H_ class varArray { public: varArray(); // void constructor int arraySize() const { return size; } // returns the size of the array int check(double number); // returns index of element containg "number" or -1 if none void addNumber(double); // adds number to the array void removeNumber(double); // deletes the number from the array ...
Assignment Predator / Prey Objectives Reading from and writing to text files Implementing mathematical formulas in C++ Implementing classes Using vectors Using command line arguments Modifying previously written code Tasks For this project, you will implement a simulation for predicting the future populations for a group of animals that we’ll call prey and their predators. Given the rate at which prey births exceed natural deaths, the rate of predation, the rate at which predator deaths exceeds births without a food...
I want to compare the runtimes and swap operations times among quick Sort, selection Sort and shell Sort here is my code: But when I create a 1000,000 size array, I can't get the result of the operations times and runtime. what's wrong with my code? and I also want to copy the array. Because I want to use same array for three sort. And for the shell Sort, I haven't learn it in my class. Can anyone help me...