Introduction to Computing
Introduction to Computing ECS 102
Popular in Course
Popular in Engineering Computer Science
This 11 page Class Notes was uploaded by Florine Cormier on Wednesday October 21, 2015. The Class Notes belongs to ECS 102 at Syracuse University taught by Marjory Baruch in Fall. Since its upload, it has received 95 views. For similar materials see /class/225572/ecs-102-syracuse-university in Engineering Computer Science at Syracuse University.
Reviews for Introduction to Computing
Report this Material
What is Karma?
Karma is the currency of StudySoup.
Date Created: 10/21/15
Algorithms for Selection Sort and Bubble Sort from p 401 of textbook modified find and return the index of the smallest value in the array within the given range int getmaxrange char list the array we are looking in int first the index of the first element we will look at int last the index of the last element we will look at indexofmax first for each value of i from first1 to last if listi is greater than listindexofmax update indexofmax to i return indexofmax void selectsort char list list to be sorted int n number of elements int tofill subscript of last element of unsorted array char temp temporary storage indexofmax index of greatest element in unsorted array for tofill down to tofill n 1 whole array unsorted 1 last 2 elements need to be put in order Find position of greatest element in unsorted array Swap it with the element at end of unsorted array Bubble Sort void bubblesort char list list to be sorted int n number of elements int last subscript of last element of unsorted array char temp temporary storage int i index of first element in pair being compared for each value of last from n 1whole array to 1 2 element array for each value of i from O to last1 if listigtlisti1 swap these two elements userand c includeltstdiohgt printF includeltstd1ibhgt rand srand RANDMAX includelttimehgt time int mainCvoid int i print some random numbers printFquotSome random numbersnquot For i1 ilt5 i printFquot10dquot rand printFquotnquot printFquotRange is 0 to dnnquotRANDMAX Some random numbers 16838 5758 10113 17515 31051 Range is 0 to 32767 How to create a good range Suppose we want numbers in the range 2 5 2 3 4 5 That is 4 consecutive numbers Step 1 take mod 4 printFquot4 consecutive numbersnquot For i1 ilt20 i printFquot3dquot rand4 printFquotnnquot But you want the lowest one to be 2 so add 2 printFquotrandom numbers Form 2 to 5nquot For i1 ilt20 i printFquot3dquot rand4 Z printFquotnquot 4consecutivenumbers 32302130013131222333 random numbers form 2 to 5 4 5 3 4 2 2 3 4 4 5 5 4 3 3 2 2 2 3 5 5 Seeding rand to get reproducible results printFquotnFirst run nquot srandC38 Fori1 ilt20 i printFquot3dquotrand100 printFquotnSecond run some seed nquot srandC38 Fori1 ilt20 i printFquot3dquotrand100 printFquotnThird run different seed nquot srand19 Fori1 ilt20 i printFquot3dquotrand100 First run 63 356699472483 52 452 52 17 16 749902061 835 Second run same seed 63 3566994724 83 52 452 52 17 16 749902061 835 Third run different seed 16 1772422 9149797654185 8833 34 1744977614 The computer can seed with the time printFquotnnThe time is now dnquot time0 printFquotUsing the time as a seed nquot srandCtimeC0 Typically do this once at the beginning of your program Fori1 ilt20 i printFquot3dquotrand100 returnCG The time is now 1110321446 Using the time as a seed 29 15 25 43 87 81 93 27 51 3O 22 83 4O 43 47 21 93 72 52 19 int fscanfmyin quotdddquot ampa ampb ampc the return value is either the number of values successfully read or a special value EOF example if fscanfrnyin quotdquot ampkEOF printfquotOut of data quot typical use While fscanfrnyin quotdquot ampk EOF process k Char f gets mystring 80 myin le the return value is a pointer If there was nothing to read it is the NULL pointer example ptr f gets mystring 80 rnyin le if pt 2 NULL printfquotY0u have reached the endquot typical use While f getsmystring 80 myin le NULL process mystring Parallel arrays int itemNumber10 double price10 0 1234 0 38632 1 6529 1 012 2 9983 2 1234 3 4567 3 9999 4 2020 4 9999 5 9987 5 2399 6 1212 6 099 7 3948 7 38632 8 0098 8 845 9 8876 9 013 Suppose we want to nd the price of item 2020 Given the function int search int theArray int arrSize int target search theArray of size arrSize for the first occurrence of target Return the index if it is there Return 1 if it does not occur in theArray We can use it int location location searchitemNumber 10 2020 if location 1 printf quotIt costs 021fnquot pricelocation else printfquotWe do not have that itemnquot Binary Search Algorithm p429 Hanly and Kottman int binarysearchint theList int highlndex int target 1 Let bottom be the subscript ot the initial array element 2 Let top be the subscript ot the last array element 3 Let found be false 4 Repeat as long as bottom isn39t greater than top and the target has not been found aLet middle be the subscript ot the element halfway between bottom and top bit the element at middle is the target i Set found to true and index to middle else it the element at middle is larger than the target ii Let top be middlel else iii Let bottom be middle 1 5 It the target was found a return the index else b return 1