SlideShare a Scribd company logo
1 of 27
C++ Programming (CST2403)
NOTES (SPRING 2015)
Prof. Elhari
By: Clara Irizarry
Clara Irizarry 9
Prof. Elhari CST2403-E340
WEEK 1: February 6, 2015
KEY TERMS
Program: set of instructions to solve a problem using programming (C++).
Algorithm: set of instructions to solve a problem using human language (English).
Flowchart: set of instructions to solve a problem using symbols.
Pseudo code: set of instructions to solve a problem using both programming language AND
human language.
FLOWCHART SYMBOLS
start / stop
input / output
process
flow (direction)
connector
Decision (if)
*for comparing
Clara Irizarry 9
Prof. Elhari CST2403-E340
Example: Write a flowchart to print the sumof 2 numbers.
Example: Write a flowchart to print yourname and address.
start
start
a, b
Sum= a+b
Name
Z
Sum
stop
Address
Name
Address
Stop
Output
s
Inputs
Clara Irizarry 9
Prof. Elhari CST2403-E340
DIAGRAM OF COMPUTER SYSTEMS
1. Input is processed by CPU.
2. CPU sends out result to output.
3. CPU also stores data in memory.
4. Data can also directly go to the output.
5. Data can be reprocessed (LOOP).
CPU
Output
Memory
Input
1 2
3 5
4
Clara Irizarry 9
Prof. Elhari CST2403-E340
MENTAL PROCESS OF SOLVING PROBLEMS USING COMPUTERS
1. Well-defined problem  Understand the problem before trying to solve it.
2. Algorithm  Write the solution to your problem.
3. Pseudo code  Details of the solution.
4. Program  Translate to C++.
5. Input  Type into computer.
6. Compile  Finalize program via compiler.
 Find errors.
