CS 241 Data Organization Using C Data Organization Using C: Variables, Printf and Simple Loops
CS 241 Data Organization Using C Data Organization Using C: Variables, Printf and Simple Loops
9/7/2010
Textbook Reading
Due Thursday, Aug 26: Sections 1.1 – 1.4
Getting Started
V i bl and
Variables dA
Arithmetic
ith ti E Expressions
i
The For Statement
Symbolic Constants
1
Quiz: Basic Syntax
One of these things is not like the others. One of
these things does not belong. Can you tell which
thing is not like the others before
f the end off this
quiz?
a) int a = 40 * 2*(1 + 3);
b) int b = (10 * 10 * 10) + 2
c) int c = (2 + 3) * (2 + 3);
d) int d = 1/2 + 1/3 + 1/4 + 1/5 + 1/6;
e) int e = 1/2 - 1/4 + 1/8 - 1/16;
3
printf function: %d
//%d: format placeholder that prints an int as a
// signed decimal number.
#include <stdio.h>
int main(void)
{ int x = 512; Output:
printf("x=%d\n", x); x=512
printf("[%2d]\n", x); [512]
printf("[%6d]\n",
i f( [%6d]\ x);
) [ 512]
printf("[%-6d]\n", x); [512 ]
printf("[-%6d]\n", x); [- 512]
return 0;
4
}
2
printf function: %f
#include <stdio.h>
int main(void)
{ float
fl x = 3.141592653589793238;
3 141592653589793238
double z = 3.141592653589793238;
printf("x=%f\n", x);
printf("z=%f\n", z);
printf("x=%20.18f\n", x);
printf("z=%20
printf( z=%20.18f\n
18f\n", z);
return 0;
x=3.141593
}
Output: z=3.141593
x=3.141592741012573242
5 z=3.141592653589793116
Significant Figures
Using /usr/bin/gcc on moons.unm.edu,
a float has 7 significant figures.
3
printf function: %e
%e: Format placeholder that prints a float or double in
Scientific Notation.
#include <stdio.h>
int main(void)
{
float x = 1.0/30000.0;
float y = x/10000;
float z = 10000.0/3.0;
printf("x=%e\n" x);
printf("x=%e\n", Output:
printf("y=%e\n", y); x=3.333333e-05
printf("z=%e\n", z);
printf("x=%.2e\n", x);
y=3.333333e-09
return 0; z=3.333333e+03
7 } x=3.33e-05
4
Keyword: sizeof
#include <stdio.h> The letter el, not the number one.
int main(void) lu: unsigned long
{
int a;
printf("Bytes in char: %lu\n", sizeof(char));
printf("Bytes in int: %lu\n", sizeof(int));
printf("Bytes in a: %lu\n", sizeof(a));
return 0;
}
printf function: %c
#include <stdio.h>
int main(void)
{
char x = 'a';
char y = 'b';
printf("The ASCII code for %c is %d\n", x, x);
printf("The ASCII code for %c is %d\n", y, y);
y++;
printf("The ASCII code for %c is %d\n", y, y);
return 0;
}
5
Section 1.2: Fahrenheit to Celsius
1) #include <stdio.h> Output: 0.0 -17.8
2) int main() 20.0 -6.7
3) { 40 0
40.0 4
4.4
4
4) float lower = 0; 60.0 15.6
5) float upper = 100; 80.0 26.7
6) float step = 20; 100.0 37.8
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) }
11 15)}
12
6
Quiz: For Loop
a) 0.0
1) #include <stdio.h> b) 45.0
2) int main() c) 50.0
3) {
d) 60.0
4) float lower = 50;
5) float upper = 75; e) 65.0
6) float step = 15;
7) float fahr;
8)
9) for (fahr = lower; fahr <= upper; fahr=fahr+step)
10) { printf("%4.1f\n", fahr);
11) }
12) }
13
1) #include <stdio.h>
On which line will the gcc
2)
3) #define LOWER 0
compiler on
4) #define UPPER = 300 moons.cs.unm.edu
5) (without using any
6) int main() options) report an error?
7) {
8) int fahr = LOWER;
9)
a) line 3
10) while (fahr <= UPPER) b) line 8
11) { printf("%d\n", fahr); c)
) li
line 9
12) fahr = fahr + 15; d) line 10
13) } e) line 11
14) }
14
7
Count Lines of Input
1) #include <stdio.h>
line 4 could be changed to:
2) int main()
char c
3) {
4) c //used to store an 8 bit character
int c; character.
5) int numberOfLines = 0; //book uses bad name: n1.
6)
7) //1) Reads a character from stdin
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 codingg standard, however, will require
q the enclosing
g { }}.
12) if (c == '\n') ++numberOfLines;
13) }
14) printf("%d\n", numberOfLines);
15) }
xbox 72 % gcc inputLineCounter.c
xbox 73 % a.out < inputLineCounter.c
15 15
8
Count Characters, Lines and Words: 1 of 2
1) #include <stdio.h>
2)
3) #define FALSE 0
4) #define TRUE -11
5)
6) int main()
7) { int charCount = 0;
8) int lineCount = 0;
9) int wordCount = 0;
10) int insideWord = FALSE;
11) char c = g
getchar();
12) while (c != EOF)
13) { ...
14) }
15) printf("Character Count = %d\n", charCount);
16) printf("Word Count = %d\n", wordCount);
17) printf("Line Count = %d\n", lineCount);
17 18) }
9
Count Characters, Lines and Words
1) int main()
2) { Why will this
3) ...//set up variables. not work?
4) char c = 'a';
5) while (c != EOF)
6) { c = getchar();
7) charCount++;
8) if (c == '\n') lineCount++;
9) if (c == ' ' || c == '\n' || c == '\t')
10) { insideWord = FALSE;
11) }
12) else if (insideWord == FALSE)
13) { insideWord = TRUE;
14) wordCount++;
15) }
16) }
17) ... //output
19 18) }
10