Download presentation
Presentation is loading. Please wait.
Published byBrett Perry Modified over 9 years ago
1
1 EPSII 59:006 Spring 2004
2
2 Exam Dates
3
3 Homework Grading In general, programs that do not compile and run will be given no more than 25% credit Points will be deducted for the following: serious compiler warnings poor style—i.e. lack of indentations to delineate blocks lack of comments poor program design—i.e. confusing logic, non- meaningful variable names, etc. lack of sufficient input-checking
4
4 % man gcc NAME gcc, g++ - GNU project C and C++ Compiler (gcc-2.95) SYNOPSIS gcc [ option | filename ]... g++ [ option | filename ]... WARNING The information in this man page is an extract from the full documentation of the GNU C compiler, and is limited to the meaning of the options. This man page is not kept up to date except when volunteers want to maintain it. If you find a discrepancy between the man page and the software, please check the Info file, which is the authoritative documentation. If we find that the things in this man page that are out of date cause significant confusion or complaints, we will stop distributing the man page. The alternative, updating the man page when we update the Info file, is impossible because the rest of the work of maintaining GNU CC leaves us no time for that. The GNU project regards man pages as obsolete and should not let them take time away from other things. For complete and current documentation, refer to the Info file `gcc' or the manual Using and Porting GNU CC (for version 2.0). Both are made from the Texinfo source file gcc.texinfo.
5
5 Important gcc options gcc -Wall - prints warnings when you compile gcc -o - renames executable file (to avoid ‘a.out’) So, to avoid problems, always invoke gcc as gcc –Wall –o
6
6 Logical Operators && ( logical AND ) Returns true if both conditions are true || ( logical OR ) Returns true if either of its conditions are true ! ( logical NOT, logical negation ) Reverses the truth/falsity of its condition Unary operator, has one operand Useful as conditions in loops ExpressionResult true && falsefalse true || falsetrue !falsetrue
7
7 Logical Operators ABA && B 000 010 100 111 ABA || B 000 011 101 111
8
8 Examples (1 && 1) = 1 (0 && 1) = 0 (1 || (1 && 0)) = 1 (0 || (1 && 1)) = 1 ((1 && 1) || 0) = 1
9
9 Confusing Equality ( == ) and Assignment ( = ) Operators Dangerous error Does not ordinarily cause syntax errors Any expression that produces a value can be used in control structures Nonzero values are true, zero values are false Example using == : if ( payCode == 4 ) printf( "You get a bonus!\n" ); Checks paycode, if it is 4 then a bonus is awarded
10
10 Confusing Equality ( == ) and Assignment ( = ) Operators Example, replacing == with = : if ( payCode = 4 ) printf( "You get a bonus!\n" ); This sets paycode to 4 4 is nonzero, so expression is true, and bonus awarded no matter what the paycode was Logic error, not a syntax error
11
11 Confusing Equality ( == ) and Assignment ( = ) Operators lvalues Expressions that can appear on the left side of an equation Their values can be changed, such as variable names x = 4; rvalues Expressions that can only appear on the right side of an equation Constants, such as numbers Cannot write 4 = x; Must write x = 4; lvalues can be used as rvalues, but not vice versa y = x;
12
12 Structured-Programming Summary Structured programming Easier than unstructured programs to understand, test, debug and modify Rules for structured programming Rules developed by programming community Only single-entry/single-exit control structures are used Rules: 1. Begin with the highest level description of the solution –ie. the “simplest flowchart “ 2. Any rectangle (action) can be replaced by two rectangles (actions) in sequence 3. Any rectangle (action) can be replaced by any control structure (sequence, if, if / else, switch, while, do / while or for ) 4. Rules 2 and 3 can be applied in any order and multiple times
13
13 Structured-Programming Summary....... Rule 2 Rule 1 - Begin with the simplest flowchart (or pseudocode) Rule 2 - Any rectangle can be replaced by two rectangles in sequence
14
14 Structured-Programming Summary Rule 3 Rule 3 - Replace any rectangle with a control structure (selection or repetition)
15
15 Structured-Programming Summary All programs can be broken down into 3 controls Sequence – unconditional set of actions Selection – if, if / else or switch Repetition – while, do / while or for Can only be combined in two ways Nesting (rule 3) Stacking (rule 2) Any selection can be rewritten as an if statement, and any repetition can be rewritten as a while statement
16
16 Structured Programming Example Develop a program that prints the Roman Numeral equivalent of numbers typed from the keyboard (numbers must be in the range of 1-3999). The program should be terminated by typing a sentinel value of -1 Example Output: Enter a number between 1 and 3999 (-1 to exit): 47 The roman numeral equivalent of 47 is: XLVII Enter a number between 1 and 3999 (-1 to exit): 12 The roman numeral equivalent of 12 is: XII Enter a number between 1 and 3999 (-1 to exit): -1 Goodbye!! %
17
17 How Do We Approach the Problem? Use stepwise refinement to develop a structured algorithm in pseudocode Convert pseudocode into a C program Debug, test, and refine the program
18
18 Top-level algortithm Initial Algorithm: Input integer values in the range 1-3999 from the from the keyboard and convert each number to its roman numeral value until a sentinal value of -1 is entered. First refinement: Input a valid integer value (or termination condition) from the keyboard: Repeat the following steps until the termination condition is satisfied: A: Display the Roman Numeral equivalent of the entered number B: Input the next valid integer value (or termination condition) from the keyboard.
19
19 Further refinement of Steps: Refinement of Step A: How do we figure out the roman numeral equivalent of a number? On possible approach is table lookup—i.e. create a table showing the roman numeral equivalent for each integer Table would be very large (4000 entries) Would have to create the entries by hand (tedious) Not scalable—what if we wanted to expand our program to handle values in the range of 1-1,000,000? So we need to come up with a conversion process Do a little research to be sure that we understand how roman numeral representation works. Play with some examples Figure out a conversion scheme that can be expressed as an algorithm
20
20 Roman Numeral Representation Digits: I represents 1 V represents 5 X represents 10 L represents 50 C represents 100 D represents 500 M represents 1000 Representation of integers 1-9: I represents 1 II represents 2 III represents 3 IV represents 4 V represents 5 VI represents 6 VII represents 7 VIII represents 8 IX represents 9
21
21 More about Roman Numeral Representation X represents 10 XX represents 20 XXX represents 30 XL represents 40 L represents 50 LX represents 60 LXX represents 70 LXXX represents 80 XC represents 90 C represents 100 CC represents 200 CCC represents 300 CD represents 400 D represents 500 DC represents 600 DCC represents 700 DCCC represents 800 CM represents 900
22
22 Digit-by-digit conversion of decimal number to roman numerals: 748 DCCXLVIII 2409 MMCD IX
23
23 Refinement of Step A based upon Digit-by Digit Conversion Step A: Display the roman numeral equivalent for the digit in the “thousands position” Display the roman numeral equivalent for the digit in the “hundreds position” Display the roman numeral equivalent for the digit in the “tens position” Display the roman numeral equivalent for the digit in the “ones position”
24
24 Further refinement: Original Step: Display the roman numeral equivalent for the digit in the “thousands position” Refinement: Determine the value of the “thousands position” digit Based upon the value of this digit display the following: 0 => 1 => M 2 => MM 3 => MMM
25
25 Further refinement Original Step: Determine the value of the the “thousands position” digit Refinement: Determine the value of the thousands position digit by dividing the input number by 1000 (integer division)
26
26 Stepwise Refinement Continued: Original Step: Display the Roman Number equivalent for the digit in the “hundreds position” Refinement: Determine the value of the “hundreds position” digit Based upon the value of this digit, display the following: 0 => 1 => C 2 => CC 3 => CCC 4 => CD 5 => D 6 => DC 7 => DCC 8 => DCCC 9 => CM
27
27 Further refinement Original step—Determine the value of the “hundreds position” digit Refinement: Compute the value of the “hundreds position” digit as follows : compute the modulus resulting from division of the input number by 1000 dividing the modulus by 100 (integer division) e.g. (3467 % 1000)/100) == 4
28
28 Stepwise Refinement Continued: Original Step: Display the Roman Number equivalent for the digit in the “tens position” Refinement: Determine the value of the “tens position” digit Based upon the value of this digit, display the following: 0 => 1 => X 2 => XX 3 => XXX 4 => XL 5 => L 6 => LX 7 => LXX 8 => LXXX 9 => XC
29
29 Further refinement Original step—Determine the value of the “tens position” digit Refinement: Compute the value of the “tens position” digit as follows : compute the modulus resulting from division of the input number by 100 divide the modulus by 10 (integer division) e.g. (3467 % 100)/10) == 6
30
30 Stepwise Refinement Continued: Original Step: Display the Roman Number equivalent for the digit in the “ones position” Refinement: Determine the value of the “ones position” digit Based upon the value of this digit, display the following: 0 => 1 => I 2 => II 3 => III 4 => IV 5 => V 6 => VI 7 => VII 8 => VIII 9 => IX
31
31 Further refinement Original step—Determine the value of the “ones position” digit Refinement: Compute the value of the “ones position” digit as follows : compute the modulus resulting from division of the input number by 10 e.g. (3467 % 10) == 7
32
32 /* Terry Braun 2/8/2004 convert decimal numbers to roman numerals input is 1-3999 exit on -1 */ main() { int num = 1; // number entered by user int done = 0; // done when done = one // place holder values int thousand=0; int hundred = 0; int ten=0; int one=0; while(!done) { printf("Enter a number between 1 and 3999(-1 to exit):"); scanf("%d",&num); if((num >= 1)&&(num<=3999)) { //valid input thousand = num / 1000; // determine value of 1000 digit hundred = ((num % 1000)/100); ten = ((num % 100)/10); one = ((num % 10)/1); // For DEBUGGING //printf("%d %d %d %d\n",thousand,hundred,ten,one);
33
33 /****** generate thouands Roman numeral(s) ******/ if(thousand == 1) { printf("M"); } else if(thousand == 2) { printf("MM"); } else if(thousand == 3) { printf("MMMM"); } else if(thousand == 0) { // do nothing } else { // Note -- this condition should never execute printf("Unexpected thousand value = %d -- exiting\n",thousand); exit(1); // Unless there is a logic error }
34
34 /****** generate hundreds Roman numeral(s) ******/ if(hundred == 1) { printf("C"); } else if(hundred == 2) { printf("CC"); } else if(hundred == 3) { printf("CCC"); } else if(hundred == 4) { printf("CD"); } else if(hundred == 5) { printf("D"); } else if(hundred == 6) { printf("DC"); } else if(hundred == 7) { printf("DCC"); } else if(hundred == 8) { printf("DCCC"); } else if(hundred == 9) { printf("CM"); } else if(hundred == 0) { // do nothing } else { printf("Unexpected hundred value = %d -- exiting\n",hundred); exit(1); }
35
35 /****** generate tens Roman numeral(s) ******/ if(ten == 1){ printf("X"); } else if(ten == 2){ printf("XX"); } else if(ten == 3){ printf("XXX"); } else if(ten == 4){ printf("XL"); } else if(ten == 5){ printf("L"); } else if(ten == 6){ printf("LX"); } else if(ten == 7){ printf("LXX"); } else if(ten == 8){ printf("LXXX"); } else if(ten == 9){ printf("XC"); } else if(ten == 0){ // do nothing } else { printf("Unexpected tens value = %d -- exiting\n",ten); exit(1); }
36
36 /****** generate ones Roman numeral(s) ******/ if(one == 1){ printf("I"); } else if(one == 2){ printf("II"); } else if(one == 3){ printf("III"); } else if(one == 4){ printf("IV"); } else if(one == 5){ printf("V"); } else if(one == 6){ printf("VI"); } else if(one == 7){ printf("VII"); } else if(one == 8){ printf("VIII"); } else if(one == 9){ printf("IX"); } else if(one == 0) { // do nothing } else { printf("Unexpected ones value = %d -- exiting\n",one); exit(1); }
37
37 printf("\n"); } // end of valid input else if(num == -1) { done = 1; // we are done } else { printf("Invalid input, please try again\n"); } } // end while } // end main
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.