DOC PREVIEW
UIUC ECE 190 - Programming Studio 3

This preview shows page 1-2-3-4-5-6 out of 17 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Programming Studio #3 ECE 190Programming Studio #3 Topics this week: – Systematic Decomposition – C LoopsAnnouncements MP1 – Checkpoint 2 due Feb 17th at 5 pm – Read handout carefully – Cheating Policy is on the website Exam 1 – 2/14, 2/15, 2/16 – 7pm-10pm – Review Session: See Exam page of website – Report conflicts via compass NO LATER THAN noon on Feb 7th (after which the conflict-request quiz will be unavailable) Some discussion sections are over capacity, so we’re looking for volunteers to switch. If your section is full and you are willing to switch, please do so on Monday.New verify command! • verify will help make sure your latest submission worked. • As with handin, to use it you must be in ece190 workspace (type ece190 in the prompt) • Navigate to the directory where your mp1.1.c file is located • Verify mp1.1 now: ece190> verify --MP 1.1 mp1.1.cReminders • Web Board • MP Late Policy • Can hand in multiple times; most recent submission is graded • Office HoursSystematic Decomposition • Do not go straight to coding • Start with a problem in plain language – Break it into smaller steps progressively – Continue until steps small enough – Finally, steps can be turned into codeExample: Power • Problem: – Design a program in C to calculate the nth power of x. The program should ask the user to input n and x. The result should be printed on the screen. • Given algorithm: Multiply X by itself N times. – 53 = Result. – 53= 5 * 5 * 5 – Loop using for, while or do-while.First Phase: Large Steps • First few steps help show program flow • For calculating powers we have 4 main steps Declare & initialize variables Request input from user Set up loop that calculates the power Print result on screenMiddle Phase: Small Steps • Break large steps into smaller steps • Specify program flow more completely • Include algorithm to calculate the power • Add control flow to allow loop behavior • Choose which variables hold which data – Result stores running product – X holds base value – N to holds power Initialize Variables Request values for x and n from user Result = X Multiply result with X Decrement N Finished? Print result No YesFinal Phase: Pseudo Code • Take small steps and convert to pseudo-code • Move from pseudo instructions into C instructions. Result = 0 Read X,N Result = X Result = Result * X N = N -1 N == 0 ? Print result No Yeswhile Loop • Syntax: while (condition) { <code> <condition update> } • Wrapped commands execute if and only if condition == true • As long as condition is true, commands within braces executed repeatedly Important!while Example int main () { int a = 5; /* Initializes while loop */ while (a>0) { /* Open brace starts loop code */ printf("The current value of a is %d\n", a ); /* loop code */ a = a - 1; /* updates condition */ } } • How many lines will this code print? Condition update Conditiondo-while Loop • do-while loops are similar to while loops • One exception: do-while loops always execute code in braces at least once • Syntax: do { <code> <condition update> } while (condition);for Loop • The most straight-forward loop • Syntax: (cvar is short for condition variable) for(<init cvar>; <condition>; <update cvar>) { <loop code> } • Order of execution 1. Initialize 2. Check condition 3. Execute loop code 4. Update condition • Repeat 2-4 until 2 failsfor Example int main () { int a; for(a=5; a>0; a=a-1)/* Initialize while loop */ { /* Open brace loop code */ printf("The current value of a is %d\n", a); /* loop code */ } } • (This code results in the same output as the while example code)Ad-hoc Debugging • printf can be very useful for debugging • For example, if your program is modifying a variable a, and you are getting the wrong result, try some printfs to track down where the result is going wrong: printf("before stuff: a = %d\n", a); /* do stuff with a */ printf("after stuff: a = %d\n", a); /* do more stuff with a */ printf("after more stuff: a = %d\n", a); • If you’re getting exceptions/errors, try using ‘status’ printfs: printf("before stuff..."); /* do stuff that may or may not cause exception */ printf("after stuff (stuff didn’t break)...");Example Exercise • Download skeleton code: wget http://courses.engr.illinois.edu/ECE190/discussion/spring11/PS3/ps3.c • main() takes int value x from user, where 1≤x≤15 • Manipulate x as follows: – Output x! if 2 ≤ x ≤ 5 or 9 ≤ x ≤ 13 – Otherwise, output x² • Implement ‘do-while’ loop to get an integer in the range 1-15 from the user, then implement range-checking in the calculate function’s ‘if’ statement • Implement the factorial using a for


View Full Document
Download Programming Studio 3
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Programming Studio 3 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Programming Studio 3 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?