1. Syntax – misspelling
2. Runtime – runs into something that cannot be done
3. Logical – mix up logic (ex: operators)
7. Run  Run the finished program.
8. Solution  Check if the solution is of the original problem, and look for errors in
program via the output.
C++ PROGRAM STRUCTURE
 C++ is case sensitive.
 First line of code: #include <iostream> (C++ standard library)
 White spaces are ignored.
 Next line: using namespace std;
 Next line: int main(){
 Every function must return a value of some type (ex: int will return int).
 LAST line: return 0;
 Semicolon is a line terminator (ends the line).
 Comments are ignored by the compiler.
 // ONE line comment AND /* ………..*/ BLOCK comment
o SAMPLE PROGRAM
#include <iostream>
using namespace std;
int main(){
return 0;
}
o COMMANDS
 cout <<  output
 cin >>  input ( must PROMPT it)
-Codes
-Statements
Clara Irizarry 9
Prof. Elhari CST2403-E340
**program1: Design a program that will print out a message.
//program1: To print "Welcome to C++".
#include<iostream>
using namespace std;
int main(){
cout <<"Welcome to C++!"<< endl;
system("pause");
return 0;
}
OVERVIEW OF C++ PROGRAMMING
*Email: CST2403ELHARI@gmail.com
- Syntax, rules, definitions Subject line  S15#__ HW# __ Name
- Selections (1, 2, 3, 4, 5)
- Switch case
- Functions
- Arrays
- Structures / Classes
- Pointers
HW#1
a) Pg.1 = COVER SHEET (setup explained)
b) Pg.2= QUESTIONS
-Define: program, algorithm, flowchart, pseudo code
-History of C++
-CPU Clock Speed (1980 – present)
-How to setup / use Microsoft Visual C++?
c) Pg.3= ANSWERS
**DUE 2/6/2015!!!
HW#2
1. Flowchart to print the product and difference of 2 numbers.
2. Flowchart to compare 2 distinct numbers.
3. Flowchart to compare ANY 2 numbers.
4. Flowchart to print the greater of two distinct numbers.
5. Flowchart to print the greater of ANY 2 numbers.
EXTRA CREDIT:Flowchart to print the greatest of ANY 3 numbers.
**DUE 2/13/2015!!!
Clara Irizarry 9
Prof. Elhari CST2403-E340
WEEK 2: February 13, 2015
ORDER OF OPERATIONS (LEVELS OF PRIORITY)
1. Unary minus (-)  ex: -+5 + (5*6) ……. (-5) + 30 = 25
2. Parenthesis ( )  order: INNER MOST
3. Exponent ^  order: OUT to IN
4. Module (%), Division (/), Multiplication (*)  order: LEFT to RIGHT
5. Addition (+), Subtraction(-)  order: LEFT to RIGHT
Ex #1: 5*5%5/5*5%5*(5-5)
5*5%5/5*5%5*0
25%5/5*5%5*0
0/5*5%5*0
0*5%5*0
0%5*0
0*0 = 0
Ex#2: (5+6)*5%10 -10%2*2-(5 %(5*5))
(5+6)*5%10 -10%2*2-(5 %25)
(5+6)*5%10 -10%2*2-5
11*5%10 -10%2*2-5
55%10 -10%2*2-5
5 – 10%2*2-5
5 – 0*2-5
5 – 0 – 5
5 – 5 = 0
Clara Irizarry 9
Prof. Elhari CST2403-E340
IDENTIFIERS (RULES)
1. Combinations of a to z, A to Z, 0 to 9 and __.
a. Variables- to define values in program
b. Program name
c. Statements
2. No blank spaces.
3. No keywords.
a. Keywords have special meaning in C++. Some words are reserved words,which are used
to perform different functions in the program.
4. Start with a letter.
5. Size of identifier (very large, so no worries).
**programs 2 to 6: Design a program that will print out the sum of 5 and 9 (in different manners, using
various cout configurations as well as variables / identifiers.
**programs 7 and 8: Design a program that will print out the sum of any 2 numbers.
ESCAPE SEQUENCE
  goes before the character you wish to print.
Ex: Print “.
cout << “ ” “ ;
Ex: Print .
cout<< “”;
*Commands
n: new line
t: tab
r: return
a: bell (BEEP)
*Data Types
char, int, float, double, boolean, string
HW#3 & 4:
**program9: Print the sum, difference and product of 2 numbers w/out using variables.
**program10: Print the sum, difference and product of 2 numbers using variables.
**program11: Print the sum, difference & product of 2 numbers w/out variables (lots of COUT).
**program12: Print the sum, difference & product of 2 numbers w/variables and CIN (lots of COUT).
**program13: Print (5+9) / 10%2 in ONE line. No variables!
**program14: Print (5+9) / 10%2 in ONE line. Use variables!
**program15: Print ""/Joe""  Jessy""//"*/
**DUE 2/27/2015!!!
Clara Irizarry 9
Prof. Elhari CST2403-E340
WEEK 3: February 27, 2015
RELATIONALS
> , < , >= , <=
EQUALITY & INEQUALITY
== !=
LOGICAL OPERATORS
NOT  ! NOT (F) = T
AND  && T when ALL are T
OR  | | T when AT LEAST ONE is T
DEMORGAN’S LAW
1. NOT (A and B) = (NOT A) or (NOT B)
 ! (A && B) = (!A) | | (!B)
2. NOT(A or B) = NOT(A) and NOT(B)
 ! (A | | B) = (!A) && (!B)
IF STATEMENT
OR
IF ELSE STATEMENT
 else = if NOT (if! ( ) )
if (condition)
statement
if (condition){
statements
}
if (condition)
statement
else
statement
Clara Irizarry 9
Prof. Elhari CST2403-E340
Ex: Write a flowchart to solve a linear equation: ax + b = 0.
Start
a, b, x
No Yes
a = 0
No Yes
“One solution” b = 0
x = -b / a “No solutions” “All solutions”
x
stop
HW#5
-Use truth table to prove DeMorgan’s LAWS to be TRUE.
**program16: To compare 2 numbers using an IF statement.
**program17: To compare 2 numbers using an IF ELSE statement.
**program18: Write a program to solve a linear equation.
**program19: To solve the quadratic equation: x = (-b +- sqrt(b^2 - 4ac)) / 2a
**DUE 3/5/2015!!!
Clara Irizarry 9
Prof. Elhari CST2403-E340
WEEK 4: March 5, 2015
QUADRATIC EQUATION FLOWCHART (from HW)
Start
a, b, c, d, x, x1, x2
a = 0
d = b2 – 4ac b = 0
d < 0 “Linear” c = 0
d = 0 “No solutions” x = -c / b “No solutions” “All solutions”
“Two “Duplicate
solutions” solutions” x
𝑥1 =
−𝑏+√d
2𝑎
x = -b / 2a
𝑥2 =
−𝑏−√d
2𝑎
x
x1, x2
stop
**program20A: Write a program to solve quadratic equation (redo).
Clara Irizarry 9
Prof. Elhari CST2403-E340
MATH OPERATORS
  sqrt *#include <cmath>
*  multiplication
+  addition * (++) OR (--)  increment / decrement
-  subtraction
/  division
%  module (remainder of division)
HW#6
- a) Define a loop. b) Explain what the loop parts are, as well as how they work and why they are
needed. HINT: There are 3 types of loops. c) What are the types of loops used in C++ programming?
Which one is the best and for what cases?
**Program#20: To print your name 5 times without using any loop.
**Program#21: To print your name 5 times using the WHILE loop.
**Program#22: To print the numbers 1 to 19 using the WHILE loop
**Program#23: To print odd numbers between 2 and 18 using loops
**Program#24: To print even numbers between 1 and 19 using loops.
**DUE 3/13/2015!!!
Clara Irizarry 9
Prof. Elhari CST2403-E340
WEEK 5: March 13, 2015
Loop: Set of instructions that are repeated when a condition is proved to be true, until the said
condition is false.
PARTS OF A LOOP
1. Initial
o Starting point of loop.
o Without it, you cannot start the loop.
2. Condition
o The loop itself.
o Without it, there is NO loop.
3. Terminator
o Stops the loop.
o Without it, you have an INFINITE loop.
TYPES OF LOOPS
1. While LOOP
The ‘WHILE’ loop repeats statements only while a condition proves to be true. The
WHILE loop should be used when you’re not sure how many times the loop has to run.
while(condition){
Statement(s)
}
2. For LOOP
The ‘FOR’ loop performs a repeatedly executes a series of statements while managing
the variable being used in the loop. This is only for a certain number of times.
for(initial value (i=0); condition; increment/decrement){
Statement(s)
}
3. Do…While LOOP
The ‘DO…WHILE’ loop repeats statements until the condition becomes false in that
loop. The DO…WHILE loop should be used in the case that you would like to test out
the truth value of the set condition.
do {
Statement(s)
} while (condition);
Clara Irizarry 9
Prof. Elhari CST2403-E340
**program25: To print odd numbers from 19 down to -5.
**program26: To print the sum of numbers between 19 and 31.
**program27: To print the sum of odd numbers between 20 and 40.
**program28: To print the sum of even numbers between 19 and 21.
**program29: To print the sum of multiples of 5 between 1 and 21.
**program30: To print every other number between 1 and 10 starting with 1.
**program31: To print every other number between 1 and 10 starting with 2.
**program32: To print the first and last numbers from 13 to 17.
**program33: To print a list of 5 numbers: 3, 2, -7, 19, -18.
**program34: To print the sum of a list of 5 numbers: 3, 2, -7, 19, -18.
**program35: To print the positive numbers of a list of 5 numbers: 3, 2, -7, 19, -18.
**program36: To print the negative numbers of a list of 5 numbers: 3, 2, -7, 19, -18 (-19 to -2).
**program37: To print the odd numbers of a list of 5 numbers: 3, 2, -7, 19, -18 between 5 and 14.
**program38: To print the first and last numbers from the list.
**program39: To print every other number on the list.
**program40: To check IF -7 exists in the list.
**program41: To count how many -7’s exist in the list.
**programs42 to 62: Same programs for #21 to #41, using FOR LOOP.
**programs63 to 83: Same program for #21 to #41, using DO WHILE LOOP.
TEST #1
 Multiple choice
 True and false
 Definitions
 Write flowcharts
 Write programs
 Find output of programs
 Switch between loops
