Академический Документы
Профессиональный Документы
Культура Документы
Computing
Loops
Control of flow
• We learned that default flow of Read b and c
instructions is sequential.
a = b+c
Display a
Read age
a group of instructions.
F
T
Read score of the student
Display sum
F
int_expr
final_stat(s)
T
stat_block
int i;
scanf("%d", &no_stu);
for (sum=i=0; i<no_stu; i++)
{
What is wrong here?
scanf("%d", &score);
sum += score;
What else?
}
What if "no_stu" is zero?
avg = sum/no_stu;
for (i=1; i<=5; ++i) for (; i<5; i++) for (i=0;; i++)
printf("%d;", i); printf("%d;", i); printf("%d;", i);
printf("{%d}",i); printf("{%d}",i); printf("{%d}",i);
Starts from anything ...;3;4;{5}
1;2;3;4;5;{6} OR MAYBE SOMETHING LIKE {795} 0;1;2;...∞
{8} 1;3;5;{6}
scanf("%d",&number);
for (i = 1; i <= number / 2 ; i++)
if (number % i == 0)
sum += i;
if (number == sum)
printf("%d is a perfect number",number);
else
printf("%d is not a perfect number",number);
F
int_expr
stat_block
scanf("%d", &n);
while (n>=0)
{
sum += n;
count++;
scanf("%d", &n);
}
avg = (count)?(float)sum/count:0;
Spring 2008 CMPE 150 – Introduction to Computing 20
while: Example 2
Consider a type of cell that reproduces by mitosis. Assume each
cell divides into two cells every second. Display the number of
cells after each second for 100 seconds. Start with one cell.
while (t<=100)
{
printf("t=%d n=%d\n", t, n);
n *= 2;
t++;
}
Spring 2008 CMPE 150 – Introduction to Computing 21
while: Example 3
Assume the user enters a sequence of 1s and
0s. Any other character marks the end of
input. Take 1's complement of the input.
char ch;
ch=getchar();
while ((ch=='0') || (ch=='1'))
{
printf("%d", !(ch-'0'));
ch=getchar();
}
Spring 2008 CMPE 150 – Introduction to Computing 22
while: Example 4
What is the output of the following code
segment?
int i;
while (++i<10);
printf("%d;", i-=2);
printf("{%d}", i);
int_expr
do
{
scanf("%d", &n);
if (n>=0)
{
sum += n;
count++;
}
} while (n>=0);
avg = (count)?(float)sum/count:0;
do
{
scanf("%d", &n);
sum += n;
printf("Do you want to exit? (y/n) ");
ch=getchar();
} while ((ch!='y')&& (ch!='Y'));
int main()
{
int line, i, j;
while (1)
{
do {
ch=getchar();
} while ((ch!=' ') &&((ch<'0')||('9'<ch));
if (ch==' ')
break;
number = (number*10)+(ch-'0');
}
Spring 2008 CMPE 150 – Introduction to Computing 34
Example
• Read an integer and print its digits in reverse order.
#include <stdio.h>
int main()
{ int num, digit;
scanf("%d",&num);
while (num)
{ digit=num%10;
num /= 10;
printf("%d", digit);
}
return 0;
}
Example
• Read a float. Exchange the decimal and whole parts. Eg: If the
input is 35.794678, the output should be 794678.35
#include <stdio.h>
int main()
{ float num, dec, whole;
scanf("%f",&num);
You think this should work, but
whole = (int) num;
dec = num-whole; it won't!!!
while (whole>1) The problem is due to the
whole /= 10; representation of floating point
while (dec != (int)dec)
numbers. Try debugging it.
dec *= 10;
num = dec+whole;
printf("%f", num);
return 0;
}
Example
• Solve the same question by considering only the first four digits after the
decimal point to be significant. Eg: If the input is 35.794678, the output
should be 7946.35.
#include <stdio.h>
int main()
{ float num, dec, whole;
int i;
scanf("%f",&num);
whole = (int) num;
dec = num-whole;
while (whole>1)
whole /= 10;
for (i=0; i<4; i++)
dec *= 10;
num = (int)dec+whole;
printf("%f", num);
getchar();
return 0;
}
You think your compiler's error messages are not
clear enough?
• Selected error messages from Apple´s MPW C compiler
(Source: http://www.ralentz.com/old/mac/humor/mpw-c-errors.html)
– "String literal too long (I let you have 512 characters, that's 3 more than ANSI said I
should)"
– "...And the lord said, `lo, there shall only be case or default labels inside a switch
statement'"
– "a typedef name was a complete surprise to me at this point in your program"
– "type in (cast) must be scalar; ANSI 3.3.4; page 39, lines 10-11 (I know you don't care,
I'm just trying to annoy you)"
– "The target of this goto is a label in a block that has an automatic variable with an
initializer"
– "This label is the target of a goto from outside of the block containing this label AND
this block has an automatic variable with an initializer AND your window wasn't wide
enough to read this whole error message"
– "Call me paranoid but finding `/*' inside this comment makes me suspicious"
– "This function has an explicit return type and deserves a return value"
– "Too many errors on one line (make fewer)"
– "Trailing comma not permitted in enum definition. (This time I'm letting you off with a
warning)"