Solution

advertisement
Tampere University of Technology
Department of Software Systems
OHJ-1106 Programming I
Exercise # 5
(02-11-2012)
(Note: Solutions should be submitted as a printed copy at the beginning of session)
Exercise 1:
i) a) Call by value: A function call in which the formal parameter receives a copy of the
contents of the corresponding actual parameter.
Call by reference: A function call in which the formal parameter receives the location
(memory address) of the caller’s actual parameter
b. Local variable: A variable declared within a block and not accessible outside of that
block.
Global variable: Any identifier declared outside of all the functions in a program.
c. Function Definition: It is a block which consists of executable statements.
Example: int func(int a) {
---------//function defination
}
Function Declaration: A function declaration consists of a return type, a name, and
a parameter list.
Example: int func(int);
ii)
a. Can a function definition appear inside the body of another function
definition?
Answer: No
b. Can a function definition contain a call to another function?
Answer: Yes
c. Can a function that returns a value have a call by reference parameter?
Yes
May a function have both call by value and a call by reference parameters?
Yes
Exercise 2:
What is the output of following code? If there is any errors modify the code? Explain
your answer?
int main() {
int a=10,b=20,c=30;
func(a,b);
cout<<a<<” ”<<b<<” ”<<c;
return 0;
}
void func(int &x, int y, int& z) {
cout<<x<<” ” <<y<<” ”<<zendl;
x=1;
y=2;
z=3;
cout<<x<<” ”<<y<<” ”<<z;
}
Answer:
void func(int &x, int y, int& z) {
cout<<x<<” ” <<y<<” ”<<zendl;
x=1;
y=2;
z=3;
cout<<x<<” ”<<y<<” ”<<z;
}
int main() {
int a=10,b=20,c=30;
func(a,b,c);
cout<<a<<” ”<<b<<” ”<<c;
return 0;
}
Output: 10 20 30
123
1 20 3
Exercise 3:
Given the declarations
const int ANGLE = 90;
char letter;
int number;
Indicate whether each of the following actual parameters would be valid using pass-byvalue, pass-by-reference, or both.
Pass-by-value
Pass-by-reference
a. Letter
*
b. ANGLE
*
c. number
*
d. number + 3
*
e. 23
*
f. ANGLE * number
*
g. abs(number)
*
*
*
Exercise 4:
What is the output of the following C++ program? Can you guess which case do the
given functions test (local scope, global scope, call by value, call by reference).
#include <iostream>
using namespace std;
int x;
void func1(int& a)
{
a = 3;
}
void func2(int b)
{
b = 4;
}
void func3()
{
int x;
x = 5;
}
void func4()
{
x = 7;
}
int main()
{
x = 15;
func1(x);
cout << x << endl;
x = 16;
func2(x);
cout << x << endl;
x = 17;
func3();
cout << x << endl;
x = 18;
func4();
cout << x << endl;
return 0;
}
Output:
3
16
17
7
Exercise 5:
The following code segment is supposed to calculate product of n natural numbers
int main() {
int n=0;
int sum =1;
while(n>0){
sum =n+sum;
n-}
cout<<sum<<endl;
}
Rewrite the code by using two functions where one function should use to calculate
product by using call by reference value and second function is used to display the
result.
Answer:
int Nsum(int & n){
int sum=1;
while(n>0){
product =n+sum;
n--;
}
return product;
}
void display(int sum){
cout<<sum<<endl;
}
int main() {
int n=0;
int sum =1;
cin>>n;
sum=Nsum(n);
display(sum);
}
Exercise 6:
i) Identify and correct the errors in following code fragments
a) int function (int n){ // void function (int n){
cout<<n;
}
int function2(int n, m){ //int m
n+=m;
function(3.2);
//function(3)
}
b) void p() { //int p()
int i;
while(true){
cout<<”enter i:”;
cin>>i;
if(i==0)
return i;
cout<<”i”<<endl;
}
}
ii) Write a program to calculate total and average of student marks in five subjects by
using functions and print the total and average in main function.
Do the above task with call by value and call by reference?
Call by value:
#include<iostream>
using namespace std;
int total(int s);
int average(int s);
int main(){
int s=5;
cout<<"Total: "<<total(s)<<endl;
cout<<"Average: "<<average(s)<<endl;
}
int total(int s){
int n=1;
int i=0;
int total =0;
while(n<=s){
cout<<"Enter mark for subject number "<<n<<": ";
cin>>i;
n++;
total+=i;
}
return total;
}
int average(int s){
int average = 0;
average = total(s)/s;
return average;
}
Call by reference:
#include<iostream>
using namespace std;
void total(int s,int & total);
void average(int s, int total, int& avg);
int main(){
int s=5,tot,avg;
total(s,tot);
average(s,tot,avg);
cout<<"Total: "<<tot <<endl;
cout<<"Average: "<<avg<<endl;
}
void total(int s, int& total ){
int n=1;
int i=0;
while(n<=s){
cout<<"Enter mark for subject number "<<n<<": ";
cin>>i;
n++;
total+=i;
}
}
void average(int s,int total,int &average){
average = total(s)/s;
}
Note: There will be a practical work after solving the above questions. The task will
be announced during the session.
Download