Computer Programming 1 Lab

2020-09-24

Chang, Chi-Hung

Outline

  • Unix Command Review
  • Vim Tips
  • Simple C Program
  • Compile Your Program
  • How to Copy Your C Program?
  • Data Type
  • Operators in C
  • Exercise 1

Unix Command Review

cd -> change directory

cd ~
cd ~s109xx/test
cd public_html
cd /usr/share
cd ..
cd ../test

Unix Command Review

ls -> list files in current directory

ls
ls -l   -> list files details in current directory
ls -a   -> list all files (include hidden files) in current directory
ls -la  -> list all files with details in current directory

Unix Command Review

How to create/delete/copy files or directory?

  • mkdir test
    Create a directory named "test" in current directory.
  • cp fileX dirY/dirZ
    Copy fileX from current directory to ./dirY/dirZ.
  • cp fileX dirY/fileZ
    Copy fileX from current directory to dirY and rename to fileZ.
  • cp -r dirX dirY
    Copy dirX from current directory to dirY.

Unix Command Review

How to create/delete/copy files or directory?

  • mv fileA dirB
    Move fileA to dirB.
  • mv dirA dirB
    • If dirB exists, then move dirA under dirB.
    • If dirB doesn't exist, dirA is renamed to dirB.
  • rm x
    Remove file x or remove directory x if x is an empty directory.
  • rm -rf x
    Remove directory x and all its contents regardless the file is write-protect or not.

Unix Command Review

  • The path used on cd, mkdir, cp, mv, and rm can be absolute path or relative path.
  • Type pwd to see what the current directory is.
  • Type whoami to see your account's name.
  • Type logout to logout the system, or you can press Ctrl+D.
  • Type Ctrl+L to clean your screen.
  • Type Ctrl+C to stop the program which is running.

Unix Command Review

  • Remember, whenever you have problems using Unix, try man command.
    man ls
    man cp
    
  • man stands for manual.

Unix Command Review

Start Ghost account elm

  • Type /usr/local/bin/elm
  • Enter y 3 times and q 1 time
    elm h:400

Vim Tips

  • i, o, a, R -> change to insert mode
  • Esc -> back to normal mode
  • :, / -> enter command-line mode
  • :w -> save your work
  • :q -> quit vim
  • :wq, :x -> save and quit
  • :q! -> quit without saving
  • :xxx -> go to line xxx
  • /xxx -> search "xxx" in this file

Vim Tips

  • v -> character visual
  • V -> line visual
  • y -> copy
  • p -> paste
  • d -> delete (cut)
  • u -> undo
  • :nohl -> no highlight
  • gg=G -> auto indent

Simple C Program

  • main() is a entry point of program
    #include <stdio.h>
    
    int main(){
        int x;
        scanf("%d", &x);
        x = x + 2;
        printf("%d\n", x);
        return 0;
    }
    
    • #include <stdio.h> is for preprocessor
    • int main(){...} -> main function
    • scanf -> input
    • printf -> output
    • return 0 -> no error

Compile Your Program

How to compile your program?

  • make if you have Makefile.
    • Like a script. It runs gcc automatically.
  • gcc, GNU compiler.
    gcc xxxxx.c
    
    • It will compile xxxxx.c and generate the executable file a.out.
    • Or dump a lot of errors.

Compile Your Program

#include <stdio.h>

int main(){
    int a;
    printf("%d", a)
    return 0;
}
[ge10919@ghost]~ gcc test.c
test.c: In function 'main':
test.c:5: error: excepted ';' before 'return'

Compile Your Program

Type ./a.out to run the program.

[ge10919@ghost]~ ./a.out
134511260
  • Here is a "initialization" problem.

How to Copy Your C Script?

How to Copy Your C Script?

How to Copy Your C Script?

  • filezilla
    • Your Ghost password
      Filezilla h:500

How to Copy Your C Script?

How to Copy Your C Script?

How to Copy Your C Script?

  • ftp ghost.cs.nccu.edu.tw (command line)
    • ls -> list files or directories on server
    • pwd -> see what the current directory is on server
    • cd -> change directory on server
    • lls -> list files or directories on local
    • lcd -> change directory on local
    • get -> copy the file from server to local
    • mget -> copy more files from server to local
    • put -> copy the file from local to server
    • mput -> copy more files from local to server

more source

How to Copy Your C Script?

  • cat is a standard Unix utility that reads files sequentially, writing them to standard output.
    [ge10919@ghost]~ cat hello_world.c
    #include <stdio.h>
    
    int main(){
        printf("Hello World!\n");
        return 0;
    }
    [ge10919@ghost]~ 
    

How to Copy Your C Script?

How to Copy Your C Script?

  • NCCUCS Web Mail
    • WebDisk
      webmail h:500

How to Copy Your C Script?

  • NCCUCS Web Mail
    • Ghost
      webmail h:500

Data Type

data type h:550

Data Type

  • printf/scanf format specifier
    format specifier h:500

Data Type

  • Type Conversion
    • Implicit type conversion
      double number = 10;
      printf("%f\n", number/3);
      
    • Conversion
      int num = 3;
      float fnum = 3.5;
      float sum;
      sum = (float)num + fnum;
      

Data Type

  • Print float or double number
    • Number of digits
      double pi = 3.14159;
      printf("%f\n", pi);
      // 3.14159
      
      // What if I want to print "3.14"?
      printf("%.2f\n", pi);
      // 3.14
      
      printf("%d\n", pi);
      // 1293080650
      

Operator in C

  • Arithmetic Operator
    • +, -, *, /

      • Example: x = a + b
    • %

      • Module Operator and remainder of after an integer division.
      • Example: z = x % y
    • ++, --

      • Increase/Decrease operator increases/decreases the integer value by one
      • Example: i++, j--

Operator in C

  • Relational Operator
    • ==, !=
      • Checks if the values of two operands are equal or not.
      • Example: a == b, x != y
    • >, <
      • Checks if the value of left operand is greater/less than the value of right operand.
      • Example: a > b, c < d
    • >=, <=
      • Checks if the value of left operand is greater/less than or equal to the value of right operand.
      • Example: a >= b, c <= d

Operator in C

  • Logical Operator
    • &&
      • Called Logical AND operator.
      • Example: A && B
    • ||
      • Called Logical OR operator.
      • Example: A || B
    • !
      • Called Logical NOT operator.
      • Example: !(A && B)

Exercise 1

There is a rectangle in plane coordinates. Give you the coordinates of the upper-left and bottom-right points of the given rectangle. Please calculate the area of the rectangle.

  • Input:
    Two lines. Each line contains two numbers.
    The two numbers in the first line are the coordinate of the upper-left point (x1,y1)(x_1, y_1). The two numbers in the secend line are the coordinate of the bottom-right point (x2,y2)(x_2, y_2).
  • Output:
    The area of the rectangle.

Any Questions?