# CS 241 Data Organization using C

Variables, printf and Simple Loops
Instructor: Joel Castellanos e-mail: joel@unm.edu j @ Web: http://cs.unm.edu/~joel/ Office: Farris Engineering Center 319 Torin Adamson: toriadam@cs.unm.edu John Donahue: jdonahue@unm.edu
9/7/2010

Due Thursday, Aug 26: Sections 1.1 – 1.4 Getting Started Variables d Arithmetic Expressions V i bl and A ith ti E i The For Statement Symbolic Constants Due Tuesday, Aug 31: Section 1.5 Character Input and Output Lab 2: variation on word counting (section 1.5.4) Due Thursday, Sept 2: Section 1.6 Arrays
2

1

x). e) int e = 1/2 . One of these things does not belong.Quiz: Basic Syntax One of these things is not like the others. i f( [%6d]\ ) printf("[%-6d]\n". return 0. Can you tell which thing is not like the others before the end of this f f quiz? a) int a = 40 * 2*(1 + 3). x).1/16. x). b) int b = (10 * 10 * 10) + 2 c) int c = (2 + 3) * (2 + 3). printf("[%6d]\n". d) int d = 1/2 + 1/3 + 1/4 + 1/5 + 1/6. x). 3 printf function: %d //%d: format placeholder that prints an int as a // signed decimal number. } Output: x=512 [512] [ 512] [512 ] [512] 4 2 . x). #include <stdio. printf("x=%d\n". printf("[%2d]\n". printf("[-%6d]\n".1/4 + 1/8 .h> int main(void) { int x = 512.

printf("z=%f\n". printf("x=%f\n". Ms. x).141592653589793116 5 Significant Figures Using /usr/bin/gcc on moons.7f\n".141592653589793238. x). printf("x=%. z).0.unm. printf( z=%20. printf("x=% 11f\n".18f\n".141592741012573242 z=3.0/3.141593 } Output: z=3.0/30000.18f\n printf("z=%20 18f\n". x=0.00003333333 Output: x=0. z). x).edu.141593 x=3.333252 3 . double z = 3. printf("x=%20. printf( x %. Significant Figure 6 x=0.0000333 printf("z=%f\n". x).11f\n .h> int main(void) { fl float x = 3 141592653589793238 3.printf function: %f #include <stdio. float x = 1. x). return 0.141592653589793238. Significant figures are not the same as decimal places. printf("x=%. z). a float has 7 significant figures.000033333333704 z=3333. float z = 10000.15f\n".0. x=3.

0/3. c=%3. x). b. printf("x=%.666667 a= 8 2 b= 3 c= 0 x= 1 4 .33e-05 Casting int to float #include <stdio. printf("a=%d b=%d c=%f x=%f\n". c.printf function: %e %e: Format placeholder that prints a float or double in Scientific Notation. cast to float. #include <stdio. c.333333e-09 z=3. a b c x). a. printf("x=%e\n".0/30000. printf("x=%e\n" x).0. int b = 3. b. return 0. printf("a=%3d b=%3d c=%3. then floating point division. y).0f x=%3. printf("z=%e\n". z). printf( a=%3d printf("a=%3d b=%3d c=%3 1f x=%3 1f\n".0.0f\n".1f x=%3. float x = (float)a/(float)b.000000 x=0.2e\n". Integer division. c.h> int main() { int a = 2. float c = a/b. printf("y=%e\n".h> int main(void) { float x = 1. x).333333e+03 x=3.1f\n a. float y = x/10000. } Output: a=2 b=3 c=0. x). then cast to float. b.333333e-05 y=3. a. return 0. } 7 Output: x=3. float z = 10000.

not the number one.h> The letter el. printf("Bytes in char: %lu\n". return 0. int main(void) lu: unsigned long { int a. } Output: Bytes in char: 1 Bytes in int: 4 Bytes in a: 4 9 printf function: %c #include <stdio. printf("Bytes in a: %lu\n". printf("The ASCII code for %c is %d\n". printf("Bytes in int: %lu\n". y++. y. printf("The ASCII code for %c is %d\n". sizeof(a)). char y = 'b'. printf("The ASCII code for %c is %d\n". x). y. sizeof(char)). sizeof(int)). return 0. y). y).Keyword: sizeof #include <stdio.h> int main(void) { char x = 'a'. } Output: The ASCII code for a is 97 The ASCII code for b is 98 The ASCII code for c is 99 10 5 . x.

7 3) { 40.0 70. 14) } 15)} 11 Quiz: While Loop 1) #include <stdio.0 -6.7 6) float step = 20.0 -17.h> 2) int main() 3) { 4) float lower = 0. 80. 12) printf("%5. 12 6 .0 40 0 4 4 4.Section 1.0)*(fahr-32.8 2) int main() 20.4 4) float lower = 0.0).0 75.1f\n".0 26.0 15.h> Output: 0.0 . fahr). 12) } 13) } What is the last number printed by the given C program? a) b) c) d) e) 0. 7) 8) float fahr = lower. 60.0 37.1f %6.2: Fahrenheit to Celsius 1) #include <stdio. 100. 5) float upper = 75. fahr.1f\n".0/9. 9) float celsius.6 5) float upper = 100.0 80. 13) fahr = fahr + step. 11) fahr = fahr + step. 9) while (fahr <= upper) 10) { printf("%4. 6) float step = 20. 10) while (fahr <= upper) 11) { celsius = (5.8 7) 8) float fahr = lower. celsius).0 60.

