ii ACKNOWLEDGEMENT I would like to thank A/P Leong Hon Wai, Mr Tok Wee Hyong and Mr Dany for reading the draft and offering me valuable comments and suggestions. Tan Tuck Choy, Aaron Instructor School of Computing National University of Singapore June 2000 CONTENTS Preface Relevant Web Sites vii viii PART I: PREPARATION FOR THE MIND Chapter 1. Learning In The University 1.1 Welcome To NUS 1.2 Handy Tips For New Students 1.3 Some Words Of Advice For Foreign Students 1.4 Different Strokes For Different Beings 1.5 Learning At Tertiary Level 1.6 Balancing The Act 1.7 Learning About Learning 1.8 Course Of Actions 1.9 Summary Article 1-1: Learning and Jigsaw Puzzles Article 1-2: Six-and-a-half cookies Article 1-3: The ‘I know that...’ trap References Web Sites Chapter 2. Problem Solving 2.1 Preliminaries 2.2 The Problem Solving Process 2.3 Creative Thinking 2.4 Summary Article 2-1: Barometer Article 2-2: Impatient Guests. Exercises References Web Site 3 4 5 5 6 6 8 12 14 15 16 18 18 19 19 21 22 24 26 27 28 29 30 32 32 PART II: PREPARATION FOR THE COMPUTING SYSTEM Chapter 3. Introduction To UNIX System 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 Introduction Rules On Computer Usage Logging In To NUSNET-III Logging Out Of NUSNET-III Installing New Applications Via Windows Shopping Logging In To A UNIX Machine Changing Your Password Logging Out Of A UNIX Machine The UNIX File System 35 36 36 37 38 39 39 42 43 43 iv Contents 3.10 Basic UNIX Commands 3.10.1 Command Format 3.10.2 Self Help: The man Command 3.10.3 Organising Your Home Directory 3.10.4 File Access Permissions 3.10.5 Manipulating Files 3.10.6 Printing Files 3.10.7 Useful UNIX Features 3.11 A Text Editor: pico 3.12 Customising Your Environment 3.13 Summary Acknowledgement Exercises References Web Site Chapter 4. Communication 44 45 46 46 49 51 53 54 55 57 59 60 60 62 62 63 4.1 Introduction 4.2 An E-mail Program: pine 4.3 The Bulleting Board: bbs 4.4 Browsing The Web 4.5 Summary Exercises Reference 64 65 69 73 75 75 75 Chapter 5. Preparing C Programs 77 5.1 5.2 5.3 5.4 5.5 Introduction Creating A Working C Program High-Level Languages And Compilation A Sample C Program Creating Your Own C Programs 5.5.1 Creating Your First C Program first.c 5.5.2 Compile And Run Your Program 5.5.3 Another Program: second.c 5.5.4 A Program With Errors: thiird.c 5.6 More Exercises 5.7 Summary Exercises 78 78 79 80 81 81 82 82 83 83 83 84 Contents PART III: PREPARATION FOR PROGRAMMING Chapter 6. Algorithmic Problem Solving 89 6.1 Algorithms 6.2 Data Types And Data Structures 6.3 Characteristics Of An Algorithm 6.4 Pseudo-codes And Flowcharts 6.5 Control Structures 6.6 Summary Exercises 90 92 92 93 95 95 96 Chapter 7. C Language Preliminaries 7.1 7.2 7.3 The C Programming Language A Sample Program Basic C Elements 7.3.1 Preprocessor Directives 7.3.2 Comment Statements 7.3.3 The main() Function 7.3.4 Reserved Keywords 7.3.5 Standard Identifiers 7.3.6 User-defined Identifiers 7.3.7 Constants 7.3.8 Variable Declarations 7.3.9 Data Types 7.4 Programming Style 7.5 Data Validation 7.6 Variables And Memory 7.7 Summary Exercises Chapter 8. Arithmetic And Relational Operations 8.1 8.2 8.3 Introduction Assignment Statement Arithmetic Operators 8.3.1 Unary Operators: +, – 8.3.2 Binary Operators: +, –, *, /, % 8.3.3 Data Type Of An Expression 8.3.4 Promotion And Casting 8.3.5 Precedence And Associativity Rules 8.3.6 Compound Assignment Operators 8.3.7 Increment (++) And Decrement (--) Operators 8.4 Mathematical Functions 8.5 Equality And Relational Operators 8.6 Logical Operators 8.7 Summary Exercises 97 98 99 102 102 103 104 106 106 106 107 108 108 109 110 111 113 114 115 116 116 118 119 119 119 120 120 121 121 122 123 124 125 126 v vi Chapter 9. Input/Output Operations And Functions 9.1 9.2 Introduction Formatting Output: printf() 9.2.1 Conversion Specification 9.2.2 Field Width And Precision 9.2.3 Flags 9.2.4 Literals And Escape Sequences 9.3 Formatting Input: scanf() 9.3.1 Field Width 9.3.2 Assignment Suppression Character * 9.4 Summary Exercises Chapter 10. Control Structures 10.1 Introduction 10.2 Selection Structure 10.2.1 The ‘if’ Construct 10.2.2 The ‘if-else’ Construct 10.2.3 Conditional Operator (?:) 10.2.4 The ‘switch’ Construct 10.3 Repetition Structure 10.3.1 The ‘while’ Construct 10.3.2 The ‘do-while’ Construct 10.3.3 Flag-controlled Loops 10.3.4 The ‘for’ Construct 10.3.5 Common Mistakes 10.4 The ‘break’ and ‘continue’ Statements 10.5 Nested Loops And Combined Structures 10.6 Summary Exercises 129 130 130 131 133 135 137 138 140 141 141 142 143 144 145 145 150 155 156 157 157 160 160 161 162 162 164 165 166 Preface vii PREFACE The Preparatory Course for Programming is intended to bring a more pleasant experience to you, new foreign student enrolling into the School of Computing. The reason being that the first programming module is often a tough nut to crack for many freshmen, especially for those who have to cope with the new environment and culture in their first few months after arrival. There are two components of this preparatory course, one aimed at the Computing students taking CS1101, and the other at the Computer Engineering students taking CS1101C. The first four chapters of this book can be read by both groups of students, while the remaining chapters gear towards the C programming language, and so are more relevant to the Computer Engineering group. The course is covered over a 13 half-day period, with two hours of lecture followed by an hour of tutorial or laboratory activities every day. Instead of solely touching on topics related to programming, we take this opportunity also to address some of the immediate concerns of a freshman. To this end, we present the series of challenges that you will face, and to get you psychologically prepared and geared for the necessary changes, to give you a head start. In this course as well as in this book, we have chosen to exclude certain areas from our discussion. Concerns and queries over curriculum matters are best answered through the various existing channels such as the School’s web site. Survival guides, which have been written by your kind Computing seniors and Computer Engineering seniors, contain more complete and up-to-date information on stuffs like dial-up numbers. Dynamic information such as these that change quickly are best left to the providers of these resources to maintain and update their information, while this book can provide reference to these resources. I have put together in this book what I think are useful to the freshmen. But this is in most part just my guess, as this is the first time such a transition module is offered. I therefore wish for generous feedback from you, to help make this manuscript of real benefit for future batches of students. The margins of the book are all ready to be filled with your ‘graffiti’. Just send in your note, be it serious advice, or light-hearted comments. You may request for anonymity, if you so wish. This book isn’t complete at the time of this writing, which means that it has plenty of room for improvement. It also means more late nights for me. I wish all of you an enjoyable stay in NUS. Aaron Tan Tuck Choy (tantc@comp.nus.edu.sg) Instructor Department of Computer Science School of Computing National University of Singapore 26 June 1999 viii Relevant Web Sites RELEVANT WEB SITES http://www.nus.edu.sg/ http://www.comp.nus.edu.sg http://www.eng.nus.edu.sg http://www.comp.nus.edu.sg/~tantc/prep/ http://www.comp.nus.edu.sg/~cs1101/ http://www.comp.nus.edu.sg/~cs1101c/ National University of Singapore School of Computing, NUS Faculty of Engineering, NUS Foreign Students Preparatory Course CS1101: Programming Methodology (Java) CS1101C: Programming Methodology (C)