You are on page 1of 10

FACULTY OF INFORMATION TECHNOLOGY

PROGRAMMING 621 – C++


1ST SEMESTER ASSIGNMENT

Name & Surname: Thabiso Xulu ITS No: 402103219

Qualification: DIT Semester: 1 Module Name: Programming

Submission Date: 13 May 2022

ASSESSMENT CRITERIA MARK EXAMINER MODERATOR


ALLOCATION MARKS MARKS
MARKS FOR CONTENT
QUESTION ONE 35
QUESTION TWO 30

QUESTION THREE 35

TOTAL MARKS 100


Examiner’s Comments:

Moderator’s Comments:

Signature of Examiner: Signature of Moderator:

1
Table of Content

Page 3 -4 Question 1
Page 4 – 6 Question 2
Page 6 - 9 Question 3

2
Question 1
#include <iostream>

using namespace std;

int area(int x) {

return x * x; }

int area(int x, int y)

{ return x * y;

double area(double b, double h)


{ return (0.5 * b) *

h;

int main()
{ int squareSize =

5;

int rectangleLength = 10;

int rectangleWidth = 7;

double triangleBase = 15;

double triangleHeight = 19;

cout << "Area of sqauare: " <<

3
area(squareSize) << "\n"; cout

<< "Area of rectangle: " <<

area(rectangleLength,

rectangleWidth) << "\n"; cout

<< "Area of triangle: " <<

area(triangleBase,

triangleHeight) << "\n";

return 0;
}

Question 2
#include <iostream> #include

<array>

using namespace std;

void enter(int arr[])

{ for(int i = 0; i < 10; i++)

cout << "Enter golf score: ";

cin >> arr[i];

if(arr[i] == -1)

{ for(int j = 0; j < i;

j++)

{ arr[j] =

arr[i];

4
}

break;

double calculateAverage(int arr[], int arraySize)

{ double sum = 0;

for(int i = 0; i < arraySize; i++)

sum += arr[i];

sum /= arraySize;

return sum;

void displayScores(int arr[], int arraySize, double avg)

{ for(int i = 0; i < arraySize; i++)

cout << "Score: " << arr[i] << "\n";

cout << "Average: " << avg << "\n";

int main()

{ int arr[10]; int arraySize = sizeof(arr) /

sizeof(int);

5
enter(arr); system("cls");

double avg =

calculateAverage(arr,

arraySize);

displayScores(arr, arraySize, avg);

return 0;

Question 3
#include <iostream> #include

<ctime>

using namespace std;

bool changeOption = true;

void menu(); int

startGame(); bool

change();

int main()

{ menu();

return 0;

void menu()

{ int option;

6
cout << "Press 1 to play game or any key to exit\n"; cin

>> option;

if(option == 1)

system("cls");

startGame();

int startGame()

{ srand(time(0)); int startNumber = 1

+ (rand() % 10); int options[5];

int playerNumber = 0; char

optionEntered;

playerNumber += startNumber;

do

char optionNumber = 'a';

cout << "Keep the number " << playerNumber << " between 1 and 10\n";

for(int i = 0; i < 5; i++)

if(changeOption == true)

options[i] = 1 + (rand() % 9);

change();

7
}

else

options[i] = rand() % 10 + (-9);

change();

for(int i = 0; i < 5; i++)

cout << optionNumber++ << ". " << options[i] << "\n";

cout << "Enter option: ";

cin >> optionEntered;

if(optionEntered == 'a')

playerNumber += options[0];

else if(optionEntered == 'b')

playerNumber += options[1];

else if(optionEntered == 'c')

playerNumber += options[2];

else if(optionEntered == 'd')

playerNumber += options[3];

8
}

else if(optionEntered == 'e')

playerNumber += options[4];

system("cls");

while(!(playerNumber < 0 || playerNumber > 10));

cout << "GAME OVER!!!\n";

bool change()

{ if(changeOption)

changeOption = false;

else

changeOption = true;

return changeOption;

9
10

You might also like