CS1010 Discussion Group 11 Week 4 – Overview of C programming.

Slides:



Advertisements
Similar presentations
CS1010 Programming Methodology
Advertisements

C Programming. printf int printf ( const char * format,... ); printf ("Characters: %c \n", 'a'); printf ("Decimals: %d %f\n", 1977, 3.14); specifierOutputExample.
11-2 Identify the parts of the “main” function, which include Preprocessor Directives main function header main function body which includes Declaration.
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
CS1061 C Programming Lecture 10: Macros, Casting and Intro. to Standard Library A. O’Riordan, 2004.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
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:
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
Computer Science 210 Computer Organization Introduction to C.
Chapter 2 Getting Started in C Programming
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
Introduction to Programming
Lecture 1 cis208 January 14 rd, Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld.
3. FORMATTED INPUT/OUTPUT. The printf Function The first argument in a call of printf is a string, which may contain both ordinary characters and conversion.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
CS1010 Programming Methodology
CS1010 Discussion Group 11 Week 5 – Functions, Selection, Repetition.
Numbers in ‘C’ Two general categories: Integers Floats
User-Written Functions
The Machine Model Memory
Computer Science 210 Computer Organization
ECE Application Programming
Functions, Part 2 of 2 Topics Functions That Return a Value
CS1010 Discussion Group 11 Week 9 – Pointers.
Chapter 2 Overview of C.
ICS103 Programming in C Lecture 3: Introduction to C (2)
CS 108 Computing Fundamentals Notes for Thursday, September 14, 2017
CS1010 Discussion Group 11 Week 6 – One dimensional arrays.
Getting Started with C.
CS1010 Discussion Group 11 Week 7 – Two dimensional arrays.
A First Book of ANSI C Fourth Edition
C programming language
CS1010 Programming Methodology
CS1010 Programming Methodology
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Administrative things
Computer Science 210 Computer Organization
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Formatted Input/Output
Lexical Elements, Operators, and the C Cystem
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
Chapter 11 Introduction to Programming in C
Lexical Elements, Operators, and the C Cystem
CSCE 206 Lab Structured Programming in C
A First Book of ANSI C Fourth Edition
Functions, Part 2 of 3 Topics Functions That Return a Value
Assignment Operators Topics Increment and Decrement Operators
UMBC CMSC 104 – Section 01, Fall 2016
Assignment Operators Topics Increment and Decrement Operators
Formatted Input/Output
WEEK-2.
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Introduction to Problem Solving and Programming
Formatted Input/Output
Variables in C Topics Naming Variables Declaring Variables
CSCE 206 Lab Structured Programming in C
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Assignment Operators Topics Increment and Decrement Operators
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Administrative things
Presentation transcript:

CS1010 Discussion Group 11 Week 4 – Overview of C programming

Slides are at http://www.comp.nus.edu.sg/~yanhwa/ HELLO! Slides are at http://www.comp.nus.edu.sg/~yanhwa/

Have you submitted your Lab #1? Do Follow Instructions Give meaningful names for variables Appropriate commenting Must do Double/triple check your indentation (gg=G in vim will auto-indent your program nicely!) Add header comment, check skeleton code given. Do not Forget to initialise variables whenever necessary Output format does not conform to requirement http://www.comp.nus.edu.sg/~cs1010/labs/2017s1/labguide.html Follow lab guidelines

What are “preprocessor directives”? Lecture Summary What are “preprocessor directives”? What is stdio.h? Why is it useful to use a macro expansion? #define, #include scanf, printf "%7d%s %c%lf" What is an escape sequence? The above format string scans the first seven characters as a decimal integer, then reads the remaining as a string until a space, new line or tab is found, then scans the first non-whitespace character following and a double-precision floating-point number afterwards. The C preprocessor modifies a source file before handing it over to the compiler The `#include' directive works by directing the C preprocessor to scan the specified file as input before continuing with the rest of the current file.

Typecasting (implicit vs explicit) Lecture Summary Typecasting (implicit vs explicit) float a = 10 / 3 float a = (float) (10/3) float a = (float) 10 / 3 float a = 10.0 / 3 The above format string scans the first seven characters as a decimal integer, then reads the remaining as a string until a space, new line or tab is found, then scans the first non-whitespace character following and a double-precision floating-point number afterwards.

How to use maths functions? Lecture Summary How to use maths functions? Include <math.h> AND Compile your program with –lm option (i.e. gcc –lm …) The above format string scans the first seven characters as a decimal integer, then reads the remaining as a string until a space, new line or tab is found, then scans the first non-whitespace character following and a double-precision floating-point number afterwards.

Lab CodeCrunch Lab #1 Have you submitted?

Tutorial 2 Q2b Superfluous and incorrect comments Inconsistent spacing and indentation Redundant statement (where?)

Tutorial 2 Q3b The value of num1 will be displayed as 123.099998 instead of 123.100000 Not all numerical values can be represented accurately in a computer, due to the finite number of bits used to represent a value. IEEE 754 Floating-Point Representation (not in CS1010, see CS2100) Double can only try to improve accuracy

Tutorial 2 Q4 Did you run the program? Inf (Infinity) NaN (Not a Number) Why do you get a “core dump”?

Tutorial 2 Q5 Explain a++ vs ++a. a++ or a-- is postfix operation - the value of a will get changed after the evaluation of expression. ++a or --a is prefix operation - the value of a will get changed before the evaluation of expression Redundancy again

countNeg  0 Tutorial 1 Q4a Why must we initialise in this case? for k from 1 to N if (a[k] < 0) countNeg  countNeg + 1; print countNeg Why must we initialise in this case?

Restricted to only numerical values Tutorial 2 Q6 num1 = num1 – num2; num2 = num1 + num2; num1 = num2 – num1; Restricted to only numerical values Risks overflow (what is overflow?) Max integer is 2^31 -1 Not general Extra time: explain overflow in https://www.youtube.com/watch?v=cFYdr4X2Nqo

Functions printf and scanf are standard library functions the main() function will return 0 to the unix environment, indicating success Functions have input (arguments) and output (return value) f(x) = x^2 in maths also has an input and output How do we define our own function?

Functions /* function prototype */ int max(int num1, int num2); // good practice to include names of parameters int main (void) { /* local variable definition */ int a = 100; int b = 200; int ret; /* calling a function to get max value */ ret = max(a, b); printf( "Max value is : %d\n", ret ); return 0; }

Functions /* function returning the max between two numbers */ int max(int num1, int num2) { /* local variable declaration */ int result; if (num1 > num2) result = num1; else result = num2; return result; }

Functions and call stack https://www.youtube.com/watch?v=jRcll9qY6b 0 Functions call stack Functions and call stack https://www.youtube.com/watch?v=jRcll9qY6b 0

FreezerV2

Freezer v2 //Freezer temperature after t time #include <stdio.h> #include <stdlib.h> #include <math.h> int main(void){ float temp, t = 0; float hours = 0, mins = 0; printf("Enter hours and minutes since power failure: "); scanf("%f %f", &hours, &mins); t = hours + mins/60; temp = (4 * pow(t, 10))/(pow(t, 9) + 2) - 20; printf("Temperature in freezer = %.2f\n", temp); return 0; } Freezer v2