1 CSC2100B Data Structure Tutorial 1 Online Judge and C.
-
Upload
beryl-ursula-flynn -
Category
Documents
-
view
218 -
download
0
Transcript of 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.
![Page 1: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/1.jpg)
1
CSC2100BData Structure
Tutorial 1
Online Judge
and C
![Page 2: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/2.jpg)
2
Your TA team
People Your tutor team
CHAN Chi Kong Email: chanck[at]cse.cuhk.edu.hk
DING Ling Email: lding[at]cse.cuhk.edu.hk
FUNG Wai Shing Email: wsfung[at]cse.cuhk.edu.hk
LI Zhaorong Email: zrli[at]cse.cuhk.edu.hk
![Page 3: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/3.jpg)
3
Course Information
Course Web Page http://www.cse.cuhk.edu.hk/~csc2100b/
Course Newsgroup or Web news news://news.erg.cuhk.edu.hk/cuhk.cse.csc2100b Please visit regularly.
Anti-plagiarism policy http://www.cuhk.edu.hk/policy/academichonesty/
![Page 4: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/4.jpg)
4
Programming Assignments
There will be both written and programming parts in assignments. Written part: submit to the assignment box in 10/F
SHB. Programming part: via Online Judge systems.
You will receive your login Id for CSC2100B online judge via your [email protected] email account. Keep it safe and do not disclose it.
![Page 5: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/5.jpg)
5
/* CSC2100@ 07123456 a1234567 assg0_question0 */#include <stdio.h>
int main(int argc, char **argv){ int a, b;
printf("Please enter two numbers: \n"); scanf("%d %d", &a, &b); printf("The numbers you entered are %d and %d \n", a, b); printf("And their sum is %d \n", a + b);
return 0;}
Writing your assignment program1 Write your program using your favorite editor, e.g., vi, vim, pico, emacs.
2 Add a header at the first line of your program:
Student Id
CSC2100B Online Judge
Login Id
Problem Id
![Page 6: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/6.jpg)
6
Submitting your program3 Compile and test your program in Unix
- To compile in Unix: gcc –o myprogram myprogram.c
(If the –o option is omitted, the compilation output will be placed in a.out by default)
- To run: ./myprogram ( or ./a.out if –o is not specified)
4 Submit to Online Judge when ready:- Login to a sparc macine- mail csc2100@pc89072 < myprogram.c
5 Wait for reply in your CSE mailbox.
![Page 7: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/7.jpg)
7
Online Judge Reply Messages Common replies:
Accepted. Congratulation! Submission Error. <= Check your header line. Compile Error….. Runtime Error…..
Time Limit Exceeded. <=Check your algorithm. Infinite loops? Output Limited Exceeded <=Check your algorithm. Infinite loops? Float Point Exceptions. <= E.g. Division by 0 Segmentation Fault, Bus Error, etc. <= Check your code, e.g,
errors in pointers. Wrong Answer. Presentation Error.
![Page 8: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/8.jpg)
8
A word about “Presentation Error” Make sure your output is EXACTLY the same
as the specification (or sample program, if provided).
Check for upper case / lower case letters. Check for extra spaces / extra blank lines.
There should be no extra spaces at the end of a line, and
no extra blank lines at the end of the outputs.
The product is
![Page 9: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/9.jpg)
9
Demo – Online JudgeQuestion Id: 2008A0Q0Write a program to calculate the products of two integers. Input the number of products to be computed. While the number of products is not existed, do:
- Input two integers- Display their product
*You can assume the inputs are within the range 0 < x < 10000.
Sample input3
1 2
3 4
5 6
Expected inputThe product is 2
The product is 12
The product is 30
![Page 10: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/10.jpg)
10
DemoAttempt #1
/* CSC2100@ 01234567 aaaaaaaa WRONG-QID */#include <stdio.h>int main(int argc, char **argv){ short a = 0; short b = 0; short p; int i=0;
int count; scanf("%d", &count); while (i < count) { scanf("%hd %hd", &a, &b); p=a*b; printf("The product is %hd \n", p); }
return 0;}
Our actual test case
in online judge3
1 2
5 2
1000 1000
Submission Error: Please check the header format of your program.
![Page 11: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/11.jpg)
11
DemoAttempt #2
/* CSC2100@ 01234567 aaaaaaaa 2008A0Q0 */#include <stdio.h>int main(int argc, char **argv){ short a = 0; short b = 0; short p; int i=0;
int count; scanf("%d", &count); while (i < count) { scanf("%hd %hd", &a, &b); p=a*b; printf("The product is %hd \n", p); }
return 0;}
Our actual test case
in online judge3
1 2
5 2
1000 1000
Runtime Error: Time Limit Exceeded.
![Page 12: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/12.jpg)
12
DemoAttempt #3
/* CSC2100@ 01234567 aaaaaaaa 2008A0Q0 */#include <stdio.h>int main(int argc, char **argv){ short a = 0; short b = 0; short p; int i=0;
int count; scanf("%d", &count); while(i <count) { scanf("%hd %hd", &a, &b); p=a*b; printf("The product is %hd \n", p); i++; }
return 0;}
Our actual test case
in online judge3
1 2
5 2
1000 1000
Wrong Answer. Please check your program.
Expected OutputThe product is 2
The product is 10
The product is 1000000
![Page 13: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/13.jpg)
13
DemoAttempt #4
/* CSC2100@ 01234567 aaaaaaaa 2008A0Q0 */#include <stdio.h>int main(int argc, char **argv){ short a = 0; short b = 0; int p; int i=0;
int count; scanf("%d", &count); while(i<count) { scanf("%hd %hd", &a, &b); p=a*b; printf("The product is %d \n", p); i++; }
return 0;}
Our actual test case
in online judge3
1 2
5 2
1000 1000
Presentation Error.
Expected OutputThe product is 2
The product is 10
The product is 1000000
![Page 14: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/14.jpg)
14
DemoAttempt #5
/* CSC2100@ 01234567 aaaaaaaa 2008A0Q0 */#include <stdio.h>int main(int argc, char **argv){ short a = 0; short b = 0; int p; int i=0;
int count; scanf("%d", &count); while(i<count) { scanf("%hd %hd", &a, &b); p=a*b; printf("The product is %d\n", p); i++; }
return 0;}
Our actual test case
in online judge3
1 2
5 2
1000 1000
Accepted. Congratulations!
Expected OutputThe product is 2
The product is 10
The product is 1000000
![Page 15: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/15.jpg)
15
The C Compilation Model
Preprocessor
Compiler
Linker
Source Code
(additional code) Libraries
Executable Code
Object Code (machine instructions)
![Page 16: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/16.jpg)
16
C operators Arithmetic: +, -, *, /, %, ++, --
int a = 10, b, c;b = a++; /* a is now 11, b is 10 */c = ++b; /* a, b, c are all 11 */
Assignment: =, +=, -=, *=, /=, %=x += 2; x %= 2;
Relational: >, <, >=, <=, ==, != Logical: &&, ||, ! Bitwise: <<, >>, &, ^, |
![Page 17: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/17.jpg)
17
Data Types in C (32-bit Machine)Character char 8 bits – 255
Integer Types
short = short int 16 bits – 65535
int 32 bits – 4 X 109
long = int 32 bits – 4 X 109
long long 64 bits – 1 X 1019
unsigned char, short, int
Floating
Point Types
float 32 bits
double 64 bits
long double 128 bits
![Page 18: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/18.jpg)
18
Notes There is no boolean type in C.
Instead, non-zero values mean “true”, zero means “false”. E.g : int i = 5; while (i) { printf(“%d \n”, i); i--; }
No class and subclasses, no methods, no interfaces. (Think of everything belongs to the same class…)
No public / private / protected... Instead, we have functions, pointers, structures, and dyn
amic memory allocations.
![Page 19: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/19.jpg)
19
Constants Constants can be defined using #define at beginning of fi
le.
/* CSC2100@ 07123456 a1234567 assg0_question0 */#include <stdio.h>#define PI 3.14159
int main(int argc, char **argv){ float radius, area;
radius = 5; area = radius * radius * PI;
printf(“The area is %f \n“, area);
return 0;}
![Page 20: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/20.jpg)
20
Functions#include <stdio.h>
int sum(int x, int y){ return x + y;}
int main(int argc, char **argv){ int a, b; printf(“Enter 2 numbers”); scanf(“%d %d’, &a, &b); printf(“%d”, sum(a, b)); return 0;}
Notes: In stardard ANSI C:
•Local variables should be declared at the beginning of the function.
•Functions should be defined or declared before they are used.
•Local variables will not be automatically initialized.
•E.g, “int a” may contain garbage until a value is assigned.
![Page 21: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/21.jpg)
21
Pointer Variables Pointer variables are variables that store memory addr
esses. Pointer Declaration:
int x, y = 5;
int *ptr; /* ptr is a POINTER to an integer variable*/
Reference operator &:ptr = &y; /*assign ptr to the MEMORY ADDRESS of y.*/
Dereference operator *:x = *ptr; /* assign x to the int that is pointed to
by ptr */
![Page 22: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/22.jpg)
22
Pointer Example (1)int x;
int y = 5;
int *ptr;51004
?2000
...
y
ptr
?1000x
![Page 23: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/23.jpg)
23
Pointer Example (1)int x;
int y = 5;
int *ptr;
ptr = &y;
51004
10042000
...
y
ptr
?1000x
![Page 24: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/24.jpg)
24
Pointer Example (1)int x;
int y = 5;
int *ptr;
ptr = &y;
x = *ptr;
51004
10042000
...
y
ptr
51000x
![Page 25: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/25.jpg)
25
Pointer Example (2)int x = 10, y = 5;
int *p1, *p2;
51004
?2000
...
y
p1
101000 x
?2004 p2
p1
p25
y
10x
Memory
![Page 26: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/26.jpg)
26
Pointer Example (2)int x = 10, y = 5;int *p1, *p2;p1 = &x;p2 = &y;
51004
10002000
...
y
p1
101000 x
10042004 p2
p1
p25
y
10x
![Page 27: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/27.jpg)
27
Pointer Example (2)*p1 = 7;
*p2 = 11;
111004
10002000
...
y
p1
71000 x
10042004 p2
p1
p211y
7x
Pictorial View Memory View
![Page 28: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/28.jpg)
28
Pointer Example (2)p2 = p1; // Not the same as *p2 = *p1
111004
10002000
...
y
p1
71000 x
10002004 p2
p1
p211y
7x
Pictorial View Memory View
![Page 29: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/29.jpg)
29
Pointer Example (3) : A function that swaps two variables
void swap (int *px, int *py) { int temp; temp = *px;
*px = *py;*py = temp;
}
int main() {int x=1; y=2;swap(&x, &y);
}
//Note: this is not possible in Java!
![Page 30: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/30.jpg)
30
Pointer Exercise (1)What will be the value of x,y, *p1 and *p2?
int x = 7, y = 11;
int *p1, *p2;
p1 = &x;
p2 = &y;
*p1 = y;
*p2 = x;
p1
p2 y
x
![Page 31: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/31.jpg)
31
Pointer Exercise (2)What will be the value of x,y, *p1 and *p2?
int x = 7, y = 11, z = 3, *p1, *p2;p2 = &x;p2 = &y;
*p2 = 5;p1 = p2;p2 = &z;y = 6;
z = *p1;*p2 = x;
p1
p2 y
x
z
![Page 32: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/32.jpg)
32
Structure A collection of values (members)
Like a class in java or C++, but without methods.
struct time {int hh;int mm;int ss;
};
. . .
struct time t1;
t1.hh=20;
t1.mm=12;
t1.ss=30;
. . .
![Page 33: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/33.jpg)
33
Structure We can also use pointer to structure.
struct time {int hh;int mm;int ss;
};
struct time *t1;
(*t1).hh=20;
Pointer to structure is very common, so we gave it a short hand. The above is equivalent to:
struct time *t1;
t1->hh=20; /* Same as (*t1).hh=20; */
![Page 34: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/34.jpg)
34
Type Definition: typedef Allow us to define alias for a data type.
typedef int My_integer_type;. . . My_integer_type x=3;
Typedef can be used for structures.typedef struct { int hh; int mm; int ss } Time_type;. . .Time_type t1;T1.hh=12; . . .
![Page 35: 1 CSC2100B Data Structure Tutorial 1 Online Judge and C.](https://reader035.fdocuments.in/reader035/viewer/2022062409/5697c00e1a28abf838cc9c2c/html5/thumbnails/35.jpg)
35
The End
Any Question?