CS1010 Programming Methodology

Slides:



Advertisements
Similar presentations
Credit hours: 4 Contact hours: 50 (30 Theory, 20 Lab) Prerequisite: TB143 Introduction to Personal Computers.
Advertisements

Introduction to C Systems Programming Concepts. Introduction to C A simple C Program A simple C Program –Variable Declarations –printf ( ) Compiling and.
WEEK 12 Class Activities Lecturer’s slides.
CS1010 Programming Methodology
WEEK 13 Class Activities Lecturer’s slides.
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.
Computer Programming w/ Eng. Applications
WEEK 5 Class Activities Lecturer’s slides.
CS1020 Week 3: 29th January 2015.
I/O: SPARC Assembly Department of Computer Science Georgia State University Georgia State University Updated Spring 2014.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
CS1010 Programming Methodology
CS1010: Programming Methodology
Zhang Hongyi CSCI2100B Data Structures Tutorial 2
CodeCrunch Getting Started.
TDBA66, VT-03 Lecture - Ch. 21 A complete C-program Display Fig. 2.1 and comment on different things such as Preprocessor directives Header files Identifiers.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Display a 12-Month Calendar CS-2301 D-term Programming Assignment #2 12-Month Calendar CS-2301 System Programming C-term 2009 (Slides include materials.
1 Lab Session-8 CSIT-121 Fall 2003 w Call by Reference w Lab Exercise 1 w Lab Exercise for Demo w Practice Problems.
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
UNIT 3 Overview of C Programming.
Assignment #2, 12- month Calendar CS-2301, B-Term Programming Assignment #2 12-Month Calendar CS-2301, System Programming for Non-Majors (Slides.
WEEK 9 Class Activities Lecturer’s slides.
Agenda Review Compiling Review Data Types Integer Division Composition C++ Mathematical Functions User Input Reading: , 8.11 Homework #3.
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
Computing II - Problem Session - 1. Some Notes on Input/Output. In C++ you used two primary functions to manage console-directed Input and Output.
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data.
CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages for Thursday.  We will go to the lab on Thursday.
1 Lab Session-8 CSIT-121 Spring 2005 Call by Reference Lab Exercise for Demo Practice Problems.
1 計算機程式設計 Introduction to Computer Programming Lecture01: Introduction and Hello World 9/10/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction.
WEEK 4 Class Activities Lecturer’s slides.
Homework Assignment #3 J. H. Wang Apr. 19, 2007.
A First C Program /* Print a Message */ #include main() { printf("This is a test!\n"); } The program is compiled and run as cc pg32.c  a.out  This is.
Fundamental Programming: Fundamental Programming Introduction to C++
UNIT 13 Separate Compilation.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
11 Project 2 Temperature Conversion (Not graded).
WEEK 6 Class Activities Lecturer’s slides.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
E-1 University of Washington Computer Programming I Lecture 5: Input and Output (I/O) © 2000 UW CSE.
9/29/99B-1 CSE / ENGR 142 Programming I Variables, Values, and Types © 1998 UW CSE.
Textbook C for Scientists and Engineers © Prentice Hall 1997 Available at NUS CO-OP at S$35.10.
WEEK 8 Class Activities Lecturer’s slides.
Exercise 1 #include int main() { printf(“Hello C Programming!\n”); return 0; } 1.Run your Visual Studio 2008 or Create a new “project” and add.
WEEK 1 Class Activities.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
CS1101: Programming Methodology
CSCI-1411 F UNDAMENTALS O F C OMPUTING L AB Shane Transue Summer
CS1010: Programming Methodology
1 Project 4: Computing Distance. 222 Computing Distance Write a program to compute the distance between two points. Recall that the distance between the.
WEEK 10 Class Activities Lecturer’s slides.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Week 12 Class Activities.
2.3 Output Formatting. Outputting Format Specify the number of spaces, “c”, used to print an integer value with specifier %cd, e.g., %3d, %4d. E.g. printf.
WEEK 1 Class Activities.
Lecture2.
CS1010 Programming Methodology
CS1010 Programming Methodology
CS1010 Programming Methodology
CS1010 Discussion Group 11 Week 4 – Overview of C programming.
ICS103 Programming in C Lecture 3: Introduction to C (2)
Lecture2.
Administrative things
C Formatted Input / Output Review and Lab Assignments
Lecture3.
Administrative things
Presentation transcript:

