Today will be the first evening for an engineering class... I've already previewed tonight's lecture material that includes some sample C++ code.
I took C++ ~10 years ago. Haven't used it since and am very rusty. I still have Borland's C++ v5.02 compiler. When plugging the teacher's code in the "old" compiler, none of them work.
Obviously, I'll get more info tonight. I was wondering if someone already could tell me what I'm missing. Has the language since and I could not use v5.02 for the suggested code? Are the progams (between ***s below) all separate sample program?
EEH
Sample Code
==========
// pe02-01.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Replace this with your code.\n";
return 0;
}
**************************
**********
**********
**********
**********
*
// yourcat.cpp -- input and output
#include <iostream>
using namespace std;
int main()
{
int fleas;
cout << "How many fleas does your cat have?\n";
cin >> fleas; // C++ input
// next line concatenates output
cout << "Well, that's " << fleas << " fleas too many!\n";
return 0;
}
**************************
**********
**********
**********
**********
*
// sqrt.cpp -- use a square root function
#include <iostream>
using namespace std;
#include <cmath> // or math.h
int main()
{
double cover, side; // use double for real numbers
cout << "How many square feet .. do you have?\n";
cin >> cover;
side = sqrt(cover); // call function, assign return value
cout << "Your square has sides of " << side;
cout << " feet << \n";
return 0;
}
**************************
**********
**********
**********
**********
*
// forloop.cpp -- introducing the for loop
#include <iostream>
using namespace std;
int main()
{
int i; // create a counter
// initialize; test ; update
for (int i = 0; i < 5; i++)
{
cout << "C++ knows loops.\n";
} //Use braces if >1 statement
cout << "C++ knows when to stop.\n";
return 0;
}
**************************
**********
**********
**********
**********
*
// uniform random number generator
#include <iostream>
#include <cmath>
using namespace std;
//function prototypes (if any)
int main()
{
// initializations
const int N=1024 ;
int i, tst;
float r, avg, x2, sum, var ;
r = 0.0 ;
tst = 1 ;
while (tst >= 0 ) // while loop
{
cout << "continue? ;
cin >> tst;
//test for termination condition
if (tst >= 0)
{
float max = pow(2.0,15.0); // math fcn.
max=max-1; // want 2 **15 - 1
cout<< "\n";
avg=0;
x2=0;
sum=0;
int init=-1;
for ( i=0; i<N; i++ )
{
r = rand() / max; // normalize {0-1}
x2=x2 + r*r; // accum. xi^2 and xi
sum = sum + r;
} // end for
// compute sample variance and average
var = ( N * x2 - sum*sum ) / ( N *( N-1) );
cout << "average = < sum / N << "Sample var. = << var << "\n";
}// end if
}// end while
return(0); /*end */
}
// end Example
**************************
**********
**********
**********
**********
*