Online Judge and C. Roy Chan. January 12, Outline Information Online Judge Introduction to C. CSC2100B Data Structures Tutorial 1

Similar documents
List, Stack and Queue Implementation

Final CSE 131B Spring 2004

Final CSE 131B Spring 2005

Lists, Stacks and Queues in C. CHAN Hou Pong, Ken CSCI2100A Data Structures Tutorial 4

PRINCIPLES OF OPERATING SYSTEMS

Procedural programming with C

MIDTERM TEST EESC 2031 Software Tools June 13, Last Name: First Name: Student ID: EECS user name: TIME LIMIT: 110 minutes

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012

CSE 303 Lecture 8. Intro to C programming

EM108 Software Development for Engineers

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

Lectures 5-6: Introduction to C

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #07. Topic: Pointer in C Date:

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Flowchart, Types, and Values

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

Structured programming

Chapter 11 Introduction to Programming in C

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

ELEC 377 C Programming Tutorial. ELEC Operating Systems

Summary of Last Class. Processes. C vs. Java. C vs. Java (cont.) C vs. Java (cont.) Tevfik Ko!ar. CSC Systems Programming Fall 2008

CSC111 Computer Science II

Page 1. Agenda. Programming Languages. C Compilation Process

Course organization. Course introduction ( Week 1)

Pointers. Memory. void foo() { }//return

Arrays and Pointers. CSC209: Software Tools and Systems Programming (Winter 2019) Furkan Alaca & Paul Vrbik. University of Toronto Mississauga

Chapter 11 Introduction to Programming in C

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

SU2017. LAB 1 (May 4/9) Introduction to C, Function Declaration vs. Definition, Basic I/O (scanf/printf, getchar/putchar)

ECE 15B COMPUTER ORGANIZATION

Hwk 1: UNIX, C Programming, and Memory Management

Lectures 5-6: Introduction to C

Arrays and Pointers. CSE 2031 Fall November 11, 2013

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

CS 61C: Great Ideas in Computer Architecture Introduction to C

Language comparison. C has pointers. Java has references. C++ has pointers and references

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 2. Spring 2016

Kurt Schmidt. October 30, 2018

Chapter 11 Introduction to Programming in C

Contents. Custom data type struct data type Fixed length memory Alias data type

Java and C CSE 351 Spring

Lecture 03 Bits, Bytes and Data Types

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

High Performance Computing MPI and C-Language Seminars 2009

Introduction to C. Zhiyuan Teo. Cornell CS 4411, August 26, Geared toward programmers

Warm-up sheet: Programming in C

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

6.096 Introduction to C++ January (IAP) 2009

[0569] p 0318 garbage

Chapter 11 Introduction to Programming in C

SU 2017 May 11/16 LAB 2: Character and integer literals, number systems, character arrays manipulation, relational operator

Basic C Program: Print to stdout. Basic C Program. Basic C Program: Print to stdout. Header Files. Read argument and print. Read argument and print

Functions & Memory Maps Review C Programming Language

Midterm CSE 131B Spring 2005

Fundamental of Programming (C)

CpSc 1111 Lab 5 Formatting and Flow Control

Pointer Lesson 2 Outline

CS201 - Lecture 1 The C Programming Language

Pointers. Pointers. Pointers (cont) CS 217

Lecture 07 Debugging Programs with GDB

Exercise Session 2 Simon Gerber

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

CSE 351. Introduction & Course Tools

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

CSE 124 Discussion (10/3) C/C++ Basics

C Language Coding Standard

CSE 333 Lecture 2 - arrays, memory, pointers


Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

CSE 333 Midterm Exam 7/29/13

Creating a C++ Program

Introduction: The Unix shell and C programming

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

ECE 250 / CPS 250 Computer Architecture. C Programming

Exercise Session 2 Systems Programming and Computer Architecture

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

BASIC ELEMENTS OF A COMPUTER PROGRAM

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

Subject: Fundamental of Computer Programming 2068

CS-211 Fall 2017 Test 1 Version A Oct. 2, Name:

Transcription:

Roy Chan CSC2100B Data Structures Tutorial 1 January 12, 2009 1 / 38

1 Information Your TA team Course Information Assignment 2 Online Judge Writing Your Assignment Program Submitting Your Program Online Judge Reply Messages Demostration 3 Introduction to C The C Compilation Model C Operators Data Types in C (32-bit Machine) 2 / 38

Information 3 / 38

