These are notes from 16 September.
// Convert F to C, based on user input
#include <iostream>
using namespace std;
int main()
{
double temp_f;
cout << "Enter temperature in F: ";
cin >> temp_f;
double temp_c;
temp_c = (temp_f - 32) / 1.8;
//temp_c = 9.0/5 * temp_f + 32;
cout << temp_f << "F is " << temp_c << "C\n";
return 0;
}
#include <iostream>
using namespace std;
int main()
{
// Conditional statement:
if(8 < 3) // false: 8 is NOT less than 3
{
// SKIP this body if condition was false
cout << "Did this print?\n";
}
cout << "Now this should print.\n";
// Conditional expression: anything
// that produces a yes/no answer (Boolean)
if(3 < 8) // YES, 3 is less than 8
{
// Execute the body, because condition was true
cout << "That was true.\n";
}
return 0;
}
// Compute area of a circle or a rectangle
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout << "Would you like the area of a:\n";
cout << " (1) circle\n";
cout << " (2) rectangle\n";
cout << "Your choice: ";
int choice;
cin >> choice;
if(choice > 2 || choice < 1)
{
cout << "ERROR: please select 1 or 2.\n";
}
if(choice == 1)
{
cout << "Enter radius: ";
double radius;
cin >> radius;
cout.precision(10);
//cout << "Math library says PI=" << M_PI << "\n";
double area = M_PI * radius * radius;
cout << "Area of that circle is " << area << "\n";
}
if(choice == 2)
{
double width, height;
cout << "Enter width: ";
cin >> width;
cout << "Enter height: ";
cin >> height;
double area = width * height;
cout << "Rectangle has area " << area << "\n";
}
return 0;
}
// Compute area of a circle or a rectangle
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int x;
cout << "Enter an integer: ";
cin >> x;
// Two precise alternatives: either one is true
// or the other. Never both, never neither.
if(x > 0)
{
cout << "positive\n";
}
if(x <= 0) // Opposite of greater than is
{ // Less-than-or-equal-to
cout << "not positive\n";
}
if(x < 0) // if-else statement
{
cout << "negative";
}
else
{
cout << "non-negative";
}
return 0;
}
// Compute area of a circle or a rectangle
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout << "Would you like the area of a:\n";
cout << " (1) circle\n";
cout << " (2) rectangle\n";
cout << "Your choice: ";
int choice;
cin >> choice;
if(choice == 1)
{
cout << "Enter radius: ";
double radius;
cin >> radius;
cout.precision(10);
//cout << "Math library says PI=" << M_PI << "\n";
double area = M_PI * radius * radius;
cout << "Area of that circle is " << area << "\n";
}
else if(choice == 2) // the if/else chain
{
double width, height;
cout << "Enter width: ";
cin >> width;
cout << "Enter height: ";
cin >> height;
double area = width * height;
cout << "Rectangle has area " << area << "\n";
}
else
{
cout << "Error: invalid choice.\n";
}
return 0;
}