Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

COS10029 – Fundamentals of Programming

Week 6  Lab exercises (Submit all tasks).

Task 6.1

This task is to demonstrate your knowledge in creating a function to do a certain task and return a result.

Submission Requirement

1.   Source code demonstrating the creation and use of the function.

2.   Screenshot of the terminal which shows the output.

Tips

Functions are generally used to calculate a value and return that value (result). The following program calculates the area of a rectangle (length*width).

#include <iostream>

using namespace std;

int main()

{

double width, height, rect_area;

cout << "Enter the width of the rectangle: ";

cin >> width;

cout << "Enter the height of the rectangle: ";

cin >> height;

rect_area=width * height;

cout << "The area of the rectangle is: " << rect_area<< endl;

return 0;

}

Instead of doing the calculation in the main, you can write a separate function that will return the result (in this case area) whenever it is called from the main function.

You need to define a function, and this function needs two values length (double value) and width (double value) to calculate the area. So when you call this function it is your responsibility to provide these values (known as parameters or arguments) to the function. This function will do the calculation of the area and the result will be returned. Here the area (result) will be a fractional value; therefore, the return value will be a double value. Now you can write the function prototype.

type of parameters


double rectangle_area(double length, double width) 

function name

The body of the function will calculate the area with the parameters (length and width) provided and the result (area) will be returned to the caller when the function ends.


double rectangle_area(double width, double height)

{

return width * height;

}


Now it is possible to call the  rectangle_area function in the main, and store the return result of calling the function into the main’s rect_area variable.

The complete program will look like this. Please remember to write your function before main function.


#include <iostream>

using namespace std;

double rectangle_area(double width, double height)

{

return width * height;

}

int main()

{

double width, height, rect_area;

cout << "Enter the width of the rectangle: ";

cin >> width;

cout << "Enter the height of the rectangle: ";

cin >> height;

rect_area=rectangle_area(width, height);

cout << "The area of the rectangle is: " << rect_area<< endl;

return 0;

}


Task 6.1 - Question

Calculate the final velocity using the equation v=u+a*t.

initial velocity u, acceleration a and time t will be given.

Example:

if u=100 m/s, a=9.8 m/s2 and t=2 s then v can be calculated as shown below

v= u + a*t

v= 100 + 9.8 * 2

= 119.6 m/s

1.   Create a program named Task6. 1.cpp

2.   Declare a function (function prototype) named find_velocity. This function will have parameters for u (   ), a (acceleration), and t (time) (all double values), and it will return a double value.

3.   Declare local variable in main as required.

4.   Main will print out Velocity 1” and calculate the final velocity based on an initial velocity of 10 m/s, time 180 seconds, with an acceleration of 9.08 m/s2 .

5.   Main will print out Velocity 2”, and calculate the final velocity based on an initial velocity of 10.5 m/s, time of 10.5 seconds, with an acceleration of 12.5 m/s2 .

6.   Main should then ask the user to enter initial velocity, time, and acceleration, and then output the final velocity for these values.

Note - find_velocity function should be called three times from the main.

Submit the code and screenshot.

Task 6.2  My drawing function

Aim

This task is to demonstrate the knowledge of creating a user-defined function and passing arguments to a function.

Task

Create a function called draw_star which receives different parameters when called.

 

Submission requirements

•    The program must draw three stars (three different colour) on the screen.

•    Your program should call draw_star() function from the main

•    The parameters of the draw_star function must be the color, location, width, and height.

Tips – start the code as shown below.

 

To draw the star shape as shown below, you need to create a rectangle and four triangles.

 

If there isn’t any line it will look like a star as shown below.

 

According to the problem statement, you will be passing five parameters to the function.

1.   Colour of your star

2.   x position of the rectangle in the star shape

3.   y position of the rectangle in the star shape

4.   width of the rectangle

5.   height of the rectangle

 

Now you can mark the different coordinates for the different shapes (one rectangle and four triangles) in your star shape.


(x2,y)

(x,y)

 

 

Width

 

 

Height

 

(x2,y2)

 

 

(mid_x,bottom_y)

 

Here colour, x, y, width and height are received as parameters. You need to declare eight more local variables to complete the star shape. They are:

1.   x2=x+width;

2.   y2=y+height;

3.   left_x=x-width;

4.   mid_y=y+height/2;

5.   mid_x=x+width/2;

6.   right_x=x2+width;

7.   top_y=y-height;

8.  bottom_y=y2+height;z

Draw a star calling the draw_star function from the main

draw_star(COLOR_GREEN, 100,100, 100, 100);

Draw three more different stars using different colours at different locations (use the same height and width).

•    draw_star(COLOR_RED, 600,100, 100, 100); - top right

•    draw_star(COLOR_BLUE, 100,400, 100, 100); - bottom left

•    draw_star(COLOR_ORANGE, 600,400, 100, 100); - bottom right

 

•    After adding all the codes, build and run your code using the following commands. Make sure to move to the folder where the program is saved using the cd command.

skm g++ Task6.2.cpp -o star

./star

•    Submit the source code and the screenshot.