Docsity

Assignment Questions - Introduction to Computer Program II | CS 1713, Assignments of Computer Science

Toggle sidebar
bg1
CS 1713, Simple C example, Tue Jan 06 1998, Page 1 of 1
runner$ cat -n example0.c
1 /* Volume of a sphere of radius r */
2 #include <stdio.h>
3 #define pi 3.14159265358979;
4 int main()
5 {
6 double r, vol;
7 /* input the radius r */
8 scanf("%lf", r);
9 /* calculate the volume */
10 vol = (4/3)*pi*r*r*r;
11 /* print radius and volume */
12 printf("Radius:%10.6f, Volume:%10.6f\n", r, vol);
13 return 0;
14 }
runner$ lint -m -u example0.c
(8) warning: variable may be used before set: r
(10) error: cannot dereference non-pointer type
statement has null effect
(10)
lint: errors in example0.c; no output created
lint: pass2 not run - errors in example0.c
runner$ cat -n example1.c
1 /* Volume of a sphere of radius r */
2 #include <stdio.h> /***************************/
3 #define pi 3.14159265358979 /* NOTE: SEMICOLON DELETED */
4 int main() /***************************/
5 {
6 double r, vol;
7 /* input the radius r */ /********************/
8 scanf("%lf", &r); /* NOTE: & INSERTED */
9 /* calculate the volume */ /********************/
10 vol = (4/3)*pi*r*r*r;
11 /* print radius and volume */
12 printf("Radius:%10.6f, Volume:%10.6f\n", r, vol);
13 return 0;
14 }
runner$ lint -m -u example1.c
function returns value which is always ignored
printf scanf
runner$ cc -o example1 example1.c
runner$ example1
1.0
Radius: 1.000000, Volume: 3.141593 (NOTE: WRONG ANSWER!)
runner$ cat example2.c
/* Volume of a sphere of radius r */
#include <stdio.h>
#define pi 3.14159265358979
int main()
{
double r, vol;
/* input the radius r */
scanf("%lf", &r);
/* calculate the volume */ /******************************/
vol = (4.0/3.0)*pi*r*r*r; /* NOTE: (4/3) NOW (4.0/3.0) */
/* print radius and volume */ /******************************/
printf("Radius:%10.6f, Volume:%10.6f\n", r, vol);
return 0;
}
runner$ example2
1.0
Radius: 1.000000, Volume: 4.188790
runner$ example2
10.0
Radius: 10.000000, Volume:4188.790205

Partial preview of the text

Download Assignment Questions - Introduction to Computer Program II | CS 1713 and more Assignments Computer Science in PDF only on Docsity!

CS 1713, Simple C example, Tue Jan 06 1998, Page 1 of 1

runner$ cat -n example0.c 1 /* Volume of a sphere of radius r / 2 #include <stdio.h> 3 #define pi 3.14159265358979; 4 int main() 5 { 6 double r, vol; 7 / input the radius r / 8 scanf("%lf", r); 9 / calculate the volume / 10 vol = (4/3)pirrr; 11 / print radius and volume */ 12 printf("Radius:%10.6f, Volume:%10.6f\n", r, vol); 13 return 0; 14 } runner$ lint -m -u example0.c (8) warning: variable may be used before set: r (10) error: cannot dereference non-pointer type

statement has null effect (10) lint: errors in example0.c; no output created lint: pass2 not run - errors in example0.c runner$ cat -n example1.c 1 /* Volume of a sphere of radius r / 2 #include <stdio.h> /***************************/ 3 #define pi 3.14159265358979 / NOTE: SEMICOLON DELETED / 4 int main() /***************************/ 5 { 6 double r, vol; 7 / input the radius r / /********************/ 8 scanf("%lf", &r); / NOTE: & INSERTED / 9 / calculate the volume / /********************/ 10 vol = (4/3)pirrr; 11 / print radius and volume / 12 printf("Radius:%10.6f, Volume:%10.6f\n", r, vol); 13 return 0; 14 } runner$ lint -m -u example1.c function returns value which is always ignored printf scanf runner$ cc -o example1 example1.c runner$ example 1. Radius: 1.000000, Volume: 3.141593 (NOTE: WRONG ANSWER!) runner$ cat example2.c / Volume of a sphere of radius r */ #include <stdio.h> #define pi 3.

int main() { double r, vol; /* input the radius r / scanf("%lf", &r); / calculate the volume / /******************************/ vol = (4.0/3.0)pirrr; / NOTE: (4/3) NOW (4.0/3.0) / / print radius and volume */ /******************************/ printf("Radius:%10.6f, Volume:%10.6f\n", r, vol); return 0; } runner$ example 1. Radius: 1.000000, Volume: 4. runner$ example 10. Radius: 10.000000, Volume:4188.