Excer 6

advertisement
Excersies Chapters 6
In Programming 2 Course
College:…Science in az Zulfi………
Program: CSI Dept.
Course Name: Artificial Intelligence
Course Code: CSI 331
Section:
Date: 28-12-1436
Number of pages: 4
The student's name:
University ID:
1
Chapter (6) : Functions
2. (Practice)
a. Write a function named check() that has three parameters. The first parameter should
accept an integer number, and the second and third parameters should accept a doubleprecision number. The function body should just display the values of data passed to
the function when it’s called. (Note: When tracing errors in functions, having the
function display values it has been passed is helpful. Quite often, the error isn’t in what
the function body does with data, but in the data received and stored.)
Answer: (a)
void Check( int x , double y , double z)
{
cout << "X="<< x <<"\t"<<
"Y="<< y <<"\t"<<
"Z="<< z << endl;
}
b. Include the function written in Exercise 2a in a working program. Make sure your
function is called from main(). Test the function by passing various data to it.
Answer: (b)
int main( )
{ int a=5;
Double b=14.5 , c=78.9;
Check(a,b,c) ;
return 0;
}
2
4. (Practice)
a. Write a function called mult() that accepts two double-precision numbers as
parameters, multiplies these two numbers, and displays the result.
Answer (a)
void mult( double a , double b )
{
cout<< a << " * "<< b << "= "<< a*b << endl;
}
b. Include the function written in Exercise 4a in a working program. Make sure your
function is called from main(). Test the function by passing various data to it.
Answer (b)
int main( )
{ Double a=14.5 , b=78.9;
mult(a,b) ;
mult(10.55, 20.8);
mult(1,2);
return 0;
}
7. (Practice) a. Write a function that produces a table of the numbers from 1 to 10, their
squares, and their cubes.
Answer (a)
void Table( void )
{
Cout<<"------------------------------------------------------------------------"<<endl;
cout << " x <<"| \t\t "<< x2 <<"| \t\t"<< x3 "<<endl;
Cout<<"------------------------------------------------------------------------"<<endl;
for( int i=1 ; i< 10 ; i++)
cout<< i << "| \t\t "<< i*i << "| \t\t "<< i*i*i << endl;
}
3
b. Include the function written in Exercise 7a in a working program. Make sure your
function is called from main(). Test the function by passing various data to it.
Answer (b)
int main( )
{
Table();
return 0;
}
9. (Program) a. The time in hours, minutes, and seconds is to be passed to a function
named totsec(). Write totsec() to accept these values, determine the total number of
seconds in the passed data, and display the calculated value.
void Totsec( int hours , int minutes , int seconds )
{
int total ;
total = 3600 * hours + 60*minutes + seconds
cout<<"total time in seconds = "<< total << endl;
}
b. Include the totsec() function written for Exercise 9a in a working program. The
main() function should correctly call totsec() and display the value the function returns.
Use the following test data to verify your program’s operation: hours = 10, minutes =
36, and seconds = 54. Make sure to do a hand calculation to verify the result your
program displays.
int main( )
{
int hours = 10, minutes = 36, seconds = 54 ;
Totsec( hours , minutes , seconds );
return 0;
}
4
10. (Program) a. The volume, V, of a sphere is given by this formula, where r is the
sphere’s radius:
4 𝜋 𝑟3
Volume =
3
Using this formula, write, compile, and run a C++ function named spherevol() that
accepts a sphere’s radius and then calculates and displays its volume.
Answer (a)
void Spherevol( float r )
{
const float pi=3.1415l
float v= 4*r*r*r *pi / 3 ;
cout<<"Volume of Sphere ="<< v<<endl;
}
b. Include the function written in Exercise 10a in a working program. Make sure your
function is called from main(). Test the function by passing various data to it.
int main( )
{
float rad=5.6;
Spherevol( rad );
Spherevol( 10 );
Spherevol( 12.7 );
return 0;
}
5
Download