Introduction to Computer Science - Exercise 2

Name: Saggi Mizrahi • ID: 032493124 • Group: מעוף א' • Date: November 1, 2013


1.

const int CURRENT_YEAR = 2008;
const char PROGRAM_TERMINATION_INPUT = 'x';

2.

Code

/* unitconvert.cpp
 * This program converts centimeters to inches
 *
 * Saggi Mizrahi 29 October 2013
 */
#include <iostream>

using namespace std;

/* Converts from centimeters and prints the input length in inches.
 * Input: a length in centimeters as a double.
 * Output: the length converted to inches.
 */
int main()
{
    // declaration of constants
    const double CONVERSION_RATION = 0.3937;

    // declaration of variables
    double lengthInCentimeters = 0;
    double lengthInInches = 0;

    // getting input from the user
    cout << "please enter a length in centimeters: ";
    cin >> lengthInCentimeters;

    // computation
    lengthInInches = CONVERSION_RATION * lengthInCentimeters;

    // outputting the result
    cout << "The length is equal " << lengthInInches << " in inches" << endl;
}

Output

please enter a length in centimeters: 10
The length is equal 3.937 in inches

3.

Code

/* radiusFinder.cpp
 * This program prints the circumference and area of a circle according to an
 * input radius.
 *
 * Saggi Mizrahi 29 October 2013
 */
#include <iostream>

using namespace std;

/*
 * Calculate a circle's circumference and area using it's radius
 * Input: a radius as a double.
 * Output: the circumference and area of a circle with said radius.
 */
int main()
{
    // declaration of constants
    const double PI = 3.14159;

    // declaration of variables
    double radius = 0;
    double circumference = 0;
    double area = 0;

    // getting input from the user
    cout << "please enter the circle radius in: ";
    cin >> radius;

    // computation
    circumference = PI * 2 * radius;
    area = PI * radius * radius;

    // outputting the result
    cout << "The circle's circumference is " << circumference << endl;
    cout << "The circle's area is " << area << endl;
}

Outputs

please enter the circle radius in: 10
The circle's circumference is 62.8318
The circle's area is 314.159

please enter the circle radius in: 0
The circle's circumference is 0
The circle's area is 0

please enter the circle radius in: 6.35
The circle's circumference is 39.8982
The circle's area is 126.677

please enter the circle radius in: 6482.
The circle's circumference is 40727.6
The circle's area is 1.31998e+08

4.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

int main()
{
 int j = -999 ,k = - 998 ,h = - 997;

 cout << "\nEnter 2 numbers:";
 cin >> j;
 cin >> k;

 cout << "\nThe variable j is " << j;
 cout << "\nThe variable k is " << k;

 cout << "\nEnter 2 numbers:";
 cin >> h;
 cin >> j;

 cout << "The variable h is " << h;
 cout << "The variable j is now " << j;
}

Outputs


Enter 2 numbers:12 17

The variable j is 12
The variable k is 17
Enter 2 numbers:15 100
The variable h is 15The variable j is now 100


Enter 2 numbers:0x1a 017 

The variable j is 0
The variable k is 0
Enter 2 numbers:The variable h is -997The variable j is now 0

cin failed at line 11



Enter 2 numbers:10 11 12 13 14 15

The variable j is 10
The variable k is 11
Enter 2 numbers:The variable h is 12The variable j is now 13

Did not fail, two last two inputs were ignored.



Enter 2 numbers:1 2 "3"

The variable j is 1
The variable k is 2
Enter 2 numbers:The variable h is 0The variable j is now 1

cin failed at line 17



Enter 2 numbers:1 2.4

The variable j is 1
The variable k is 2
Enter 2 numbers:The variable h is 0The variable j is now 1

cin failed at line 17



Enter 2 numbers:1 abcd

The variable j is 1
The variable k is 0
Enter 2 numbers:The variable h is -997The variable j is now 1

cin failed at line 11

5.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
using namespace std;
void main () {
    int j, k;
    double r;
    char c;

    c = '9';
    j = (int)c;
     r = 8.2;
     k = (int)r;
     k = k + 0.9;
     j = r + 0.9;
     c = (char)r;
}

משתנה

שורה

j

k

r

c

8

לא מאותחל

לא מאותחל

לא מאותחל

57

9

57

לא מאותחל

לא מאותחל

57

10

57

לא מאותחל

8.2

57

11

57

8

8.2

57

12

57

8

8.2

57

13

57

8

9.1

57

14

57

8

9.1

9

6.

