Last active
June 12, 2019 07:19
-
-
Save sannimichaelse/fc68da0ef7d768385f680ed81df14d2f to your computer and use it in GitHub Desktop.
Solve all assignments and questions given in the c++ class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.cpp | |
// Algorithms | |
// | |
// Created by Sanni Michael on 01/05/2019. | |
// Copyright © 2019 Sanni Michael. All rights reserved. | |
/**** The programs written below makes use of functions which are later called in the main function. | |
This allows us to familiarize ourself with modular programming | |
It also allow us to call the main function once instead of having to call it all the time which is cpu intensive | |
*****/ | |
#include <iostream> | |
#include <vector> | |
#include <numeric> | |
#include <string> | |
using namespace std; | |
// TEST : QUESTION 1 | |
void testQuestionOne() | |
{ | |
// List and compute the average of all integers from 1 to 50 | |
int sum = 0; | |
int counter = 0; | |
double average = 0.0; | |
cout << "List And Average Of One To Fifty" <<endl; | |
for (int k = 1; k<=50; k++) { | |
cout << k << "\n"; | |
counter++; | |
sum+=k; | |
} | |
average = sum / counter; | |
cout << "\nAverage = " << average << "\n\n"; | |
} | |
// TEST : QUESTION 2 | |
void testQuestionTwo() | |
{ | |
int sum = 0; | |
int count = 0; | |
double avg = 0.0; | |
cout <<"List And Average Of All Numbers Divisible By Three From Forty Nine To One" <<endl; | |
for (int j = 49; j>=1; j--) { | |
if(j % 3 == 0){ | |
cout << j << "\n"; | |
count++; | |
sum+=j; | |
} | |
} | |
avg = sum / count; | |
cout << "\nAverage = " << avg << "\n\n"; | |
} | |
// C++ PRACTICAL SOLUTIONS | |
void weekOne() | |
{ | |
int numberOfMonths; | |
double payRise = 0.25; | |
double sum = 0.0; | |
cout << "Please Enter Number of Month : "; // This should be 7 according to the queation | |
cin >> numberOfMonths; | |
vector <double> monthArray(numberOfMonths); | |
for (int i = 0; i < numberOfMonths; i++) | |
{ | |
cout << "Month #" << i+1 << " "; | |
cin >> monthArray[i]; | |
} | |
//cout<<accumulate(monthArray.begin(),monthArray.end(),0); // This will print out the sum of all salaries in array | |
for (size_t i=0; i<monthArray.size(); ++i) | |
{ | |
sum+=monthArray[i] * payRise; | |
//cout << monthArray[i] << endl; // For printing out individual elements in the vector/array | |
} | |
cout << "\nSum of backdated salary for the past seven months after pay rise "<<endl; | |
cout << "\nSum : "<<sum<<endl; | |
} | |
void weekSix() | |
{ | |
// Program to welcome lecturer to class; | |
char lecturerName[100]; | |
int gender; | |
cout << "Enter Lecturer Name And Gender"; | |
cout << "\n NB : Enter 1 for male and 2 for female"; | |
cout << "\nLecturer Name "; | |
cin >> lecturerName; | |
cout << "\nLecturer Gender "; | |
cin >> gender; | |
switch (gender) { | |
case 1: | |
cout << "\nWelcome : Mr " << lecturerName << endl; | |
break; | |
case 2: | |
cout << "\nWelcome : Mrs " << lecturerName << endl; | |
break; | |
default: | |
cout << "\nWRONG INPUT" <<endl; | |
break; | |
} | |
// Program to print Hello World; | |
cout << "\nHello World"<<endl; | |
} | |
void weekSeven() | |
{ | |
// Program to find average of the scores of four exams taken by a student | |
int sum = 0; | |
double avg = 0.0; | |
int scoreOne,scoreTwo,scoreThree,scoreFour; | |
cout << "Enter Score 1 \n"; | |
cin >> scoreOne; | |
cout << "Enter Score 2 \n"; | |
cin >> scoreTwo; | |
cout << "Enter Score 3 \n"; | |
cin >> scoreThree; | |
cout << "Enter Score 4 \n"; | |
cin >> scoreFour; | |
sum = sum + scoreOne + scoreTwo + scoreThree + scoreFour; | |
avg = sum / 4; | |
cout << "Average of Scores = "<<avg<<endl; | |
} | |
// Call all user defined functions in the main function | |
int main() | |
{ | |
testQuestionOne(); | |
testQuestionTwo(); | |
weekOne(); | |
weekSix(); | |
weekSeven(); | |
return 0; | |
} | |
// FIND THE AREA AND PERIMETER OF TRIANGLE USING C++ CLASS | |
#include <iostream> | |
#include <cmath> | |
using namespace std; | |
class AreaAndPerimeterOfTriangle | |
{ | |
public: | |
double sideA = 0.0; | |
double sideB = 0.0; | |
double sideC = 0.0; | |
double perimeter = 0.0; | |
double s = 0.0; | |
double areaSum = 0.0; | |
//Default Constructor | |
AreaAndPerimeterOfTriangle() | |
{ | |
//cout << "Default Constructor will always be called" << endl; | |
} | |
//Parameterized Constructor | |
//Constructor to assign values to data members | |
AreaAndPerimeterOfTriangle(double s1,double s2, double s3) | |
{ | |
sideA = s1; | |
sideB = s2; | |
sideC = s3; | |
//cout << "Parametrized Constructor called" << endl; | |
} | |
// Method to find perimeter of triangle | |
void peri() | |
{ | |
perimeter = sideA + sideB + sideC; | |
cout << "Perimeter of the triangle is " << perimeter << endl; | |
} | |
// Method to find area of triangle | |
void area() | |
{ | |
s = sideA + sideB + sideC / 2; | |
areaSum = s * (s - sideA) * (s - sideB) * (s - sideC); | |
cout << "Area of Triangle : "<< sqrt(areaSum) <<endl; | |
} | |
}; | |
// Test Driver | |
int main() { | |
// areaObject will call Default Constructor | |
AreaAndPerimeterOfTriangle areaObject; | |
// Create an instance of the AreaAndPerimaterOfTriangle class and initiliaze your Constructor | |
// Parameterized Constructor | |
AreaAndPerimeterOfTriangle areaObject2(22.6,14.7,13.8); | |
// Call perimater of triangle | |
areaObject2.peri(); // Perimeter of the triangle is 51.1 | |
// Call area of triangle | |
areaObject2.area(); // Area of Triangle : 925.307 | |
return 0; | |
} | |
/***************************************************************************************************************************** | |
2018 C++ EXAMINATION ANSWERS | |
*****************************************************************************************************************************/ | |
/***************************** | |
QUESTION 1 - German Questions | |
****************************/ | |
i. object | |
ii. private and public | |
iii. 0 | |
iv. name | |
v. local | |
vi. pointer | |
vii. int mat1[4][3]; | |
viii. main | |
ix. | |
//Function prototype declaration | |
double add(float a, float b, int c); | |
//Function prototype definition | |
double add(float a, float b, int c){ | |
double sum = 0; | |
sum = a + b + c; | |
return sum; | |
} | |
x. | |
//Function prototype declaration | |
void linearArray(int arr[]); | |
//Function prototype definition | |
void linearArray(int arr[]){ | |
for (int i = 0; i < ARRAYSIZE(arr); i++) { | |
cout << " i "<< arr[i]; | |
} | |
} | |
xi. const double waterRate = 5.65; | |
xii. Bjarne Stroustrup at AT&T Labs | |
xiii. memory address | |
xiv. | |
<conio.h> | |
<ctype.h> | |
<string.h> | |
<math.h> | |
<cmath> | |
<stdlib.h> | |
xv. enum grade { A = 10, B = 12, C = 23 }; | |
xvi. superset | |
xvii. <cmath> | |
xviii. break | |
xix. ampersand | |
xx. destructor | |
/***************************** | |
QUESTION 2 - True/False | |
****************************/ | |
i. F | |
ii. F | |
iii. F | |
iv. F | |
v. F | |
vi. T | |
vii. F | |
viii. F | |
ix. F | |
x.F | |
xi. T | |
xii. F | |
xiii. F | |
xiv. T | |
xv. F | |
xvi. T | |
xvii. F | |
xviii. T | |
xix. T | |
xx. T | |
/***************************** | |
QUESTION 3 | |
****************************/ | |
3a. | |
3b. int | |
short int | |
long int | |
signed long int | |
unsigned long int | |
char | |
wchar_t | |
3c. n--; | |
--n; | |
n = n - 1; | |
n-=1; | |
/***************************** | |
QUESTION 4 | |
****************************/ | |
4a. | |
#include <iostream> | |
#include <cmath> | |
using namespace std; | |
int main(){ | |
double array[25]; | |
for (int z = 0; z < 25; z++) { | |
cin >> array[z]; | |
} | |
// Print array in reverse order | |
for (int i = 24; i >= 0; i--) { | |
cout << "\ni = " <<array[i]<<endl; | |
} | |
return 0; | |
} | |
4b. x = 8, y = 3; | |
i. x*=x+y--; | |
int x = 8; | |
int y = 3; | |
int expr = x*=x+y--; | |
cout << expr << endl; // 88 | |
why? | |
same as x = x * x+y--// NB; dont solve it this way, you will get an entirely diff answ | |
according to operator preceedence assignment operators are 16th on the table so that means it executes operands | |
without assignment first which is x+y = 11; y-- means assign 3 to y before decrementing, --y means decrement before | |
assigning to y.then x+y = 11 * x which gives 88. Because we are assigning the result to x, the value of y will not | |
be decremented except when printed on the next line | |
https://github.com/MicrosoftDocs/cpp-docs/blob/master/docs/cpp/cpp-built-in-operators-precedence-and-associativity.md | |
ii. x/=++y; | |
answer = 2 | |
why? | |
increment first before assigning then dividing by 8 which is 8/4 = 2; | |
iii. y+=y-x++ | |
answer = -2 | |
iv. x%=++y; | |
answer = 0; | |
v. y-=++x; | |
answer = -6 | |
/***************************** | |
QUESTION 6 | |
****************************/ | |
6a. | |
#include <iostream> | |
#include <cmath> | |
using namespace std; | |
int main(){ | |
int num1; | |
int num2; | |
double num3 = 0.0; | |
double num4 = 0.0; | |
double sum = 0.0; | |
double average = 0.0; | |
double sumSquare = 0.0; | |
cout << "Enter two integer and two double values" << endl; | |
cin >> num1 >> num2 >> num3 >> num4; | |
sum = num1 + num2 + num3 + num4; | |
average = sum / 4; | |
sumSquare = sum * sum; | |
cout << "Sum = " << sum << endl; | |
cout << "Average = " << average << endl; | |
cout << "Square of sum = " << sumSquare << endl; | |
return 0; | |
} | |
6b. VALID IDENTIFER | |
- myFees | |
- Else | |
- returns | |
- CHAR | |
INVALID IDENTIFIER | |
- Student Name | |
- public | |
- #plate_number | |
- 1stAttempt | |
- Else | |
- return | |
- phone-Number | |
/***************************** | |
QUESTION 7 | |
****************************/ | |
7b. | |
// FIND THE AREA AND PERIMETER OF RECTANGLE USING C++ CLASS | |
#include <iostream> | |
#include <cmath> | |
using namespace std; | |
class AreaAndPerimeterOfRectangle | |
{ | |
public: | |
int length; | |
int breadth; | |
int perimeter; | |
int areaOfRec; | |
//Default Constructor | |
AreaAndPerimeterOfRectangle() | |
{ | |
//cout << "Default Constructor will always be called" << endl; | |
} | |
//Parameterized Constructor | |
//Constructor to assign values to data members | |
AreaAndPerimeterOfRectangle(int a,int b) | |
{ | |
length = a; | |
breadth = b; | |
//cout << "Parametrized Constructor called" << endl; | |
} | |
// Method to find perimeter of rectangle | |
void peri() | |
{ | |
perimeter = 2 * (length + breadth); | |
cout << "Perimeter of the rectangle is " << perimeter << endl; | |
} | |
// Method to find area of triangle | |
void area() | |
{ | |
areaOfRec = length * breadth; | |
cout << "Area of Recangle : "<< areaOfRec <<endl; | |
} | |
}; | |
// Test Driver | |
int main() { | |
int length; | |
int breadth; | |
cout<< "Enter Length and Breadth of Rectangle" << endl; | |
cin >> length >> breadth; | |
// areaObject will call Default Constructor | |
AreaAndPerimeterOfRectangle areaObject; | |
// Create an instance of the AreaAndPerimaterOfRectangle class and initiliaze your Constructor | |
// Parameterized Constructor | |
AreaAndPerimeterOfRectangle areaObject2(length,breadth); | |
// Call perimater of rectangle | |
areaObject2.peri(); | |
// Call area of rectangle | |
areaObject2.area(); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment