Object-Oriented Programming Object-Oriented Programming (COM357) Lab Week 4 Exercise 1: Functions with no parameters and no return values We learned three types of function. Te first type is a function with no parameter and no return value. There are two functions printline() and value() defined below and called from the main. void printline(); void value(); void main() { printline(); value(); printline(); } /* Function printline */ void printline() { int i; for(i=1;i<=20;i++) cout << “?”; cout << “\n”; } /* Function value */ void value() { float a, b, sum; cout << “Enter value of a and b: ”; cin >> a>>b; sum = a+b; if (a>b) cout<< “Golden ration is:”<<sum/a; else cout<< “Golden ration is:”<<sum/b; cout<<”\n”; } http://www.scis.ulster.ac.uk/~siddique Object-Oriented Programming Exercise 2: Functions with parameters and no return values Convert the functions in Exercise 1 into functions with parameters and no return value. Also show how you change the function to return the value. /* program with parameters and no return values*/ void printline(char); void value(float, float); void main() { float x, y; cout << “Enter value for x and y:”; cin>>x>>y; printline(‘_’); value(x,y); printline(‘*’); } void printline(char ch); { int i; for(i=1;i<=20;i++) cout << ch; cout << “\n”; } void value(float a, float b) { sum = a+b; if (a>b) cout<< “Golden ration is:”<<sum/a; else cout<< “Golden ration is:”<<sum/b; cout<<”\n”; } http://www.scis.ulster.ac.uk/~siddique Object-Oriented Programming Exercise 3: Function with parameter and return values 1 1 s * s a s b s c , where s a b c , Area of triangle A 4 2 {a, b, c} are the lengths of the sides of the triangle. Write a function with minimum set of parameters which calculates the area of a triangle and returns it. Explain the formal parameters and the actual parameters. Test your program with different set of values. float area(float, float, float); void main() { float x, y, z, A; cout << “Enter three values :: ”; cin >> x>>y>>z; A = area(x,y,z); /* x,y z are actual parameters */ cout << “Area of triangle is ” << A; } float area(float a, float b, float c) { float s=(a+b+c)/2; double d=((s-a)*(s-b)*(s-c)); return (sqrt(d)/4); } Exercise 4: Pattern generation using parametric function A function with no parameter is defined below. Convert the function with two parameters to control the size of the pattern and call the function in main with actual parameters. void main() { int n; cout<<”Enter n”<<endl; cin>>n; dotpattern(); //call function with actual parameter } http://www.scis.ulster.ac.uk/~siddique Object-Oriented Programming The dotpattern should be a square pattern of dimension mxm. Parameterise the function dotpattern() and call it from the main function with appropriate actual parameter. void dotpattern() //parameterise function here { int i,j; for(i=0;i<m;i++) { for(j=0;j<m;j++) { If(i<=j) cout<<“ ||”; // use any other suitable pattern else cout<<” =”; // use any other suitable pattern }//end j cout<<endl; }//end i }//end func Exercise 5: This exercise is for super-mouse group. Given below is a Pascal triangle. Generate different Pascal triangle, e.g. upside down, using the source code given below. A function with no parameter is defined below. Convert the function with two parameters to control the size of the pattern and call the function in main with actual parameters. Generate the pattern below using a parametric function and call it in the main program. * *** ***** ******* ********* void pattern() { int row, c, n, temp; cout<<"Enter the number of rows in pyramid of stars you wish to see "<<endl; cin>>n; http://www.scis.ulster.ac.uk/~siddique Object-Oriented Programming temp = n; for ( row = 1 ; row <= n ; row++ ) { for ( c = 1 ; c < temp ; c++ ) cout<<" "; temp--; for ( c = 1 ; c <= 2*row - 1 ; c++ ) cout<<"*"; cout<<endl; } } Write the programs in Exercise 3 and 4, compile, debug and run the program. Once the program is running, show it to module coordinator/demonstrator for records. Save the program in folder on your U drive. You will need the programs for the open book lab assessment in week 11. Pattern A pattern is a discernible regularity in the world or in a manmade design. As such, the elements of a pattern repeat in a predictable manner. A geometric pattern is a kind of pattern formed of geometric shapes and typically repeating like wallpaper. Patterns are seen in the nature. For example: http://www.scis.ulster.ac.uk/~siddique Object-Oriented Programming Human always try to imitate nature and generate patterns and fractals. For example 1 * *** ***** ******* ********* http://www.scis.ulster.ac.uk/~siddique 232 34543 4567654 567898765 Object-Oriented Programming Patterns are every where. A fractal is an object or quantity that displays self-similarity, in a somewhat technical sense, on all scales. A great example of how fractals can be constructed with just a few terms is favorite fractal, the Mandelbrot Set. Named for its discoverer, the previously mentioned mathematician Benoit Mandelbrot, the Mandelbrot Set describes a fantastical shape that displays amazing self-similarity no matter what scale it is looked at and can be rendered with this simple equation: zn+1 = zn2 + c This exercise is for fun for all others: Generate the number pattern (also called Pascal triangle). 1 232 34543 4567654 567898765 void main() { int n, j, i, num = 1, space; //i= row and j=column scanf("%d",&n); space = n - 1; http://www.scis.ulster.ac.uk/~siddique Object-Oriented Programming for ( i = 1 ; i <= n ; i++ ) { num = i; for ( j = 1 ; j <= space ; j++ ) printf(" "); space--; for ( j = 1 ; j <= i ; j++ ) { printf("%i", num); num++; } num--; num--; for ( j = 1 ; j < i ; j++) { printf("%d", num); num--; } printf("\n"); } } http://www.scis.ulster.ac.uk/~siddique