CS1010 Programming Methodology Lecturer’s slides http://www.comp.nus.edu.sg/~cs1010/ WEEK 2 Class Activities

Week 2: Basic C Programming CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Week 2: Basic C Programming Data Type Ex #1: Size of Data Types Program Structure Ex #2: Testing scanf() and printf() Ex #3: Distance Conversion Ex #4: Temperature Conversion Ex #5: Freezer Math functions Ex #6: Freezer (version 2) CodeCrunch Things-To-Do Announcements

Exercise #1: Size of Data Types (1/2) CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Exercise #1: Size of Data Types (1/2) We will do an exercise in class to explore the aforementioned information about data types Unit3_DataTypes.c Copy the above program into your current directory cp ~cs1010/lect/prog/unit3/Unit3_DataTypes.c . Or download program from CS1010 Lectures page and transfer it into your UNIX account: http://www.comp.nus.edu.sg/~cs1010/2_resources/lectures.html Destination directory; ‘.’ means current directory Pathname of source file

Exercise #1: Size of Data Types (2/2) CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Exercise #1: Size of Data Types (2/2) How do you compile Unit3_DataTypes.c into an executable file called DataTypes in just one step? gcc –Wall Unit3_DataTypes.c –o DataTypes What are the sizes of the 4 data types explored? Data type Size in bytes int float double char 4 4 8 1

Exercise #2: Testing scanf() and printf() (1/2) CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Exercise #2: Testing scanf() and printf() (1/2) We will do an exercise in class to explore scanf() and printf() functions Unit3_TestIO.c Copy the above program into your current directory cp ~cs1010/lect/prog/unit3/Unit3_TestIO.c . Or download program from CS1010 Lectures page and transfer it into your UNIX account: http://www.comp.nus.edu.sg/~cs1010/2_resources/lectures.html

Exercise #2: Testing scanf() and printf() (2/2) CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Exercise #2: Testing scanf() and printf() (2/2) Format specifier: What if you use %f on integer, or %d on float? Why? What if you enter a real number say 12.3 for variable a? Why? What if you enter 23.3 for variable f? What is printed? Why? Experiment with different width specifiers and decimal place specifiers. Eg: %5.2f, %7.3f, etc.

Exercise #3: Distance Conversion (1/2) CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Exercise #3: Distance Conversion (1/2) Convert distance from miles to kilometres Unit3_MileToKm.c The program is given (which you can copy to your directory as earlier instructed), but for this exercise we want you to type in the program yourself as a practice in using vim The program is shown in the next slide

Exercise #3: Distance Conversion (2/2) CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Exercise #3: Distance Conversion (2/2) Unit3_MileToKm.c // Unit3_MileToKm.c // Converts distance in miles to kilometers. #include <stdio.h> #define KMS_PER_MILE 1.609 int main(void) { float miles, // input - distance in miles. kms; // output - distance in kilometers /* Get the distance in miles */ printf("Enter distance in miles: "); scanf("%f", &miles); // Convert the distance to kilometres kms = KMS_PER_MILE * miles; // Display the distance in kilometres printf("That equals %9.2f km.\n", kms); return 0; }

