Unformatted text preview:

11/25/2012CS 241 Data Organization using CVariables and Simple LoopsInstructor: Joel Castellanose-mail: [email protected]: http://cs.unm.edu/~joel/Office: Farris Engineering Center 319Read: Kernighan & Ritchie Due Tuesday, Jan 241.5: Character Input and Output Due Thursday, Jan 261.6: Arrays Due Tuesday, Jan 311.7: Functions1.8: Function Arguments - call by Value1.9: Character Arrays22Cannot Connect to moons.cs.unm.eduIf moons.cs.unm.edu does not work, trytrucks.cs.unm.eduIf neither works, 1. Make sure you are not doing something wrong.2. Contact CS support:  e-mail: [email protected] Help Desk - 277-3527 3Distributed Computing: When the crash of a computer you've never heard of stops you from getting any work done.Quiz: '=' SymbolIn The C programming language, the ‘=’ symbol is most accurately read:a) “Equals”b) “Assign the value of the expression right side to the variable on the left.”c) “Is equivalent to.”d) “A mathematical symbol used to indicate equality.”e) “A conditional symbol used to indicate equality.”435Assignment Statementx = y * 3 * (z + 1);Assignment OpperatorOne and only one VariableExpressionx + 1 = y;6Statements in C1) #include <stdio.h>2) void main(void)3) {4) int x;5)6)int y = 7;7)8)x = 3 * (y + 1);9)10)printf("x=%d\n", x);11)}Variable Declaration StatementFunction DefinitionFunction CallCompound Statement: Variable Declaration & AssignmentOutput:x=24Assignment Statement47Logical Operators1) #include <stdio.h>2) void main(void)3) {4) int a = 5;5) int b = 2;6) int c = 7;7)8)printf("%d\n", a + b < c);9) printf("%d\n", a + b == c);10) printf("%d\n", a - b == c);11) printf("%d\n", a - b != c);12) }== Equal to Logical Operator!= Not Equal Logical OperatorOutput:0101WHILE loop81. #include <stdio.h>2. void main(void)3. {4. int x=1;5.6.while (x<200)7. { 8. printf("[%d] ", x);9. x = x * 2;10. }11. printf("\n");12. }[1] [2] [4] [8] [16] [32] [64] [128]x = 1x<200?printf("\n")print xx = x*2yesno59Quiz: FOR Loop1) #include <stdio.h>2) void main(void)3) { float lower = 50;4) float upper = 75;5) float step = 15;6) float f; 7)8)for (f = lower; f <= upper; f = f + step)9) { printf("%4.1f\n", f);10) }11)}What is the last number printed by the given C program?a) 45.0 b) 50.0 c) 60.0d) 65.0 e) 75.0FOR loop & WHILE loop101. int i;2. for (i=0; i<8; i++)3. { printf("[%d: %d] ", i, i%4);4. }5. printf("\n");6.7.i=0;8. while (i<8)9. { printf("[%d: %d] ", i, i%4);10. i++;11. }12. printf("\n");[0: 0] [1: 1] [2: 2] [3: 3] [4: 0] [5: 1] [6: 2] [7: 3][0: 0] [1: 1] [2: 2] [3: 3] [4: 0] [5: 1] [6: 2] [7: 3]611Section 1.2: Fahrenheit to Celsius1) #include <stdio.h>2) void main(void)3) {4) float lower = 0;5) float upper = 100;6) float step = 20;7)8)float fahr = lower;9) float celsius;10) while (fahr <= upper)11) { celsius = (5.0/9.0)*(fahr-32.0);12) printf("%5.1f %6.1f\n", fahr, celsius);13) fahr = fahr + step;14) }15)}0.0 -17.820.0 -6.740.0 4.460.0 15.680.0 26.7100.0 37.8Output:12Quiz: Find the Syntax Error1) #include <stdio.h>2)3)#define LOWER 04) #define UPPER = 3005)6)void main(void)7) {8) int f = LOWER;9)10)while (f <= UPPER)11) { printf("%d\n", f);12) f = f + 15;13) }14)}On which line will the gcc compiler on moons.cs.unm.edu (without using any options) report an error?a) line 3b) line 8c) line 9d) line 10e) line


View Full Document

UNM CS 241L - Variables and Simple Loops

Download Variables and Simple Loops
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 Variables and Simple Loops 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 Variables and Simple Loops 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?