Your TA team CHAN Kai Chi Email: kcchan[at]cse.cuhk.edu.hk FUNG Wai Shing Email: wsfung[at]cse.cuhk.edu.hk MA Hao Email: hma[at]cse.cuhk.edu.hk 4 / 38

Course Information Course Web Page http://wiki.cse.cuhk.edu.hk/irwin.king/teaching/csc2100b/2009 Course Newsgroup news://news.erg.cuhk.edu.hk/cuhk.cse.csc2100b Anti-plagiarism Policy http://www.cuhk.edu.hk/policy/academichonesty 5 / 38

Assignment 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 sxxxxxxx@mailserv.cuhk.edu.hk email account. Keep it safe and do not disclose it. 6 / 38

Online Judge 7 / 38

Writing Your Assignment Program Write your program using your favorite editor, e.g., vi, vim, pico, emacs Add a header at the first line of your program 8 / 38

/* CSC2100@ 09123456 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; } 9 / 38

/* CSC2100@ 09123456 a1234567 assg0 question0 */ 1 Course Code: CSC2100@ 2 Student ID: 09123456 3 Login ID: a1234567 4 Problem ID: assg0 question0 10 / 38

Submitting Your Program Compile and test your program in Unix To compile in Unix: gcc -o myprogram myprogram.c To run:./myprogram Submit to Online Judge when ready Login to a sparc machine mail csc2100@pc89072 < myprogram.c Wait for reply in your CSE mailbox 11 / 38

Accepted Online Judge Reply Messages 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 Division by 0 Segmentation Fault, Bus Error,... Check your code, e.g. errors in pointers. Wrong Answer. Presentation Error. 12 / 38

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. 13 / 38

Demostration Question Id: 2009A0Q0 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 exceeded, do: Input two integers Display their product You can assume the inputs are within the range 0 < x < 10000. Sample input 3 1 2 3 4 5 6 Expected output P: 2 P: 12 P: 30 14 / 38

Attemp #1 /* CSC2100@ 01234567xx aaaaaaaaxx 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("p: %hd \n", p); } } return 0; Submission Error: Please check the header format of your program. 15 / 38

Attemp #2 /* CSC2100@ 01234567xx aaaaaaaaxx 2009A0Q0 */ #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("p: %hd \n", p); } } return 0; Runtime Error: Time Limit Exceeded. 16 / 38

Attemp #3 /* CSC2100@ 01234567xx aaaaaaaaxx 2009A0Q0 */ #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("p: %hd \n", p); i++; } } return 0; Wrong Answer: Please check your program. 17 / 38

Attemp #4 /* CSC2100@ 01234567xx aaaaaaaaxx 2009A0Q0 */ #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("p: %d \n", p); i++; } } return 0; Presentation Error 18 / 38

Attemp #5 /* CSC2100@ 01234567xx aaaaaaaaxx 2009A0Q0 */ #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("p: %d\n", p); i++; } } return 0; Accepted: Congratulations! 19 / 38

Introduction to C 20 / 38

The C Compilation Model 21 / 38

Arithmetic: +,-,*,/,%,++,-- C Operators 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: <<,>>,&,^, 22 / 38

Size 23 / 38

Notes There is no boolean type in C. Instead, non-zero values mean true, zero means false. 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. 24 / 38

Constants Constants can be defined using #define at beginning of file. /* CSC2100@ 09123456 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; 25 / 38

#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; } Functions 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. 26 / 38

Pointer Variables Pointer variables are variables that store memory addresses. Pointer Declaration: int x, y = 5; int *ptr; /*ptr is a POINTER to Reference operator &: an integer variable*/ 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 */ 27 / 38

int x; int y = 5; int *ptr; ptr = &y; x = *ptr; Pointer Example 1 28 / 38

int x = 10, y = 5; int *p1, *p2; p1 = &x; p2 = &y; Pointer Example 2 29 / 38

*p1 = 7; *p2 = 11; Pointer Example 2 30 / 38

Pointer Example 2 p2 = p1; // Not the same as *p2 = *p1 31 / 38

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); return 0; } //Note: this is not possible in Java! 32 / 38

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; 33 / 38

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; 34 / 38

Structure A collection of values (members) struct time { int hh; int mm; int ss; };... struct time t1; t1.hh=20; t1.mm=12; t1.ss=30;... Like a class in java or C++, but without methods. 35 / 38

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

Structure 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;... 37 / 38

Question 38 / 38