3.11
Identify and correct the errors in each of the following. [Note: There may be more than one
error in each piece of code.]
a)
if ( age >= 65 );
printf( "Age is greater than or equal to 65\n" );
else
printf( "Age is less than 65\n" );
b)
int x = 1, total;
while ( x <= 10 ) {
total += x;
++x;
}
c)
While ( x <= 100 )
total += x;
++x;
d)
while ( y > 0 ) {
printf( "%d\n", y );
++y;
}
Jawaban:
a) Simbol ‘;’ di akhir perintah if
tidak diperlukan.
Koreksi:
if ( age >= 65 )
printf( "Age is greater than or equal to 65\n" );
else
printf( "Age is less than 65\n" );
b) variabel ‘total’ tidak memiliki nilai awal sebelum
dijumlahkan dengan variabel x.
Koreksi:
int x = 1, total;
total = 0;
while ( x <= 10 ) {
total += x;
++x;
}
c) fungsi while yang tidak diawali dan
ditutup dengan tanda kurung kurawal { } hanya akan mengambil 1 instruksi
dibawahnya saat kondisi terpenuhi. Jadi, while hanya akan menjalankan proses ‘total +=
x;’, kemudian mengulang kembali proses tanpa menjumlahkan nilai x. Hal ini
akan menyebabkan pengulangan yang tak terbatas.
Koreksi:
while ( x <= 100 ) {
total += x;
++x;
}
d) Dengan kondisi ‘( y > 0 )’, proses ‘++y;’ tidak akan pernah bisa membuat kondisi while
bernilai False. Dengan kata lain, bila proses dalam while dapat dijalankan
sekali, maka proses dalam while akan dijalankan berulang kali tanpa batas.
Koreksi:
while ( y > 0 ) {
printf( "%d\n", y );
--y;
}
3.13
Jawaban:
1
4
9
16
25
36
49
64
81
100
3.14
Write a single pseudocode statement that indicates each of the following:
a) Display the message "Enter two numbers".
b) Assign the sum of variables x, y, and z to variable p.
c) The following condition is to be tested in an if…else selection statement: The current
value of variable m is greater than twice the current value of variable v.
d) Obtain values for variables s, r, and t from the keyboard.
Jawaban:
a) print
“Enter two numbers”
b) p=x+y+z
c) if (m>2*v)
d) scan s
scan r
scan t
3.17
#include<stdio.h>
main()
{
float total, tank, gused, miles, mpg,
overall;
total=0;
tank=0;
printf ("Enter the gallons used (-1 to
end): ");
scanf
("%f",&gused);
while (gused!=-1) {
printf ("Enter the miles driven: ");
scanf
("%f",&miles);
mpg=miles/gused;
printf ("The miles / gallon for this tank
was %f\n",mpg);
total=total+mpg;
tank=tank+1;
printf ("Enter the gallons used (-1 to
end): ");
scanf
("%f",&gused);
}
overall=total/tank;
printf ("The overall average miles/gallon
was %f",overall);
getch ();
return 0;
}
3.18
Jawaban:
#include<stdio.h>
main()
{
int acc;
float bb, tch, tcr, limit, finb;
printf ("Enter
account number (-1 to end):");
scanf ("%d",&acc);
while (acc!=-1) {
printf("Enter
beginning balance:");
scanf("%f",&bb);
printf("Enter
total charges:");
scanf("%f",&tch);
printf("Enter
total credits:");
scanf("%f",&tcr);
printf("Enter
credit limit:");
scanf("%f",&cl);
finb=bb+tch-tcr;
if(finb>cl) {
printf("Account:%d\n",acc);
printf("Credit
limit:%f\n",cl);
printf("Balance:%f\n",finb);
printf("Credit Limit
Exceeded.\n");
}
printf("Enter
account number (-1 to end):");
scanf("%d",&acc);
}
getch();
return 0;
}
Tidak ada komentar:
Posting Komentar