Kalamazoo RESA 1 Algebra II in EMIT Pathway
Basics to C++ Programming
Topics
Basic Text Output Basic Input Basic Loops Software Overview: Dev-C++ Compiler
A compiler takes the code that is written and converts it into machine code for input/output purposes. There are a number of compilers in the market place. This is just one of many. It is a free open source software produced by
1. Load Dev C++ Software
2. Read/Close or Close through the Tips of the Day. Do Not Check the option to “Don’t Display Tips at Startup”
3. File > New > Source File (this will create an area where the program will be written)
Programing Area Compile/Run Menu
Compile:
Converts programming code to machine code for the OS to read
Run: runs the program. Note muse compile program prior to running
Compile and Run Rebuild All Debug
Kalamazoo RESA 2 Algebra II in EMIT Pathway
Basic Program Start Coding
Libraries
#Include <iostream> : Calls the library within Dev C++ to control the proper Input/Output of C++ programming language. Used as the first line of any program.
using Namespace std; : Calls the standard viewing space. Used as the second line of code in any program
#include <cmath> : Advance math library that includes sin, cos, tan, etc.
#include <cstdlib> :Advance math library that includes statistical analysis data
Basic Codes
Int main() : Initiates the main body of the program
{ } : Braces represents the start and end of a program. All programming code must be between the first and last brace
; : ends a line of code
\n : Written after a set of text in “ “ to enter down to the next line endl; : drops the cursor down to the next line on the screen
return 0; : Ends all programs. Sends back a value of 0 (Null) back to the operating system telling it the program has been completed. May Return other values if the program is a subroutine to a Main Program.
Input/Output Code
cout << “ “ : Sends whatever is listed between quotes to the screen or what the value of a variable is equal to.
cin >> : Has the user enter some form of data Variable Types
int: Integer Value or Whole numbers. Can also be declared as a short or long value
float: Allows for real number values to be used. Allows use of decimal values. Can also be declared as float or double char: Single character entry
string: Word character entry
bool: Logical entry. Typically True/False Statements
Kalamazoo RESA 3 Algebra II in EMIT Pathway
Mathematical Symbols
Standard Symbols +, - , *, /
Mathematical Comparison Symbol Description
== = “equals”
!= Not Equals
> > Greater Than
< < Less Than
>= >= Greater than or Equal to
<= <= Less Than or Equal to
&& && And Statement
|| And/Or Statement (Keystroke shift+backslash key)
Statements
If (condition) { statements }Carries out the Statements of Condition if True
If (condition) { statement }
else{
statements }
Checks to see if Condition is True, if so then carries out the Statement below and after skips the else. If Condition is False then carries out the Else Statements.
If (condition) { statement }
else if (Condition) { statements
} Else{
Statements }
Checks to see if Condition is True, if so then carries out the Statement below and after skips the if/else and else commands. If the first Condition is False then checks the second condition: If true then carries out the statement if False then carries out the final Else Statements.
Kalamazoo RESA 4 Algebra II in EMIT Pathway
Loops
While (conditions, iterator variable, variable decrement or increment) {
statements }
Goes through the loop while the Conditional Statement is True. Once False the Program will fall out of the loop continuing on with the program.
For (conditions, iterator variable, variable decrement or increment) {
Statements }
Goes through the loop while the Conditional Statement is True. Once False the Program will fall out of the loop continuing on with the program.
Do { Statements
} while (conditions, iterator variable, variable decrement or increment);
Goes through the loop while the Conditional Statement is True. Once False the Program will fall out of the loop continuing on with the program.
Kalamazoo RESA 5 Algebra II in EMIT Pathway
Program 1: Hello World
Type the follow line of code. Be sure to copy exactly. NOTE a missed bracket, parentheses, or semi-colon will result in the software not working. The actual program code will be in BOLD lettering and Line Notations will be in italic.
// Symbol represents a notation for that line (AKA comment about the line of code). Typically programmers will use this type of notation to describe what is happening on that line of code. We will use it as a learning tool. You DO NOT need to copy down this part of the code unless you want to as reference.
#include <iostream> //Calls library for Input/Output for the Operating System to configure
using namespace std; //simplifies some of the coding when asking for input or output
int main() //Starts the program. All programs will begin with this coding
{ //brace represents the program parameter
char a; //Declares character variable
cout << "Hello World" << endl; //cout << will output data to the operating system; endl; will be the same as hitting enter on the keyboard. This will drop the cursor down to the next line.
cin >> a; // Keeps output window open.
return 0; //return 0; will send back a zero value to the start of the program, thus ending the program
} //the final brace will complete the program. Since we started with a
right brace at the beginning of the program, we must end with the opposite brace (left facing) to formally close off the program; similar to what would occur in a algebraic function.
Save File
Show Teacher Completed Program
Kalamazoo RESA 6 Algebra II in EMIT Pathway
C++ Input
To input data into a program you must first declare a variable (I.E Int, Char, String, Real, etc) in order to be able to record the user input. The variables should be declared at the beginning of the program below the first brace. NOTE theoretically a programmer can declare a variable in anywhere in the program, but this is seen as sloppy and against best practice.
Below there are added lines to the Hello World! Program. Add the additional lines in. We will be adding a string variable that will be asking your name.
To simplify the sample I have gotten rid of the line descriptors for the program and added new line descriptors for the new lines.
#include <iostream>
using namespace std;
int main() {
char a;
string firstname; //declares variable string which is equal to a word. If a
single letter is desired for entry the variable type would be a char.
cout << "Hello World" << endl;
cout << "What is your name?";
cin >> firstname; // cin>> allows user to type in info and assign to a variable cout << "Hello " << firstname << " my name is PC" << endl; // provides text with embedded version of
variable. endl; drops the next command to a line below. Try removing and see what happens.
cin >> a;
return 0;
}
Save File
Show Teacher Completed Program
Kalamazoo RESA 7 Algebra II in EMIT Pathway
Assignment: Have the program have you enter your age. Declare a variable that is int (integer) for your age. Be careful because this will only let you enter whole numbers (no decimals). What
happens if you enter a number that is a decimal?
If Statement
If statements allow us to make comparisons then if the comparison is true execute a sets/blocks of code. There are also if/else statements that allow us to make a comparison and if the comparison is true execute a sets/blocks of code and if false execute a different set line(s) of code.
This program will have us use your age as a comparison using an if/else statement. We will compare age to 40. Anything below will say you are young and anything above will say you are over the hill.
#include <iostream>
using namespace std;
int main() {
char a;
string firstname;
int age;
cout << "Hello World" << endl;
cout << "What is your name?";
cin >> firstname;
cout << "Hello " << firstname << " my name is PC" << endl;
// CODE FOR ASKING YOUR AGE HERE. THIS IS CODE FROM THE PREVIOUS PAGE. BLOCKED OUT FOR YOUR TO WRITE.
if (age <= 40) {
cout <<"You are a youngster" << endl;
} else
Kalamazoo RESA 8 Algebra II in EMIT Pathway {
cout << "You are Over the Hill!" << endl;
} cin >> a;
return 0;
}
Assignment:
Add an if statement that says the following1. 0-10 or below you should have parent permission to be on the computer 2. 11-20 this is way too easy for you
3. 21-79 You know your stuff
4. 80 or above I am impressed you are tech savvy
Loops
For: Runs a loop until a condition becomes true. I.E counting the number of times through a loop and exiting the loop once a counter gets to a certain value.
While: Runs a loop as long as a certain condition is true.
We will add a for loop in this program to have a person enter 4 numbers and find the average. 1st we need to make 4 new variables
1. counter 2. average 3. number 4. total
Note the use of a common next to the age variable. We can add , next to a variable to add more on a single line. This helps reduce the length of the code. We also have to declare a value for the counter. We will make the average, total, and number a real number which is declared as float type of variableso the program can output a value with decimals. This resets the counter each time the program is run.
Kalamazoo RESA 9 Algebra II in EMIT Pathway
#include <iostream>
using namespace std;
int main() {
char a;
string firstname;
int age;
float average=0, number=0, total=0; //variables for finding average of 4 numbers cout << "Hello World" << endl;
cout << "What is your name?";
cin >> firstname;
cout << "Hello " << firstname << " my name is PC" << endl;
// CODE FOR ASKING YOUR AGE HERE if (age <= 40)
{
cout <<"You are a youngster" << endl;
} else {
cout << "You are Over the Hill!" << endl;
}
cout << “You will enter 4 numbers and we will find the average for you.” << endl;
for (int counter =1; counter <= 4; counter++) {
cout << “enter #“ << counter << “:”;
cin >> number;
Kalamazoo RESA 10 Algebra II in EMIT Pathway total = number+total;
average = total/counter;
}
cout << “The summation of numbers entered is: “<< total << endl;
cout << “The average of your numbers is:” << average << endl;
cin >> a;
return 0;
}
Save File
Show Teacher Completed Program
Kalamazoo RESA 11 Algebra II in EMIT Pathway
Do/While Loop
Do/While and While loops allows the user to continue a loop as long as a condition is True. Often times used to ask the user if they want to continue a specific task.
#include <iostream>
using namespace std;
int main() {
char a;
string firstname;
int age;
float average=0, number=0, total=0; //variables for finding average of 4 numbers cout << "Hello World" << endl;
cout << "What is your name?";
cin >> firstname;
cout << "Hello " << firstname << " my name is PC" << endl;
// CODE FOR ASKING YOUR AGE HERE if (age <= 40)
{
cout <<"You are a youngster" << endl;
} else {
cout << "You are Over the Hill!" << endl;
}
Kalamazoo RESA 12 Algebra II in EMIT Pathway do
{
cout << “You will enter 4 numbers and we will find the average for you.” << endl;
for (int counter =1; counter <= 4; counter++) {
cout << “enter #“ << counter << “:”;
cin >> number;
total = number+total;
average = total/counter;
}
cout << “The summation of numbers entered is: “<< total << endl;
cout << “The average of your numbers is:” << average << endl;
cout >> “ Press any key to continue or press x to exit”;
cin >> a;
}
while (a != ‘x’); // Compares the input of variable a to see if the loop should continue. Loop will // continue as long as the variable a does not equal x
return 0;
}
Save File
Show Teacher Completed Program
Kalamazoo RESA 13 Algebra II in EMIT Pathway
1-Hour Block Program(s)
1. Factorials
a. Input: Allow user to input the number they want to find the factorial of b. Output the following
i. Users number the user wanted to find the factorial of ii. What the factorial is
I.E Input: 3
The Factorial of 3! = 6
c. Ask if the user would like to find another factorial. Repeat until user wants to exit.
2. Change
a. Input: User enters as a decimal the change they received from a purchase b. Output:
i. The total number of quarters, dimes, nickels and pennies the user will receive back. Note:
Program will be designed to give back the fewest amount of coins (I.E $0.54 = 2 quarters and 4 pennies; NOT 54 pennies or an other combination).
c. Ask the user if they would like to run the program again
Kalamazoo RESA 14 Algebra II in EMIT Pathway
2- Hour Block Program: Calculator
Create a program using C++ that does the following.
Problem: Calculator Program
1. Ask the user their name. When outputting any answer use their name before you give the answer.
2. Have the user choose between Add, Subtract, Divide, or Multiply
3. Have the user input total number of values to be Added, Subtracted, Divided, or Multiplied (may enter 2 or more values if the user enters 0 or 1 tell them this is an error and try again and you may only have 2 values for dividing and 0 cannot be the denominator.)(Hint: will need to use this input in a loop)
May consider using numbers or letters for the user to input to pick their operation.
Note if you use letters and make a comparison in an If statement the letter chosen needs to be in ‘ ‘. See Example Below (using the letter a for addition entry)
if (choice == ‘a’ )
{ statement (I.E Output/Input, calculations, etc.) }
4. Have User input values
5. Output the answer by telling the user this is your answer.
6. Ask User if they want to solve another problem.
a. If yes redisplay the options
b. If no exit the program with a friendly message
c. See note above if using letters y or n for yes and no to continue or exit.
7. Make the program user friendly and inviting to use.