COMP s1 Lecture 1

Similar documents
Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1. Programming Fundamentals

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

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

Computers and Computation. The Modern Computer. The Operating System. The Operating System

Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1 Lecture 7. COMP s1. Before we begin. Strings. Programming Fundamentals. Overview. Andrew Bennett

Beyond this course. Machine code. Readings: CP:AMA 2.1, 15.4

Introduction: The Unix shell and C programming

COL100 Lab 2. I semester Week 2, Open the web-browser and visit the page and visit the COL100 course page.

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.

Procedures, Parameters, Values and Variables. Steven R. Bagley

CS 253: Intro to Systems Programming 1/21

2. Numbers In, Numbers Out

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

printf( Please enter another number: ); scanf( %d, &num2);

Lab 1 Introduction to UNIX and C

AMCAT Automata Coding Sample Questions And Answers

Lab 1 Introduction to UNIX and C

Laboratory 1 Semester 1 11/12

CSE 351. Introduction & Course Tools

CSC111 Computer Science II

Algorithms, Data Structures, and Problem Solving

Computer Programming: Skills & Concepts (CP) Variables and ints

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 1. Spring 2011

Welcome! COMP s1. Programming Fundamentals

Exercise 1: Basic Tools

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

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

Saint Louis University. Intro to Linux and C. CSCI 2400/ ECE 3217: Computer Architecture. Instructors: David Ferry

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

CpSc 111 Lab 3 Integer Variables, Mathematical Operations, & Redirection

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Introduction to C. Winter 2019

The C language. Introductory course #1

Introduction to Supercomputing

2. Numbers In, Numbers Out

Dynamic Memory Allocation and Command-line Arguments

Program Organization and Comments

Getting started with UNIX/Linux for G51PRG and G51CSA

Problem Set 1: Unix Commands 1

CpSc 1011 Lab 3 Integer Variables, Mathematical Operations, & Redirection

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Warm-up sheet: Programming in C

CSE 303 Lecture 8. Intro to C programming

EL2310 Scientific Programming

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

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

Week 1, continued. This is CS50. Harvard University. Fall Cheng Gong

FALL 2017 CSCI 304 LAB1 (Due on Sep-19, 11:59:59pm)

CS 220: Introduction to Parallel Computing. Input/Output. Lecture 7

EL2310 Scientific Programming

Introduction to C An overview of the programming language C, syntax, data types and input/output

CS113: Lecture 4. Topics: Functions. Function Activation Records

Presented By : Gaurav Juneja

Intermediate Programming, Spring Misha Kazhdan

Control Structure: Selection

C: How to Program. Week /Mar/05

C Tutorial: Part 1. Dr. Charalampos C. Tsimenidis. Newcastle University School of Electrical and Electronic Engineering.

Introduction to C. CS 2060 Week 1. Prof. Jonathan Ventura. Outline Introduction Introduction to C Homework 1 C Coding Style Wrap-up

Kurt Schmidt. October 30, 2018

Unit 13. Linux Operating System Debugging Programs

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Introduction to C Programming

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

APT Session 4: C. Software Development Team Laurence Tratt. 1 / 14

CC112 Structured Programming

last time in cs recitations. computer commands. today s topics.

Debugging (Part 1) The material for this lecture is drawn, in part, from The Practice of Programming (Kernighan & Pike) Chapter 5

Programming and Data Structure Laboratory (CS13002)

mith College Computer Science CSC231 Bash Labs Week #10, 11, 12 Spring 2017 Introduction to C Dominique Thiébaut

High Performance Computing MPI and C-Language Seminars 2009

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

CSCI 2132 Software Development. Lecture 8: Introduction to C

Introduction to Linux

BLM2031 Structured Programming. Zeyneb KURT

LAB 1 INTRODUCTION TO LINUX ENVIRONMENT AND C COMPILER

COMP1917: 09 Arrays and Strings

Friday, September 16, Lab Notes. Command line arguments More pre-processor options Programs: Finish Program 1, begin Program 2 due next week

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

EECS 211 Lab 2. Getting Started. Getting the code. Windows. Mac/Linux

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

Introduction to Unix - Lab Exercise 0

Introduction to Linux. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

Recitation 2/18/2012

Introduction to Linux

Physics 306 Computing Lab 1: Hello, World!

Fundamentals of Programming. Lecture 3: Introduction to C Programming

An overview of Java, Data types and variables

EL2310 Scientific Programming LAB2: C lab session. Patric Jensfelt, Andrzej Pronobis

CS50 Supersection (for those less comfortable)

1. The Mac Environment in Sierra Hall 1242

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

Approximately a Final Exam CPSC 206

Fundamentals of Programming Session 4

Getting started with Hugs on Linux

Computer Systems Lecture 9

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Transcription:

COMP1511 18s1 Lecture 1 1 Numbers In, Numbers Out Andrew Bennett <andrew.bennett@unsw.edu.au> more printf variables scanf

2 Before we begin introduce yourself to the person sitting next to you why did they decide to study computing?

Admin 3 tutorials and laboratories start in week 1 (some of you have already had tutes and labs) lecture recordings are on WebCMS 3 make sure you have home computing set up

Getting Help 4 post on the course forum I (or others) will answer so everyone can see talk to me after the lecture talk to your tutor help labs admin: cs1511@cse.unsw.edu.au if it needs to go to me directly: andrew.bennett@unsw.edu.au

