doc

advertisement
Lab 4
This manual covers:
i.
ii.
iii.
Functions
Programmer defined functions
Random numbers
Exercise 4.1
Write a program that prints the following rectangular on the screen:
**************
*
*
*
*
**************
i.
ii.
iii.
Call a function drawRectangular( ) without parameter which is not
returning any value.
Write the function accordingly.
Print the output on the screen.
Exercise 4.2
Write the following program to show the use of local variable main ( ) and defined
function. Compile, link and run it.
/*variable scope for powerTwo( )*/
#include <stdio.h>
float powerTwo(float); //function prototype
int main ()
{ float a, b; //local variable for main ( )
a = 3;
printf(“%1.1f to the power of two: %2.1f\n”,a,powerTwo(a));
return 0;
}
//i and j are a local variable for powerTwo function
float powerTwo(float i)
{
float j;
j = i * i;
return j;
}
Exercise 4.3
Write a program that calculates an area of a circle based on a given radius value:
i.
Write a main function. Get an input of radius from user.
ii.
Call a function circleArea ( ).
iii.
Write the function accordingly
iv.
Print the calculated area of the circle on the screen
(Hint: circle area = (22/7)*radius2)
Exercise 4.4
The power that a heater consumes can be calculated using the formula
P=vi
where p represents the power, v stands for voltage and i is the current. Write a function
that calculates and returns p when given specific values for v and i. The function should
match the following prototype:
double HeaterPower(double voltage, double current);
Write a program that demonstrates that your function works.
Exercise 4.5
Write a program that:
Ask user to input two integer values, Value1 and Value2 in main function.
Write a programmer defined function that compares the two integers and
return the maximum value to the main function.
i.
ii.
Exercise 4.6
Write, compile, build and run the following program. It shows how to generate a random
number using rand ().
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int k;
printf("\nRandom numbers:\n");
for (k=1; k<=10; k++)
{
printf("%i ",rand());
}
printf("\n");
return 0;
}
--------------------------------------------------------------------------------------------------------------Exercise 4.7
Modify the above program by asking the user to input a seed value for srand(). A
different value of seed is used to generate different random number.
Exercise 4.8
Write the following program that generates 10 different random numbers. Ask inputs
from user for the random number limits (between a and b, a<b). Compile, build and run
the program
#include<stdio.h>
#include<stdlib.h>
int main (void)
{
unsigned int seed;
int a, b, k;
int rand_int(int a, int b);
printf("enter a positive integer seed value:");
scanf("%u",&seed);
srand(seed);
printf("\nenter integer limits a:");
scanf("%i",&a);
printf("\nenter integer limits b:");
scanf("%i",&b);
printf("\nrandom numbers:\n");
for (k=1;k<=10; k++)
{
printf("%i ",rand()%(b-a+1)+a);
}
return 0;
}
Exercise 4.9
Modify the above program that generates 10 different floating point random numbers
using the following function:
((double)rand()/RAND_MAX)*(b-a)+a
Test your program with the following ranges:
i.
ii.
iii.
-1.2 to 3.5
3 to 10
12.5 to 25.3
LAB ASSIGNMENT 2
Part 1
Construct a program to compute the area of a triangle. Define a function for this calculation:
double areaT (int x1, int y1, int x2, int y2, int x3, int y3)
The program will read in 3 pairs of coordinates x,y (0  x,y  1000), and then call the function
areaT to compute and display the area of triangle (measure to 2 decimal places).
Given a triangle with point1 = x1 , y1 ; point2 = x2 , y 2 ; point3 = x3 , y3
Area of the triangle =
y 3  y1   x2  x1    y 2  y1   x3  x1 
Note: x denotes the absolute value of x
2
Sample input
0,0 1,0 0,1
2,5 16,18 12,9
65,312 54,87 34,78
12,45 89,1 7,2
Sample output
area is 0.50
area is 37.00
area is 2200.50
area is 1765.50
Part 2
Complete the program below to convert a number into words. A user-defined function (numTOword) is
given in the program. The function converts a special number into a word. Do not modify this function!
Your task is to complete the main program by calling this numTOword function, so that when the user
inputs a number (1–99), the program will display a phrase corresponding to the input number. You can
assume that the input number is between 1 and 99.
Input by user
56
3
60
99
Sample output
fifty six
three
sixty
ninety nine
Given program:
#include <stdio.h>
void numTOword (int n)
/* don't change this function! */
/* display 1 - 20, 30, 40, 50, 60, 70, 80, 90 in words */
{
switch (n) {
case 1:
printf (" one");
break;
case 2:
printf (" two");
break;
case 3:
printf (" three");
break;
case 4:
printf (" four");
break;
case 5:
printf (" five");
break;
case 6:
printf (" six");
break;
case 7:
printf (" seven");
break;
case 8:
printf (" eight");
break;
case 9:
printf (" nine");
break;
case 10:
printf (" ten");
break;
case 11:
printf (" eleven");
break;
case 12:
printf (" twelve");
break;
case 13:
printf (" thirteen");
break;
case 14:
printf (" forteen");
break;
case 15:
printf (" fifteen");
break;
case 16:
printf (" sixteen");
break;
case 17:
printf (" seventeen");
break;
case 18:
printf (" eighteen");
break;
case 19:
printf (" nineteen");
break;
case 20:
printf (" twenty");
break;
case 30:
printf (" thirty");
break;
case 40:
printf (" forty");
break;
case 50:
printf (" fifty");
break;
case 60:
printf (" sixty");
break;
case 70:
printf (" seventy");
break;
case 80:
printf (" eighty");
break;
case 90:
printf (" ninety");
break;
}
}
void main ()
/* This main function converts a number (1 - 99) into words */
{
int num;
scanf ("%d", &num);
/* Your task: complete this part, so that the program will display the
input number in words */
/* numTOword function is defined for you -- Don't change it */
}
Continue to work on the above question. Make the following changes to work with numbers from 1 to
999:
•
Change main into a function void convert99 (int n) – converts a number between 1 and 99
into words.
• Write the main program which will read a number between 1 and 999 from the user and then
convert that number into words.
Here are some sample inputs and outputs:
Input by user
Sample output
fifty six
56
ninety nine
99
three
3
one hundred and eighty four
184
six hundred
600
three hundred and nine
309
Download