# ab Week 4 ```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&lt;=20;i++)
cout &lt;&lt; “?”;
cout &lt;&lt; “\n”;
}
/* Function value */
void value()
{
float a, b, sum;
cout &lt;&lt; “Enter value of a and b: ”;
cin &gt;&gt; a&gt;&gt;b;
sum = a+b;
if (a&gt;b)
cout&lt;&lt; “Golden ration is:”&lt;&lt;sum/a;
else
cout&lt;&lt; “Golden ration is:”&lt;&lt;sum/b;
cout&lt;&lt;”\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 &lt;&lt; “Enter value for x and y:”;
cin&gt;&gt;x&gt;&gt;y;
printline(‘_’);
value(x,y);
printline(‘*’);
}
void printline(char ch);
{
int i;
for(i=1;i&lt;=20;i++)
cout &lt;&lt; ch;
cout &lt;&lt; “\n”;
}
void value(float a, float b)
{
sum = a+b;
if (a&gt;b)
cout&lt;&lt; “Golden ration is:”&lt;&lt;sum/a;
else
cout&lt;&lt; “Golden ration is:”&lt;&lt;sum/b;
cout&lt;&lt;”\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 &lt;&lt; “Enter three values :: ”;
cin &gt;&gt; x&gt;&gt;y&gt;&gt;z;
A = area(x,y,z); /* x,y z are actual parameters */
cout &lt;&lt; “Area of triangle is ” &lt;&lt; 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&lt;&lt;”Enter n”&lt;&lt;endl;
cin&gt;&gt;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&lt;m;i++)
{
for(j=0;j&lt;m;j++)
{
If(i&lt;=j)
cout&lt;&lt;“ ||”; // use any other suitable pattern
else cout&lt;&lt;” =”; // use any other suitable pattern
}//end j
cout&lt;&lt;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&lt;&lt;&quot;Enter the number of rows in pyramid of stars you wish to
see &quot;&lt;&lt;endl;
cin&gt;&gt;n;
http://www.scis.ulster.ac.uk/~siddique
Object-Oriented Programming
temp = n;
for ( row = 1 ; row &lt;= n ; row++ )
{
for ( c = 1 ; c &lt; temp ; c++ )
cout&lt;&lt;&quot; &quot;;
temp--;
for ( c = 1 ; c &lt;= 2*row - 1 ; c++ )
cout&lt;&lt;&quot;*&quot;;
cout&lt;&lt;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(&quot;%d&quot;,&amp;n);
space = n - 1;
http://www.scis.ulster.ac.uk/~siddique
Object-Oriented Programming
for ( i = 1 ; i &lt;= n ; i++ )
{
num = i;
for ( j = 1 ; j &lt;= space ; j++ )
printf(&quot; &quot;);
space--;
for ( j = 1 ; j &lt;= i ; j++ )
{
printf(&quot;%i&quot;, num);
num++;
}
num--;
num--;
for ( j = 1 ; j &lt; i ; j++)
{
printf(&quot;%d&quot;, num);
num--;
}
printf(&quot;\n&quot;);
}
}
http://www.scis.ulster.ac.uk/~siddique
```