Introduction to Computing
Introduction to Computing ECS 102
Popular in Course
Popular in Engineering Computer Science
This 22 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 42 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.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 10/21/15
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 WHILE LOOP DO WHILE LOOP before loo before loop Ste S p steps p J V 7 loop steps condition loop steps true after loop false steps after loop steps ARRAYS int ages4 ages0 ages 1 K V ages2 5 ages 3 de nition Sets up memory ages2 5 we can assign to an array element int n We can use ages2 anywhere we can use 11 ex t 3 ages1 formula if ages3 gt 65 comparison abs ages 2 as an argument scanfquotdquot ampages0 in a scanf SOME WAYS TO DECLARE DEFINE AN ARRAY double x3 11 22 33 initialization KW 11 41 22 42 33 int m 2 4 6 m0 2 mm m2 uh GK int n4510 n0 5 nl 10 n2 n3 0 CHARACTER ARRAYS char c3 39a39 39b39a 39039 c0 a c1 b c2 c char d quota big dogquot called a string ONLY for char d0 d1 d2 d3 d4 d5 d6 d7 d3 d9 0 the null character 39a39 is one character quotaquot is two characters a and O USING ARRAYS int t5 int i for i0 ilt5 i ti 2i t0 0 tl 2 t2 4 t3 6 s 9 t4 i 112345 If we now try t 3 6 9 12 15 illegal Only works when rst declare variable t5 10 computer doesn39t complain Very bad Something gets messed up printfquotdquot t1 Computer doesn39t complain Prints junk char word9 quotcomputerquot int i word 0 c 1 0 2 m 3 4 5 6 7 8 for i7 igt0 i printfquotcquot wordi output retupmoc preceeding statements if condition one statement else one statement continue execution preceeding statements quotelse clausequot one statement executes if condition is false if condition quotthen clausequot one statement executes if condition is true con nue execu on x2 if xgt5 printfquotx is bigger than 5quot else printfquotx is not bigger than 5quot printfquotx is a number maybe big maybe smallquot Another method of selection that only works for particular values not ranges Example switch grade case 39A39 printt Well donen break case 39B39 printt Not badn break default printt A little less TV might helpn printt Your grade is cn grade switch grade printf Well donen break case 39B39 i printf Not badn f break 1 default printf A little less TVn break printf Your grade is cn grade L You can have multiple values giving the same option Here 1 is Sunday and count from there switch day case 2 case 4 case 6 case 3 case 5 case 1 case 7 printf gymn break printt art n break printt seepn printt Enjoy your day simpiecard Simulate a deck of cards Provide some useful Functions For working with cards The cards 101 113 are A through of clubs 201 213 are A through of diamonds 301 313 are A through of hearts 401 413 are A through of spades inciudeltstdiohgt includeltstd1ibhgt For randC srandC includelttimehgt For timeC deFine DECKSIZE 52 void displayCint First int last int cards J When a parameter is For a whole array use This does NOT make a copy of the array but uses the original void exchangeCint arrayA int n int 1astinA int arrayB int m int lastinB int mainCvoid int deckDECKSIZE int suit i n0 j int myhand3 yourhand3 int top0 seed the random number generator srandCtimeC0 create a deck 101 102 112 113 then 201 202 213 301 302 For suit100 suitlt400 suit100 For i1 ilt13 i decknsuit i n end For i end For suit printFquotThe decknquot displayC051deck Deal two 3 card hands yourhandidecktop myhandidecktop printFquotnnYour Handnquot displayC02yourhand printFquotnnMy Handnquot displayC02myhand swap one of mine with one From the deck irand3 0 1 2 jrand51 top16 6 7 50 51 exchangeCmyhandi2deckj51 printFquotnnMy Hand after swapnquot displayC02myhand printFquotnnThe Deck after swapnquot displathop51deck returnC0 Print a collection oF cards each as a rank and a suit Prints them all on one line First and last are the subscript For the First and last card to be printed cards is the array oF cards void displayCint First int last int cards int i For iFirst ilt1ast i print the rank switcthardsi100 case 1 printFCquot Aquot break case 11 printFCquot Jquot break case 12 printFCquot Qquot break case 13 printFCquot Kquot break deFault printFquotZdquotcardsi100 end switch print the suit switcthardsi100 case 1 printFquot 10squotquot clubsquot break case 2 printFquot 10squotquot diamondsquot break case 3 printFquot 10squotquot heartsquot break case 4 printFquot 10squotquot spadesquot break deFauit printFCquot INVALID CARDquot break end switch printFCquot quot iF i First6 5 printFquotnquot end For i void exchangeCint arrayA int n int 1astinA int arrayB int m int lastinB Swap the n th entry of arrayA with the m th entry of arrayB Make sure they are not outside their arrays IF either is beyond last do nothing int temp iFCngt1astinA mgt1astinB return temparrayAn arrayAnarrayBm arrayBmtemp k k simplecard Simulate a deck of cards Provide some useful functions for The cards 101 113 are A through K 201 213 are A through K 301 313 are A through K 401 413 are A through K working with cards of clubs of diamonds of hearts of spades includeltstdiohgt includeltstdlibhgt includelttimehgt for rand srand for time define DECKSIZE 52 void displayint first int last int cards whole array use of the array but When a parameter is for a This does NOT make a copy uses the original void exchangeint arrayA int n int lastinA int arrayB int m int lastinB int mainvoid int deckDECKSIZE int suit i n0 j int myhand3 yourhand3 int top0 lowest index of deck that has a card seed the random number generator srandtime0 create a deck 101 102 112 113 then 201 202 213 301 302 for suit100 suitlt400 suit100 for i1 ilt13 i decknsuit i n end for i end for suit printfquotThe decknquot display051deck Deal two 3 card hands for i0 ilt3 i yourhandideckt0p7 myhandideCktOP7 printfquotnnYour Handnquot display02yourhand printfquotnnMy Handnquot display02myhand swap one of mine with one from the deck irand3 0 1 2 jrand51 top1top top 6 7 50 51 exchangemyhandi2deckj51 printfquotnnMy Hand after swapnquot display02myhand printfquotnnThe Deck after swapnquot displaytop51deck return0 Print a collection of cards each as a rank and a suit Prints them all on one line first and last are the subscript for the first and last card to be printed cards is the array of cards void displayint first int last int cards int i for ifirst iltlast i print the rank switchcardsi100 case 1 printfquot Aquot break case 11 printfquot Jquot break case 12 printfquot Qquot break case 13 printfquot Kquot break default printfquot2dquotcardsi100 end switch print the suit switchcardsi100 case 1 printfquot 10squot39 case 2 printfquot 10squot case 3 printfquot 10squot39 heartsquot break case 4 printfquot 10squot39 spadesquot break default printfquot INVALID CARDquot break end switch printfquot quot if i first6 5 printfquotnquot end for i clubsquot break diamondsquot break void exchangeint arrayA int n int lastinA int arrayB int m int lastinB Swap the n th entry of arrayA with the m th entry of arrayB Make sure they are not outside their arrays If either is beyond last do nothing int temp ifngtlastinA mgtlastinB return temparrayAn arrayAnarrayBm arrayBmtemp
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'