200 likes | 306 Views
Research Topics in Computational Science. Agenda. Survey Overview. Notes on the Survey:. End on Time I will try to end 10 min early for work & qus This isn’t too slow Nobody said slow, but some said fast Upload Class Code More time on my code (I will slow down)
E N D
Agenda • Survey Overview
Notes on the Survey: • End on Time • I will try to end 10 min early for work & qus • This isn’t too slow • Nobody said slow, but some said fast • Upload Class Code • More time on my code (I will slow down) • Go Quicker: with questions • Go Slower: with in class coding
Today’s Agenda • Code with notes • practice for homework set • Download the PPT to follow along with while coding
Indexing And Arrays • In C we start counting at 0 • The “1st” value is actually the “0th” value in C
Array Indeciesdouble x = {0.0, 3.14, …, 1.1, 2.7} The computer needs to set aside memory, then the values can be placed in memory. For an array of n elements we start at element 0 and access up to element n-1.
Arrays • An array is a data object of a fixed size to hold many variables of the same type • int[] a, b, c; // Will create 3 integer arrays • This doesn’t allocate memory for the array • a = new int[10]; • Allocates memory for 10 ints in array a • int[] a = {0,1}; //declare, allocate, assign values
Lists • List<int> • Creates an int list • List<double> • Creates a double list • List<string> • creates a list of strings • List<someType> • creates a list of the type specified between <> • We can create lists of any object • We can have a list of buttons or images
List Functions • x = myList[3]; • gets the value at index 3 from the list • myList.Add(3.14); • Adds the value 3.14 to the list • myList.Remove(3.14); • removes first occurance of 3.14 from the list • myList.RemoveAt(3); • removes the item myList[3]
c++, c--, ++c, --c • Only used for “int” • c++ and ++c will add 1 to the variable c • c++ will use c, THEN add 1 • ++c will add 1 to c, then use it • c-- and --c will subtract 1 from the variable c • c-- will use c, then subtract 1 • --c will subtract 1, then use c Example: c = 0; mbox(c++);//displays 0 mbox(c);// displays 1 mbox(++c);//displays 2
For Loop • This will loop a specified number of times, AND increment a variable (usually i, j, or k) • There is a standard loop that is used MOST of the time • You can change as needed
For Code for (inti=0; i<5; i++) { mbox(“Hello” + name[i]); } • This will say hello to the first five names in the array “name” • Start at i=0 • Continue while i<5 • At the end of every loop do the operation i++
For Each Loop • This will loop through each element of an array or list • This can be very useful when we don’t know how many things are in our list/array
ForEach Code int cider = 0; foreach(intage in ages){ if(age<20) cider++; /* count the number of cider drinkers */ } • We have to declare the type, here it is an int • We have to give a temp name, age is a clear name for this • We have to give the name of our array/list
While Loop • While loops can run forever! • Becareful of infinite loops • These will run while a statement is true
While Loop Example boolmyval = 1; while( myVal < 100 ) { myVal = myVal * 2; } • This will find the smallest power of 2 greater than 100. • After each loop it checks the while
Do While Loop (aka do loop) • This loop will ALWAYS execute one time • At the end of the loop it checks to see if it should run again
Do While Loop myVal = 200; do { myVal *= 2; } while (myVal < 100); • This does the same thing as the last loop. • Even though 200 will give a false value for “while” it will still do the loop one time
Switch Control • Select based on matching input • This is good when you have a lot of cases to be tested for.
Switch Code switch( word ) { case “boy”: // do this break; case “girl”: // do this break; default: // do this break; } • If the variable word is the same as “boy” it will do the first statement • it will try every CASE until it finds one that matches or the end. • Every case must have a break.