Example # 1: minimal essentials, output, semi-colon, comments( /* */), newline
Example # 2: variables, input, expressions, comments (//), arithmetic, precedence
Example # 3: if statement, assignment (=), indentation, blocks {}, exit( -1), sanity checks
Example # 4: looping, C style input (scanf) and output (printf), display format size(e.g. %4d)
Example # 1: minimal essentials, output, semi-colon, comments( /* */), newline
/* This program simply shows an output line only Author: Charles Babbage Jr. System: C++ on Sun network, g++ compiler Date: 1/1/02 Input: None Output: A one line message */ #includeusing namespace std; int main() { cout<< "Hello everyone!\n"; return 0; }
Example # 2: variables, input, expressions, comments (//), arithmetic, precedence
/*
This program converts Fahrenheit to Celsius.
Author: Dale Reed
System: C++ on UIC Sun network, g++ compiler
Date: 8/26/2002
Input: Fahrenheit temperature
Output: Equivalent Celsius temperature
Sample run:
Please enter the Fahrenheit temperature: 85
85 fahrenheit is 29.4444 celsius.
*/
#include
using namespace std;
main()
{
int fahrenheit, celsius; //Declare the variables used
cout<< "Please enter the Fahrenheit temperature: "; //Prompt
cin>> fahrenheit; //Read in fahrenheit
// Compute and display result
cout<< fahrenheit
<< " fahrenheit is "
<< (fahrenheit - 32) * 5.0/9.0
<< " celsius."
<< endl;
}
Example # 3: if statement, assignment (=), indentation, blocks {}, exit( -1), sanity checks
/* * This program converts either way between Fahrenheit and Celsius. * * Author: Dale Reed * System: C on UIC Sun network, g++ compiler * Date: 1/1/96 * Input: 1 for F.-> C., 2 for C.-> F., and the source temperature * Output: Equivalent temperature in either C. or F. */ #include#include using namespace std; main() { // Variable declaration section int sourceTemperature, answer, menuChoice; cout<< "\nPlease choose one of the following:\n"; cout<< " 1. Farenheit to Celsius\n"; cout<< " 2. Celsius to Farenheit\n"; cout<< " 3. Exit from program\n"; cout<< "Your choice: "; cin>> menuChoice; // Read in the desired menu choice // Process menu option if (menuChoice == 3) { // If menu choice was 3, exit from program exit( 0); // normal program termination } if (menuChoice > 3) { // Abort on invalid menu options cout<< "*** Invalid menu option entered.*** Aborting...\n"; exit( -1); // Abnormal program termination } //Prompt for temperature cout<< "Please enter the source temperature: "; cin>> sourceTemperature; // Calculate correct answer depending on menu selection if (menuChoice == 1) { //F. to C. answer = (sourceTemperature - 32) * 5.0/9.0; } else if (menuChoice == 2) { // C. to F. answer = sourceTemperature * 9.0/5.0 + 32; } else { // This should never be executed! cout<< "***Sanity check failed. Invalid menuChoice*** Aborting...\n"; exit( -1); // leave the program } cout<< sourceTemperature << " converted is: " << answer << endl; return 0; }
Example # 4: looping, C style input (scanf) and output (printf), display format size(e.g. %4d)
/* This program calculates temperature conversions, showing a table
with farenheit and celsius temperatures.
Input: Program asks for the number of rows in the temperature conversion table
Output: Table showing Fahrenheit in increments of 20 & corresponding Celsius
Author: Dale Reed
System: SUN, CC compiler
Revision History:
8/27/96 Updated for EECS 171 class at UIC
Input: Program asks for the number of rows in the temperature conversion table
Output: Table showing Fahrenheit in increments of 20 & corresponding Celsius
Running the program looks like this:
This program gives a table showing Farenheit to Celsius
temperature conversions, in Fahrenheit increments of 20 degrees.
Enter a value for the number of table rows (< 11): 4
Farenheit Celsius
0 -17
20 -6
40 4
60 15
*/
#include
main()
{
/* Variable declaration section */
int step = 20; /* step size */
int fahrenheit = 0; /* initialize fahrenheit starting value */
int celsius = 0; /* initialize fahrenheit starting value */
int rows = 0; /* use to keep track of number of table rows */
int i; /* loop counter */
/* Prompt for and get the counter for number of table rows */
printf("\nThis program gives a table showing Farenheit to Celsius \n");
printf("temperature conversions, in Fahrenheit increments of 20 degrees.\n\n");
printf("Enter a value for the number of table rows (< 11): " );
scanf("%d", &rows);
if ( rows > 10) { /* Verify number of rows is valid */
printf("Sorry, number must be < 11. Aborting...\n");
exit( -1);
}
/* Main loop, calculating the numbers and displaying the table as we go */
printf("Farenheit Celsius\n"); /*display table title */
i = 0; /* Initialize loop counter just before it is used */
while (i < rows) {
celsius = (5.0/9.0) * (fahrenheit-32.0);
/* Note that "%4d" below signifies a decimal display format in 4 spaces */
printf(" %4d %4d\n", fahrenheit, celsius);
fahrenheit = fahrenheit + step;
i = i + 1;
}
}