/* triprinter.cpp
 * This program prints seven letter words as an upside down triangle and then
 * writes the individual letters ASCII values.
 *
 * Saggi Mizrahi 29 October 2013
 */

#include <iostream>

using namespace std;

/* Input: a seven letter word
 * Output: The word written as an upside down triangle and then the individual
 *         letters' ASCII values seperated with spaces.
 */
int main() {
    // declaration of variables
    char c1;
    char c2;
    char c3;
    char c4;
    char c5;
    char c6;
    char c7;

    // getting input from the user
    cout << "please enter a 7 letter word: ";
    cin >> c1 >> c2 >> c3 >> c4 >> c5 >> c6 >> c7;

    // outputting the upside down triangle
    cout << c1 << " " << c2 << " " << c3 << " " << c4 << endl;
    cout << "  " << c5 << " " << c6 << endl;
    cout << "   " << c7 << endl;

    // outputting seperator empty line
    cout << endl;

    // outputting ASCII values
    cout << (int)c1 << " " << (int)c2 << " " << (int)c3 << " ";
    cout << (int)c4 << " " << (int)c5 << " " << (int)c6 << " ";
    cout << (int)c7 << endl;
}

Outputs

please enter a 7 letter word: student
s t u d
  e n
   t

115 116 117 100 101 110 116

please enter a 7 letter word: abcdefg
a b c d
  e f
   g

97 98 99 100 101 102 103

7.

Code

/* drivetime.cpp
 * Program that calculates the arrivale time given the start time and duration
 * in seconds
 *
 * Saggi Mizrahi 29 October 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;

    // declaration of variables
    int hours;
    int minutes;
    int seconds;
    int duration;

    // 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;

    // caluculating the end time
    seconds += duration;

    minutes += seconds / SECONDS_IN_MINUTE;
    seconds %= SECONDS_IN_MINUTE;

    hours += minutes / MINUTES_IN_HOUR;
    minutes %= MINUTES_IN_HOUR;

    // outputting the result
    cout << hours << " " << minutes << " " << seconds << endl;
}

Outputs

please enter the starting time (H M S) followed by drive duration in seconds: 12 31 10 4004
13 37 54

8.

Code

/* risha.cpp
 *
 * A program that reads 4 numbers and prints out their "Risha Maximum" and
 * their average in an accurate representation and rounded up to the nearest
 * whole number.
 *
 * Saggi Mizrahi 29 October 2013
 */

#include <iostream>

/* Even though the exercise asked to import stdlib.h for __max I can't do it
 * since __max() is non standard and MSVC specific. Since I'm using GCC I chose
 * to just implement it here. I know we are not supposed to use stuff we
 * haven't learned yet (ie. Macros) but it's the only way I can get this to
 * compile.
 */
#define __max(a, b) ((a) > (b) ? (a) : (b))

using namespace std;

/* Input: 4 integers.
 * Output: Their "Risha Maximum" their average and their average rounded up to
 *         the nearest whole number.
 */
int main() {
    // declaration of variables
    int num1;
    int num2;
    int num3;
    int num4;

    int max2;
    int max3;
    int max4;

    double average;

    // getting input from the user
    cout << "please enter 4 positive numbers: ";
    cin >> num1 >> num2 >> num3 >> num4;

    // calculating the maximums
    max2 = __max(num1, num2);
    max3 = __max(max2, num3);
    max4 = __max(max3, num4);

    // outputting maximums
    cout << num1 << ", " << max2 << ", " << max3 << ", " << max4 << endl;

    // calculating average
    average = (num1 + num2 + num3 + num4) / 4.;

    // outputting average
    cout << average << endl;
    cout << int(average + .5) << endl;
}

Outputs

please enter 4 positive numbers: 5 1 12 9
5, 5, 12, 12
6.75
7

Expressions

1.

Expression: x = y;
Expression value: 5
Side effects: x is set to 5 (the value of y)

2.

Expression: z = x = y;
Expression value: 5
Side effects: z is set to 6 (the value of the expression x = y which results in x being set to 6 (the value of y))

3.

Expression: x = ++y;
Expression value: 6
Side effects: x is set to 6 (the value of y + 1) after y is incremented.

4.

Expression: x = y++;
Expression value: 5
Side effects: x is set to 5 (the value of y) before y is incremented.

5.

Expression: x = ++y + 4;
Expression value: 10
Side effects: x is set to 10 (the value of y + 4) after y is incremented.

6.

Expression: x = y++ + 4;
Expression value: 9
Side effects: x is set to 10 (the value of y + 4) before y is incremented.