Tutorial 6 C++

Exercise 1

#include <iostream>

using std::cout;

using std::cin;

int main()

{

const float arrf[10]{ 1.5, 2.6, 3.8, 4.2, 5.9, 6.75, 7.9, 8.127, 9.6, 10.5 };

int num1;        //    0,   1,   2,   3,   4,   5,    6,    7,    8,    9

cout << “Please enter a number between 0 and 9 : “;

cin >> num1;

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

cout << arrf[num1] << “\n”;

return 0;

}

Exercise 2

#include <iostream>

using std::cout;

using std::cin;

int main()

{

const int arri[10]{ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };

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

cout << arri[i] << “\n”;

cout << “\n”;

for (int u = 9; u > 0 – 1 ; u–)

cout << arri[u] << “\n”;

cout << “\n”;

for (int y = 0; y < 10; y += 2)

cout << arri[y] << “\n”;

cout << “\n”;

for (int t = 9; t > 0 – 1; t -= 3)

cout << arri[t] << “\n”;

return 0;

}

Exercise 3

#include <iostream>

#include <random>

using std::cout;

using std::cin;

int main()

{

int day;

int arr[365];

for (int d = 0; d < 365; d++)

{

arr[d] = rand() % 71 – 20;

}

cout << “The temperature of this day is: “;

for (int d = 0; d < 365; d++)

cout << “Day ” << d + 1 << “d” << arr[d] << “c” << “,”;

cout << “\nWhat day would you like to know the temp for?: “;

cin >> day;

cout << “The temperature on that day was: ” << arr[day – 1] << ” degrees”;

return 0;

}

Exercise 4

Exercise 5

#include <iostream>

using std::cout;

using std::cin;

int main()

{

const int STUDMARK = 20;

int sum = 0;

int hi_score = 0;

int low_score = 0;

int arr[STUDMARK];

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

{

cout << “Please enter a number: ” << i + 1 << “:\n”;

cin >> arr[i];

sum += arr[i];

}

hi_score = arr[STUDMARK];

{

for (int u = 1; u < STUDMARK; u++)

if (hi_score < arr[u])

hi_score = arr[u];

}

low_score = arr[STUDMARK];

for (int y = 1; y > STUDMARK; y–)

{

if (low_score > arr[y])

low_score = arr[y];

}

cout << “The adverage mark is : ” << sum / STUDMARK << “\n”;

cout << “The highest mark is : ” << hi_score << “\n”;

cout << “The lowest mark is : ” << low_score << “\n”;

return 0;

}

Exercise 6

#include <iostream>

#include <iomanip>

using std::cout;

using std::cin;

//2D arrays

int main()

{

int table[12][12];

int num1, num2;

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

{

for (int j = 0; j < 12; j++)

{

table[i][j] = ((i + 1) * (j + 1));

cout << “|” << std::setw(3) << std::setfill(‘0’) << table[i][j];

}

cout << “|\n”;

cout << “————————————————-\n”;

}

cout << “Enter two numbers to multiply: \n”;

cin >> num1;

cin.ignore();

cin >> num2;

cout << “\nThe answer is: ” << table[num1-1][num2-1]; // -1 as the computer counts from 0

return 0;

}

Exercise 7

int main()

{

int age = 0, birth_date = 0, height = 0;

string first_name;

string second_name;

string pet;

string colour;

string pet_flavour;

string gew_dog;

cout << “What is your age and first and second name in that order?:\n”;

cin >> age;

cin.ignore(0);

cin >> first_name;

cin.ignore(0);

cin >> second_name;

cout << “Thank you ” << first_name << “, what is your favourite colour?:\n”;

cin >> colour;

cout << “Wow! I like that colour too, what is the name of a pet you have had?:\n”;

cin >> pet;

cout << “What flavour pet was ” << pet << “?\n”;

cin >> pet_flavour;

cout << “Finally, what is the name of your grandad’s wifes dog?: \n”;

cin >> gew_dog;

cout << “\n”;

cout << “Anyway, here is a story about you and your pet:\n”;

cout << “Once apon a time, there was a magical wizard named ” << first_name << ” ” << second_name << ” who lived in a huge wizard tower in the center of Skegness along with his ” << pet_flavour << ” ” << pet << ” . ” << first_name << “‘s wizard tower was painted in their favourite colour, ” << colour << “. Sadly, the evil villain ” << gew_dog << ” did not like their favourite shade of ” << colour << “\n”;

cout << “The evil tyrant ” << gew_dog << ” charged towards their house at a top speed of around 3 – 6 mph second, and bellows about hating that colour.” << pet << ” then bites his ankles and the 122cm tall tyrant topples to the ground.” << first_name << ” ” << second_name << ” stands above the toppled manlet and proudly states in all his wizardous glory I have defeated the small man.”;

cout << “\n The End!\n”;

return 0;

}

Exercise 8

#include <iostream>

using std::cout;

using std::cin;

int main()

{

const int arr[10][5];

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

{

cout << “Please enter student id: “;

cin >> arr[i][0];

for (int j = 1; j < 4; j++)

{

cout << “Please enter student mark: “;

cin >> arr[i][j];

}

arr[i][4] = (arr[i][1] + arr[i][2] + arr[i][3]) / 3;

cout << arr[i][4] << “\n”;

}

return 0;

}

Design a site like this with WordPress.com
Get started