More Python-Esque C Features - Lecture Slides | CSSE 120, Study notes of Software Engineering

Material Type: Notes; Class: Intro to Software Development; Subject: Computer Sci & Software Engr; University: Rose-Hulman Institute of Technology; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 08/16/2009

koofers-user-jlr-1
koofers-user-jlr-1 🇺🇸

10 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
MORE PYTHON-ESQUE
C FEATURES
CSSE 120—Rose Hulman Institute of Technology
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download More Python-Esque C Features - Lecture Slides | CSSE 120 and more Study notes Software Engineering in PDF only on Docsity!

MORE PYTHON-ESQUEC FEATURES CSSE 120—Rose Hulman Institute of Technology

Getting Values from Functions

^

Just like in Python (almost)

^

Consider the function:^ †^

double

convertCtoF(double

celsius)

{

return

32.

+

9.


celsius

/

5.0;

}

^

How would we get result from a function in Python?^ †^

fahr = convertCtoF(20.0)

^

What's different in C?^ †^

Need to declare the type of fahr † Need a semi-colon

Or Else What?

^

if gpa > 2.0:

print "safe" elif gpa >= 1.0:

print "trouble" else:

print "sqrt club"

^

Python:^ †^

Colons and indenting † elif

^

if (gpa > 2.0) {

printf("safe"); } else if (gpa >= 1.0) {

printf("trouble"); } else {

printf("sqrt club"); }

^

C:^ †^

Parentheses, braces † else if

Optional Braces

^

Braces group statements

^

Can omit for singlestatement bodies

^

if (gpa > 2.0)

printf("safe"); else if (gpa >= 1.0)

printf("trouble"); else

printf("sqrt club");

Ahh. That's better!

^

What is printed in eachcase?

^

if (n > 0) {

if (a > 0)

printf("X");

} else {

printf("Y"); }

Case

n^

a

1

1

1

2

-^

1

3

1

4

-^

Use braces toavoid confusion!

Does C have a boolean type? 0

^

Enter the following C code in Eclipse: void testBoolean(int n, int m) {

int p = n < m; printf("Is %d less than %d? %d\n", n,

m, p);

}

^

Add a couple of test calls to your

main()

function:

testBoolean(2,3);

testBoolean(3,2);

^

0 in C is like

False

in Python

^

All other numbers are like

True

A Little Input, Please

^

To read input from user in C, use

scanf()

^

Syntax:

scanf(, , …)

^

Example:

int age;scanf("%d", &age);

Another Example

^

To read input from user in C, use

scanf()

^

Syntax:

scanf(, , …)

^

Example: double f, g;printf("Enter two real numbers separated by a comma:");fflush(stdout);scanf("%lf,%lf", &f, &g);printf("Average: %5.2f\n", (f + g)/2.0);

Pushes prompt stringto user before askingfor input.

ell-eff = "long float"Why not d for double?

Comma is matchedagainst user input

Tetris Project Presentation

^

Each team has 5 minutes MAX to demo their Tetrisproject

^

Each team will use the instructor’s laptop to do so

^

A team may use one of their laptops only if theirdemo fails to work on the instructor’s laptop

^

In addition to showing that the project works, teamswill demo and discuss additional features they haveadded to their project