190 likes | 202 Views
CS1010 Discussion Group 11. Week 4 – Overview of C programming. HELLO!. S lides 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
E N D
CS1010 Discussion Group 11 Week 4 – Overview of C programming
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
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?
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
Lecture Summary • How to use maths functions? • Include <math.h> AND • Compile your program with –lm option (i.e. gcc –lm …)
CodeCrunch Lab #1 Have you submitted? Lab
Tutorial 2 Q2b Superfluous and incorrect comments Inconsistent spacingand 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
Tutorial 1 Q4a Why must we initialise in this case? countNeg0 for k from 1 to N if (a[k]< 0) countNeg countNeg + 1; print countNeg
Tutorial 2 Q6 Restricted to only numerical values Risks overflow (what is overflow?) Max integer is 2^31 -1 Not general num1 = num1 – num2; num2 = num1 + num2; num1 = num2 – num1;
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 call stack Functions and call stack https://www.youtube.com/watch?v=jRcll9qY6b0
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; }