next up previous
Next: Practical Problems Up: Overview of C++ Previous: Calling Functions with Arrays

Activity

1.
Without using global variables, what is the proper way to get information into a function from the caller?

2.
Without using global variables, what are the proper two ways to get information out of a function to the caller?

3.
What is produced by the following program?
    #include <iostream.h>
    
    //add the prototypes of functions
    
    void three(void) { cout << "3\n"; }
    void two(void) { cout << "2 "; three(); }
    void one(void) { cout << "1 "; two(); }
    void main(void) { cout << "Counting: "; one(); }

4.
What is produced by the following program?
    #include <iostream.h>
    // add the prototypes of functions
    
    void John(void) { cout << "John "; Jack(); }
    void Jane(void) { cout << "Jane " << endl; }
    void Jill(void) { cout << "Jill "; John(); }
    void Jack(void) { cout << "Jack "; Jane(); }
    void main(void) { cout << "Hi, "; Jill(); cout << "!";}

5.
What happens if you call the following function:
    void hi(void) {
      cout << "Hello\n" ;
      hi();
      }




6.
What is produced by:
    #include <iostream.h>
    void set(void) {
      int a;
      a=1;
      }
    void main(void) {
      int a;
      a=0;
      set();
      cout << a;
      }

7.
What is produced by:
    #include <iostream.h>
    int a;
    void set(void) {
      a=1;
      }
    void main(void) {
      a=0;
      set();
      cout << a;
      }

8.
Is the following code legal in C++? What does it produce?
    #include <iostream.h>
    void out(int houses, int horses) {
     cout << houses << " " << horses;
      }
    void main(void) {
      int boats=10, sheep=2;
      out(boats, sheep);
      }


9.
What is produced by the following program?
    #include <iostream.h>
    void show(float a) {
      cout << a << endl;
      }
    void main(void) {
      float a=1, b=2;
      show(b);
      }


10.
What is produced by:
    #include <iostream.h>              
    void out(int a, int b) {           
      cout <<  a << " " << b << endl; 
      }                                  
    void main(void) {                  
      int a=1, b=2;                   
      out(b,a);                       
      }

11.
What is produced by:
    #include <iostream.h>
    int out(int a, int b) {
      cout << a << " " << b << " "; 
      return a+b;
      }
    void main(void) {
      cout << "(" << out(1,2) << ")" << endl;
      }

12.
What is produced by:
    #include <iostream.h>
    int fun(int i) {
      if (i==1) return 1;
      else return i*fun(i-1);
      }
    void main(void) {
      cout << fun(1) << " " << fun(2) << " " << fun(3) << " "
           << fun(4) << " " << fun(5) << endl;
      }


13.
What does the following code produce:
    #include <iostream.h>
    int ratio(int a, int b) {
      return a/b;
      }
    void main(void) {
      int a=1, b=2;
      cout << ratio(a,b) << " " << ratio(b,a);
      }

14.
What happens for:
    #include <iostream.h>
    void out(int i) {
      cout << i << endl;
      }
    void main(void) {
      out(1.0);
      }




15.
What is produced by:
    #include <iostream.h>
    void swap(int a, int b) {
      int tmp;
      tmp=a;
      a=b;
      b=tmp;
      }
    void main(void) {
      int a=1, b=2;
      cout << a << " " << b << endl;
      swap(a,b);
      cout << a << " " << b << endl;
      }

16.
What is produced by:
    #include <iostream.h>
    void swap(int &a, int &b) {
      int tmp;
      tmp=a;
      a=b;
      b=tmp;
      }
    void main(void) {
      int a=1, b=2;
      cout << a << " " << b << endl;
      swap(a,b);
      cout << a << " " << b << endl;
      }
17.
Correct all of the syntax errors in the following function definition. Do not change any lines that do not contain syntax errors.
	void f(int x, int y, double& z);
	{
	int X=y+z;
	x/=2;
	int X=4;
	z-=5;
	return x+z+1;
	}
18.
Assume that the following function prototype have been given:

	
		void fact1(int x);
		int fact2(int z);
		void fact3();
		int fact4();
		double fact5(int x, int y, int z);

which of the following are legal statements? for the ones that are not indicate why they are not legal

(a)
fact1(25);
(b)
fact2(35);
(c)
int x=fact4;
(d)
int y=fact3(3)+fact4();
(e)
cout<<fact1(5);
(f)
fact1(int 7);
(g)
fact1(7,8);
(h)
double z=fact5(3,4,5);
(i)
cout <<fact5(fact(3),2,3);
(j)
cout <<fact5(3,4,fact5(3,4,5));

19.
Assume that the following function definition has been given:
	double f(int x, double y)
	{ 
	y=x-1;
	x=x%3;
	return x+y;
	}
What output is generated by each of the following code fragments.

(a)
	double val=2.5;
	int num=13;
	double res=f(num,val);
	cout << val<<' '<<num<<' '<< res << endl;
(b)
	int x=12;
	double y=10.3;
	cout << x<<' ' << y <<' '<<f(x,y) << endl;
(c)
	int y=10;
	double x=2.6;
	cout << x<<' '<<y <<' '<<f(y,x)<<endl;
	cout << x<<' '<<y <<' '<<f(x,y)<<endl;

20.
Fill up the missing statements (get help from the assistants)

/* This program initialize a one dimensional array then sort to order the array Several bugs are introduced in the program */ #include <iostream.h>
void displ(); // function to display the array
main();
{

int num[7]=5,8,1,2,4,24,19
int a, b, t;
int size,

size=7;
disp(); // Display the array

// This portion of the program will sort the array

for(a=1; a<size;a++)
for(b=size-1;b>=a;b -)
if(num[b-1] >num[b]){

t=num[b-1];
num[b-1]=num[b];
num[b]=t;
}
}
//display sorted array
cout << "Sorted array is ";
for(t=0;t<size;t++)
cout << num[t] << ' ';
return 0; }

21.
Modify the previous program to include two functions

22.
Use functions to write a program that takes the x and y coordinates of two points and returns the distance between them. Then write another function that calculates the distance between any point (x1,y1) and the line whose equation is

Ax+By+C=0

ihint The distance equation between any point (x1,y1) and the line is given by

\begin{displaymath}
distance=\frac{Ax_{1}+By_{1}+C}{\sqrt{A^{2}+B^{2}}}\end{displaymath}


next up previous
Next: Practical Problems Up: Overview of C++ Previous: Calling Functions with Arrays
Yousef Haik
2/23/1998