Computer Science 394
Physics 313
Microcomputer Control
Dickinson College
Spring Semester 1999
Grant Braught
Class #2 - Introduction to Programming pt. 2
Main Ideas For Today 
- Nested For Loops
- If statements
- Logical Operators
- functions
- parameters and arguments
- return values
- Arrays
Activities 
Complete the following activities with the assitance of the TA and Prof. Braught. Be sure to keep a complete record of what you have done in your lab notebook. (The Lab Notebook specifications are now on-line.)
Nested For Loops:
It is very common that you will want to use one for loop inside another for loop. For example the following program will print a grid of X's that is 10 columns wide and 8 rows high.
#include
void main()
{
int i = 0;
int j = 0;
for (i=1; i<=8; i++) // Loop for each Row in the grid...
{
for (j=1; j<=10; j++) // Loop for each of the columns in the ith row...
{
cout << "X "; // Print the X in the (i,j) location.
}
cout << endl; // End the current row and move on to the next.
}
}
- Modify this progrm so that it asks the user for the dimensions of the grid to print and then prints a grid of the requested dimensions.
- Save this program in your directory as "Grid.cpp" we will come back to it later.
If Statements:
If statements provide a way of executing parts of your program only under particular circuimstances. For example the following program will produce output only if the user enters a number that is greater than 0.
#include
void main()
{
int theInput = 0;
cout << "Enter a number: ";
cin >> theInput;
if (theInput > 0)
{
cout << theInput << " is greater than 0!" << endl;
}
}
It is also possible to use an else clause with an if statement. The else clause will be executed only when the condition of the if statement is false. For example the following program prints a message specifiing if the number is greater than 0 or less than or equal to zero.
#include
void main()
{
int theInput = 0;
cout << "Enter a number: ";
cin >> theInput;
if (theInput > 0)
{
cout << theInput << " is greater than 0!" << endl;
}
else
{
cout << the Input << " is less than or equal to 0!" << endl;
}
}
You can also use additional if statements within an else clause to further refine which parts of your program are executed. For example the following program detects the three cases of a number being greater than, less than, or equal to 0.
#include
void main()
{
int theInput = 0;
cout << "Enter a number: ";
cin >> theInput;
if (theInput > 0)
{
cout << theInput << " is greater than 0!" << endl;
}
else
{
if (theInput < 0)
{
cout << theInput << " is less than 0!" << endl;
}
else
{
cout << the Input << "is equal to 0!" << endl;
}
}
}
- Modifiy your Grid.cpp program so that it asks the user for a particular row to be printed using O's instead of X's.
Logical Operators:
Often you will want the condition of an if statement to depend on more than one condition. You can combine as many conditions as you want in an if statement by using the Logical Operators. These operators include:
The following program determines if a number entered by the user is between 1 and 10 but is not 5.
#include
void main()
{
int theInput = 0;
cout << "Enter a number [1..4,6..10]: ";
cin >> theInput;
if (theInput >= 1 && theInput <= 10 && theInput != 5)
{
cout << "You follow directions well." << endl;
}
else
{
if (theInput == 5) // Notice == is use to check for equality!
{
cout << "Notice that the valid numbers do not include 5." << endl;
}
else
{
cout << "Come on, you can do better than that.
}
}
}
- Write a program that checks to make sure a number entered by the user is not in the range [10..20].
- Modify your grid.cpp program so that the user can enter both a row and a column to be printed as O's instead of X's.
- Modify your grid.cpp program so that the user can enter a row and a column for a single element of the grid to be printed as a Z.
Functions:
As your programs become longer and more complicated it will be useful to break them up into parts called functions. Functions provide a convienent way to organize your programs into logical components. While the following program does not strictly need a functions it does demonstrate how they work.
#include
// You need to let your program know about the functions
// you will be using. These are called prototypes. We will
// write the actual code of the function later...
void funcOne();
void main()
{
cout << "This line comes from main..." << endl;
funcOne();
cout << "This line comes from main too..." << endl;
}
void funcOne()
{
cout << "This line comes from funcOne(); << endl;
}
- Add two more functions to this program called funcTwo and funcThree.
- Change the main function so that it presents a menu to the user and asks them which of the three functions they want to run. When run your program might produce the following:
--------------
1. funcOne
2. funcTwo
3. funcThree
---------------
choice: {User types choice here}
- When the user enters their choice main should call the appropriate function (this will require an if statement.)
Parameters and Arguments:
Sometimes a function will need information about the value of variables in the part of the program from which it was called. For example consider the following program that:
asks the user to enter a temperature in degrees celcius and then calls a function that converts that temperature into degrees Fahrenheit and prints the answer.
#include
// Prototypes.
void Cel2Fahr(double tempInCel); // tempInCel is a paramenter to the function.
void main()
{
double userTempC = 0;
cout << "Enter a temperature in Celcius: ";
cin >> userTempC;
Cel2Fahr(userTempC); // userTempC is called an argument
// to the function.
}
void Cel2Fahr(double tempInCel) // the parameter tempInCell will have
// whatever value userTempC had in main.
{
double tempInFahr = 0;
tempInFahr = (9.0/5.0)*tempInCel + 32;
cout << tempInCel << " Celcius is " <<
tempInFahr << " Fahrenheit." << endl;
}
- Add another function Fahr2Cel to this program. Farh2Cel should have one parameter, a temperature in Fahrenheit, which it will convert to Celcuis and print the result.
- Modify main so that it first asks for a temperature in Celcius and converts it to Faharenheit and then asks for a temperature in Fahrenheit and converts it to Celcuis.
- Save this program as TempConv.cpp we will use it again later.
Return Values:
Often a function will perform a calculation and we will not want the result to be displayed but rather we will want to save the result for later use. This can be done by having the function return a value. The following program is a modification to the Cel2Fahr program from above:
#include
// Prototypes.
double Cel2Fahr(double tempInCel); // Note: the double before the Cel2Fhar where
// void used to be. This indicates that this
// function will return a double value.
void main()
{
double userTempC = 0;
double convTempF = 0;
cout << "Enter a temperature in Celcius: ";
cin >> userTempC;
convTempF = Cel2Fahr(userTempC); // The value returned by Cel2Fahr will be
// place into the variable convTempF.
cout << userTempC << " Celcius is " <<
convTempF << " Fahrenheit." << endl;
}
double Cel2Fahr(double tempInCel) // the parameter tempInCell will have
// whatever value userTempC had in main.
{
double tempInFahr = 0;
tempInFahr = (9.0/5.0)*tempInCel + 32;
return tempInFahr; // Return the result of the calculation.
}
- Modify your TempConv.cpp program so that your Fahr2Cel function returns its result instead of displaying it.
- Display the result of your conversion from Fahrenheit to Celcius by adding cout statements to main.
Arrays:
Arrays will provide a method for storing and accessing a sequence of data elements using a single variable. The easiest way to think of an array is as a column in a spreadsheet. However, in the case of an array all of the cells in the column must be of the same type. The following program creates an array, stores a few values in it and then accesses those elements.
#include
void main()
{
int myArray[25]; // Create an array with 25 total slots.
// These slots will be numbered 0..24
myArray[0] = 22; // The slots in the array start with 0.
myArray[13] = -232;
myArray[24] = 73120; // The last slot in this array is 24.
cout << "myArray[0] = " << myArray[0] << endl;
cout << "myArray[13] = " << myArray[13] << endl;
cout << "myArray[24] = " << myArray[24] << endl;
}
- Write a program that:
- creates an array of double values with 10 slots.
- reads in 10 numbers, one at a time, from the user and places them into the 10 slots.
- Writes out the 10 numbers on a single line separated by commas.
Homework Assignment 
Due Tuesday 2/2/99
Write a program that does the following:
- Read in a list of numbers from the user (10 numbers is fine.)
- Display a menu with the following options:
- Total - sum up all of the numbers.
- Product - multiply all of the numbers.
- Average - average the numbers.
- Ask the user what statistic they would like about their numbers (i.e. present a menu)
- Call a function that calculates and returns the desired statistic. You should use one function for each of the statistics. These functions should be similar to Cel2Fahr but will probably need to include a bit more code. (Hint: a for loop!)
NOTE: The prototype for a function that has an array of doubles as a parameter
will look as follows:
double computeTotal(double theArray[]);
You will call this function as follows:
double theTotal = 0;
double myArray[10];
{Load your array with values...}
theTotal = computeTotal(myArray);
- Display the result.