Clara Irizarry 9
Prof. Elhari CST2403-E340
WEEK 6: March 27, 2015
Switch Cases
Ex: **program84: To print out a number in words using if else
#include<iostream>
usingnamespacestd;
int main(){
int a;
cout << "Enter a number:";
cin >> a;
if (a == 5)
cout << "Five n";
else if (a == 9)
cout << "Nine n";
else if (a == 0)
cout << "Zero n";
else
cout << "Negative n";
system("pause");
return 0;
}
Ex: **program85: To print out a number in words WITH SWITCH CASES
#include<iostream>
usingnamespacestd;
int main(){
int a;
cout << "Enter a number:";
cin >> a;
switch (a){
case5: cout << "Five n";
break;
case9: cout << "Nine n";
break;
case0: cout << "Zero n";
break;
default: cout << "Negative n";
break;
}
system("pause");
return 0;
}
Clara Irizarry 9
Prof. Elhari CST2403-E340
Ex: **program86: To print letter grades with IF statements
#include<iostream>
usingnamespacestd;
int main(){
int a;
cout << "Enter a number:";
cin >> a;
if (a >= 90 && a <= 100)
cout << "A n";
else if (a >= 80 && a <= 89)
cout << "B n";
else if (a >= 70 && a <= 79)
cout << "C n";
else if (a >= 60 && a <= 69)
cout << "D n";
else
cout << "F n";
system("pause");
return 0;
}
Ex: **program87: To print letter grades with SWITCH CASES (hint: use / )
#include<iostream>
usingnamespacestd;
int main(){
int a;
int grade;
cout << "Enter a number:";
cin >> a;
grade = a / 10;
switch (grade){
case10: cout << "A n";
break;
case9: cout << "A n";
break;
case8: cout << "B n";
break;
case7: cout << "C n";
break;
case6: cout << "D n";
break;
default: cout << "F n";
break;
}
system("pause");
return 0;
}
Clara Irizarry 9
Prof. Elhari CST2403-E340
HW#7
- 1) Predefined Functions. 2) 9 examples. 3) Define parts of the function. 4) Mention something about
parameters.
**DUE 4/17/2015!!!
HW#8
- 1) Functions: Sending Parameters by value VS. by reference. 2) Arrays. 3) One-dimensional array.
4) 2 (or multi) dimensional array. **program92: solve the quadratic equation using functions and
parameters (5 functions).
**DUE 4/25/2015!!!
Clara Irizarry 9
Prof. Elhari CST2403-E340
WEEK 7: April 17, 2015
Functions
Group of commands already defined for programming, under one name.
Ex: sqrt()  Math library  return value
Predefined Functions
Exists in standard library; independent from classes / structure
Ex: sqrt, pow, abs, time, clock, cos, sin, tan, exp
**program87: 12 Predefined functions
#include<iostream>
usingnamespacestd;
int main(){
cout << "sqrt(9.0)= " << sqrt(9.0) << endl;
cout << "cos(9.0)= " << cos(9.0) << endl;
cout << "sin(9.0)= " << sin(9.0) << endl;
cout << "tan(9.0)= " << tan(9.0) << endl;
cout << "exp(9.0)= " << exp(9.0) << endl;
cout << "asin(9.0)=" << asin(9.0) << endl;
cout << "acos(9.0)= " << acos(9.0) << endl;
cout << "atan(9.0)= " << atan(9.0) << endl;
cout << "log(9.0)= " << log(9.0) << endl;
cout << "abs(9.0)= " << abs(9.0) << endl;
cout << "log2(9.0)= " << log2(9.0) << endl;
cout << "log10(9.0)= " << log10(9.0) << endl;
system("pause");
return 0;
}
Recursive Functions
A function is initialized and it calls itself (recall).
Ex: 4! = 4 * 3! = 24
3! = 3 * 2! = 6
2! = 2 * 1! = 2
1! = 1  BASE CASE
Clara Irizarry 9
Prof. Elhari CST2403-E340
User-Defined Function
1. fP  function prototype / declaration (NOT variable)
 return type function name (parameter);
 Ex: int Joe(int, int);
 Ex: void gg(int, float);
 Ex: double Two_sol(double, double, double);
2. fC  function call
 Ex: cout << Joe();
 Ex: Joe(5, 0);
3. fD  function definition
return type function name (){
STATEMENT(S)
return function name;
}
**program88: Void functions to print "Welcome to the world of functions!"
**program89: To print the sum and product of 2 numbers using functions w/out parameters.
**program90: to print the sum and product of 2 numbers using functions w/ parameters.
**program91: solve linear equation using 3 functions for solutions & parameters.
1. No_sol();  void
2. Two_sol();  int (2 parameters)
3. All_sol();  void
Clara Irizarry 9
Prof. Elhari CST2403-E340
WEEK 8: April 24, 2015
Sending Parameters by Value
• When sending parameters by value, the original values are unaffected when values are
changed within the main function.
**program93: Passing parameters by value.
#include<iostream>
usingnamespacestd;
void f(int x, inty){
x = 88;
y = 99;
}
int main(){
int a = 22;
int b = 33;
cout << "a = " << a << ", b = " << b << endl;
f(a, b);
cout << "a = " << a << ", b = " << b << endl;
system("pause");
return 0;
}
OUTPUT:
a = 22, b = 33
a = 22, b = 33
Sending Parameters by Reference
• When sending parameters by reference,the original values are affected when values are
changed within the main function.
• Once the main function calls the user function, new values can be given to the function, and
the original ones will be changed. More than one value can be returned.
• The symbol “ & “ is used alongside parameters to declare that they will be passed by
reference (Ex:int &y).
22
9988
33
a b
x y
Clara Irizarry 9
Prof. Elhari CST2403-E340
**program94: Passing parameters by reference.
#include<iostream>
usingnamespacestd;
void f(int&x, int&y){
x = 88;
y = 99;
}
int main(){
int a = 22;
int b = 33;
cout << "a = " << a << ", b = " << b << endl;
f(a, b);
cout << "a = " << a << ", b = " << b << endl;
system("pause");
return 0;
}
OUTPUT:
a = 22, b = 33
a = 88, b = 99
**programs95 to 96: Passing parameters by both value and reference.
#include<iostream>
usingnamespacestd;
void f(int x, int&y){
x = 88;
y = 99;
}
int main(){
int a = 22;
int b = 33;
cout << "a = " << a << ", b = " << b << endl;
f(a, b);
cout << "a = " << a << ", b = " << b << endl;
system("pause");
return 0;
}
OUTPUT:
a = 22, b = 33
a = 22, b = 99
a b
22
9988
33
a, x b, y
a b, y
22
88
99
x
Clara Irizarry 9
Prof. Elhari CST2403-E340
Passing by Value Passing by reference
Ex: int x; Ex: int &x;
Formal parameter x is a local variable Formal parameter x is a local reference
CANNOT change the actual parameter Synonym of ACTUAL parameter
Actual parameter may be a constant,
variable, or expression
Actual parameter CAN change, and
must be a variable
Actual parameter is READ-ONLY Actual parameter is READ-WRITE
**program97: Area and circumference of a circle using 1 function.
**program98: Area and circumference of a circle using 2 functions.
**program99: Area and circumference of a circle passing parameters by value and reference.
HW#9
- 1) Arrays. 2) One-dimensional array. 3) 2 (or multi) dimensional array.
**DUE 5/1/2015!!!
Clara Irizarry 9
Prof. Elhari CST2403-E340
WEEK 9: May 1, 2015
Arrays
A list of elements that have the same data type.
Examples:
o Float elements  [5.0, 6.0, 9.5, 10.7]
o Letters  [‘I’, ‘N’, ‘P’, ‘A’, ‘B’]
o Words  [“Jack”, “Joe”, “Jessica”, “Vick”]
*An array of arrays = 2-dimensional (multi) array
Array Structure
An array  A = 5, 2, -4, 7, 9
*Size = number of elements of the array
*Index subscript = all arrays start with index = 0.
*Notation: A[0] = 5
*Declaration: (data type) (array name) [ size ] = { elements }
*Last element is of index = [ size ] – 1.
A
A [0] A [1] A [2] A [3] A [4]
**program100: Create a table using arrays (basic).
**program101: Create a table using arrays,but add values for each element of the array.
**program102: Sum of the elements of the array.
**program103: Array of STRINGS.
**program104: Print a table (2-D array).
Print the following table:
5 2 -6
15 12 -16
115 112 -116
0 0 0
**program105: Print a table (2-D array).
Print the following table:
0 2 -6
0 12 -16
0 112 -116
0 0 0
Same as #99, but first column is all zeros.
5 2 -4 7 9
Clara Irizarry 9
Prof. Elhari CST2403-E340
HW#10
 Structures
