comp26120 algorithms and imperative programming
play

COMP26120: Algorithms and Imperative Programming Lecture C1: - PowerPoint PPT Presentation

COMP26120: Algorithms and Imperative Programming Lecture C1: Introduction to C Pete Jinks School of Computer Science, University of Manchester Autumn 2011 COMP26120 Lecture C1 1/25 Review Course-unit Contents: Algorithms C


  1. COMP26120: Algorithms and Imperative Programming Lecture C1: Introduction to C Pete Jinks School of Computer Science, University of Manchester Autumn 2011 COMP26120 Lecture C1 1/25

  2. Review Course-unit Contents: – Algorithms – C programming Course-unit Approach: – Active Learning – Reading COMP26120 Lecture C1 2/25

  3. Review Course-unit Contents: – Algorithms – C programming Course-unit Approach: – Active Learning – Reading COMP26120 Lecture C1 2/25

  4. Lecture Outline C Resources Comparing Java and C Input/Output COMP26120 Lecture C1 3/25

  5. Lecture C1: You are here C Resources Comparing Java and C Input/Output COMP26120 Lecture C1 C 4/25

  6. Motivation New Programming Language Paradigm Concepts Useful for certain applications – uncluttered view of hardware (“high-level assembly-code”) – performance CV COMP26120 Lecture C1 C 5/25

  7. Motivation New Programming Language Paradigm Concepts Useful for certain applications – uncluttered view of hardware (“high-level assembly-code”) – performance CV COMP26120 Lecture C1 C 5/25

  8. Motivation New Programming Language Paradigm Concepts Useful for certain applications – uncluttered view of hardware (“high-level assembly-code”) – performance CV COMP26120 Lecture C1 C 5/25

  9. A little history Originally from late 60’s - published 1972 1989/1990 ISO standard (C89, C90) 1999/2000 ISO standard (C99) 201? (C1X) COMP26120 Lecture C1 C 6/25

  10. Lecture C1: You are here C Resources Comparing Java and C Input/Output COMP26120 Lecture C1 Resources 7/25

  11. Online Resources Starting points: Syllabus : http://www.cs.manchester.ac.uk/undergraduate/ programmes/courseunits/syllabus.php?code=COMP26120 Course-unit : http://www.cs.manchester.ac.uk/ugt/COMP26120/ QandA : http://qanda.cs.man.ac.uk/questions/3842/ resources-for-learning-c (tag: comp261-algorithms+c) Point to: – Online C course in Moodle – Online “Standard C” book (Plauger & Brodie) – etc. COMP26120 Lecture C1 Resources 8/25

  12. Online Resources Starting points: Syllabus : http://www.cs.manchester.ac.uk/undergraduate/ programmes/courseunits/syllabus.php?code=COMP26120 Course-unit : http://www.cs.manchester.ac.uk/ugt/COMP26120/ QandA : http://qanda.cs.man.ac.uk/questions/3842/ resources-for-learning-c (tag: comp261-algorithms+c) Point to: – Online C course in Moodle – Online “Standard C” book (Plauger & Brodie) – etc. COMP26120 Lecture C1 Resources 8/25

  13. More Resources man pages etc. Book list on syllabus: - The C Programming Language (Kernighan & Ritchie) - C: A Reference Manual (Harbison & Steele) - Expert C Programming (van der Linden) COMP26120 Lecture C1 Resources 9/25

  14. Online C course http://moodle.cs.man.ac.uk/file.php/28/coursedata/c cbt/ frontpage.html Navigation: begin return map index Themes : – Java to C – Information Representation – Control Flow – Program Structuring – Input & Output Colour-coding: green, yellow, orange, red Dependencies/Routes (arrows) e.g. use for C labs COMP26120 Lecture C1 Resources 10/25

  15. Online C course http://moodle.cs.man.ac.uk/file.php/28/coursedata/c cbt/ frontpage.html Navigation: begin return map index Themes : – Java to C – Information Representation – Control Flow – Program Structuring – Input & Output Colour-coding: green, yellow, orange, red Dependencies/Routes (arrows) e.g. use for C labs COMP26120 Lecture C1 Resources 10/25

  16. Online C course http://moodle.cs.man.ac.uk/file.php/28/coursedata/c cbt/ frontpage.html Navigation: begin return map index Themes : – Java to C – Information Representation – Control Flow – Program Structuring – Input & Output Colour-coding: green, yellow, orange, red Dependencies/Routes (arrows) e.g. use for C labs COMP26120 Lecture C1 Resources 10/25

  17. Online C course http://moodle.cs.man.ac.uk/file.php/28/coursedata/c cbt/ frontpage.html Navigation: begin return map index Themes : – Java to C – Information Representation – Control Flow – Program Structuring – Input & Output Colour-coding: green, yellow, orange, red Dependencies/Routes (arrows) e.g. use for C labs COMP26120 Lecture C1 Resources 10/25

  18. Online C course http://moodle.cs.man.ac.uk/file.php/28/coursedata/c cbt/ frontpage.html Navigation: begin return map index Themes : – Java to C – Information Representation – Control Flow – Program Structuring – Input & Output Colour-coding: green, yellow, orange, red Dependencies/Routes (arrows) e.g. use for C labs COMP26120 Lecture C1 Resources 10/25

  19. Lecture C1: You are here C Resources Comparing Java and C Input/Output COMP26120 Lecture C1 Comparing Java and C 11/25

  20. Assumptions You have successfully completed 1st-year Java (or equivalent): – you know how to design non-trivial programmes – you know how to encode your designs using Java You are going to learn C based on this C and Java have a lot of similarities, and a lot of differences Look at programming concepts, saying for each whether C is – similar to Java (but ...) – different from Java (as follows ...) COMP26120 Lecture C1 Comparing Java and C 12/25

  21. Assumptions You have successfully completed 1st-year Java (or equivalent): – you know how to design non-trivial programmes – you know how to encode your designs using Java You are going to learn C based on this C and Java have a lot of similarities, and a lot of differences Look at programming concepts, saying for each whether C is – similar to Java (but ...) – different from Java (as follows ...) COMP26120 Lecture C1 Comparing Java and C 12/25

  22. SalaryAnalysis You should have seen the Java version (or something very similar) last year. Compare the Java and C versions Check center character: “ ” = same in both “ | ” = in both, but different “ < ” = only in .java “ > ” = only in .c Start making a list of similarities and differences. Please work with your neighbour(s). COMP26120 Lecture C1 Comparing Java and C 13/25

  23. Lecture C1: You are here C Resources Comparing Java and C Input/Output COMP26120 Lecture C1 Input/Output 14/25

  24. Streams Moodle theme: Input and Output man stdio etc. Built-in: System.in → stdin System.out → stdout System.err → stderr e.g. System.out.print(...) → fprintf (stdout, ...) COMP26120 Lecture C1 Input/Output 15/25

  25. Streams Moodle theme: Input and Output man stdio etc. Built-in: System.in → stdin System.out → stdout System.err → stderr e.g. System.out.print(...) → fprintf (stdout, ...) COMP26120 Lecture C1 Input/Output 15/25

  26. Streams Moodle theme: Input and Output man stdio etc. Built-in: System.in → stdin System.out → stdout System.err → stderr e.g. System.out.print(...) → fprintf (stdout, ...) COMP26120 Lecture C1 Input/Output 15/25

  27. Creating your own Streams Moodle: Input and Output Streams, Manipulating Streams import java.util.Scanner; import java.io.File; → #include < stdio.h > #include <stdlib.h> Scanner sScan = new Scanner(new File(filen)); → FILE * stream= fopen (filen, "r"); if (!stream) { fprintf(stderr, "can’t open %s \ n", filen); exit(-1); } ? → fclose (stream); COMP26120 Lecture C1 Input/Output 16/25

  28. Creating your own Streams Moodle: Input and Output Streams, Manipulating Streams import java.util.Scanner; import java.io.File; → #include < stdio.h > #include <stdlib.h> Scanner sScan = new Scanner(new File(filen)); → FILE * stream= fopen (filen, "r"); if (!stream) { fprintf(stderr, "can’t open %s \ n", filen); exit(-1); } ? → fclose (stream); COMP26120 Lecture C1 Input/Output 16/25

  29. Creating your own Streams Moodle: Input and Output Streams, Manipulating Streams import java.util.Scanner; import java.io.File; → #include < stdio.h > #include <stdlib.h> Scanner sScan = new Scanner(new File(filen)); → FILE * stream= fopen (filen, "r"); if (!stream) { fprintf(stderr, "can’t open %s \ n", filen); exit(-1); } ? → fclose (stream); COMP26120 Lecture C1 Input/Output 16/25

  30. Writing characters Moodle: Character Manipulation, Revisited putchar(ch) – character fputc(stream, ch) putchar(ch) is fputc(stdout,ch) printf(...) – formatted fprintf(stream, ...) sprintf(string, ...) printf(...) is fprintf(stdout, ...) COMP26120 Lecture C1 Input/Output 17/25

  31. Writing characters Moodle: Character Manipulation, Revisited putchar(ch) – character fputc(stream, ch) putchar(ch) is fputc(stdout,ch) printf(...) – formatted fprintf(stream, ...) sprintf(string, ...) printf(...) is fprintf(stdout, ...) COMP26120 Lecture C1 Input/Output 17/25

  32. printf Moodle: (Reading and) Writing System.out.print (String.format(...)); → printf (...); ("%2d earns %5d, which is %5d %s", i, j, k, s) print ln ("...", ...) → printf("... \ n ", ...) System.out.println("mean is: \ t" + mSal); System.out.println("rounds to: \ t" + mSalR); → printf("mean is: \ t%f \ n" "rounds to: \ t%d \ n", mSal, mSalR); COMP26120 Lecture C1 Input/Output 18/25

Recommend


More recommend