Last active
October 14, 2020 09:22
-
-
Save lablnet/062fa41b647343c804212bbba4f1ad01 to your computer and use it in GitHub Desktop.
Lab 02 tasks
This file contains 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
// | |
// Created by lablnet on 10/14/20. | |
// | |
#include <iostream> | |
// we can use C++ vector to make it dynamic size but that was not requirement. | |
struct Weather { | |
float totalRainFall[6]; | |
float temp[6]; | |
float lowTemp[6]; | |
float avgTemp[6]; | |
int sumOfRainFall(); | |
float avgRainFall(); | |
}; | |
int Weather::sumOfRainFall() { | |
int sum = 0; | |
for (int i = 0; i < 5; i++) { | |
sum += totalRainFall[i]; | |
} | |
return sum; | |
} | |
float Weather::avgRainFall() { | |
return sumOfRainFall() / 5; | |
} | |
int main(void) { | |
Weather w; | |
std::cout << "Enter the records" << std::endl; | |
for (int i = 0; i < 5; i++) { | |
std::cout << "Enter records for " << i + 1 << " month "; | |
std::cout << "Enter total rainfall: "; | |
std::cin >> w.totalRainFall[i]; | |
std::cout << "Enter total temperature: "; | |
std::cin >> w.temp[i]; | |
std::cout << "Enter lower temperature: "; | |
std::cin >> w.lowTemp[i]; | |
std::cout << "Enter average temperature: "; | |
std::cin >> w.avgTemp[i]; | |
} | |
std::cout << "Total rainfall: " << w.sumOfRainFall() << std::endl; | |
std::cout << "Average rainfall: " << w.avgRainFall() << std::endl; | |
return 0; | |
} |
This file contains 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
#include <iostream> | |
void add(int, int); | |
void sub(int, int); | |
void mul(int, int); | |
void divide(int, int); | |
// optional | |
void power(int, int); | |
void menu(void) { | |
char op; | |
int a, b; | |
std::cout << "Select operator: \n" | |
<< " + for addition \n" | |
<< " * for multiplication \n" | |
<< " - for subtraction \n" | |
<< " ^ for power \n" | |
<< " / for division \n"; | |
std::cin >> op; | |
std::cout << "Enter first number: "; | |
std::cin >> a; | |
std::cout << "Enter second number: "; | |
std::cin >> b; | |
switch (op) { | |
case '+': | |
add(a, b); | |
break; | |
case '-': | |
sub(a, b); | |
break; | |
case '*': | |
mul(a, b); | |
break; | |
case '/': | |
divide(a, b); | |
break; | |
case '^': | |
power(a, b); | |
break; | |
} | |
} | |
int main() { | |
menu(); | |
return 0; | |
} | |
void add(int a, int b) { | |
std::cout << "Addition of " << a << " + " << b << " = " << a + b; | |
} | |
void sub(int a, int b) { | |
std::cout << "Subtraction of " << a << " - " << b << " = " << a - b; | |
} | |
void mul(int a, int b) { | |
std::cout << "Multiplication of " << a << " * " << b << " = " << a * b; | |
} | |
void divide(int a, int b) { | |
try { | |
std::cout << "Division of " << a << " / " << b << " = " << a / b; | |
} catch (...) { | |
std::cout << "Number can not divided by zero"; | |
} | |
} | |
void power(int a, int b) { | |
int result; | |
while (b != 0) { | |
result *= a; | |
--b; | |
} | |
std::cout << result; | |
} |
This file contains 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
// | |
// Created by lablnet on 10/14/20. | |
// | |
#include <iostream> | |
#include <string> | |
struct MovieData { | |
std::string title; | |
std::string director; | |
int yearReleased; | |
float runningTime; // in mints. | |
void displayInfo(); | |
}; | |
void MovieData::displayInfo() { | |
std::cout << "Title: " << title << std::endl; | |
std::cout << "Director: " << director << std::endl; | |
std::cout << "Year Released: " << yearReleased << std::endl; | |
std::cout << "Running time: " << runningTime << " Mints" << std::endl; | |
} | |
int main(void) { | |
MovieData m1; | |
MovieData m2; | |
std::cout << "Enter movie 1 data: "; | |
std::cout << "Enter title: "; | |
std::cin >> m1.title; | |
std::cout << "Enter director: "; | |
std::cin >> m1.director; | |
std::cout << "Enter release year: "; | |
std::cin >> m1.yearReleased; | |
std::cout << "Enter running time in mints: "; | |
std::cin >> m1.runningTime; | |
std::cout << "Enter movie 2 data: "; | |
std::cout << "Enter title: "; | |
std::cin >> m2.title; | |
std::cout << "Enter director: "; | |
std::cin >> m2.director; | |
std::cout << "Enter release year: "; | |
std::cin >> m2.yearReleased; | |
std::cout << "Enter running time in mints: "; | |
std::cin >> m2.runningTime; | |
std::cout << "Movie 1\n"; | |
m1.displayInfo(); | |
std::cout << "Movie 2\n"; | |
m2.displayInfo(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment