Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Princess Nora Bint Abdulrahman University
College of Computer and Information Sciences
Department of Computer Sciences
CS 240-CS141
Computer Programming 1
Sheet # 4 Function
1st Semester 1433-1434H
Question 1:
Give the intToDouble function header that takes an integer argument number, and returns a doubleprecision result.
Answer: double intToDouble(int number)
Question 2:
Find the errors in each of the following program segment, and explain how the error can be corrected:
int g()
{
cout<< "Inside Function g" << endl;
int h()
{
cout<< "Inside Function h" << endl;
}
}
Answer:
int g()
{
cout<< "Inside Function g" << endl;
}
int h()
{
cout<< "Inside Function h" << endl;
}
int sum( int n )
{
if ( n == 0 )
return 0;
else
n + sum( n-1 );
}
Answer:
int sum( int n )
{
if ( n == 0 )
return 0;
else
return (n + sum( n-1 ));
}
void f( double a );
{
float a;
Princess Nora Bint Abdulrahman University
College of Computer and Information Sciences
Department of Computer Sciences
cout << a << endl;
}
Answer:
void f( double a )
{
float a;
cout << a << endl;
}
void product()
{
int a,b,c;
int result;
cout << "Enter three integers: ";
cin >> a >> b >> c;
result = a * b * c;
cout<< "Result is " << result;
return result;
}
Answer:
void product()
{
int a,b,c;
int result;
cout << "Enter three integers: ";
cin >> a >> b >> c;
result = a * b * c;
cout<< "Result is " << result;
}
CS 240-CS141
Computer Programming 1
Sheet # 4 Function
1st Semester 1433-1434H
Princess Nora Bint Abdulrahman University
College of Computer and Information Sciences
Department of Computer Sciences
CS 240-CS141
Computer Programming 1
Sheet # 4 Function
1st Semester 1433-1434H
Question 3:
Trace the following programs, and show the output:
#include<iostream>
using namespace std;
void startStop(int,int);
int main()
{
int start=8, stop=3;
startStop(start,stop);
start=2, stop=-2;
startStop(start,stop);
return 0;
}
void startStop(int sr, int sp)
{
int n;
cout << "start on "<<sr<< endl;
for(n = (sr-1); n >= sp; n--)
{if(n==sp)
cout << "stop on " << n << endl;
else
cout << "down to " << n << endl;}
}
Answer:
n
Loop #
sp
sr
Stop
Start
3
8
3
8
7
1
2
6
5
4
3
1
2
3
4
5
1
2
3
4
0
-1
-2
-2
2
-2
Princess Nora Bint Abdulrahman University
College of Computer and Information Sciences
Department of Computer Sciences
CS 240-CS141
Computer Programming 1
Sheet # 4 Function
1st Semester 1433-1434H
#include<iostream>
using namespace std;
void one(double,int);
double two(int);
void one(double p1,int m)
{
double x;
x=two(m);
p1 = m + x;
m *=10;
cout<<"x inside function one = "<<x<<endl;
cout<<"m inside function one = "<<m<<endl;
cout<<"p1 = "<<p1<<endl;
}
double two(int n)
{
double x;
x = 2*n;
cout<<"x inside function two = "<<x<<endl;
return n;
}
int main(){
double x=0,y=4;
cout<<"function main variables' value before function one
call"<<endl;
cout<<"x = "<<x<<endl;
cout<<"y = "<<y<<endl;
cout<<"***********************"<<endl;
one(x,y);
cout<<"***********************"<<endl;
cout<<"function main variables' value after function one
call"<<endl;
cout<<"x = "<<x<<endl;
cout<<"y = "<<y<<endl;
return 0;
}
Princess Nora Bint Abdulrahman University
College of Computer and Information Sciences
Department of Computer Sciences
CS 240-CS141
Computer Programming 1
Sheet # 4 Function
1st Semester 1433-1434H
Answer:
Problems:
Problem 1:
Write a program with a function named cube which accept a number and return its cube. Then use this
function to print the cube of the numbers from 1 to 5 .
Answer:
#include<iostream>
using namespace std;
int cube( int y );
void main()
{
int x;
for ( x = 1; x <= 5; x++ )
cout<< cube( x ) << endl;
}
int cube( int y )
{
return (y * y * y);
}
Problem 2:
Princess Nora Bint Abdulrahman University
College of Computer and Information Sciences
Department of Computer Sciences
CS 240-CS141
Computer Programming 1
Sheet # 4 Function
1st Semester 1433-1434H
Write a program with a function named calculator which takes three argument, two doubles as the
operand and one character as the operator. Based on the selected operator, perform the required
operation. Operations expected are +, -, *,and /
Answer:
Problem 3:
Write a program that reads a number from the user, then uses a function isPrime that takes the number
as its parameter. The function should determine if the number passed to it is prime or not. If the
number is prime the function should return true, if the number is not prime the function should return
false.
Princess Nora Bint Abdulrahman University
College of Computer and Information Sciences
Department of Computer Sciences
CS 240-CS141
Computer Programming 1
Sheet # 4 Function
1st Semester 1433-1434H
Problem 4:
Write a function that computes the sum of digits of a given integer n.
Note that n is passed as a parameter.