[ back to toc ]

C ++ Program

Date: 2002/05/16 09:33

Q:
My program should print out means, standard deviations, the covariance,
and the correlation coefficients using these formulas
Mean(X)=($X)?NM
CoVariance(X,Y) = ($XY)/N - Mean(X) * Mean(Y)
StDev(X)=sqrt(CoVariance(X,X))
CorrelationCoeff(X,Y)=CoVariance(X,Y)/(StDev(X) * stDev(Y))

My program output should look like this:
1 91.30 68.40
and so on, but my program is not reading both values. It's just reading
the X not the Y. can you tell me what I am doing wrong? Did I need to
send you a copy of my program?
A:
>Did I need to send you a copy of my program?

Definitely.

Regards,
Peter
Q:
// This program reads in an array, calculcates the sum,
// and prints out the result in a neat table.
// It illustrates Array Operations and File Input.

#include <fstream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <assert.h>

enum {Size = 100};

{
double Sum=0;
int k;
for (k=0;k<N;k++) Sum += X[k];
return (Sum);
}

{
return ( SumArray(X,N)/N );
}

{
ifstream DataFile(FileName); //input-File Stream. Name is in FileName
double tmp;
N = 0;
while(true) { // Will break out on EOF
DataFile>>tmp; //Read in Data
if ( ! DataFile.good() || (N >= Size) ) break; //breaks on EOF
X[N++] = tmp ; // Store New Data in Array & Increment N
}
if (N<=0) { //a Validity Check
cerr<<"\n\a FATAL ERROR...No Data Read from File "<<FileName<<"\n\n";
exit(EXIT_FAILURE);
}
}

{
int k;
cout<<"\nPrinting Array Of "<<N<<" entries. \n\n";
cout<<" K X[K] \n";
cout.setf(ios::showpoint); //needed for columns
cout.setf(ios::fixed,ios::floatfield); // Use Fixed (Not Scientific)
Notation
for (k=0;k<N;k++)
cout<<setw(5)<<k<<" "<<setw(8)<<setprecision(2)<<X[k]<<endl;
}

main()
{
double Array[Size]; //space for 0 ,..., Size-1
int N;

ReadArray("CS218_Tprt.dat",Array,N); //reading from Test file
assert(N>0); //internal consistency check (in DEBUG configuration)
PrintArray(Array,N);
cout<<"\nSum is "<<SumArray(Array,N)<<"\n";
cout<<"The average is "<<Average(Array,N)<<"\n";
cout<<"\nHave a Nice Day!"<<"\n";
return(EXIT_SUCCESS); //EXIT_SUCCESS is #defined as 0 in stdlib.h
}

A:
The program you have sent me does not even compile.

verhas@verhaslinux:~$ gcc -o test test.cc
test.cc:14: parse error before `{'
test.cc:17: parse error before `for'
test.cc:17: `N' was not declared in this scope
test.cc:17: parse error before `;'
test.cc:17: syntax error before `++'
test.cc:29: ANSI C++ forbids declaration `N' with no type
test.cc:30: parse error before `while'
test.cc:33: ANSI C++ forbids declaration `X' with no type
test.cc:33: variable-size type declared outside of any function
test.cc:33: invalid initializer
test.cc:34: parse error before `}'
test.cc:37: ANSI C++ forbids declaration `exit' with no type
test.cc:37: `int exit' redeclared as different kind of symbol
/usr/include/stdlib.h:520: previous declaration of `void exit(int)'
test.cc:38: parse error before `}'
test.cc:44: syntax error before `<'
test.cc:45: syntax error before `<'
test.cc:46: syntax error before `.'
test.cc:47: syntax error before `.'
test.cc:48: parse error before `;'
test.cc:48: syntax error before `++'
test.cc: In function `int main()':
test.cc:58: implicit declaration of function `int ReadArray(...)'
test.cc:60: implicit declaration of function `int PrintArray(...)'
test.cc:61: implicit declaration of function `int SumArray(...)'
test.cc:62: implicit declaration of function `int Average(...)'

[ back to toc ]