Exercise #4: Temperature Conversion (1/2) CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Exercise #4: Temperature Conversion (1/2) Write a program to convert a temperature from Fahrenheit degrees to Celsius degrees: 𝑐𝑒𝑙𝑠𝑖𝑢𝑠= 5 9 ×(𝑓𝑎ℎ𝑟𝑒𝑛ℎ𝑒𝑖𝑡 −32) Use vim to create FtoC.c. Correct/compile your program till it is free of errors. Tip: You may copy Unit3_MilesToKm.c to FtoC.c to save typing. How to copy a file in UNIX? Sample output: Enter temperature in Fahrenheit: 32.5 That equals 0.277778 Celsius.

Exercise #4: Temperature Conversion (2/2) CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Exercise #4: Temperature Conversion (2/2) Test your program on the following inputs: 32.5, 0, -54.3, 100 (and others of your choice) Do you get the correct answers? Enter temperature in Fahrenheit: 32.5 That equals 0.277778 Celsius. Enter temperature in Fahrenheit: 0 That equals -17.777778 Celsius. Enter temperature in Fahrenheit: -54.3 That equals -47.944444 Celsius. Enter temperature in Fahrenheit: 100 That equals 37.777778 Celsius. (Optional) Format the number of output digits to 2 decimal places (Optional) Write another program to convert Celsius to Fahrenheit

Exercise #5: Freezer (1/3) CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Exercise #5: Freezer (1/3) Write a program freezer.c that estimates the temperature in a freezer (in oC) given the elapsed time (hours) since a power failure. Assume this temperature (T) is given by where t is the time since the power failure. Your program should prompt the user to enter how long it has been since the start of the power failure in hours and minutes, both values in integers. Note that you need to convert the elapsed time into hours in real number (use type float) For example, if the user entered 2 30 (2 hours 30 minutes), you need to convert this to 2.5 hours before applying the above formula.

Exercise #5: Freezer (2/3) CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Exercise #5: Freezer (2/3) Refer to the sample run below. Follow the output format. Enter hours and minutes since power failure: 2 45 Temperature in freezer = -13.63 How long does it take the freezer to get to zero degree? Which of the following is the closest answer? 3 hours 4 hours 10 minutes 6 hours 30 minutes 8 hours This exercise is mounted on CodeCrunch as a practice exercise.

Exercise #5: Freezer (3/3) CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Exercise #5: Freezer (3/3) Write a program freezer.c that estimates the temperature in a freezer (in Celsius) given the elapsed time (hours) since a power failure. Assume this temperature (T) is given by: where t is the time since the power failure. Thinking about the algorithm: What are the variables (and their types) for input data? What are the variables (and their types) for output? Is there any formatting of output? What are the variables (and their types) for intermediate results? How to compute the result? 2 int variables: hours, minutes 1 float variable: temperature Yes, 2 decimal places 1 float variable: hours_float Use the given formula

Exercise #6: Freezer (version 2) CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Exercise #6: Freezer (version 2) Write a program freezerV2.c that replaces the old formula in freezer.c with this: where t is the time since the power failure. Time limit: 15 minutes Which math function(s) should you use? This exercise is mounted on CodeCrunch as a practice exercise.

CodeCrunch (https://codecrunch.comp.nus.edu.sg/) CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) CodeCrunch (https://codecrunch.comp.nus.edu.sg/) CodeCrunch is an online submission system we use in CS1010 You are to submit your lab assignments through CodeCrunch Your first assignment: Lab #0 Volume of Box Non-graded practice exercises are also mounted on CodeCrunch for your own attempt CodeCrunch provides instant feedback on your program correctness upon submission We will go through the Introduction to CodeCrunch slides here.

CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Things-To-Do Continue the exercises on your own if you cannot complete them during sectional class Continue to practise the UNIX commands and vim on your own Revise Chapter 1 and Chapter 2 Preparation for next week: Read Chapter 3 The Basics of C Read Chapter 5 Functions Read Chapter 4 (Lessons 4.1 to 4.6)

CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Announcements Discussion classes start in week 3 (next week) Check the venue Attendance will be taken

CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) End of File