-
The following experiment should give
the same result as the previous one. Only the logic has been rearranged.
Complete the flowchart and the program so that the output is same
as before.
#include
<stdio.h>
int main(void)
{
int i, k,
a = 0, b = 0, lower = 0;
printf("Enter the sample
input line by line:\n");
for(i = 1;
i <= 7; i = i + 1)
{
scanf_s("%d",
&k, 1);
if(k < 90)
if(k < 80)
________________________________
else
________________________________
else
____________________________________
}
printf("A's = %d\tB's
= %d\tLower = %d\n", a, b, lower);
return 0;
}

|
if(k < 90)
if(k < 80)
lower = lower + 1;
else
b = b + 1;
else
a = a + 1;


|
| |
|
-
Next, let us test the conditions for
all five grades, namely
A,
B,
C,
D and
F.
In the blank space, for each grade, place the appropriate statement
something like the following:
printf("A.\n");
Each else that is lined up under an
if is
that condition’s false side. Complete the code and the flowchart.
#include
<stdio.h>
int main(void)
{
int i, k;
printf("Enter the sample
input line by line:\n");
for(i = 1;
i <= 7; i = i + 1)
{
scanf_s("%d",
&k, 1);
if(k < 90)
if(k < 80)
if(k < 70)
if(k < 60)
____________________________
else
_____________________________
else
_____________________________
else
______________________________
else
____________________________
}
return 0;
}
|
if(k < 90)
if(k < 80)
if(k < 70)
if(k < 60)
printf("Grade F.\n");
else
printf("Grade D.\n");
else
printf("Grade C.\n");
else
printf("Grade B.\n");
else
printf("Grade
A.\n");

|
|
-
On the F (false) side of k < 90? Only A grades are selected.
On the T (true) side of that condition, which grades are selected,
A, B, C, D and/or F?
-
On the T side of k < 80?, which grades are selected?
-
On the T side of k < 70?, which grades are selected? What about
on the F side?
-
Grades that end up getting a B must go through how many decision
diamonds?
-
Grades that end up getting a D must go through how many decision
diamonds?
Ans:
-
B, C, D and F.
-
C, D and F.
-
On the True side are D and F. On the False
side, C was selected.
-
2 decision diamonds.
-
4 decision diamonds.
|
| |
|
-
The following experiment performs the
same steps as in the previous one. However, since the logic is rearranged,
the
printf()
will need to be placed at different locations. Complete the code
and the flowchart.
#include
<stdio.h>
int main(void)
{
int i, k;
printf("Enter the sample
input line by line:\n");
for(i = 1;
i <= 7; i = i + 1)
{
scanf_s("%d",
&k, 1);
if(k > 69)
if(k > 89)
________________________
else if(k
> 79)
________________________
else
__________________________
else if(k
> 59)
__________________________
else
____________________________
}
return 0;
}
|
if(k > 69)
if(k > 89)
printf("Grade A.\n");
else if(k
> 79)
printf("Grade B.\n");
else
printf("Grade C.\n");
else if(k
> 59)
printf("Grade D.\n");
else
printf("Grade F.\n");

|
|


-
F grades will go through two conditions: k > 69?, which would
be false and k > 59?, which also would be false. How many conditions
that D grades go through?
-
How many conditions that C grades go through?
-
How many conditions that A grades go through?
-
If k > 79? were changed to k <= 80?, then what changes would
be necessary in the flowchart?
-
When a grade that is read into the variable k enters this set
of nested if’s, it has a choice of going through how many different
paths?
-
The control of execution may take how many different paths at
any one time?
Ans:
-
Also 2 conditions same as F but both are
True.
-
3 conditions.
-
2 conditions.
-
The True (T) and False (F) positions need
to be exchanged for the k > 79 decision diamond.
-
5 different paths based on the path toward
the Stop.
-
At any one time it will take 2 paths based
on the True (T) and False (F) paths.
|
| |
|
-
The following experiment performs the
same steps as in the last two previous experiments. However, it
count the number of grades in each category instead of printing
them. Complete the code and the flowchart.
#include
<stdio.h>
int main(void)
{
int i, k,
a=0, b=0, c=0, d=0, f=0;
printf("Enter the sample
input line by line:\n");
for(i = 1;
i <= 7; i = i + 1)
{
// for older compiler
you can try using scanf()
scanf_s("%d",
&k, 1);
if(k <= 59)
____________________________
else if(k
<= 89)
if(k <= 69)
_______________________________
else if(k
<= 79)
_______________________________
else
_______________________________
else
_____________________________
}
printf("A's = %d\t",
a);
printf("B's
= %d\t", b);
printf("C's = %d\t",
c);
printf("D's = %d\t",
d);
printf("F's
= %d\n", f);
return 0;
}
|
if(k <= 59)
f = f + 1;
else if(k <= 89)
if(k <= 69)
d = d + 1;
else if(k
<= 79)
c = c + 1;
else
b = b + 1;
else
a = a + 1;

|
|

-------------------------------------------------------------------------------------------------

|
|
For each of these questions, choose from
among the grades of
A,
B,
C,
D
and
F.
-
Which grades(s) are selected on the
true side of
k <= 59?
-
Which grades(s) are selected on the
false side of
k <= 59?
-
Which grades(s) are selected on the
true side of
k <= 89?
-
Which grades(s) are selected on the
false side of
k <= 89?
-
Which grades(s) are selected on the
true side of
k <= 69?
-
Which grades(s) are selected on the
false side of
k <= 69?
-
Which grades(s) are selected on the
true side of
k <= 79?
-
Which grades(s) are selected on the
false side of
k <= 79?
|
-
F.
-
A,
B, C and D.
-
B,
C and D.
-
A.
-
D.
-
B
and C.
-
C.
-
B.
|
| |
|
-
Run the following program and key in
the following sample input (without the commas):
16,
21,
17,
43,
7,
52,
-1. The program
will determine the smallest number entered.
#include
<stdio.h>
int main(void)
{
int k, smallest;
printf("Enter integers,
when");
printf(" done enter
a ");
printf("negative number\n");
scanf_s("%d",
&k, 1);
// assign the first number
to smallest variable
smallest = k;
// iterate while k >=
0
for( ; k >=
0; )
{
// if the entered number
is < smallest
if(k < smallest)
// then assign the number
to smallest variable...
smallest = k;
// read the next input....repeat
scanf_s("%d",
&k, 1);
}
// print the smallest
number...
printf("The smallest
number is %d\n", smallest);
return 0;
}
-
Draw a tracechart for this experiment (left to you!).
-
What was the first value of the variable
smallest?
-
The first time that the condition in the if statement was encountered,
what were the values of k and
smallest?
-
The second time that the if condition was tested, what were
the values of k and smallest?
-
The third time that the if condition was tested, what were the
values of k and smallest?
-
During the loop, the value of smallest was changed. What were
the different values of smallest?
-
Does an if statement require a corresponding else statement?
Why?
-
Is the scanf_s() executed inside
the loop when the k < smallest
is true or false, or irrespective of it?
-
If the data entered were 11, 22, 13, 19, 16, -1, how many times
would smallest be changed?
To see
the flow of this program clearer and used for troubleshooting you
can add several line of codes as shown below.
#include <stdio.h>
int main(void)
{
int k, smallest;
printf("Enter
integers, when");
printf(" done
enter a ");
printf("negative
number\n");
scanf_s("%d",
&k, 1);
// assign the first number to
smallest variable
smallest =
k;
printf("smallest
= %d, k = %d at pos1.\n", smallest, k);
// iterate while k >= 0
for( ; k >=
0; )
{
// if the entered number
is < smallest
printf("smallest = %d, k = %d at pos2.\n", smallest, k);
if(k < smallest)
// then assign the number
to smallest variable...
smallest = k;
printf("smallest = %d, k = %d at pos3.\n", smallest, k);
// read the next input....repeat
scanf_s("%d", &k, 1);
printf("smallest = %d, k = %d at pos4.\n", smallest, k);
}
// print the smallest number...
printf("The
smallest number is %d\n", smallest);
return 0;
}
// Sample inputs: 16, 21, 17,
43, 7, 52, -1 and 11, 22, 13, 19, 16, -1
|


-
Left
for your assignment.
-
16
-
k
= 16, smallest =16.
-
k
= 21, smallest = 16.
-
k
= 17, smallest = 16.
-
16
and 7.
-
Not
really. It can be standalone in testing just a condition.
-
From
the flowchart we can see that the scanf_s() executed inside the
loop irrespective of k < smallest is True or False.
-
0
time. It is already a smallest number entered as the first input.

|