Download Class Notes - csit.parkland.edu

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Law of large numbers wikipedia , lookup

Large numbers wikipedia , lookup

Addition wikipedia , lookup

Mathematics of radio engineering wikipedia , lookup

Elementary mathematics wikipedia , lookup

Location arithmetic wikipedia , lookup

Transcript
Alex Jerez
CSC123
Summer 2007
Lesson 4 Notes & Lab
Questions about last Lab
Some description about grades
Review of while & do-while loops
Remember the required parts for these loops:
while (<Boolean expression>)
{
block of code
// important: some condition needs to
break the loop
}


Pros:
o
o
Cons:
o
do
{
<statements>
// important: some condition needs to
break the loop
} while (<expression>);
They are great to do any kind of iteration
Great control to do infinite loops for menus
Certain loop structures are needed all the time, so there has to be a better way of
writing them. (remember, just like the switch structure is a fancy sequence of
if’s, there has to be a fancy way to write a while loop)
for loop
Another way of write while loops that have a defined number of iterations (or that need to run over
certain range of numbers).
Structure:
 for (<initialization> ; <boolean expression> ; <increment>)
{
<statements>
}
Ex:
A program used to calculate the sum of the first 10 positive numbers.
int counter = 0;
for (int counter=0; counter < 11; i++)
int result = 0;
{
while ( counter < 11)
result += counter;
{
counter++;
result += counter;
}
count << result;
counter++;
}

for loops can have different types of increments:
o single increment ++
o variable increment
 +=# (ex: +=5 // increments by 5)
 *=# (ex: *=2 // increment by doubling the value)
o negative –
Nested Loops:


Remember that we created an if-statement inside another if-statement,
looping statements can also work the same way.
Each of the loops will control one stage of the iterations.
Ex:
Display the numbers between 1 and 50. Each line should show
1 …10
11….20
21….30, etc
First of all how to do this “in words”
 count from 1 to 10, change line
 cout from 11 to 20, change line

….. keep doing it until you get to 50
 If 50 stop the loop
How to do this with a while loop”
// initialization of variables
int counter = 1;
int lineCounter = 1;
while (counter<=50)
{
if (lineCounter == 10) // we have to check when the lineCounter is 10 so that
{
// we can add an endl to switch line
cout << endl;
lineCounter = 1; // reset the lineCounter of the line
}
else
{
cout << “ “ << counter << “ “; // since we are not at the end, then just increment
lineCounter++;
// the lineCounter and the normal value counter
counter ++;
}
}
Now, rewrite this with a for-loop:
for (int counter=1; counter <=50; counter++) // count 50 numbers
{
for (int lineCounter=1; lineCounter<=10;lineCounter++) // for every 10 numbers display
{
// them in a line
cout << “ “ << counter << “ “;
}
cout << endl;
}
Math Functions
What is a function and how do we use it? – Remember rand()?


C++ provides with a set of define math functions to calculate things like : sin,
cos, tan, arcTan, square root, powers, logs
#include <cmath>
What is a function name? parameter? Returned value?
If you are calling a math function, make sure there is a variable waiting to get
the result. It is not required by the compiler but it is a logical error.
Overloaded functions & some problems



This is called explicit casting
What is implicit casting then?
static_cast<type>(value): ex sqrt(static_cast<double>(input))



Static cast:
Lab 4
Due by Midnight June 25th, 07
Create a program that shows a menu from where the user can select any of the following options:
Option 1:
Using a for-loop, display numbers in a given increment between 0 and 100. Ask the user for what
the increment should be and then show the numbers. Just display one after another.
User input 2  0, 2, 4, 6,….. , 98, 100
User input 10  0, 10, 20, 40 … 80,90,100
Option 2:
Ask the user to enter a positive number and display:
a) square root b) sin c) cos d) log and e) Raise it to a power 4
Option 3:
X^n means that you are going to multiply X n times. Ex: 2^3 = 2*2*2 = 8, 3^4 = 3*3*3*3= 81
Ask the user for two values (X and n) and raise X to the nth power.
Option 4:
You are going to write a square matrix of 0’s. A square matrix has the same number of rows and
colums. So, ask the user for how many rows/colums the square matrix should be. Once the user
enters the inputs display as the following example:
User: 5 
00000
00000
00000
00000
00000
User: 3 
000
000
000
Option 5: exit the program. (The same as any other input != 1,2,3,4)