0% found this document useful (0 votes)
66 views

CS 241 Data Organization Using C Data Organization Using C: Variables, Printf and Simple Loops

This document provides information about the CS 241 Data Organization using C course, including instructor contact information, reading assignments, and sample C code examples and quizzes about basic syntax, variables, loops, and formatting output using printf. The last code example counts the number of lines in input by reading characters from standard input and incrementing a counter when a newline is detected.

Uploaded by

vishnu_c_gprec
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

CS 241 Data Organization Using C Data Organization Using C: Variables, Printf and Simple Loops

This document provides information about the CS 241 Data Organization using C course, including instructor contact information, reading assignments, and sample C code examples and quizzes about basic syntax, variables, loops, and formatting output using printf. The last code example counts the number of lines in input by reading characters from standard input and incrementing a counter when a newline is detected.

Uploaded by

vishnu_c_gprec
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

CS 241

Data Organization using C


Variables, printf and Simple Loops

Instructor: Joel Castellanos


e-mail: [email protected]
@
Web: http://cs.unm.edu/~joel/
Office: Farris Engineering Center 319
Torin Adamson: [email protected]
John Donahue: [email protected]

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

„ 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
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.

Significant figures are not the same as


decimal places. Ms. Significant
float x = 1.0/30000.0; Figure
float z = 10000.0/3.0;
printf("x=%.7f\n", x);
printf("x=%
printf( x %.11f\n
11f\n",
, x);
printf("x=%.15f\n", x);
printf("z=%f\n", z); x=0.0000333
x=0.00003333333
Output: x=0.000033333333704
6 z=3333.333252

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

Casting int to float


#include <stdio.h>
int main()
{ Integer division, then
int a = 2; cast to float.
int b = 3;
cast to float, then
float c = a/b; floating point division.
float x = (float)a/(float)b;

printf("a=%d b=%d c=%f x=%f\n", a, b, c, x);


printf("a=%3d
printf( a=%3d b=%3d c=%3
c=%3.1f
1f x=%3
x=%3.1f\n
1f\n", a
a, b
b, c
c, x);
printf("a=%3d b=%3d c=%3.0f x=%3.0f\n", a, b, c, x);
return 0;
}
Output: a=2 b=3 c=0.000000 x=0.666667
a= 2 b= 3 c= 0 x= 1
8

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;
}

Output: Bytes in char: 1


Bytes in int: 4
Bytes in a: 4

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;
}

Output: The ASCII code for a is 97


The ASCII code for b is 98
The ASCII code for c is 99
10

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)}

Quiz: While Loop


1) #include <stdio.h> What is the last number
2) int main()
printed by the given C
3) {
4) float lower = 0; program?
5) float upper = 75;
6) float step = 20; a) 0.0
7) b) 60.0
8) float fahr = lower; c) 70.0
9) while (fahr <= upper)
10) { printf("%4.1f\n", fahr); d) 75.0
11) fahr = fahr + step; e) 80.0
.
12) }
13) }

12

6
Quiz: For Loop

What is the last number printed


by the given C program?

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

Quiz: Find the Syntax Error

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

Count Characters, Lines and Words: 1.5.4


1) #include <stdio.h>
2) #define IN 1 /* inside a word */ Coding style used in textbook:
3) #define OUT 0 /* outside a word */ a) Variable names too short.
4)
) int main()
()
5) { int c, n1, nw, nc, state; b) Multiple actions in one line.
6) state = OUT; c) Leaves out { } when body of
7) n1 = nw = nc = 0; block is only one statement.
8) while (( c = getchar()) != EOF) {
9) ++nc; d) Terse output.
10) if (c == '\n')
11) ++n1;
12) if (c == ' ' || c == '\n' || c == '\t‘)
13) state = OUT;
14) else if (state == OUT) {
15) state = IN; e) { at end of line.
16) ++nw; f) Indent of 5 spaces.
17) }
18) }
19) printf("%d %d %d\n", n1, nw, nc);
16
20) }

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) }

Count Characters, Lines and Words: 2 of 2


1) int main()
Why is newline
2) {
twice checked?
3) ...//set up variables.
4) char c = getchar();
5) while (c != EOF)
6) { charCount++;
7) if (c == '\n') lineCount++;
8) if (c == ' ' || c == '\n' || c == '\t')
9) { insideWord = FALSE;
10) }
11) else if (insideWord == FALSE)
12) { insideWord = TRUE;
13) wordCount++;
14) }
15) c = getchar();
16) }
17) ... //output
18 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

You might also like