# ICS 103 – Computer Programming in C Summer Semester 2008 (073) ```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(&quot;Enter side of a cube &gt; &quot;);
scanf(&quot;%lf&quot;,&amp;side);
printf(&quot;Surface area is %.2f\n&quot;, SurfaceArea(side));
printf(&quot;Volume is %.2f&quot;,Volume(side));
return(0);
}
Sample Output:
Enter side of a cube &gt; 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(&quot;Enter 3 angles &gt; &quot;);
scanf(&quot;%d%d%d&quot;,&amp;a,&amp;b,&amp;c);
if (IsTriangle(a,b,c))
printf(&quot;Angles form a triangle&quot;);
else
printf(&quot;Angles don't form a triangle&quot;);
return(0);
}
Sample Output:
Enter 3 angles &gt; 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(&quot;Enter 3 integers &gt; &quot;);
scanf(&quot;%d%d%d&quot;,&amp;a,&amp;b,&amp;c);
Divisible(a, b, c);
return(0);
}
Sample Output:
Enter 3 integers &gt; 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(&quot;Enter a positive integer&gt;&quot;);
scanf(&quot;%d&quot;, &amp;a);
printf(&quot;\nThe list of primes less than %d&gt;\n&quot;, a);
for(int i=2; i&lt;=100; i++)
{
2
if (IsPrime(i))
printf(&quot;%d\t&quot;);
}
return(0);
}
Sample Output:
Enter a positive integer &gt; 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
```