Reasons
Definition
Live example
Examples of programs (2-3)
 Classes
Reasons
Definition
Live example
Examples of programs (2-3)
**program106: Array A: 5, 2, 10, -9, 6. Write a program to swap the 2nd and 3rd element, so that the new
array is: 5, 10, 2, -9, 6.
**DUE 5/8/2015!!!
Clara Irizarry 9
Prof. Elhari CST2403-E340
WEEK 10:May 8, 2015
Structure
A collection of various types of data under one element.
Ex: Book
o Title
o Author
o Genre
o ID #
**program107: Create and populate a structure to hold information about a student.
How to Use Structures in a Program
1. Define structure. Don’t forget semicolon at the end of structure declaration.
 SHORTCUT:Give each element in structure value in an array of specific structure type.
 Initializer list: gives values to elements in structure if already known.
2. Call structure with a new variable. Gives access to structure content.
3. Give elements in structure value. Values are assigned through new variable, followed by the
name of the element inside the structure. Could be more than one.
4. Output of program. Call each structure element through the declared variable in the main
function.
**program108: Create and populate a struct to hold information about a student using an initializer list.
**program109: Write a function which defines and returns a struct holding information about a student.
**program110: Write a function which returns a structure holding information about a student, using user
input to populate structure.
Classes
A collection of various types of data under one element; can also store functions.
-Has access modifiers:
 public  whole program can access class content.
 private  hidden from rest of program; can be called by a public function.
**program111: Find circumference and area of a circle using classes to assign area and circumference to
main function.
Clara Irizarry 9
Prof. Elhari CST2403-E340
WEEK 11:May 15, 2015
Pointer
A variable that references the location (in memory) of another variable, as well as the content
(value) of the variable.
Ex: int a = 5; &a = AB672; (address – HEX (base 16))
Reason?
 Pass variables around through program.
 Arrays (basically).
 Memory allocation.
Using Pointers
 To declare a pointer:
o Ex: int *ptr;
 To reference ADDRESS:
o Ex: ptr = &a;
 To DEREFERENCE using pointer:
o Ex: *ptr = a;
Pointer Proofs
Ptr = &a;
*ptr = a;
*ptr = *(&a) = a
a = *ptr;
a = * (&a)
a = a
ptr = &a;
ptr = &(*ptr)
ptr = ptr;
*(ptr) = *(&a)
*(&a) = * ptr;
a = * ptr;
a = a;
**program112: Linear equation program using classes to create the conditions of the equation and call in
main function.
**program113: To check how many times -7 is on a list using classes to count number of -7s and call this
class in main.
**program114: Use address of operator to find memory location of stored value.
Clara Irizarry 9
Prof. Elhari CST2403-E340
**program115: Use address of operator AND Dereference (*) operator to find stored value in particular
memory location.
**program116: Use Dereference (*) operator and address of operator (&) together.
FINAL EXAM
 Test #1 (1 to 2 questions)
 Definitions
o Class, loop (types, parts), computer systems (parts / diagram), while loop ( why it
is so powerful)
o Look at HWs!
 Programs (116 programs)
o All .cpp FILES in 1 folder on USB.
o BACKUP files in at least 3 places!
 Time  BEFORE 9PM.
 Proofs / Examples / Programs
o Structure
o Classes
o Pointers

More Related Content

What's hot

Stack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptxStack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptxsandeep54552
 
csharp repitition structures
csharp repitition structurescsharp repitition structures
csharp repitition structuresMicheal Ogundero
 
Infix to postfix expression in ds
Infix to postfix expression in dsInfix to postfix expression in ds
Infix to postfix expression in dsRohini Mahajan
 
escape sequences and substitution markers
escape sequences and substitution markersescape sequences and substitution markers
escape sequences and substitution markersMicheal Ogundero
 
Evaluation of postfix expression
Evaluation of postfix expressionEvaluation of postfix expression
Evaluation of postfix expressionAkhil Ahuja
 
02 iec t1_s1_oo_ps_session_02
02 iec t1_s1_oo_ps_session_0202 iec t1_s1_oo_ps_session_02
02 iec t1_s1_oo_ps_session_02Pooja Gupta
 
Seminar 2 coding_principles
Seminar 2 coding_principlesSeminar 2 coding_principles
Seminar 2 coding_principlesmoduledesign
 
Chapter 5 Balagurusamy Programming ANSI in c
Chapter 5 Balagurusamy Programming ANSI  in cChapter 5 Balagurusamy Programming ANSI  in c
Chapter 5 Balagurusamy Programming ANSI in cBUBT
 
Seminar 2 coding_principles
Seminar 2 coding_principlesSeminar 2 coding_principles
Seminar 2 coding_principlesmoduledesign
 
My lecture infix-to-postfix
My lecture infix-to-postfixMy lecture infix-to-postfix
My lecture infix-to-postfixSenthil Kumar
 

What's hot (20)

Operators
OperatorsOperators
Operators
 
Stacks
StacksStacks
Stacks
 
Stack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptxStack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptx
 
selection structures
selection structuresselection structures
selection structures
 
csharp repitition structures
csharp repitition structurescsharp repitition structures
csharp repitition structures
 
Ansi c
Ansi cAnsi c
Ansi c
 
Infix to postfix expression in ds
Infix to postfix expression in dsInfix to postfix expression in ds
Infix to postfix expression in ds
 
C lab-programs
C lab-programsC lab-programs
C lab-programs
 
escape sequences and substitution markers
escape sequences and substitution markersescape sequences and substitution markers
escape sequences and substitution markers
 
Evaluation of postfix expression
Evaluation of postfix expressionEvaluation of postfix expression
Evaluation of postfix expression
 
Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions
 
c programing
c programingc programing
c programing
 
02 iec t1_s1_oo_ps_session_02
02 iec t1_s1_oo_ps_session_0202 iec t1_s1_oo_ps_session_02
02 iec t1_s1_oo_ps_session_02
 
Infix to postfix
Infix to postfixInfix to postfix
Infix to postfix
 
C++ control loops
C++ control loopsC++ control loops
C++ control loops
 
Seminar 2 coding_principles
Seminar 2 coding_principlesSeminar 2 coding_principles
Seminar 2 coding_principles
 
Chapter 5 Balagurusamy Programming ANSI in c
Chapter 5 Balagurusamy Programming ANSI  in cChapter 5 Balagurusamy Programming ANSI  in c
Chapter 5 Balagurusamy Programming ANSI in c
 
Seminar 2 coding_principles
Seminar 2 coding_principlesSeminar 2 coding_principles
Seminar 2 coding_principles
 
