0% found this document useful (0 votes)
9 views1 page

Exercise 4

Uploaded by

Aniq Azman
Copyright
© © All Rights Reserved
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)
9 views1 page

Exercise 4

Uploaded by

Aniq Azman
Copyright
© © All Rights Reserved
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/ 1

Exercises Chapter 5(Programming Errors) 3.

Identify the errors and rewrite the correct program

1. Find the error in each of the following:


1 #include <math.h>
a) for(x=100,x>=1,x++) 2 main(){
printf(“%d\n”,x); 3 int i,j,k;
4 double sum
b) The following code should input an integer and a 5 k=0;i=2;
character and print them. Assume the user types 6 for(i=1;i<10;i--){
as input 100 A. 7 J+=k-i;
8 sum+=sqrt(j);
scanf(“%d”,&intval); 9 }
charval=getchar(); 10
printf(“integer: %f\ncharacter: 11 printf(“sum=%d”,sum);
%c\n”,&intval,&charval); 12 }

c) The following code should print whether a given


is odd or even: 4. Identify the errors and rewrite the correct program

switch(value%2){ 1 #include <stdio.h>


case 0: 2 #define size 10;
printf(“even integer\n”); 3 main(){
case 1: 4 int i,j,k;
printf(“odd integer\n”); 5 double x[10];
6 for(i=0,i<=10,i++){
d) for(x=.000001;x==.0001;x+=.000001){ 7 Scanf(“%d”,&x[i]);
printf(“%.7f\n”,x); 8 Y+=x[i];
} 9
10 }
e) The following code should output the even 11 printf(“sum=%d”,Y);
integers from 2 to 100: 12 }

counter =2;
Do{ 5. Identify the errors and rewrite the correct program
if(counter%2==0){
printf(“%d\n”,counter); 1 #include <stdio.h>
} 2
counter+=2; 3 main(){
}While(counter<100); 4 int x,y,z;
5 x=val(1,4);
2. Identify the errors and rewrite the correct program 6 y=val(10,x);
7 z=val(y,15);
1 #include <math.h> 8 printf(“x=%d\ny=%d\nz=%d”,x,y);
2 main(){ 9 }
3 int i,j,k; 10
4 double sum 11 int val(int a, int b){
5 k=0;i=2; 12 int i, func=0;
6 do{ 13 for(i=0,i<=10,i++);
7 j=k-1; 14 func+=i;
8 sum+=abs(j); 15 Return(func);
9 i++; 16 }
10 while(i!=0)
11 printf(“sum=%f”,sum);
12 }

NBS sem 2 2017/2018

You might also like