1 CSC2100B Data Structure Tutorial 1 Online Judge and C.

Slides:



Advertisements
Similar presentations
Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance.
Advertisements

Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
C Programming Day 1 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
Kernighan/Ritchie: Kelley/Pohl:
Zhang Hongyi CSCI2100B Data Structures Tutorial 2
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
C Primer CAS CS210 Ying Ye Boston University. Outline Hello, world Basics in C Comparison of C and Java.
CS100A, Fall 1997, Lectures 221 CS100A, Fall 1997 Lecture 22, Tuesday 18 November Introduction To C Goal: Acquire a reading knowledge of basic C. Concepts:
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
C Programming Day 2 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Computer Science 210 Computer Organization Introduction to C.
Chapter 11 Programming in C Compilation vs. Interpretation Different ways of translating high-level language Compilation translates code into machine.
UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming1 Introduction to C – Part 2.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Introduction to C Tom Chao Zhou CSC2100B Data Structures Tutorial 1.
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
C++ Tutorial Hany Samuel and Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2006 by Douglas.
UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
1 Programming in C Hello World! Soon I will control the world! Soon I will control the world!
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
Introduction to Programming
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
CHAPTER # 2 Part 2 PROGRAMS AND DATA 1 st semster King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Computer Programming for Engineers
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Online Judge System Tom Chao Zhou CSC2100B Data Structures Tutorial 2.
Introduction to C Zhengwei Yang CSC2100 Data Structures Tutorial 1.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial.
UNIMAP Sem2-07/08EKT120: Computer Programming1 Week 2 – Introduction to Programming.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
Basic Data Types & Memory & Representation. Basic data types Primitive data types are similar to JAVA: char int short long float double Unlike in JAVA,
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Bill Tucker Austin Community College COSC 1315
Basic Data Types & Memory & Representation
Chapter # 2 Part 2 Programs And data
The Machine Model Memory
Computer Science 210 Computer Organization
A bit of C programming Lecture 3 Uli Raich.
Java Primer 1: Types, Classes and Operators
EDUSITE Introduction to C manjari2707.wordpress.com.
INC 161 , CPE 100 Computer Programming
C Basics.
Computer Science 210 Computer Organization
CS 240 – Lecture 18 Command-line Arguments, Typedef, Union, Bit Fields, Pointers to Functions.
Yuanming Yu CSC2100B Data Structures Tutorial 1
Introduction to C Topics Compilation Using the gcc Compiler
Chapter # 2 Part 2 Programs And data
Introduction to C Topics Compilation Using the gcc Compiler
More Loops Topics Relational Operators Logical Operators for Loops.
Variables in C Topics Naming Variables Declaring Variables
DATA TYPES There are four basic data types associated with variables:
Presentation transcript:

1 CSC2100B Data Structure Tutorial 1 Online Judge and C

2 Your TA team People Your tutor team CHAN Chi Kong  chanck[at]cse.cuhk.edu.hk DING Ling  lding[at]cse.cuhk.edu.hk FUNG Wai Shing  wsfung[at]cse.cuhk.edu.hk LI Zhaorong  zrli[at]cse.cuhk.edu.hk

3 Course Information Course Web Page Course Newsgroup or Web news news://news.erg.cuhk.edu.hk/cuhk.cse.csc2100b Please visit regularly. Anti-plagiarism policy

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 Keep it safe and do not disclose it.

5 /* a assg0_question0 */ #include 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 program 1 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

6 Submitting your program 3 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 < myprogram.c 5 Wait for reply in your CSE mailbox.

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.

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

9 Demo – Online Judge Question Id: 2008A0Q0 Write 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 < Sample input Expected input The product is 2 The product is 12 The product is 30

10 Demo Attempt #1 /* aaaaaaaa WRONG-QID */ #include 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 judge Submission Error: Please check the header format of your program.

11 Demo Attempt #2 /* aaaaaaaa 2008A0Q0 */ #include 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 judge Runtime Error: Time Limit Exceeded.

12 Demo Attempt #3 /* aaaaaaaa 2008A0Q0 */ #include 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 judge Wrong Answer. Please check your program. Expected Output The product is 2 The product is 10 The product is

13 Demo Attempt #4 /* aaaaaaaa 2008A0Q0 */ #include 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 judge Presentation Error. Expected Output The product is 2 The product is 10 The product is

14 Demo Attempt #5 /* aaaaaaaa 2008A0Q0 */ #include 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 judge Accepted. Congratulations! Expected Output The product is 2 The product is 10 The product is

15 The C Compilation Model Preprocessor Compiler Linker Source Code (additional code) Libraries Executable Code Object Code (machine instructions)

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: >, &, ^, |

17 Data Types in C (32-bit Machine) Characterchar8 bits – 255 Integer Types short = short int16 bits – int32 bits – 4 X 10 9 long = int32 bits – 4 X 10 9 long 64 bits – 1 X unsigned char, short, int Floating Point Types float32 bits double64 bits long double128 bits

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 dynamic memory allocations.

19 Constants Constants can be defined using #define at beginning of file. /* a assg0_question0 */ #include #define PI int main(int argc, char **argv) { float radius, area; radius = 5; area = radius * radius * PI; printf(“The area is %f \n“, area); return 0; }

20 Functions #include 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.

21 Pointer Variables Pointer variables are variables that store memory addresses. 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 */

22 Pointer Example (1) int x; int y = 5; int *ptr; ? y ptr ? 1000 x

23 Pointer Example (1) int x; int y = 5; int *ptr; ptr = &y; y ptr ? 1000 x

24 Pointer Example (1) int x; int y = 5; int *ptr; ptr = &y; x = *ptr; y ptr x

25 Pointer Example (2) int x = 10, y = 5; int *p1, *p2; ? y p x ? 2004 p2 p1 p2 5 y 10 x Memory

26 Pointer Example (2) int x = 10, y = 5; int *p1, *p2; p1 = &x; p2 = &y; y p x p2 p1 p2 5 y 10 x

27 Pointer Example (2) *p1 = 7; *p2 = 11; y p x p2 p1 p2 11 y 7 x Pictorial View Memory View

28 Pointer Example (2) p2 = p1;// Not the same as *p2 = *p y p x 2004 p2 p1 p2 11 y 7 x Pictorial View Memory View

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!

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

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

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

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

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

35 The End Any Question?