8) 9) for (fahr = lower. e) 65. 7) float fahr.0 2) int main() c) 50. 9) 10) while (fahr <= UPPER) 11) { printf("%d\n". fahr).1f\n". fahr).h> 2) 3) #define LOWER 0 4) #define UPPER = 300 5) 6) int main() 7) { 8) int fahr = LOWER.0 4) float lower = 50. 12) fahr = fahr + 15. fahr=fahr+step) 10) { printf("%4.0 5) float upper = 75. 11) } 12) } 13 Quiz: Find the Syntax Error 1) #include <stdio. 13) } 14) } On which line will the gcc compiler on moons. 6) float step = 15.unm.edu (without using any options) report an error? a) b) c) ) d) e) line line line li line line 3 8 9 10 11 14 7 .cs.h> b) 45.0 3) { d) 60.0 1) #include <stdio.Quiz: For Loop What is the last number printed by the given C program? a) 0. fahr <= upper.

c xbox 73 % a. however.c 15 Count Characters. 6) //1) Reads a character from stdin 7) 8) //2) Compares the character to EOF (End Of File code) 9) while (( c = getchar()) != EOF) 10) { //Your textbook does not enclose this if statement in { } 11) //Our coding standard. state. if (c == '\n') ++n1.Count Lines of Input 1) #include <stdio. nc). while (( c = getchar()) != EOF) { d) Terse output. block is only one statement. character.out < inputLineCounter. 12) if (c == '\n') ++numberOfLines.h> line 4 could be changed to: 2) int main() char c 3) { 4) int c //used to store an 8 bit character c.4 1) 2) 3) 4) ) 5) 6) 7) 8) 9) 10) 11) 12) 13) 14) 15) 16) 17) 18) 19) 16 20) #include <stdio. state = IN. will require the enclosing { } g q g }. //book uses bad name: n1. n1. int main() () b) Multiple actions in one line. 5) int numberOfLines = 0. 15) } 15 xbox 72 % gcc inputLineCounter. numberOfLines). else if (state == OUT) { e) { at end of line. ++nw. f) Indent of 5 spaces. state = OUT.h> Coding style used in textbook: #define IN 1 /* inside a word */ #define OUT 0 /* outside a word */ a) Variable names too short. 13) } 14) printf("%d\n". { int c. ++nc. c) Leaves out { } when body of n1 = nw = nc = 0. nw.5. } } printf("%d %d %d\n". Lines and Words: 1. nc. } 8 . n1. if (c == ' ' || c == '\n' || c == '\t‘) state = OUT. nw.

18) } 17 Count Characters.. charCount). wordCount). 14) } 15) c = getchar().. 4) char c = getchar(). 13) wordCount++. 11) char c = getchar(). lineCount).Count Characters.. 9) int wordCount = 0.. 10) int insideWord = FALSE. 7) if (c == '\n') lineCount++.//set up variables. 8) int lineCount = 0.. 16) } 17) . Lines and Words: 2 of 2 1) int main() Why is newline 2) { twice checked? 3) .h> 2) 3) #define FALSE 0 4) #define TRUE -1 1 5) 6) int main() 7) { int charCount = 0. g 12) while (c != EOF) 13) { . 17) printf("Line Count = %d\n". 14) } 15) printf("Character Count = %d\n". 5) while (c != EOF) 6) { charCount++. //output 18) } 18 9 . Lines and Words: 1 of 2 1) #include <stdio. 8) if (c == ' ' || c == '\n' || c == '\t') 9) { insideWord = FALSE. 10) } 11) else if (insideWord == FALSE) 12) { insideWord = TRUE.. 16) printf("Word Count = %d\n".

11) } 12) else if (insideWord == FALSE) 13) { insideWord = TRUE.//set up variables. 5) while (c != EOF) 6) { c = getchar().. //output 18) } 19 10 . 14) wordCount++.Count Characters. not work? 4) char c = 'a'. 9) if (c == ' ' || c == '\n' || c == '\t') 10) { insideWord = FALSE.. 8) if (c == '\n') lineCount++.. Lines and Words 1) int main() Why will this 2) { 3) . 7) charCount++.. 15) } 16) } 17) .