These are notes from 9 September.
// Any line that starts with double-slash
// is a "comment." -- it is ignored by
// the compiler.
#include <iostream>
using namespace std; // my namespace
int main()
{
/* Another comment --
can go for multiple lines */
cout << "Hello again, CS102.\n";
cout << "This is another stmt."
<< " and more stuff here.\n";
return 0;
}
// variables.cpp
#include <iostream>
using namespace std;
int main()
{
// Variable is a named storage location.
// Must declare the TYPE of the variable.
// (1) int -- an integer (whole number)
// (2) double -- a floating-point number
int x; // Declare x as an integer
double y;
x = 13; // "x gets the value 13"
y = 3.1415 * 24;
cout << "This is an integer: "
<< x << "\n";
cout << "Your result is:"
<< y << "\n";
x = 80;
cout << "This is an integer: "
<< x << "\n";
return 0;
}
#include <iostream>
using namespace std;
int main()
{
double temp_f, temp_c;
temp_c = 22; // input
// calculation
//temp_f = 9*temp_c/5 + 32; // works!
//temp_f = 9/5*temp_c + 32; // fails!
//temp_f = 1.8*temp_c + 32; // works
temp_f = 9.0/5*temp_c + 32; // works!
// output
cout << temp_c << "C is "
<< temp_f << "F\n";
return 0;
}
#include <iostream>
using namespace std;
int main()
{
double temp_f, temp_c;
//temp_c = 22; // input
cout << "Type a temperature in C: ";
cin >> temp_c;
// calculation
temp_f = 9.0/5*temp_c + 32; // works!
// output
cout << temp_c << "C is "
<< temp_f << "F\n";
return 0;
}