This is my updated program but it is telling me the file will not open.
//Matthew HW9
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
using namespace std;
const int MAXVALUES=100;
int loadArray(ifstream& infile, double resist[], int MAXDATA);
double findMax(double data[], int numData);
double findMin(double data[],int limit); //find the maximum array
double calcMean(double data[], int numData);
double geomMean(double data[], int numData);
double harmMean(double data[], int numData);
void sortValues(double data[], int numData);
double rmsAvg(double data[], int numData);
int main(){
int numValues=0;
double resist[MAXVALUES];
int limit=numValues;
ifstream infile("HW9resist.txt");
if(!infile){
//if condtion is true
cout<<"Error: can't open file";
return 1;
}
numValues=loadArray(infile, resist, MAXVALUES);
cout<<"Matthew Cibulka, Program 9";
cout<<"The maximum value is:"<<findMax(resist,numValues);
cout<<"The minimum value is:"<<findMin(resist,numValues);
cout<<"The mean is"<<calcMean(resist,numValues);
cout<<"The geometric mean is:"<<geomMean(resist,numValues);
cout<<"The rms average is:"<<rmsAvg(resist,numValues);
cout<<"The harmonic mean is:"<<harmMean(resist,numValues);
sortValues(resist,numValues);
cout<<"The sorted list of values is:";
for(int i=0;i<numValues;i++){
cout<<resist[i]<<endl;
}
return 0;
}
int loadArray(ifstream& infile, double data[], int MAXDATA){
int numData=0;
while(numData < MAXDATA && infile >> data[numData]){
numData++;
}
if(numData >= MAXDATA){
//if condition is true
cout<<"Maximum size of array met"<<endl;
}
if(!infile.eof()){
cout<<"Bad data found"<<endl;
}
return numData;
}
double findMax(double data[], int numData){
double Max=0;
int i=1;
for(int i =1;i<numData;i++){
if(data[i]>Max){
Max=data[i];
i=i++;
}
return Max;
}
}
PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/ http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
Started in on your program until I found the I can not compile the program.
"MAXVALUES" is initialized to zero. Although this is good inside you use this variable to define an array of zero length which does not work."MAXVALUES" needs to be at least 1 or greater, 2 would or greater would be better.
You have a prototype for "loadArray", but no function definition. Inside main you have function calls that have no prototype or function definition. In the "loadArray" prototype you have "MAXDATA", This leads me to believe that it is a constant variable that I see no definition for except in the prototype. If this is not a constant like "MAXVALUES" it should be in lower case letters except for the "D".
If you write the line double resist[MAXVALUES]{} this way the empty {}s will initialize each element of the array to 0.0.
You check to see if the input stream is open, but if it is not you may not be able to see the error message before the screen closes. I added this line in the if statement:
std::this_thread::sleep_for(std::chrono::seconds(3)); // Requires header files "chrono" and "thread"
Be sure to include the header files.
I will work on getting what I have to compile while you work on what i have said.
As I read through the instruction I am not sure if the first numbers are examples or the input file. Clarification of this would help or the input file you are using so I and others can work with the same numbers that you are.
I suggest you write a function to read in the array and a second function to output it so that you know it has been read correctly.
Then ... AND ONLY THEN ... you write ONE new function at a time, testing as you go along. They seem to be in order of increasing difficulty.
I suggest you start with something like the template below. Write those two functions at the bottom FIRST, and nothing else until they are working correctly. Otherwise, you will simply be hit with a screenful of errors.
This is what I changed. It says it's giving me errors for findMax()
calcMean() geomMean() rmsAvg() harmMean() sortValues(). Says I did not declare and I'm not sure how to do that. Also, I need double findMax(double data[], int numData){
} in there too and I'm not sure what to put in it. (Also: MAXVALUES in mine is the same as MAXDATA in yours).
//Matthew Cibulka HW9
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
using namespace std;
const int MAXVALUES=100;
int loadArray(ifstream& infile, double data[], int MAXDATA);
int main(){
int numValues=0;
double resist[MAXVALUES];
findMax();
ifstream infile("HW9resist.txt");
if(!infile){
//if condtion is true
cout<<"Error: can't open file";
return 1;
}
numValues=loadArray(infile, resist, MAXVALUES);
cout<<numValues<<endl;
cout<<"Matthew Cibulka, Program 9";
cout<<"The maximum value is"<<findMax(resist,numValues);
cout<<"The minimum value is"<<findMin(resist,numValues);
cout<<"The mean is"<<calcMean(resist,numValues);\
cout<<"The geometric mean is"<<geomMean(resist,numValues);
cout<<"The rms average is"<<rmsAvg(resist,numValues);
cout<<"The harmonic mean is"<<harmMean(resist,numValues);
sortValues(resist,numValues);
cout<<"The sorted list of values is";
for(int i=0;i<numValues;i++){
cout<<resist[i]<<endl;
}
return 0;
}
int loadArray(ifstream& infile, double data[], int MAXDATA){
int numData=0;
while(numData < MAXDATA && infile >> data[numData]){
numData++;
}
if(numData >= MAXDATA){
//if condition is true
cout<<"Maximum size of array met"<<endl;
}
if(!infile.eof()){
cout<<"Bad data found"<<endl;
}
return numData;
}
double findMax(double data[], int numData){
}
You have written the line cout << "The maximum value is " << findMax(resist,numValues);
so it will try and find function findMax() ... which you haven't written yet. Nor have you declared such a function before the point of first use.
Similarly with all the other functions.
Remove (or comment out) all those other cout statements until the functions they refer to are ready.
As I suggested, DO ONE THING AT A TIME and build up slowly. Please don't completely ignore advice.
Also, enclose code snippets within code tags. @Handy Andy has already asked you to do this and given the relevant links. It makes code considerably more readable.
With this program, it is outputting the minimum value as 0, which is not correct, so what do I change? I believe everything else is correct.
//Matthew HW9
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
using namespace std;
const int MAXVALUES=100;
int loadArray(ifstream& infile, double resist[], int MAXDATA);
double findMax(double data[], int numData);
double findMin(double data[],int limit); //find the maximum array
double calcMean(double data[], int numData);
double geomMean(double data[], int numData);
double harmMean(double data[], int numData);
void sortValues(double data[], int numData);
double rmsAvg(double data[], int numData);
int main(){
int numValues=0;
double resist[MAXVALUES];
int limit=numValues;
ifstream infile("HW9resist.txt");
if(!infile){
//if condtion is true
cout<<"Error: can't open file";
return 1;
}
numValues=loadArray(infile, resist, MAXVALUES);
cout<<"Matthew Cibulka, Program 9\n";
cout<<"Sample Resistance Study\n";
cout<<"\nThe maximum value is: "<<fixed<<setprecision(2)<<findMax(resist,numValues)<<fixed<<setprecision(2);
cout<<"\nThe minimum value is: "<<findMin(resist,numValues);
cout<<"\nThe mean is: "<<fixed<<setprecision(6)<<calcMean(resist,numValues);
cout<<"\nThe geometric mean is: "<<geomMean(resist,numValues);
cout<<"\nThe rms average is: "<<rmsAvg(resist,numValues);
cout<<"\nThe harmonic mean is: "<<harmMean(resist,numValues);
sortValues(resist,numValues);
cout<<"\nThe sorted list of values is:";
for(int i=0;i<numValues;i++){
cout<<" "<<fixed<<setprecision(2)<<resist[i]<<endl;
}
return 0;
}
int loadArray(ifstream& infile, double data[], int MAXDATA){
int numData=0;
while(numData < MAXDATA && infile >> data[numData]){
numData++;
}
if(numData >= MAXDATA){
//if condition is true
cout<<"Maximum size of array met"<<endl;
}
if(!infile.eof()){
cout<<"Bad data found"<<endl;
}
return numData;
}
double findMax(double data[], int numData){
double Max=0;
int i=1;
for(int i=1;i<numData;i++){
if(data[i]>Max){
Max=data[i];
}
return Max;
}
}
Put your code in code tags as you have been requested multiple times.
Amongst other things:
- you aren't considering data[0] in finding either minimum or maximum;
- you declare i twice as an int in findMin() and findMax() - the first won't be used;
- writing double calcMean=0; in a routine called calcMean() isn't going to work; similar problems in harmMean().
- writing Sum=Sum; is a bit pointless;
- double Sum=pow(data[0],2); is wrong in geomMean() - and it's a product here, not a sum;
- use sqrt() not pow( ... , 0.5).
I'm not having another look until I see the code in code tags.