Question
IN C++ I have this program I do have a code too but I'm not able to read any files. I am getting out put
IN C++ I have this program I do have a code too but I'm not able to read any files.
I am getting out put like this.
all the codes and problem are listed below.
I am using macbook for these. also, codeblocks.
-------------------------------------------------------------------------------------------------------------
output:
-------------------------------------------------------------------------------------------------------------
Enter Name for first File:
Class_A.txt
Enter Name for second File:
Class_B.txt
Enter Name for third File:
Class_C.txt
File not found!: No such file or directory
File not found!: No such file or directory
File not found!: No such file or directory
For Class A
Error!! Data not foundFor Class A
Error!! Data not foundFor Class A
------------------------------------------------------------------------------------
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 doesnt require an array. Finding the median and the percentages does require an array.
- The user should be prompted for the three file names which you should store in sring variables for file processing.
- The program will read values until the user encounters an EOF for that file to quit.
- The program must provide an error message for zero or other negative numbers.
- Do not presume that all the numbers will be integers..
- The program must provide reasonable prompts and error messages to the users.
- The programs output must be properly labelled.
- The program must handle the case of an empty array properly by producing a reasonable error message. It must not attempt to do any calculations on the empty array.
- The programs output does not need to be lined up perfectly.
- You must display the deviations from the average as positive numbers with some wording (e.g., 50.253% below and25.22% above) rather than as a simple number (e.g., -50.253% and 25.22%) for each file with a heading for each file corresponding to the lable as given below..
- Finally It should have embedded in each file a lable for that file ( name of the scores) such as class A, class B , class C etc.
- Print out the lable for the file and the average for the file with the highest average and its value. Do the same for the file with the highest median, and f inally the file with the highest average of the percentage below the average for that file and its value.
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 wont 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 isnt 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
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; size_t sz = 0; 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 :"<
//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,temp;
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 : "< } //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 "< if(classAMedian>classBMedian && classAMedian>classCMedian) { cout<<"File A had the highest median of "< if(classAAvergageBelow>classBAvergageBelow && classAAvergageBelow>classCAvergageBelow) { cout<<"File A had the highest average percent of the scores below the average with a value of "< system("Pause"); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started