for loops › for (variable set; condition; incremental or decrement){ // loop beginning › } //...

23
Computer Science 1 Mr. Hudson’s First Problem Set Objective : Talk about key concepts from the homework discuss workload and how better to use our time. Manage the concepts and gain understanding of self-learning. Post Objective : Identify techniques and strategies for better problem solving cases in the future.

Upload: lambert-carroll

Post on 29-Dec-2015

233 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning

Computer Science 1Mr. Hudson’s First Problem Set

Objective : Talk about key concepts from the homework discuss

workload and how better to use our time. Manage the concepts and gain

understanding of self-learning.Post Objective : Identify techniques

and strategies for better problem solving cases in the future.

Page 2: For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning

Loops -

For Loops › for (variable set; condition; incremental or

decrement){ // loop beginning › } // loop end

While loops › while (condition) { // beginning › }// end of loop

Do While› do{ // beginning › }while(condition);

Page 3: For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning

Selections

One Way (if statements)

Two Way if else

Multiple Way if else if else or switch case

Page 4: For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning

$$$$$$$$$$$$$$$$$$$$$$$$

IOMANIPWith the deriective

#include <iomanip>

We gain several functions that allow for output manipulation

fixed, setprecision(#), setw(#), showpoint, hidepoint,

Page 5: For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning

setw()

left right internal

Page 6: For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning

showpos / noshowpos

Page 7: For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning

fixed / unsetf

Page 8: For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning

showpoint / hidepoint

Page 9: For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning

setprecision()

Page 10: For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning

Data Types

Boolean › bool x = true; also x =1;› bool y = false; also y = 0;

Characters› char a = ‘Y’;

This means that the variable a holds Capital Y, you compare against a Capital Y with single quotes around the letter to denote Character, it can be, number, letter, or symbol.

Page 11: For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning

Data Types

String› string s = “Yes”; also s = “Go go oG”;› String is a group of characters, you denote

a string with double quotes. Integer

› Int x = 5; Integers are whole numbers, no decimal

numbers, but both positive and negative.

Page 12: For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning

Data Types

Double› double currency = 5.25; or 5.00 or 4

Double is able to hold decimal places Float

› float number = 2523.23424; Float can hold more information than double,

but also takes more memory to declare

Page 13: For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning

Arrays…

Arrays are of any data type and are cells that are called by index.

› Integer array that holds 5 values is declared

› int arrayName [5] = {0,1,2,3,4};

› You call the value 3 by calling its index› arrayName [index] = 3;

What does Index =

Page 14: For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning

Arrays

Think of arrays as a list of numbers, where you keep count.› Best way to deal with arrays is generally in

for loops, where you can repeat actions a set number of times.

Page 15: For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning

Arrays

Think of them as Rows or Columns…

› What if you wanted to keep both Rows and Columns in a list of a single array?

Page 16: For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning

Arrays

An array is a sequence of data› 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89

is a sequence of ints› "Matthew", "Mark", "Luke", "John"

is a sequence of strings

int fibonacci[] = {1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89};

string books[] = {"Matthew", "Mark", "Luke", "John"};

Page 17: For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning

Declaring Arrays

Class of data int, float, string Identifier ages, heights Subscript operator [ ] (square brackets)

Size (length) 10 Initial values {1, 1, 2, 3, 5, 8}

int ages[10];int fibonacci[] = {1, 1, 2, 3, 5, 8};string books[] = {"Matthew", "Mark"};int numbers[8] = {1, 1, 2, 3, 5}

Page 18: For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning

Accessing Arrays

We access elements of an arraywith the array's identifier and the subscript operator []

Array elements count from zero (0)

cout << "Book 0 is " << books[0] << '\n';cout << "Book 3 is " << books[3] << '\n';

for (int e=0; e<11; e++){ // use elements 0-10cout << "Element " << e << " is "

<< fibonacci[e] << endl;}

Page 19: For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning

matt@LAPTOP$ arrays Book 0 is MatthewBook 3 is JohnElement 0 is 1Element 1 is 1Element 2 is 2Element 3 is 3Element 4 is 5Element 5 is 8Element 6 is 13Element 7 is 21Element 8 is 34Element 9 is 55Element 10 is 89

Page 20: For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning

for loop

print e and element at eis e<11

e=0

e++

yes

no

There are 11 elements, 0 to 10

Page 21: For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning

Setting Array Elements

We can set the value of an array element the same way

fibonacci[0]=1;

cout << fibonacci[0];

We get the value of an array element with the subscript operator

Page 22: For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning

Multidimensional array

The name sounds strange, the declaration is a little strange, but here is the best part.

› Keeping a list of 5 columns and 10 rows is as simple as creating a single array

› Declaring the appropriate data type double myTable [5] [10]; Now you 50 junk values

Page 23: For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning

Initialize or Get/Store Values…

for(int i = 0; i < 5; i++)› for(int j = 0; j < 10; j++)myTable [i][j] = 0;