My lecture infix-to-postfix
My lecture infix-to-postfixMy lecture infix-to-postfix
My lecture infix-to-postfix
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
 

Similar to CST2403 NOTES

Answers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
Answers To Selected Exercises For Fortran 90 95 For Scientists And EngineersAnswers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
Answers To Selected Exercises For Fortran 90 95 For Scientists And EngineersSheila Sinclair
 
4. programing 101
4. programing 1014. programing 101
4. programing 101IEEE MIU SB
 
Programming in C by SONU KUMAR.pptx
Programming in C by SONU KUMAR.pptxProgramming in C by SONU KUMAR.pptx
Programming in C by SONU KUMAR.pptxSONU KUMAR
 
Computer paper 3 may june 2004 9691 cambridge General Certificate of educatio...
Computer paper 3 may june 2004 9691 cambridge General Certificate of educatio...Computer paper 3 may june 2004 9691 cambridge General Certificate of educatio...
Computer paper 3 may june 2004 9691 cambridge General Certificate of educatio...Alpro
 
Logical Expressions in C/C++. Mistakes Made by Professionals
Logical Expressions in C/C++. Mistakes Made by ProfessionalsLogical Expressions in C/C++. Mistakes Made by Professionals
Logical Expressions in C/C++. Mistakes Made by ProfessionalsPVS-Studio
 
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa Thapa
 
C chap02
C chap02C chap02
C chap02Kamran
 
Dti2143 chapter 3 arithmatic relation-logicalexpression
Dti2143 chapter 3 arithmatic relation-logicalexpressionDti2143 chapter 3 arithmatic relation-logicalexpression
Dti2143 chapter 3 arithmatic relation-logicalexpressionalish sha
 
IRJET- Switch Case Statements in C
IRJET-  	  Switch Case Statements in CIRJET-  	  Switch Case Statements in C
IRJET- Switch Case Statements in CIRJET Journal
 
Learning C programming - from lynxbee.com
Learning C programming - from lynxbee.comLearning C programming - from lynxbee.com
Learning C programming - from lynxbee.comGreen Ecosystem
 

Similar to CST2403 NOTES (20)

C++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdfC++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdf
 
Python for Beginners(v2)
Python for Beginners(v2)Python for Beginners(v2)
Python for Beginners(v2)
 
What is c
What is cWhat is c
What is c
 
Answers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
Answers To Selected Exercises For Fortran 90 95 For Scientists And EngineersAnswers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
Answers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
 
4. programing 101
4. programing 1014. programing 101
4. programing 101
 
Programming in C by SONU KUMAR.pptx
Programming in C by SONU KUMAR.pptxProgramming in C by SONU KUMAR.pptx
Programming in C by SONU KUMAR.pptx
 
C # (2)
C # (2)C # (2)
C # (2)
 
Computer paper 3 may june 2004 9691 cambridge General Certificate of educatio...
Computer paper 3 may june 2004 9691 cambridge General Certificate of educatio...Computer paper 3 may june 2004 9691 cambridge General Certificate of educatio...
Computer paper 3 may june 2004 9691 cambridge General Certificate of educatio...
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Logical Expressions in C/C++. Mistakes Made by Professionals
Logical Expressions in C/C++. Mistakes Made by ProfessionalsLogical Expressions in C/C++. Mistakes Made by Professionals
Logical Expressions in C/C++. Mistakes Made by Professionals
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptx
 
C chap02
C chap02C chap02
C chap02
 
C chap02
C chap02C chap02
C chap02
 
Dti2143 chapter 3 arithmatic relation-logicalexpression
Dti2143 chapter 3 arithmatic relation-logicalexpressionDti2143 chapter 3 arithmatic relation-logicalexpression
Dti2143 chapter 3 arithmatic relation-logicalexpression
 
IRJET- Switch Case Statements in C
IRJET-  	  Switch Case Statements in CIRJET-  	  Switch Case Statements in C
IRJET- Switch Case Statements in C
 
Spl book salution
Spl book salutionSpl book salution
Spl book salution
 
Learning C programming - from lynxbee.com
Learning C programming - from lynxbee.comLearning C programming - from lynxbee.com
Learning C programming - from lynxbee.com
 
C-LOOP-Session-2.pptx
C-LOOP-Session-2.pptxC-LOOP-Session-2.pptx
C-LOOP-Session-2.pptx
 

