Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS1010 Discussion Group 11 Week 4 – Overview of C programming."— Presentation transcript:

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

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

3 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 Follow lab guidelines

4 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.

5 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.

6 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.

7 Lab CodeCrunch Lab #1 Have you submitted?

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

9 Tutorial 2 Q3b The value of num1 will be displayed as instead of 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

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

11 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

12 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?

13 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

14 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?

15 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; }

16 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; }

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

18 FreezerV2

19 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


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

Similar presentations


Ads by Google