---
# System prepended metadata

title: 2023 OOP Homework 1

---

---
tags: 2023 OOP 
---
# 2023 OOP - Homework 1
* Deadline: 10/21 - 14:00
* Upload your homework to moodle platfarm.
* Please consult with TA if you have any question.
    * FB or Email

:::info
There are five questions in homework1. Please answer the question and put the execute result picture in report.docx.
:::


## Problem 1 (20% + 5%)
Compare the difference between using "inline" function and regular function calls. Follow the steps below and write your analysis in a file called hw1.docx.

### Step 1:

Compile the **p1.cpp**, then run the executable. The total time taken to run this program (in seconds) will be displayed as the final several lines on the screen. This is the time spent for regular function calls. Modify the code properly and run this program **10** times and record the total time taken for each run. Then take the average and name it as **Avg_no_inline**.

#### For example: After you run the program 10 times, you may get 
```
1st run: 76.1 seconds
2nd run: 76.3 seconds
3rd run: 77.2 seconds
4th run: 75.1 seconds
5th run: 78.1 seconds
6th run: 77.1 seconds
7th run: 76.1 seconds
8th run: 78.1 seconds
9th run: 76.2 seconds
10th run: 77.1 seconds
Then compute Avg_no_inline and you get the average of these 10 numbers = 76.74
```
### Step 2
Change **```cube_regular()```** in line 28 of p1.cpp to  **```cube_inline()```**. Then save the file and re-compile it, then run the executable. The total time taken to run this program will be displayed again on the screen. This is the time spent for running inline usage. Run this program **10** times and record the total time taken for each run. Then take the average and name it as **Avg_inline**. 

#### For example: After you run the program 10 times, you may get 
```
1st run: 76.1 seconds
2nd run: 76.3 seconds
3rd run: 77.2 seconds
4th run: 75.1 seconds
5th run: 78.1 seconds
6th run: 77.1 seconds
7th run: 76.1 seconds
8th run: 78.1 seconds
9th run: 76.2 seconds
10th run: 77.1 seconds
Then compute Avg_inline and you get the average of these 10 numbers = 76.74
```


### Additional questions (5%)
Change **```cube_regular()```** in line 28 of p1.cpp to  **```cube_macro()```**. Then save the file and re-compile it, then run the executable. The total time taken to run this program will be displayed again on the screen. This is the time spent for running inline usage. Run this program **10** times and record the total time taken for each run. Then take the average and name it as **Avg_macro**. 

::: success
Score:
1. Screenshot the results after running cube_regular() and cube_inline() ten times, then put in report.docx. (10%)
2. Compare the difference between the cube_regular(), cube_inline(). (10%)
3. Compare the difference between the cube_macro() and cube_inline(). (5%)
:::
 

## Problem 2 (20%)
Please define two namespaces, **dimensionTwo** and **dimensionThree**, and put a function named "print()" in both namespaces. The function of print() is:

* The print() in the dimenesionTwo print "This is dimentionTwo".
* The print() in the dimenesionThree print "This is dimentionThree".

Finally, In the main function, dimensionTwo uses namespace to call the print function, and dimensionThree uses the scope resolution operator(::) to call the print function.

::: success
Score:
1. Write the program and screenshot the output result. (10%)
2. Explain what **namespaces** are and explain how the code works. (10%)
:::

## Problem 3 (20%)
Write a complete C++ program with the two alternate functions specified below, each of which multiplies the variable count by itself, defined in main. Then compare and contrast the two approaches. These two functions are

a) function **multipleByValue** that passes a copy of count by value, multiplies the copy by itself and returns the new value and 
b) function **multipleByReference** that passes count by reference via a reference parameter and multiplies the original value of count by itself through its alias(i.e., the refernece parameter)

::: success
Score:
1. Write the program and screenshot the output result. (6%)
2. Explain what the **reference** is and how it works. (7%)
3. Explain what differnce between the **passing by value** and **passing by reference** are and explain how the code works.(7%)
:::

## Problem 4 (20%)

Write a program that uses a function template called swap the contents of two arguments. Test the program using integer, character and floating-point arguments.

::: success
Score:
1. Write the program and screenshot the output result. (10%)
2. Explain what the **template** is and how it works. (10%)
:::

## Problem 5 (20%)

Please fill in the **AAAA**, **BBBB**, **CCCC** and answer the following question.

``` =C++
#include <iostream>
using namespace std;

void swap1(int* a, int* b);
void swap2(int& a, int& b);
void swap3(int& a, int& b);
void swap4(int* a, int* b);

int main(){

    int a = 1; 
    int b = 2;
    
    cout << "a=" << a << "; b=" << b << endl; 
    
    // swap by AAAA
    swap1(&a, &b);
    cout << "a=" << a << "; b=" << b << endl;
    
    // swap by BBBB
    swap2(a, b);
    cout << "a=" << a << "; b=" << b << endl;

}

void swap1(int* a, int* b){
    /* 
        CCCC  
              */
}

void swap2(int& a, int& b){
    int tmp = b;
    b = a; 
    a = tmp;
}

void swap3(int& a, int& b){
    int& tmp = b;
    b = a;
    a = tmp;
}

void swap4(int* a, int* b){
    int* tmp = b;
    b = a;
    a = tmp;
}

```
::: success
Score:
1. Write the program and screenshot the output result. (2%)
2. Fill in the correct answer of **AAAA**, **BBBB**, **CCCC**.(2% ea)
3. Run the function **swap3()** and explain why this is the result. (6%)
4. Run the function **swap4()** and explain why this is the result. (6%)
:::


## Note 
Compress all the files (including your report and source code files), and name the compressed file as ```A1115500_hw3.zip(or .rar)``` using your student ID. Then upload the compressed file to the **moodle** platform. 

The file structure should be like following forms:
```
|-A1115500_hw1.zip (.rar)
| |-p1.cpp
| |-p2.cpp
| |-p3.cpp
| |-p4.cpp
| |-p5.cpp
| |-report.pdf
| |-(And other files...)
```
or
```
|-A1115500_hw1.zip (.rar)
| |-A1105500 (Folder)
| | |-p1.cpp
| | |-p2.cpp
| | |-p3.cpp
| | |-p4.cpp
| | |-p5.cpp
| | |-report.pdf
| | |-(And other files...)
```

**Don't cheating**, or you will get 0 for this homework. If you can’t finish this homework before deadline, just hand in your unfinished code and report.

**Be honest with yourself.**