Hello World 5 // Prints out a friendly message. // Andrew Bennett <andrew.bennett@unsw.edu.au> // 2018-02-27 #include <stdio.h> int main (void) { // Print out the famous 'hello world' message. printf ("Hello, world!\n"); } return 0;

Navigating on Unix 6 pwd shows where you currently are $ pwd /import/ravel/2/andrewb ls lists the items in the current directory $ ls 18s1 bin lib public_html tmp web mkdir makes a new directory $ mkdir cs1511 $ ls 18s1 bin cs1511 lib public_html tmp web

Navigating on Unix 7 cd changes directory $ cd cs1511 $ pwd /import/ravel/2/andrewb/cs1511 $ ls $ cd.. changes into the previous directory $ cd.. $ pwd /import/ravel/2/andrewb

Writing a Program 8 to create a C program from the terminal, open a text editor like gedit $ gedit hello.c & once the code is written and saved compile it with dcc! $ dcc -o hello hello.c

Programming is a construction exercise 9 think about the problem write down a proposed solution break each step into smaller steps convert the basic steps into instructions in the programming language use an editor to create a file that contains the program use the compiler to check the syntax of the program test the program on a range of data

Compiling 10 remember: we write C programs for humans to read. a C program must be translated into machine code to be run. this process is known as compilation, and is performed by a compiler. we will use a compiler named dcc for COMP1511 dcc is actually a custom wrapper around a compiler named clang. another widely used compiler is called gcc.

The overall process 11

(re-)introducing: printf 12 printf ("hello world!\n") prints the text "hello world!" to the terminal

Printing more than just words 13 Can we print more than just words?

Printing more than just words 14 Can we print more than just words? Yes! printf the f stands for formatted

Let s try it out! 15

// Prints out the sum of two numbers. // Andrew Bennett <andrew.bennett@unsw.edu.au> // 2018-02-28 16 #include <stdio.h> int main (void) { // Sum two numbers, and print them out. printf ("The sum of 3 and 5 is %d\n", 3 + 5); } return 0;

we can change the numbers to add different values together and print them out! 17 but that s boring if it can t be dynamic, and it sucks to do it by hand.

Introducing variables! 18

Variables and Types 19 Variables are used to store data think boxes Each variable has a data type this is the size and structure of the box For the next few weeks, we ll only use two data types: int stores whole numbers: 2, 17, -5 float stores floating-point numbers: 3.14159, 2.71828 we ll look at other types in future weeks. can you think of any other types that would be useful?

Variables 20 declare the first time a variable is mentioned, we need to specify its type. initialise before using a variable we need to assign it a value. assign to give a variable a value. int num; // Declare num = 5; // Initialise (also Assign)... num = 27; // Assign

Variables 21 we can also declare and initialise in the same step: int num = 5; // Declare and Initialise... num = 27; // Assign

Variable Naming (and other identifiers) 22 must be made up of letters, digits and underscores ( _ ) the first character must be a letter are case sensitive ( num1 and Num1 are different) Keywords like if, while, do, int cannot be used as identifiers

Printing Variables Out 23 No variables: printf ("Hello World\n"); A single variable: int num = 5; printf ("num is %d\n", num);

Printing Variables Out 24 More than one variable: int num1 = 5; int num2 = 17; printf("num1 is %d and num2 is %d\n", num1, num2); The order of arguments is the order they will appear: int num1 = 5; int num2 = 17; printf ("num2 is %d and num1 is %d\n", num2, num1);

printf s placeholders 25 int uses %d

Numbers in: scanf 26 int num = 0; scanf ("%d", &num); printf ("num = %d\n", num); Note that the variable is still initialised. (Not necessary, but good practice.) Note the & before the variable name. Don t forget it!

Reading Variables In 27 Multiple variables (space separated): int num1 = 0; int num2 = 0; scanf ("%d %d", &num1, &num2); printf ("num1 = %d and num2 = %d\n", num1, num2); Multiple variables (comma separated): int num1 = 0; int num2 = 0; scanf ("%d, %d", &num1, &num2); printf ("num1 = %d and num2 = %d\n", num1, num2); Note the space or comma between the variables.

making decisions 28 different behaviour in different situations

Driving, Take 1 29 Write a program which asks the user to enter their age. If they are at least 16 years old, then, display You can drive. Then, whether or not they can drive, display Have a nice day.

Driving, Take 1 30 Write a program which asks the user to enter their age. If they are at least 16 years old, then, display You can drive. Then, whether or not they can drive, display Have a nice day.

Driving, Take 1: Step by Step 31 Print How old are you? Read in their age. If their age is 16: print You can drive. Print Have a nice day.

// Can a user drive? // Andrew Bennett <andrew.bennett@unsw.edu.au> // 2017-07-31 32 #include <stdio.h> #include <stdlib.h> int main (int argc, char *argv[]) { printf ("How old are you? "); int age = 0; if (age >= 16) { printf ("You can drive.\n"); } printf ("Have a nice day.\n"); } return EXIT_SUCCESS;

Detour: Defining Constant Values 33 Using the same value numerous times in a program becomes high maintenance if the value changes and needs to be changed in many places. (You may miss one!) Other developers may not know (or you may forget!) what this magical number means. #define MIN_DRIVING_AGE 16 note there is no semicolon at the end of this line

Feedback? 55 bit.do/comp1511-feedback-week1