Name: Saggi Mizrahi • ID: 032493124 • Group: מעוף א' • Date: November 5, 2013
/* diet.cpp
* This program recommends vague diet menus according to age, weight and
* height.
*
* Saggi Mizrahi 05 November 2013
*/
#include <iostream>
using namespace std;
/* Calculate optimal diet according to age, weight and height
* Input: age, weight and height.
* Output: the recommended diet for your age and height to weight ratio.
*/
int main()
{
// declaration of constants
const char DIET_OPTION_A = 'A';
const char DIET_OPTION_B = 'B';
const char DIET_OPTION_C = 'C';
const char DIET_OPTION_NONE = 'N';
// declaration of variables
// All variables are set to -1 to help detect algorithmic errors.
int age = -1;
double weight = -1;
int height = -1;
double ratio = -1;
char optimal_diet = DIET_OPTION_NONE;
// getting input from the user
cout << "please enter your age (in years): ";
cin >> age;
cout << "please enter your weight (in KG): ";
cin >> weight;
cout << "please enter your height (in centimeters): ";
cin >> height;
// compute ratio
if (weight != 0) {
ratio = height / weight;
}
// find appropriate diet
// first check if we are in the correct ranges
if (age < 11 or age > 120 or ratio < 0.5 or ratio > 5) {
optimal_diet = DIET_OPTION_NONE;
} else if (ratio < 2) {
optimal_diet = DIET_OPTION_A;
} else if (ratio < 3.5) {
if (age <= 40) {
optimal_diet = DIET_OPTION_B;
} else {
optimal_diet = DIET_OPTION_C;
}
} else if (age >= 21 and age <= 40) {
optimal_diet = DIET_OPTION_A;
} else {
optimal_diet = DIET_OPTION_C;
}
// outputting the result
if (optimal_diet == DIET_OPTION_NONE) {
cout << "no suitable diet found" << endl;
} else {
cout << "the best diet option for you is option " <<
optimal_diet << endl;
}
}
Check given example:
please enter your age (in years): 18
please enter your weight (in KG): 60
please enter your height (in centimeters): 180
the best diet option for you is option B
Check weight is 0:
please enter your age (in years): 18
please enter your weight (in KG): 0
please enter your height (in centimeters): 180
no suitable diet found
Check too young:
please enter your age (in years): 10
please enter your weight (in KG): 60
please enter your height (in centimeters): 180
no suitable diet found
Check too old:
please enter your age (in years): 121
please enter your weight (in KG): 60
please enter your height (in centimeters): 180
no suitable diet found
check ration between 0.5 and 2:
please enter your age (in years): 50
please enter your weight (in KG): 60
please enter your height (in centimeters): 60
the best diet option for you is option A
check ratio to low:
please enter your age (in years): 50
please enter your weight (in KG): 60
please enter your height (in centimeters): 10
no suitable diet found
/* order.cpp
* This program receives 3 numbers and prints them out in descending order.
*
* Saggi Mizrahi 05 November 2013
*/
#include <iostream>
using namespace std;
/* Order 3 numbers in descending order.
* Input: 3 numbers.
* Output: The numbers printed out in descending order.
*/
int main()
{
// declaration of variables
int num1;
int num2;
int num3;
int tmp;
// getting input from the user
cout << "please enter 3 numbers: ";
cin >> num1 >> num2 >> num3;
// order numbers
if (num3 > num2) {
tmp = num3;
num3 = num2;
num2 = tmp;
}
if (num2 > num1) {
tmp = num2;
num2 = num1;
num1 = tmp;
}
if (num3 > num2) {
tmp = num3;
num3 = num2;
num2 = tmp;
}
// outputting the result
cout << num1 << " " << num2 << " " << num3 << endl;
}
please enter 3 numbers: 1 2 3
3 2 1
please enter 3 numbers: 1 3 2
3 2 1
please enter 3 numbers: 3 2 1
3 2 1
please enter 3 numbers: 2 3 1
3 2 1
please enter 3 numbers: 1 3 2
3 2 1
please enter 3 numbers: 2 1 3
3 2 1
please enter 3 numbers: 3 1 2
3 2 1
/* drivetime.cpp
* Program that calculates the arrivale time given the start time and duration
* in seconds
*
* Saggi Mizrahi 5 November 2013
*/
#include <iostream>
using namespace std;
/* Input: 4 integers representing hours, minutes, seconds and duration in
* seconds.
* Output: The arrival time.
*/
int main() {
// declaration of constants
const int SECONDS_IN_MINUTE = 60;
const int MINUTES_IN_HOUR = 60;
const int HOURS_IN_DAY = 24;
// declaration of variables
int hours;
int minutes;
int seconds;
int duration;
bool inputValid = true;
// getting input from the user
cout << "please enter the starting time (H M S) " <<
"followed by drive duration in seconds: ";
cin >> hours >> minutes >> seconds >> duration;
// validate input
if (seconds < 0 || seconds > 59) {
cout << "Invalid number of seconds: " << seconds <<
". number must be between 0 and 59" << endl;
inputValid = false;
}
if (minutes < 0 || minutes > 59) {
cout << "Invalid number of minutes: " << minutes <<
". number must be between 0 and 59" << endl;
inputValid = false;
}
if (hours < 0 || hours > 23) {
cout << "Invalid number of hours: " << hours <<
". number must be between 0 and 23" << endl;
inputValid = false;
}
if (!inputValid) {
// Input is ivalid, bail out
return 1;
}
// caluculating the end time
seconds += duration;
minutes += seconds / SECONDS_IN_MINUTE;
seconds %= SECONDS_IN_MINUTE;
hours += minutes / MINUTES_IN_HOUR;
minutes %= MINUTES_IN_HOUR;
hours %= HOURS_IN_DAY;
// outputting the result
cout << hours << " " << minutes << " " << seconds << endl;
return 0;
}
Input from assignment:
please enter the starting time (H M S) followed by drive duration in seconds: 12 31 10 4004
13 37 54
Totally wrong input:
please enter the starting time (H M S) followed by drive duration in seconds: -1 -1 -1 4004
Invalid number of seconds: -1. number must be between 0 and 59
Invalid number of minutes: -1. number must be between 0 and 59
Invalid number of hours: -1. number must be between 0 and 23
There never seem to be enough hours in a day:
please enter the starting time (H M S) followed by drive duration in seconds: 25 10 14 4
Invalid number of hours: 25. number must be between 0 and 23
/* round.cpp
* Program that rounds numbers with various algorithms
*
* Saggi Mizrahi 5 November 2013
*/
#include <iostream>
using namespace std;
/* Input: An algorithm of choice and a real number
* Output: The number rounded according to the selected algorithm.
*/
int main() {
// declaration of constants
const int ROUND_DOWN = 1;
const int ROUND_UP = 2;
const int ROUND = 3;
// declaration of variables
double num = 0;
int roundAlgorithm = -1;
int result = 0;
// select the algorithm
cout << "available algorithms" << endl;
cout << "\t1.Floor round" << endl;
cout << "\t2.Ceiling round" << endl;
cout << "\t3.Round to the nearest whole number" << endl;
cout << "please choose a round algorithm: ";
cin >> roundAlgorithm;
// get the number
cout << "please enter a number you wish to round: ";
cin >> num;
// round number
switch (roundAlgorithm) {
case ROUND_DOWN:
result = num;
break;
case ROUND_UP:
result = num;
if (num > result) {
result++;
}
break;
case ROUND:
result = num + 0.5;
break;
}
// outputting the result
cout << result << endl;
return 0;
}
available algorithms
1.Floor round
2.Ceiling round
3.Round to the nearest whole number
please choose a round algorithm: 1
please enter a number you wish to round: 0.5
0
available algorithms
1.Floor round
2.Ceiling round
3.Round to the nearest whole number
please choose a round algorithm: 2
please enter a number you wish to round: 0.5
1
available algorithms
1.Floor round
2.Ceiling round
3.Round to the nearest whole number
please choose a round algorithm: 3
please enter a number you wish to round: 0.5
1
available algorithms
1.Floor round
2.Ceiling round
3.Round to the nearest whole number
please choose a round algorithm: 3
please enter a number you wish to round: 0.9
1
available algorithms
1.Floor round
2.Ceiling round
3.Round to the nearest whole number
please choose a round algorithm: 3
please enter a number you wish to round: 0.4
0
available algorithms
1.Floor round
2.Ceiling round
3.Round to the nearest whole number
please choose a round algorithm: 2
please enter a number you wish to round: 0.9999999
1
available algorithms
1.Floor round
2.Ceiling round
3.Round to the nearest whole number
please choose a round algorithm: 2
please enter a number you wish to round: 0.0000000001
1
/* stars.cpp
* This program prints out a lovely asterisk formation
*
* Saggi Mizrahi 05 November 2013
*/
#include <iostream>
using namespace std;
/* This program prints out a asterisk formation as two right triangles touching
* each other.
* Input: number of lines.
* Output: The said formation.
*/
int main()
{
// declaration of constants
const char ASTERISK = '*';
const char SPACE = ' ';
// declaration of variables
int n;
int totalWidth;
// getting input from the user
cout << "please enter a number: ";
cin >> n;
// draw shape
totalWidth = n * 2;
for (int line = 0; line < n; line++) {
for (int i = 0; i < totalWidth; i++) {
if (i <= line || i >= totalWidth - line - 1) {
cout << ASTERISK;
} else {
cout << SPACE;
}
}
cout << endl;
}
return 0;
}
for (...;...;...) // Loop that goes line by line
{
for (...;...;...) // Loop that prints spaces and asterixes
{
}
// New line
}
Check given example:
please enter a number: 5
* *
** **
*** ***
**** ****
**********
/* pyramid.cpp
* This program prints out a letter pyramid
*
* Saggi Mizrahi 05 November 2013
*/
#include <iostream>
using namespace std;
/* This program prints out a letter pyramid
* each other.
* Input: the central brick in the letter pyramid.
* Output: The letter pyramid.
*/
int main()
{
// declaration of constants
const char START_CHAR = 'a';
// declaration of variables
char endChar;
int numLines;
int numSpaces;
// getting input from the user
cout << "please enter a char: ";
cin >> endChar;
// draw shape
numLines = endChar - START_CHAR + 1;
for (int i = 0; i < numLines; i++) {
numSpaces = numLines - i - 1;
for (int j = 0; j < numSpaces; j++) {
cout << " ";
}
for (int j = 0; j < i; j++) {
cout << char(START_CHAR + j);
}
for (int j = i; j >= 0; j--) {
cout << char(START_CHAR + j);
}
cout << endl;
}
return 0;
}
for (...;...;...) // Loop that goes line by line
{
for (...;...;...) // Loop that prints spaces
{
}
for (...;...;...) // Loop that prints letters in ascending order
{
}
for (...;...;...) // Loop that prints letter in descending order
{
}
// New line
}
Check given example:
please enter a char: d
a
aba
abcba
abcdcba
/* shtrudelville.cpp
* This program prints out a number formation
*
* Saggi Mizrahi 05 November 2013
*/
#include <iostream>
using namespace std;
/* This program prints out a number formation
*
* Input: two numbers.
* Output: The requested formation.
*/
int main()
{
// declaration of constants
const char SEPERATOR = '@';
// declaration of variables
int n;
int k;
// getting input from the user
cout << "please enter 2 numbers: ";
cin >> n >> k;
// draw shape
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
for (int s = 0; s < n; s++) {
cout << (s + i) % n;
}
cout << SEPERATOR;
}
cout << endl;
}
return 0;
}
for (...;...;...) // Loop that goes line by line
{
for (...;...;...) // Loop goes section by section
{
for (...;...;...) // Loop that prints the numbers in each section
{
}
// Seperator
}
// New line
}
Check given example:
please enter 2 numbers: 5 3
01234@01234@01234@
12340@12340@12340@
23401@23401@23401@
34012@34012@34012@
40123@40123@40123@
/* hashcash.cpp
* This program prints out a $# formation
*
* Saggi Mizrahi 05 November 2013
*/
#include <iostream>
using namespace std;
/* This program prints out a $# formation
*
* Input: two numbers.
* Output: The requested formation.
*/
int main()
{
// declaration of constants
const char SEPERATOR = '@';
const char CHAR_A = '$';
const char CHAR_B = '#';
// declaration of variables
int n;
int k;
char startChar = CHAR_A;
char endChar = CHAR_B;
// getting input from the user
cout << "please enter 2 numbers: ";
cin >> n >> k;
// draw shape
for (int i = 1; i <= n; i++) {
for (int j = 0; j < k; j++) {
for (int s = 0; s < n; s++) {
if (s < i) {
cout << startChar;
} else {
cout << endChar;
}
}
cout << SEPERATOR;
}
cout << endl;
// Flip chars
startChar = startChar == CHAR_A ? CHAR_B : CHAR_A;
endChar = endChar == CHAR_A ? CHAR_B : CHAR_A;
}
return 0;
}
for (...;...;...) // Loop that goes line by line
{
for (...;...;...) // Loop goes section by section
{
for (...;...;...) // Loop that prints the symbols
{
}
// Seperator
}
// New line
}
Check given example:
please enter 2 numbers: 5 3
$####@$####@$####@
##$$$@##$$$@##$$$@
$$$##@$$$##@$$$##@
####$@####$@####$@
$$$$$@$$$$$@$$$$$@
/* averages.cpp
* This program takes numbers and prints out the algebraical and geometrical averages.
*
* Saggi Mizrahi 05 November 2013
*/
#include <iostream>
#include <math.h>
using namespace std;
/* This program takes numbers and prints out the algebraical and geometrical averages.
*
* Input: 10 numbers.
* Output: The algebraical and geometrical (when possible) averages.
*/
int main()
{
// declaration of constants
const int NUM_ENTRIES = 10;
// declaration of variables
int positiveEntriesNum = 0;
int num;
int sum = 0;
int mul = 1;
// getting input from the user
for (int i = 0; i < NUM_ENTRIES; i++) {
cout << "please enter a number: ";
cin >> num;
sum += num;
if (num > 0) {
positiveEntriesNum++;
mul *= num;
}
}
if (positiveEntriesNum == 0) {
cout << "No positive numbers entered, no geometrical" <<
" average is possible." << endl;
} else {
cout << "Geometric average is: " <<
pow(mul, 1./positiveEntriesNum) << endl;
}
cout << "Algebraic average is: " << sum / double(NUM_ENTRIES) << endl;
return 0;
}
please enter a number: 1
please enter a number: 2
please enter a number: 3
please enter a number: 4
please enter a number: 5
please enter a number: 6
please enter a number: 7
please enter a number: 8
please enter a number: 9
please enter a number: 0
Geometric average is: 4.14717
Algebraic average is: 4.5
please enter a number: 0
please enter a number: 0
please enter a number: 0
please enter a number: 0
please enter a number: 3
please enter a number: 3
please enter a number: 0
please enter a number: 0
please enter a number: 0
please enter a number: 0
Geometric average is: 3
Algebraic average is: 0.6
please enter a number: 0
please enter a number: 0
please enter a number: 0
please enter a number: 0
please enter a number: 0
please enter a number: 0
please enter a number: 0
please enter a number: 0
please enter a number: 0
please enter a number: 0
No positive numbers entered, no geometrical average is possible.
Algebraic average is: 0
/* averages.cpp
* This program takes numbers and prints out the algebraical and geometrical averages.
*
* Saggi Mizrahi 05 November 2013
*/
#include <iostream>
#include <math.h>
using namespace std;
/* This program takes numbers and prints out the algebraical and geometrical averages.
*
* Input: The amount of numbers to read and then that amount of numbers.
* Output: The algebraical and geometrical (when possible) averages.
*/
int main()
{
// declaration of constants
// declaration of variables
int numEntries = 10;
int positiveEntriesNum = 0;
int num;
int sum = 0;
int mul = 1;
cout << "please enter number of entries: ";
cin >> numEntries;
// getting input from the user
for (int i = 0; i < numEntries; i++) {
cout << "please enter a number: ";
cin >> num;
sum += num;
if (num > 0) {
positiveEntriesNum++;
mul *= num;
}
}
if (positiveEntriesNum == 0) {
cout << "No positive numbers entered, no geometrical" <<
" average is possible." << endl;
} else {
cout << "Geometric average is: " <<
pow(mul, 1./positiveEntriesNum) << endl;
}
if (numEntries == 0) {
cout << "No numbers entered, no algebraic" <<
" average is possible." << endl;
} else {
cout << "Algebraic average is: " << sum / double(numEntries) <<
endl;
}
return 0;
}
please enter number of entries: 10
please enter a number: 1
please enter a number: 2
please enter a number: 3
please enter a number: 4
please enter a number: 5
please enter a number: 6
please enter a number: 7
please enter a number: 8
please enter a number: 9
please enter a number: 0
Geometric average is: 4.14717
Algebraic average is: 4.5
please enter number of entries: 10
please enter a number: 0
please enter a number: 0
please enter a number: 0
please enter a number: 0
please enter a number: 3
please enter a number: 3
please enter a number: 0
please enter a number: 0
please enter a number: 0
please enter a number: 0
Geometric average is: 3
Algebraic average is: 0.6
please enter number of entries: 10
please enter a number: 0
please enter a number: 0
please enter a number: 0
please enter a number: 0
please enter a number: 0
please enter a number: 0
please enter a number: 0
please enter a number: 0
please enter a number: 0
please enter a number: 0
No positive numbers entered, no geometrical average is possible.
Algebraic average is: 0
please enter number of entries: 0
No positive numbers entered, no geometrical average is possible.
No numbers entered, no algebraic average is possible.
/* averages.cpp
* This program takes numbers and prints out the algebraical and geometrical averages.
*
* Saggi Mizrahi 05 November 2013
*/
#include <iostream>
#include <math.h>
using namespace std;
/* This program takes numbers and prints out the algebraical and geometrical averages.
*
* Input: N numbers terminated with -1.
* Output: The algebraical and geometrical (when possible) averages.
*/
int main()
{
// declaration of constants
// declaration of variables
int numEntries = 0;
int positiveEntriesNum = 0;
int num = 0;
int sum = 0;
int mul = 1;
// getting input from the user
while(num >= 0) {
cout << "please enter a number: ";
cin >> num;
if (num >= 0) {
numEntries++;
sum += num;
if (num > 0) {
positiveEntriesNum++;
mul *= num;
}
}
}
if (positiveEntriesNum == 0) {
cout << "No positive numbers entered, no geometrical" <<
" average is possible." << endl;
} else {
cout << "Geometric average is: " <<
pow(mul, 1./positiveEntriesNum) << endl;
}
if (numEntries == 0) {
cout << "No numbers entered, no algebraic" <<
" average is possible." << endl;
} else {
cout << "Algebraic average is: " << sum / double(numEntries) <<
endl;
}
return 0;
}
please enter a number: 1
please enter a number: 2
please enter a number: 3
please enter a number: 4
please enter a number: 5
please enter a number: 6
please enter a number: 7
please enter a number: 8
please enter a number: 9
please enter a number: 0
please enter a number: -1
Geometric average is: 4.14717
Algebraic average is: 4.5
please enter a number: 0
please enter a number: 0
please enter a number: 0
please enter a number: 0
please enter a number: 3
please enter a number: 3
please enter a number: 0
please enter a number: 0
please enter a number: 0
please enter a number: 0
please enter a number: -1
Geometric average is: 3
Algebraic average is: 0.6
please enter a number: 0
please enter a number: 0
please enter a number: 0
please enter a number: 0
please enter a number: 0
please enter a number: 0
please enter a number: 0
please enter a number: 0
please enter a number: 0
please enter a number: 0
please enter a number: -1
No positive numbers entered, no geometrical average is possible.
Algebraic average is: 0
please enter a number: -1
No positive numbers entered, no geometrical average is possible.
No numbers entered, no algebraic average is possible.
/* decoct.cpp
* Program that prints out the decimal value of an octal number
*
* Saggi Mizrahi 5 November 2013
*/
#include <iostream>
using namespace std;
/* Input: An octal number.
* Output: The decimal representation of the input octal value.
*/
int main() {
// declaration of variables
char c = '\0';
int value = 0;
// collect input and build result
cout << "please enter an octal number: ";
c = cin.get();
while (c != '\n') {
value = (value * 8) + (c - 48);
c = cin.get();
}
// output result
cout << value << endl;
return 0;
}
please enter an octal number: 375
253
/* palindrome.cpp
* Program that prints if an input number is a palindrome or not.
*
* Saggi Mizrahi 5 November 2013
*/
#include <iostream>
using namespace std;
/* Input: A number.
* Output: outputs whether the input number is palindrome or not.
*/
int main() {
// declaration of variables
int number = 0;
int mirrorNumber = 0;
// collect input
cout << "please enter a number: ";
cin >> number;
// Create mirrored number
for (int tmp = number; tmp > 0; tmp /= 10) {
mirrorNumber *= 10;
mirrorNumber += tmp % 10;
}
// Output results
cout << number << " is ";
if (mirrorNumber != number) {
cout << "not ";
}
cout << "a palindrome" << endl;
return 0;
}
please enter a number: 5073705
5073705 is a palindrome
please enter a number: 507375
507375 is not a palindrome