Upload
lambert-carroll
View
226
Download
4
Embed Size (px)
Citation preview
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.
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);
Selections
One Way (if statements)
Two Way if else
Multiple Way if else if else or switch case
$$$$$$$$$$$$$$$$$$$$$$$$
IOMANIPWith the deriective
#include <iomanip>
We gain several functions that allow for output manipulation
fixed, setprecision(#), setw(#), showpoint, hidepoint,
setw()
left right internal
showpos / noshowpos
fixed / unsetf
showpoint / hidepoint
setprecision()
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.
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.
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
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 =
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.
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?
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"};
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}
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;}
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
for loop
print e and element at eis e<11
e=0
e++
yes
no
There are 11 elements, 0 to 10
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
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
Initialize or Get/Store Values…
for(int i = 0; i < 5; i++)› for(int j = 0; j < 10; j++)myTable [i][j] = 0;