LectureIV-Programming in 'C'.pptx

LectureIV-Programming in 'C'.pptx - C Language Types...

Info icon This preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
Types, Control Flow, and Functions 1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University C Language
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Contents 2 Introduction Data Types Arrays & Functions Control Flow Program Structure
Image of page 2
Introduction: Example ‘C’ code 3 #include ... /* print Fahrenheit-Celsius table for fahr = 0, 20, 40, ..., 300, using a loop */ main() { int fahr, celsius; int lower, upper, step; lower = 0; /* lower limit of temperature scale */ upper = 300; // upper limit step = 20; // step size fahr = lower; while (fahr <= upper) { celsius = 5 * (fahr-32) / 9; // integer value? printf(" %d\t%d\n ", fahr, celsius); fahr = fahr + step; } } ex13.c ex13.c
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
printf(" %d \t %d \n", fahr, celsius); %d specifies an integer argument (d: d ecimal). Output 0 -17 20 -6 40 4 60 15 80 26 100 37 120 48 140 60 160 71 180 82 ……..
Image of page 4
5 Is there any problem in the above output? The Celsius temperatures in the previous out are not accurate. For example 0 o F is -17.8 o C. Then? We can use float data type for fahr and celsius instead of int . float fahr, celsius; Then how to print real numbers using printf() ? celsius = 5 * (fahr-32) / 9; // okay? printf(" %3.0f \t %6.1f \n", fahr, celsius); %f specifies a floating point argument (f: f loating point). 6.1 means Print 6 digits before the decimal point (called width) and one digit after the decimal point (called precision). ex14.c ex14.c
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
6 Some easy errors: printf(" %d \t %6.1f \n", fahr, celsius); ??? printf(" %3.0f \t %6.1f \n", fahr); ??? printf(" %6.1f \n", fahr, celsius); ???
Image of page 6
Review 7
Image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Boolean values in ‘C’ 8 When evaluating integers as Booleans in C, are negative numbers true or false? A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.
Image of page 8
9 If -Else
Image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Arrays 10 /* * C Program to Find the Largest Number in an Array */ #include <stdio.h> int main() { int array[50], size, i, largest;
Image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern