ICS 103 – Computer Programming in C Summer Semester 2008 (073)

advertisement
ICS 103 – Computer Programming in C
Summer Semester 2008 (073)
Lab # 7 (Function with Input Parameters)
Objective:
Learn how to write user defined functions.
Scope:
The student should understand the following concepts by the end of this lab:
1.
Functions returning one or more results
2.
void functions with input parameters.
Discussion:
The following problem will be discussed in the class:
Complete the following program by writing the two functions SurfaceArea and Volume that
take a side of a cube and return its surface area and volume respectively. Run the program and test
it. [Given that Surface Area = 6a2, Volume = a3]
int main(void) {
double side;
printf("Enter side of a cube > ");
scanf("%lf",&side);
printf("Surface area is %.2f\n", SurfaceArea(side));
printf("Volume is %.2f",Volume(side));
return(0);
}
Sample Output:
Enter side of a cube > 5.2
Surface area is 162.24
Volume is 140.61
Exercises #1:
1.
Open a new file and write the following data into it:
3 8 20
2.
Save the file into your working directory as sides.dat.
3.
Write a program that reads those three integer numbers from sides.dat file as sides of three
cubes. The program then calculates their surface area and volumes and prints the result in
cubes.dat file.
1
Exercise # 2:
Complete the following program by writing a logical function IsTriangle that checks whether
three given integer angles (in degrees) form a triangle or not (i.e. their sum is 180 or not). Run the
program and test it.
int main(void){
int a,b,c;
printf("Enter 3 angles > ");
scanf("%d%d%d",&a,&b,&c);
if (IsTriangle(a,b,c))
printf("Angles form a triangle");
else
printf("Angles don't form a triangle");
return(0);
}
Sample Output:
Enter 3 angles > 75 60 45
Angles form a triangle.
Exercise #3:
Write a void function having three parameters, a, b, and c that will display all integers divisible by c
between a and b. Then complete the following program.
int main(void){
int a,b,c;
printf("Enter 3 integers > ");
scanf("%d%d%d",&a,&b,&c);
Divisible(a, b, c);
return(0);
}
Sample Output:
Enter 3 integers > 75 100 7
77 84 91 98 are divisible by 7.
HW # 5
Due: next lab session
Complete the following program by writing a logical function IsPrime that checks whether or not
a given integer is a prime. A prime is an integer divisible only by itself and 1. The minimum prime
is 2.
int main(void){
int a;
printf("Enter a positive integer>");
scanf("%d", &a);
printf("\nThe list of primes less than %d>\n", a);
for(int i=2; i<=100; i++)
{
2
if (IsPrime(i))
printf("%d\t");
}
return(0);
}
Sample Output:
Enter a positive integer > 15
The list of primes less than 15
2 3 5 7 11 13
Submission Guidelines:
1. The homework must be submitted at the beginning of the next lab session in both soft and hard
copies.
2. Late homework is not accepted.
3. The homework must be solved individually. Any kind of cheating will result in F grade for all
parties involved.
3
Download