CST2403 NOTES

  • 1. C++ Programming (CST2403) NOTES (SPRING 2015) Prof. Elhari By: Clara Irizarry
  • 2. Clara Irizarry 9 Prof. Elhari CST2403-E340 WEEK 1: February 6, 2015 KEY TERMS Program: set of instructions to solve a problem using programming (C++). Algorithm: set of instructions to solve a problem using human language (English). Flowchart: set of instructions to solve a problem using symbols. Pseudo code: set of instructions to solve a problem using both programming language AND human language. FLOWCHART SYMBOLS start / stop input / output process flow (direction) connector Decision (if) *for comparing
  • 3. Clara Irizarry 9 Prof. Elhari CST2403-E340 Example: Write a flowchart to print the sumof 2 numbers. Example: Write a flowchart to print yourname and address. start start a, b Sum= a+b Name Z Sum stop Address Name Address Stop Output s Inputs
  • 4. Clara Irizarry 9 Prof. Elhari CST2403-E340 DIAGRAM OF COMPUTER SYSTEMS 1. Input is processed by CPU. 2. CPU sends out result to output. 3. CPU also stores data in memory. 4. Data can also directly go to the output. 5. Data can be reprocessed (LOOP). CPU Output Memory Input 1 2 3 5 4
  • 5. Clara Irizarry 9 Prof. Elhari CST2403-E340 MENTAL PROCESS OF SOLVING PROBLEMS USING COMPUTERS 1. Well-defined problem  Understand the problem before trying to solve it. 2. Algorithm  Write the solution to your problem. 3. Pseudo code  Details of the solution. 4. Program  Translate to C++. 5. Input  Type into computer. 6. Compile  Finalize program via compiler.  Find errors. 1. Syntax – misspelling 2. Runtime – runs into something that cannot be done 3. Logical – mix up logic (ex: operators) 7. Run  Run the finished program. 8. Solution  Check if the solution is of the original problem, and look for errors in program via the output. C++ PROGRAM STRUCTURE  C++ is case sensitive.  First line of code: #include <iostream> (C++ standard library)  White spaces are ignored.  Next line: using namespace std;  Next line: int main(){  Every function must return a value of some type (ex: int will return int).  LAST line: return 0;  Semicolon is a line terminator (ends the line).  Comments are ignored by the compiler.  // ONE line comment AND /* ………..*/ BLOCK comment o SAMPLE PROGRAM #include <iostream> using namespace std; int main(){ return 0; } o COMMANDS  cout <<  output  cin >>  input ( must PROMPT it) -Codes -Statements
  • 6. Clara Irizarry 9 Prof. Elhari CST2403-E340 **program1: Design a program that will print out a message. //program1: To print "Welcome to C++". #include<iostream> using namespace std; int main(){ cout <<"Welcome to C++!"<< endl; system("pause"); return 0; } OVERVIEW OF C++ PROGRAMMING *Email: CST2403ELHARI@gmail.com - Syntax, rules, definitions Subject line  S15#__ HW# __ Name - Selections (1, 2, 3, 4, 5) - Switch case - Functions - Arrays - Structures / Classes - Pointers HW#1 a) Pg.1 = COVER SHEET (setup explained) b) Pg.2= QUESTIONS -Define: program, algorithm, flowchart, pseudo code -History of C++ -CPU Clock Speed (1980 – present) -How to setup / use Microsoft Visual C++? c) Pg.3= ANSWERS **DUE 2/6/2015!!! HW#2 1. Flowchart to print the product and difference of 2 numbers. 2. Flowchart to compare 2 distinct numbers. 3. Flowchart to compare ANY 2 numbers. 4. Flowchart to print the greater of two distinct numbers. 5. Flowchart to print the greater of ANY 2 numbers. EXTRA CREDIT:Flowchart to print the greatest of ANY 3 numbers. **DUE 2/13/2015!!!
  • 7. Clara Irizarry 9 Prof. Elhari CST2403-E340 WEEK 2: February 13, 2015 ORDER OF OPERATIONS (LEVELS OF PRIORITY) 1. Unary minus (-)  ex: -+5 + (5*6) ……. (-5) + 30 = 25 2. Parenthesis ( )  order: INNER MOST 3. Exponent ^  order: OUT to IN 4. Module (%), Division (/), Multiplication (*)  order: LEFT to RIGHT 5. Addition (+), Subtraction(-)  order: LEFT to RIGHT Ex #1: 5*5%5/5*5%5*(5-5) 5*5%5/5*5%5*0 25%5/5*5%5*0 0/5*5%5*0 0*5%5*0 0%5*0 0*0 = 0 Ex#2: (5+6)*5%10 -10%2*2-(5 %(5*5)) (5+6)*5%10 -10%2*2-(5 %25) (5+6)*5%10 -10%2*2-5 11*5%10 -10%2*2-5 55%10 -10%2*2-5 5 – 10%2*2-5 5 – 0*2-5 5 – 0 – 5 5 – 5 = 0
  • 8. Clara Irizarry 9 Prof. Elhari CST2403-E340 IDENTIFIERS (RULES) 1. Combinations of a to z, A to Z, 0 to 9 and __. a. Variables- to define values in program b. Program name c. Statements 2. No blank spaces. 3. No keywords. a. Keywords have special meaning in C++. Some words are reserved words,which are used to perform different functions in the program. 4. Start with a letter. 5. Size of identifier (very large, so no worries). **programs 2 to 6: Design a program that will print out the sum of 5 and 9 (in different manners, using various cout configurations as well as variables / identifiers. **programs 7 and 8: Design a program that will print out the sum of any 2 numbers. ESCAPE SEQUENCE  goes before the character you wish to print. Ex: Print “. cout << “ ” “ ; Ex: Print . cout<< “”; *Commands n: new line t: tab r: return a: bell (BEEP) *Data Types char, int, float, double, boolean, string HW#3 & 4: **program9: Print the sum, difference and product of 2 numbers w/out using variables. **program10: Print the sum, difference and product of 2 numbers using variables. **program11: Print the sum, difference & product of 2 numbers w/out variables (lots of COUT). **program12: Print the sum, difference & product of 2 numbers w/variables and CIN (lots of COUT). **program13: Print (5+9) / 10%2 in ONE line. No variables! **program14: Print (5+9) / 10%2 in ONE line. Use variables! **program15: Print ""/Joe"" Jessy""//"*/ **DUE 2/27/2015!!!
  • 9. Clara Irizarry 9 Prof. Elhari CST2403-E340 WEEK 3: February 27, 2015 RELATIONALS > , < , >= , <= EQUALITY & INEQUALITY == != LOGICAL OPERATORS NOT  ! NOT (F) = T AND  && T when ALL are T OR  | | T when AT LEAST ONE is T DEMORGAN’S LAW 1. NOT (A and B) = (NOT A) or (NOT B)  ! (A && B) = (!A) | | (!B) 2. NOT(A or B) = NOT(A) and NOT(B)  ! (A | | B) = (!A) && (!B) IF STATEMENT OR IF ELSE STATEMENT  else = if NOT (if! ( ) ) if (condition) statement if (condition){ statements } if (condition) statement else statement
  • 10. Clara Irizarry 9 Prof. Elhari CST2403-E340 Ex: Write a flowchart to solve a linear equation: ax + b = 0. Start a, b, x No Yes a = 0 No Yes “One solution” b = 0 x = -b / a “No solutions” “All solutions” x stop HW#5 -Use truth table to prove DeMorgan’s LAWS to be TRUE. **program16: To compare 2 numbers using an IF statement. **program17: To compare 2 numbers using an IF ELSE statement. **program18: Write a program to solve a linear equation. **program19: To solve the quadratic equation: x = (-b +- sqrt(b^2 - 4ac)) / 2a **DUE 3/5/2015!!!
  • 11. Clara Irizarry 9 Prof. Elhari CST2403-E340 WEEK 4: March 5, 2015 QUADRATIC EQUATION FLOWCHART (from HW) Start a, b, c, d, x, x1, x2 a = 0 d = b2 – 4ac b = 0 d < 0 “Linear” c = 0 d = 0 “No solutions” x = -c / b “No solutions” “All solutions” “Two “Duplicate solutions” solutions” x 𝑥1 = −𝑏+√d 2𝑎 x = -b / 2a 𝑥2 = −𝑏−√d 2𝑎 x x1, x2 stop **program20A: Write a program to solve quadratic equation (redo).
  • 12. Clara Irizarry 9 Prof. Elhari CST2403-E340 MATH OPERATORS   sqrt *#include <cmath> *  multiplication +  addition * (++) OR (--)  increment / decrement -  subtraction /  division %  module (remainder of division) HW#6 - a) Define a loop. b) Explain what the loop parts are, as well as how they work and why they are needed. HINT: There are 3 types of loops. c) What are the types of loops used in C++ programming? Which one is the best and for what cases? **Program#20: To print your name 5 times without using any loop. **Program#21: To print your name 5 times using the WHILE loop. **Program#22: To print the numbers 1 to 19 using the WHILE loop **Program#23: To print odd numbers between 2 and 18 using loops **Program#24: To print even numbers between 1 and 19 using loops. **DUE 3/13/2015!!!
  • 13. Clara Irizarry 9 Prof. Elhari CST2403-E340 WEEK 5: March 13, 2015 Loop: Set of instructions that are repeated when a condition is proved to be true, until the said condition is false. PARTS OF A LOOP 1. Initial o Starting point of loop. o Without it, you cannot start the loop. 2. Condition o The loop itself. o Without it, there is NO loop. 3. Terminator o Stops the loop. o Without it, you have an INFINITE loop. TYPES OF LOOPS 1. While LOOP The ‘WHILE’ loop repeats statements only while a condition proves to be true. The WHILE loop should be used when you’re not sure how many times the loop has to run. while(condition){ Statement(s) } 2. For LOOP The ‘FOR’ loop performs a repeatedly executes a series of statements while managing the variable being used in the loop. This is only for a certain number of times. for(initial value (i=0); condition; increment/decrement){ Statement(s) } 3. Do…While LOOP The ‘DO…WHILE’ loop repeats statements until the condition becomes false in that loop. The DO…WHILE loop should be used in the case that you would like to test out the truth value of the set condition. do { Statement(s) } while (condition);
  • 14. Clara Irizarry 9 Prof. Elhari CST2403-E340 **program25: To print odd numbers from 19 down to -5. **program26: To print the sum of numbers between 19 and 31. **program27: To print the sum of odd numbers between 20 and 40. **program28: To print the sum of even numbers between 19 and 21. **program29: To print the sum of multiples of 5 between 1 and 21. **program30: To print every other number between 1 and 10 starting with 1. **program31: To print every other number between 1 and 10 starting with 2. **program32: To print the first and last numbers from 13 to 17. **program33: To print a list of 5 numbers: 3, 2, -7, 19, -18. **program34: To print the sum of a list of 5 numbers: 3, 2, -7, 19, -18. **program35: To print the positive numbers of a list of 5 numbers: 3, 2, -7, 19, -18. **program36: To print the negative numbers of a list of 5 numbers: 3, 2, -7, 19, -18 (-19 to -2). **program37: To print the odd numbers of a list of 5 numbers: 3, 2, -7, 19, -18 between 5 and 14. **program38: To print the first and last numbers from the list. **program39: To print every other number on the list. **program40: To check IF -7 exists in the list. **program41: To count how many -7’s exist in the list. **programs42 to 62: Same programs for #21 to #41, using FOR LOOP. **programs63 to 83: Same program for #21 to #41, using DO WHILE LOOP. TEST #1  Multiple choice  True and false  Definitions  Write flowcharts  Write programs  Find output of programs  Switch between loops
  • 15. Clara Irizarry 9 Prof. Elhari CST2403-E340 WEEK 6: March 27, 2015 Switch Cases Ex: **program84: To print out a number in words using if else #include<iostream> usingnamespacestd; int main(){ int a; cout << "Enter a number:"; cin >> a; if (a == 5) cout << "Five n"; else if (a == 9) cout << "Nine n"; else if (a == 0) cout << "Zero n"; else cout << "Negative n"; system("pause"); return 0; } Ex: **program85: To print out a number in words WITH SWITCH CASES #include<iostream> usingnamespacestd; int main(){ int a; cout << "Enter a number:"; cin >> a; switch (a){ case5: cout << "Five n"; break; case9: cout << "Nine n"; break; case0: cout << "Zero n"; break; default: cout << "Negative n"; break; } system("pause"); return 0; }
  • 16. Clara Irizarry 9 Prof. Elhari CST2403-E340 Ex: **program86: To print letter grades with IF statements #include<iostream> usingnamespacestd; int main(){ int a; cout << "Enter a number:"; cin >> a; if (a >= 90 && a <= 100) cout << "A n"; else if (a >= 80 && a <= 89) cout << "B n"; else if (a >= 70 && a <= 79) cout << "C n"; else if (a >= 60 && a <= 69) cout << "D n"; else cout << "F n"; system("pause"); return 0; } Ex: **program87: To print letter grades with SWITCH CASES (hint: use / ) #include<iostream> usingnamespacestd; int main(){ int a; int grade; cout << "Enter a number:"; cin >> a; grade = a / 10; switch (grade){ case10: cout << "A n"; break; case9: cout << "A n"; break; case8: cout << "B n"; break; case7: cout << "C n"; break; case6: cout << "D n"; break; default: cout << "F n"; break; } system("pause"); return 0; }
  • 17. Clara Irizarry 9 Prof. Elhari CST2403-E340 HW#7 - 1) Predefined Functions. 2) 9 examples. 3) Define parts of the function. 4) Mention something about parameters. **DUE 4/17/2015!!! HW#8 - 1) Functions: Sending Parameters by value VS. by reference. 2) Arrays. 3) One-dimensional array. 4) 2 (or multi) dimensional array. **program92: solve the quadratic equation using functions and parameters (5 functions). **DUE 4/25/2015!!!
  • 18. Clara Irizarry 9 Prof. Elhari CST2403-E340 WEEK 7: April 17, 2015 Functions Group of commands already defined for programming, under one name. Ex: sqrt()  Math library  return value Predefined Functions Exists in standard library; independent from classes / structure Ex: sqrt, pow, abs, time, clock, cos, sin, tan, exp **program87: 12 Predefined functions #include<iostream> usingnamespacestd; int main(){ cout << "sqrt(9.0)= " << sqrt(9.0) << endl; cout << "cos(9.0)= " << cos(9.0) << endl; cout << "sin(9.0)= " << sin(9.0) << endl; cout << "tan(9.0)= " << tan(9.0) << endl; cout << "exp(9.0)= " << exp(9.0) << endl; cout << "asin(9.0)=" << asin(9.0) << endl; cout << "acos(9.0)= " << acos(9.0) << endl; cout << "atan(9.0)= " << atan(9.0) << endl; cout << "log(9.0)= " << log(9.0) << endl; cout << "abs(9.0)= " << abs(9.0) << endl; cout << "log2(9.0)= " << log2(9.0) << endl; cout << "log10(9.0)= " << log10(9.0) << endl; system("pause"); return 0; } Recursive Functions A function is initialized and it calls itself (recall). Ex: 4! = 4 * 3! = 24 3! = 3 * 2! = 6 2! = 2 * 1! = 2 1! = 1  BASE CASE
  • 19. Clara Irizarry 9 Prof. Elhari CST2403-E340 User-Defined Function 1. fP  function prototype / declaration (NOT variable)  return type function name (parameter);  Ex: int Joe(int, int);  Ex: void gg(int, float);  Ex: double Two_sol(double, double, double); 2. fC  function call  Ex: cout << Joe();  Ex: Joe(5, 0); 3. fD  function definition return type function name (){ STATEMENT(S) return function name; } **program88: Void functions to print "Welcome to the world of functions!" **program89: To print the sum and product of 2 numbers using functions w/out parameters. **program90: to print the sum and product of 2 numbers using functions w/ parameters. **program91: solve linear equation using 3 functions for solutions & parameters. 1. No_sol();  void 2. Two_sol();  int (2 parameters) 3. All_sol();  void
  • 20. Clara Irizarry 9 Prof. Elhari CST2403-E340 WEEK 8: April 24, 2015 Sending Parameters by Value • When sending parameters by value, the original values are unaffected when values are changed within the main function. **program93: Passing parameters by value. #include<iostream> usingnamespacestd; void f(int x, inty){ x = 88; y = 99; } int main(){ int a = 22; int b = 33; cout << "a = " << a << ", b = " << b << endl; f(a, b); cout << "a = " << a << ", b = " << b << endl; system("pause"); return 0; } OUTPUT: a = 22, b = 33 a = 22, b = 33 Sending Parameters by Reference • When sending parameters by reference,the original values are affected when values are changed within the main function. • Once the main function calls the user function, new values can be given to the function, and the original ones will be changed. More than one value can be returned. • The symbol “ & “ is used alongside parameters to declare that they will be passed by reference (Ex:int &y). 22 9988 33 a b x y
  • 21. Clara Irizarry 9 Prof. Elhari CST2403-E340 **program94: Passing parameters by reference. #include<iostream> usingnamespacestd; void f(int&x, int&y){ x = 88; y = 99; } int main(){ int a = 22; int b = 33; cout << "a = " << a << ", b = " << b << endl; f(a, b); cout << "a = " << a << ", b = " << b << endl; system("pause"); return 0; } OUTPUT: a = 22, b = 33 a = 88, b = 99 **programs95 to 96: Passing parameters by both value and reference. #include<iostream> usingnamespacestd; void f(int x, int&y){ x = 88; y = 99; } int main(){ int a = 22; int b = 33; cout << "a = " << a << ", b = " << b << endl; f(a, b); cout << "a = " << a << ", b = " << b << endl; system("pause"); return 0; } OUTPUT: a = 22, b = 33 a = 22, b = 99 a b 22 9988 33 a, x b, y a b, y 22 88 99 x
  • 22. Clara Irizarry 9 Prof. Elhari CST2403-E340 Passing by Value Passing by reference Ex: int x; Ex: int &x; Formal parameter x is a local variable Formal parameter x is a local reference CANNOT change the actual parameter Synonym of ACTUAL parameter Actual parameter may be a constant, variable, or expression Actual parameter CAN change, and must be a variable Actual parameter is READ-ONLY Actual parameter is READ-WRITE **program97: Area and circumference of a circle using 1 function. **program98: Area and circumference of a circle using 2 functions. **program99: Area and circumference of a circle passing parameters by value and reference. HW#9 - 1) Arrays. 2) One-dimensional array. 3) 2 (or multi) dimensional array. **DUE 5/1/2015!!!
  • 23. Clara Irizarry 9 Prof. Elhari CST2403-E340 WEEK 9: May 1, 2015 Arrays A list of elements that have the same data type. Examples: o Float elements  [5.0, 6.0, 9.5, 10.7] o Letters  [‘I’, ‘N’, ‘P’, ‘A’, ‘B’] o Words  [“Jack”, “Joe”, “Jessica”, “Vick”] *An array of arrays = 2-dimensional (multi) array Array Structure An array  A = 5, 2, -4, 7, 9 *Size = number of elements of the array *Index subscript = all arrays start with index = 0. *Notation: A[0] = 5 *Declaration: (data type) (array name) [ size ] = { elements } *Last element is of index = [ size ] – 1. A A [0] A [1] A [2] A [3] A [4] **program100: Create a table using arrays (basic). **program101: Create a table using arrays,but add values for each element of the array. **program102: Sum of the elements of the array. **program103: Array of STRINGS. **program104: Print a table (2-D array). Print the following table: 5 2 -6 15 12 -16 115 112 -116 0 0 0 **program105: Print a table (2-D array). Print the following table: 0 2 -6 0 12 -16 0 112 -116 0 0 0 Same as #99, but first column is all zeros. 5 2 -4 7 9
  • 24. Clara Irizarry 9 Prof. Elhari CST2403-E340 HW#10  Structures Reasons Definition Live example Examples of programs (2-3)  Classes Reasons Definition Live example Examples of programs (2-3) **program106: Array A: 5, 2, 10, -9, 6. Write a program to swap the 2nd and 3rd element, so that the new array is: 5, 10, 2, -9, 6. **DUE 5/8/2015!!!
  • 25. Clara Irizarry 9 Prof. Elhari CST2403-E340 WEEK 10:May 8, 2015 Structure A collection of various types of data under one element. Ex: Book o Title o Author o Genre o ID # **program107: Create and populate a structure to hold information about a student. How to Use Structures in a Program 1. Define structure. Don’t forget semicolon at the end of structure declaration.  SHORTCUT:Give each element in structure value in an array of specific structure type.  Initializer list: gives values to elements in structure if already known. 2. Call structure with a new variable. Gives access to structure content. 3. Give elements in structure value. Values are assigned through new variable, followed by the name of the element inside the structure. Could be more than one. 4. Output of program. Call each structure element through the declared variable in the main function. **program108: Create and populate a struct to hold information about a student using an initializer list. **program109: Write a function which defines and returns a struct holding information about a student. **program110: Write a function which returns a structure holding information about a student, using user input to populate structure. Classes A collection of various types of data under one element; can also store functions. -Has access modifiers:  public  whole program can access class content.  private  hidden from rest of program; can be called by a public function. **program111: Find circumference and area of a circle using classes to assign area and circumference to main function.
  • 26. Clara Irizarry 9 Prof. Elhari CST2403-E340 WEEK 11:May 15, 2015 Pointer A variable that references the location (in memory) of another variable, as well as the content (value) of the variable. Ex: int a = 5; &a = AB672; (address – HEX (base 16)) Reason?  Pass variables around through program.  Arrays (basically).  Memory allocation. Using Pointers  To declare a pointer: o Ex: int *ptr;  To reference ADDRESS: o Ex: ptr = &a;  To DEREFERENCE using pointer: o Ex: *ptr = a; Pointer Proofs Ptr = &a; *ptr = a; *ptr = *(&a) = a a = *ptr; a = * (&a) a = a ptr = &a; ptr = &(*ptr) ptr = ptr; *(ptr) = *(&a) *(&a) = * ptr; a = * ptr; a = a; **program112: Linear equation program using classes to create the conditions of the equation and call in main function. **program113: To check how many times -7 is on a list using classes to count number of -7s and call this class in main. **program114: Use address of operator to find memory location of stored value.
  • 27. Clara Irizarry 9 Prof. Elhari CST2403-E340 **program115: Use address of operator AND Dereference (*) operator to find stored value in particular memory location. **program116: Use Dereference (*) operator and address of operator (&) together. FINAL EXAM  Test #1 (1 to 2 questions)  Definitions o Class, loop (types, parts), computer systems (parts / diagram), while loop ( why it is so powerful) o Look at HWs!  Programs (116 programs) o All .cpp FILES in 1 folder on USB. o BACKUP files in at least 3 places!  Time  BEFORE 9PM.  Proofs / Examples / Programs o Structure o Classes o Pointers