1 / 19

CS1010 Discussion Group 11

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

jbennett
Télécharger la présentation

CS1010 Discussion Group 11

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  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 http://www.comp.nus.edu.sg/~cs1010/labs/2017s1/labguide.html Follow lab guidelines

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

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

  6. Lecture Summary • How to use maths functions? • Include <math.h> AND • Compile your program with –lm option (i.e. gcc –lm …)

  7. CodeCrunch Lab #1 Have you submitted? Lab

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

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

  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. Tutorial 1 Q4a Why must we initialise in this case? countNeg0 for k from 1 to N if (a[k]< 0) countNeg countNeg + 1; print countNeg

  13. 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;

  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 call stack Functions and call stack https://www.youtube.com/watch?v=jRcll9qY6b0

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

More Related