1chooo
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # PPL-HW1 Assignment 1 of CE2004, Principles of Programming Languages ##### Score: 100 points ##### Due Time: 24:00 6th April > #### P.S.: > 1. You need to type your answers in a file and print them out in answer sheets, then submit your answer sheets to the TAs through new-eeclass. > 2. Late submission will not be accepted. > 3. You can discuss these questions with your classmates; however, copying other student’s answers is strictly prohibited. --- ### (1) (6 points) The CPU of Mary’s computer can complete an instruction more quickly than the CPU of Tom’s CPU; hence, Mary’s computer can always complete a program more quickly than Tom’s computer. Is the above statement correct? Give your explanation. #### Ans. This statement is definitely not correct. Faster CPU indeed accompany with the faster speed of processing the program; nonetheless, the factors deciding the speed of completing a program not only stick to how quickly of the CPU, but also relate to the size of memory, amount of storage, efficiency of the code, and so on. Hence, despite of Mary's computer with a faster CPU, her computer not always complete a program more quickly than Tom's computer if Tom's computer with larger memory, storage or if the program is not optimized for the Mary's CPU architecture. --- ### (2) (12 points) Good language readability can improve writability. Good language writability is detrimental to readability. #### (a) Which one of the above two statements is correct? Which one of the above two statements is wrong? #### (b) Give your explanation. #### Ans. Good language readability can improve writability. This statement is correct. Once the programming is easily to read which means more understandable either, it can help the programmer easily to express their thoughts and efficiently to realize what others' communicating about. Good language writability is detrimental to readability. This statement is wrong. Actually, if language is easy to write, it often leads to writing more accurately, clearly, and easier to understand. Above all, good programming language readability and writability are not the opposite relationship. Both are important for the developer to cummunicate mutually. Therefore, we have to take care of both of them when we are designing the program to make our code better. --- ### (3) (9 points) What follows is an excerpt of a **Javascript** program. Assume before location 1, variable `list` has never been used. ```c : -- location 1 list = [1, 2] prefix= list -- location 2 prefix = 47 list = prefix -- location 3 : ``` #### (a) At location 1, what is the data type of variable `list`? #### (b) At location 2, what is the data type of variable `prefix`? #### (c) At location 3, what is the data type of variable `list`? #### Ans. ##### (a) The data type of variable `list` is array. ##### (b) The data type of variable `prefix` is array. ##### (c) The data type of variable `list` is number. --- ### (4) (12 points) A program consists of the following two files, `fileu.c` and `filev.c` ```c /*============ fileu.c ================*/ int a = 100; // location 1 extern int t; // location 2 int bar(int y) { // location 3 int x; // location 4 x=y+t; // location 5 return(x); } // location 6 /*============ filev.c ================*/ #include<stdio.h> int t=9; // location 7 extern int a; // location 8 extern int bar(int); // location 9 int main(){ // location 10 int z; // location 11 printf("a=%d\n", a); printf("bar(3)=%d\n",bar(3)); } // location 12 ``` #### (a) List the locations of all variable definitions in the above two files. #### (b) List the locations of all variable declarations in the above two files. #### (c) List the locations of all function definitions in the above two files. #### (d) List the locations of all function declarations in the above two files. P.S.: A function formal parameter is also deemed as a variable. #### Ans. ##### (a) Variable definitions * fileu.c: * Location 1: a (global variable) * Location 4: x (local variable) * filev.c: * Location 7: t (global variable) * Location 11: z (local variable) ##### (b) Variable declarations * fileu.c: * Location 2: t (external variable) * filev.c: * Location 8: a (external variable) ##### (c) Function definitions * fileu.c: * Location 3: bar function ##### (d) Function declarations * filev.c: * Location 9: bar function (external function) --- ### (5) (8 points) What follows is a **C** program. ```c #include <stdio.h> int total_income, total_visitors_global; void zoo(char *name, int visitors) { int adult, children; static int total_visitors=0; : total_visitors=total_visitors+visitors; // location 1 total_visitors_global=total_visitors; : } int main() { int ticket_price_each_animal_type=2; printf(“Good Morning!\n”); // location 2 zoo(“giraff”, 600); zoo(“elephant”, 300); zoo(“hippo”,100); total_income=ticket_price_each_animal_type*total_visitors_global; : } ``` #### (a) At location 1, list the names of variables or parameters that have memory assigned to it. #### (b) At location 2, list the names of variables or parameters that have memory assigned to it. #### Ans: ##### (a) Location 1 * total_visitors: static local variable and being assigned 0. * visitors: parameter of `zoo` function, the value is assigned by the value passed by calling `zoo`. ##### (b) Location 2 * ticket_price_each_animal_type: local variable and being assigned 2. * total_visitors_global: global variable and being assigned value of total_visitors. --- ### (6) (9 points) What follows is the content of program `add_a.c`. ```c /*----------------------------------------------------*/ #include <stdio.h> int a=1 , b=6; int c[10000]={1}; int main() { a=b+c[0]; /* location 1*/ } /*----------------------------------------------------*/ ``` Assume `add_a.exe` is the executable of `add_a.c`. What follows is the content of program `add_b.c`. ```c /*----------------------------------------------------*/ #include <stdio.h> int a=1 , b=6; int c[10000]; int main() { c[0]=1; a=b+c[0]; /*location 2*/ } /*-----------------------------------------------------*/ ``` Assume `add_b.exe` is the executable of `add_b.c`. #### (a) At location 1 of `add_a.c` what is the value of variable `a`? #### (b) At location 2 of `add_b.c` what is the value of variable `a`? #### (c) For files `add_a.exe` and `add_b.exe`, which of these two files has larger size and why? #### Ans. ##### (a) The value of variable `a` is 7. ##### (b) The value of variable `a` is 7. ##### (c) `add_a.exe` is larger than `add_b.exe`. It is that in `add_a.c` the whole of the array c is assigned initially with 1. However, in `add_b.c` is only the first element of array c is assigned 1. Therefore, `add_a.exe` is larger than `add_b.exe`. --- ### (7) (12 points) #### (a) What follows is a C program. ```c # include <stdio.h> int a; int bar(int x, int y) { int b; return b = x+y; } int main() { int *p; p = (int *) malloc (sizeof(int)); *p = bar (8,9); } ``` In the above program, ##### (i) which variables are static variables? ##### (ii) And which variables are stack dynamic variables? ##### (iii) And which variables are explicit-heap dynamic variables? P.S.: A function formal parameter is also deemed as a variable. #### (b) What follows is a Java program excerpt. ```java class Circle { int setVariable(int s) { int r; r=6; return s+r; } } public class ShowArea { public static void main(String args[]) { Circle cir= new Circle(); int a; a= cir.setVariable(8); } } ``` In the above program, ##### (i) which variables are static variables? ##### (ii) And which variables are stack dynamic variables? ##### (iii) And which variables are explicit-heap dynamic variables? #### Ans. ##### (a) * (i) There are no static varables. * (ii) `a, b, p, x, y`. * (iii) `p` because it is allocated using malloc(). ##### (b) * (i) There are no static variables. * (ii) `cir` and `a` because they are declared inside a `main()`. * (iii) There are no explicit-heap dynamic variables. --- ### (8) (10 points) What follows is the content of a C program `example.c`. ```c #include <stdio.h> // location 11 int bar(int x) { int a, b, c; // location 1 c=x; // location 2 b=x*9; // location 3 a=foo(); // location 4 return a; // location 5 } int foo() { int a=1, b=2, c; // location 6 c=a+b; // location 7 return c; // location 8 } int main() { int a=1, b=2, c=3; // location 9 return bar(a); // location 10 } ``` #### (a) Are variable `b` defined at location 1, variable `b` defined at location 6, and variable `b` defined at location 9 the same variable? #### (b) During run time, at what locations of the above program, variable `b` defined at location 9 and variable `b` defined at location 1 have storage bound to them, but variable `b` defined at location 6 does not have storage bound to it? #### Ans. ##### (a) * Location 1: only being defined. * Location 6: being defined and assigned 2 * Location 9: being defined and assigned 2 In sum up, they are not the same variable. ##### (b) * During run time, variable `b` defined at `location 9` has storage bound to it when execuating `main` function. * Variable `b` defined at `location 1` has storage bound to it only when the `bar` function is called, and then released when `bar` returns. * Variable `b` defined at `location 6` has storage bound to it only when the `foo` function is called, and then released when `foo` returns. --- ### (9) (8 points) Assume each integer variable uses four bytes to store it values. And each float point variable uses four bytes to store its value. For the following two C program excerpts, (a) and (b), which of them have a type error? Explain your answers. #### (a) ```c int a; union course { int b; float c; } security; security.b = 3; // location 1 a = security.b; // location 2 ``` #### (b) ```c int a; union course { int b; float c; } security; security.c = 3.3; // location 3 a = security.c; // location 4 ``` #### Ans. In program(a), No type error. `a` is defined as `int` type, then in the union of `course` the type of `b` is `int` type. Therefore, in location 1, the variable 3 assigned to `security.b` is acceptable. In program(b), Type error occur in the location 4. `security.c` is defined as `float` type, `a` is defined as `int` type; therefore, while `security.c` is assigned to `a`, it will occur the type error and it might have resulted in truccation error, which is a loss of precision. --- ### (10) (8 points) What follows is a **Fortran** program. ```f PROGRAM Hello IMPLICIT NONE INTEGER :: Patrick island, a PRINT *, 'Welcome to Fortran' a=6 Pat rick is land = 19 Patrick island = Pat rick is land + a ! line 8 END PROGRAM Hello ``` After line 8 of the above program is executed, ##### (i) what is the value of variable `Patrick island`? ##### (ii) what is the value of variable `Pat rick is land`? #### Ans: ##### (i) It will occur the `syntax error`, `Patrick island` and `Pat rick is land` is not allowed, we can rename with `Patrick_island`, then we got the value of `Patrick_island`, 25. ##### (ii) It also occur the `syntax error`, `Pat rick is land` is not allowed; therefore the value of it is not relevant. --- ### (11) (6 points) Compiler Optimization tries to improve programs by making them smaller or faster or both. Hence, we should always use compiler optimization to compile our programs. Is the above claim correct? Give your explanation. #### Ans. "We should always use compiler optimization to compile our programs" is not completely correct. It may not always the best choice to conduct the program. * Optimization might have resulted in some bugs because not every cases are suitable to optimize the code. * Optimization will increase the time of conducting the larger projects because it take more time to optimize, not to conduct. * Hard to debug. It might have broken the original style of the code that we can't find the error when we want to realize what we have written. * Continue with the last points, hard to maintain because the original style of the code have changed, then we need to take more time to understand. Above all, it is not always suitable for optimization with the compiler, it will decrease the value of the programmer. We have to realize the results when using the tools we have, then decide which tools we can use case-by-cases to prevent the results we don't hope.

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully