Arrays

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;
}
}

double findMin(double data[],int numData){
int i=1;
double Min=data[0];
for(int i=1;i<numData;i++){
if(data[i]<Min){
Min=data[i];
i++;
}
return Min;
}
}

double calcMean(double data[], int numData){
double Sum=data[0];
double calcMean=0;
int i=0;
for(int i=1;i<numData;i++){
if(data[i]<calcMean){
Sum=Sum;
i++;
}
double calcMean=Sum/i;
return calcMean;
}
}


double geomMean(double data[], int numData){
double Sum=pow(data[0],2);
double geomMean=0;
int i=0;
for(int i=1;i<numData;i++){
Sum=Sum*data[i];
i++;
}
geomMean=pow(Sum,(1/i));
return geomMean;
}


double harmMean(double data[], int numData){
double Sum=1/data[0];
int i=0;
for(int i=1;i<numData;i++){
if(i<numData){
Sum=Sum+(1/data[i]);
i++;
}
double harmMean=i/Sum;
return harmMean;
}
}

void sortValues(double data[], int numData){
double number=0;
for(int hold=0;hold<numData-1;hold=hold+1){
for(int comp=hold+1;comp<numData;comp=comp+1){
if(data[comp]<data[hold]){
number=data[hold];
data[hold]=data[comp];
data[comp]=number;
}
}
}
}

double rmsAvg(double data[], int numData){
double Sum=pow(data[0],2);
double rmsAvg=0;
int i=0;
for(int i=1;i<numData;i++){
if(data[i]<rmsAvg){
Sum=Sum+(pow(data[i],2));
i++;
}
}
rmsAvg=pow((Sum/i),0.5);
return rmsAvg;
}
Last edited on
Hello Mattrat,

Before I dig into this I will say this first.

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.


Andy
Hello Mattrat,

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.

Hope that helps,

Andy
Hello Mattrat,

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.

Andy
mattrat wrote:
What else am I supposed to do?


You do one thing at a time.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;

const int MAXDATA = 100;

int loadArray( istream &in, double data[] );
void writeArray( double data[], int size );

//=====================================================================


int main()
{
   double resist[MAXDATA];

   ifstream infile( "HW9resist.txt" );
   if ( !infile )
   {
      cout << "Unable to open file\n";
      return 1;
   }

   int numValues = loadArray( infile, resist );

   cout << "Original array: ";
   writeArray( resist, numValues );
   cout << '\n';
}


//=====================================================================


int loadArray( istream &infile, double data[] )
{
   // WRITE a routine to read from stream infile and RETURN the number of items read
}


//=====================================================================


void writeArray( double data[], int size )
{
   // WRITE a routine to output an array
}


//===================================================================== 


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.
Last edited on
double calcMean(double resist [], int limit){
}

double geomMean(double resist[], int limit){}

double harmMean(double resist [], int limit){}

void sortValues(double resist[], int limit){}

double rmsAvg(double data[], int limit){
double sum=rms
}

Not sure what to put inside these functions

How do you write calcMean in this function
Last edited on
Well, start with calcMean(). How would you calculate the mean if you were given a set of numbers?
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;
}
}

double findMin(double data[],int numData){
int i=0;
double Min=0;
for(int i=1;i<numData;i++){
if(data[i]<Min){
Min=data[i];

}
return Min;
}
}


double calcMean(double data[], int numData){
double Sum=data[0];
double calcMean=0;
int i=0;
while(i<numData){
Sum=Sum;
i++;
}
calcMean=Sum/i;
return calcMean;
}


double geomMean(double data[], int numData){
double Sum=pow(data[0],2);
double geomMean=0;
int i=0;
while(i<numData){
Sum=Sum*data[i];
i++;
}
geomMean=pow(Sum,(1.0/i));
return geomMean;
}


double harmMean(double data[], int numData){
double Sum=1/data[0];
int i=0;
while(i<numData){
Sum=Sum+(1/data[i]);
i++;
}
double harmMean=i/Sum;
return harmMean;
}

void sortValues(double data[], int numData){
double number=0;
for(int hold=0;hold<numData-1;hold=hold+1){
for(int comp=hold+1;comp<numData;comp=comp+1){
if(data[comp]<data[hold]){
number=data[hold];
data[hold]=data[comp];
data[comp]=number;
}
}
}
}

double rmsAvg(double data[], int numData){
double Sum=pow(data[0],2);
double rmsAvg=0;
int i=0;
while(i<numData){
Sum=Sum+(pow(data[i],2));
i++;
}
rmsAvg=pow((Sum/i),0.5);
return rmsAvg;
}
Last edited on
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.
Last edited on
What numbers does your input file contain ?
Does it have any negative numbers?
Topic archived. No new replies allowed.