340 likes | 366 Views
CISC105 – General Computer Science. Class 4 – 06/14/2006. Office Hours. Michael Haggerty When: Friday 12-1 (by appointment) Where: Pearson 115B Robert Derelanko When: M-F 11-1 (by appointment) Where: Mitchell 219 By appointment? –
E N D
CISC105 – General Computer Science Class 4 – 06/14/2006
Office Hours • Michael Haggerty • When: Friday 12-1 (by appointment) • Where: Pearson 115B • Robert Derelanko • When: M-F 11-1 (by appointment) • Where: Mitchell 219 • By appointment? – • You will need to contact us to set up an appointment to meet with you • We are willing to help you as much as you need, however, we do not want to waste time away by sitting in an empty office! • If you cannot meet during the times above contact me and we will make alternate arrangements
Exam is next week! • Format is True/False, Multiple Choice, Short Answer, Write a Program • Exam will cover Chapters 1-4, class notes, Lab00 and Lab01 (except loops) • Vocabulary • Compiling, Linking & executing a program • Software Development Method • Coding – understand how to read/write a program • Variable types ( int char double ) • Functions – declaration, definition, use • printf – understand formatting • scanf • if…else & switch (Selection) • Operator Precedence
Review • Algorithm – series of steps to follow in order to solve a problem • Program – the implementation of an algorithm written using the rules of the programming language • Can be in machine code or assembly • High-Level Language (like C) must be complied and linked before execution • Process – a program in execution
Review – Problem to ProgramThe Software Development Method • Use the software development method to define your algorithm • Problem Definition – usually given to you in class/lab • Analyze Problem (inputs, outputs, relevant formulas) • Design Algorithm – what steps should you take to get input and transform it to the output • Implement Algorithm – write C code based on the algorithm • Test – Use test cases to determine if you implemented the algorithm correctly • Maintain – an important step used for “production” software (not necessary for what we are doing here)
Review Program Structure • Preprocessor Directives (#include & #define) • The main function (return(0)) • Variable Types and Declaration (int double & char) • Variable Manipulation • I/O Statements – printf() scanf() • Function arguments • Format String (placeholders – multiple placeholders) • Print list • Output Format • \n and the cursor • Numbers
Review Functions • Function Declaration • What • Where • Why • Function Definition • What • Where • Function Use • What • Where • How • Function Input and Output Arguments
Review Selection Structures (if…else) • Execution of a statement (s) is dependent on a condition • Single, compound, multiple alternatives • General Structure if (condition) statement(s) if condition is true else statement(s) if condition is false • if statements can be nested within other if statements – use {} to clarify which statement(s) belong together
Review Selection Structures (switch()) • Used instead of nested loops where you can utilize jumps • break; & default:
Review - switch Example switch(n) { case 0: printf("You typed zero.\n"); break; case 3: case 5: case 7: printf("n is a prime number\n"); break; case 2: printf("n is a prime number\n"); case 4: case 6: case 8: printf("n is an even number\n"); break; case 1: case 9: printf("n is a perfect square\n"); break; default: printf("Only single-digit numbers are allowed\n"); }
Lab and Projects • Read the Lab Assignment BEFORE lab (Lab02 is posted now – I’ll try to get the rest of the labs posted ASAP) • I will no longer hand out a hardcopy of the lab! • Refer to the book and class notes for syntax on writing C programming – the TA is there to help you not write your programs!
Repetition Control Structures • Repetition Control Structures are used whenever we need to repeat a group of steps in a program (loop). • The statements that are repeated are contained in the loop body (using {} again!) • There are many types of loops that we can use in an algorithm – C has statements that can run all loop types
Loop Types • Counting – when we know exactly how many time the loop will repeat (use while or for) • Sentinel-Control – Input a list of data in any length ended by a special value (use while or for) • Endfile-Control – input a list of data from a data file (use while or for) • Input Validation – Repeated interactive input of data until a value is within a valid range (use do-while) • General Conditional – Repeated input until a desired condition is met (use while or for)
Counting loop • Algorithm Steps • Initialize loop control variable • Test condition • Condition True • Execute Statement(s) • Update loop control variable • Condition False • Exit Loop
Counting Loop and while • The C identifier while can be used to control a counting loop: initialize loop variable; while (loop repetition condition) { statement(s) run while condition is true; update condition; } control flow continues here when condition is false; • Without initialization an Updating the loop can: • Never run • Become endless
Counting Loop and while • What is the output of the following code: int i = 0; while (i <= 5) { printf(“i = %3d\n”, i); i = i + 1; } printf(“All done!\n);
Compute Payroll • Accumulator – value to store a value being computed in increments during the execution of a loop • 1-Which variable is the accumulator in this program? • total_pay • 2-What would the output this program be? # emp = 3Emp1 – Hours 50 Rate 5.25Emp2 – Hours 6 Rate 5.00Emp3 – Hours 15 Rate 7.00 Pay is $262.50 Pay is $ 30.00 Pay is $105.00 All employees processedTotal payroll is $ 397.50
Counting Loop and for • The C identifier for can be used to control a counting loop for (initialization expression; loop repetition condition; update expression) statement • Similar to a while loop except initialization, condition and update are together.
Increment and Decrement Operator • We have seen loop update expressions written as counter = counter + 1; or counter += 1; • We can also use: • counter++; is postfix increment – it returns the value of counter then increments • ++counter; is prefix increment – it increments counter and then returns the new value • Substitute -- for ++ to get a decrement operator • counter--; • --counter;
Using loops with increments other than 1 • You can control loops with update expressions other than 1 • counter += 10; will increment counter by 10 • Counter -= 10; will decrement counter by 10 • Incrementing/Decrementing by values other than 1 would be identified in your algorithm (the Design phase of the Software Development Method) – see Example 5.4 in Hanly/Koffman p.227.
Conditional Loops • Sometimes you will not know the number of time that you need to run a loop • Number of repetitions relies on data entered • Keep requesting data until the data is valid • The C identifiers while or for can be used to control conditional loops
Conditional Loop Example printf(“Enter a positive number >”); scanf(“%d”, value); while (value < 0) { printf(“Enter a positive number >”); scanf(“%d”, value); } Input validation Example
do…while loop • The do…while loop can be use to reduce program input and take the form do { statement(s); } while (condition); • Commonly used for input validation
Do…while example do { printf(“Enter a positive number >”); scanf(“%d”, value); } while (value < 0);
Sentinel Controlled Loops • Loops that run until a sentinel value (an end marker that follows the last data item) is entered • Sentinel Controlled loops still follow the sequence of: • Initialize • Check condition • Update
Using for in a Sentinel Loop • Can we use for as a Sentinel Loop Control? printf(“Enter first score (or %d to quit)>”,SENTINEL); for (scanf(“%d”, &score); /* Initialize */ score != SENTINEL; /* Test Condition */ scanf(“%d”, &score)) /* Update */ { score += score; printf(“Enter next score (or %d to quit)>”,SENTINEL); }