1 cse1301 computer programming lecture 5: components of a c program (part 1)

69
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

Post on 21-Dec-2015

221 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

1

CSE1301Computer Programming

Lecture 5:Components of a C Program

(Part 1)

Page 2: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

2

Topics

• Structure of a C program

• Values and variables

• Expressions

• Function calls

• Comments

Page 3: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

3

From Algorithms to Programs

• Both are sets of instructions on how to do a task

• Algorithm: – talking to humans, easy to understand– in plain (English) language

• Program:– talking to computer (compiler)– can be regarded as a “formal expression” of an

algorithm

Page 4: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

4

#include <stdio.h>

#include <string.h>

main()

{

char *text = "this is a test\n";

char *ptr = text, w[50];

int i;

for (i=0; sscanf(ptr, "%s", w) > 0; i++)

{

if ((i==0) || (i==3))

printf("%s ", w);

ptr += strlen(w) + 1;

}

} C Program

Example: What is the Output?

Page 5: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

5

let the sentence be "this is a test”

go to the first word in the sentence

while (we have not reached the end of the sentence)

{

if (this is the first or the fourth word in the sentence)

{

output the word, followed by a space

}

go to the next word in the sentence

}

Algorithm

Output is: this test

Example: What is the output?

Page 6: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

6

Basic Structure of a C Program

output “Hello World!”

Algorithm: #include <stdio.h> int main(){printf(“Hello World!”);

return 0;}

C Program:

Example: Hello World

Page 7: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

7

Basic Structure of a C Program (cont)

#include <stdio.h> int main(){

printf(“Hello World!”);

return 0;}

C Program:

Example: Hello world

Includes standard input/output library of procedures.

Read: “Hash-include”

Page 8: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

8

Basic Structure of a C Program (cont)

#include <stdio.h> int main(){

printf(“Hello World!”);

return 0;}

C Program:

Example: Hello world

Instruction (function call) to output “Hello World!”

Page 9: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

9

int main(){

return 0;}

Example -- Count to 10

Print out numbers 0 to 9

set count to 0while ( count is less than 10 ){

output countadd 1 to count

}

Page 10: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

10

#include <stdio.h>

int main(){

return 0;}

Example -- Count to 10 (cont)

Print out numbers 0 to 9

set count to 0while ( count is less than 10 ){

output countadd 1 to count

}

Page 11: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

11

#include <stdio.h>

/* Print out numbers 0 to 9 */

int main(){

return 0;}

Example -- Count to 10 (cont)

Print out numbers 0 to 9

set count to 0while ( count is less than 10 ){

output countadd 1 to count

}

Comment

Page 12: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

12

#include <stdio.h>

/* Print out numbers 0 to 9 */int main(){ int count;

return 0;}

Example -- Count to 10 (cont)

Print out numbers 0 to 9

set count to 0while ( count is less than 10 ){

output countadd 1 to count

}

Variable declaration

Page 13: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

13

#include <stdio.h>

/* Print out numbers 0 to 9 */int main(){

int count;

count = 0;while ( count < 10 ){

printf(“%d\n”, count);

count++;}

return 0;}

Example -- Count to 10 (cont)

Print out numbers 0 to 9

set count to 0while ( count is less than 10 ){

output countadd 1 to count

}

Page 14: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

14

#include <stdio.h>

/* Print out numbers 0 to 9 */int main(){

int count;

count = 0;while ( count < 10 ){

printf(“%d\n”, count);

count++;}

return 0;}

Example -- Count to 10 (cont)

Print out numbers 0 to 9

set count to 0while ( count is less than 10 ){

output countadd 1 to count

}

Assignment of a value (right expression) to a variable (left).

Page 15: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

15

#include <stdio.h>

/* Print out numbers 0 to 9 */int main(){

int count;

count = 0;while ( count < 10 ){

printf(“%d\n”, count);

count++;}

return 0;}

Example -- Count to 10 (cont)

Print out numbers 0 to 9

set count to 0while ( count is less than 10 ){

output countadd 1 to count

} No semi-colon here!

Page 16: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

16

#include <stdio.h>

/* Print out numbers 0 to 9 */int main(){

int count;

count = 0;while ( count < 10 ){

printf(“%d\n”, count);

count++;}

return 0;}

Example -- Count to 10 (cont)

Print out numbers 0 to 9

set count to 0while ( count is less than 10 ){

output countadd 1 to count

}

Page 17: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

17

#include <stdio.h>

/* Print out numbers 0 to 9 */int main(){

int count;

count = 0;while ( count < 10 ){

printf(“%d\n”, count);

count++;}

return 0;}

Example -- Count to 10 (cont)

Print out numbers 0 to 9

set count to 0while ( count is less than 10 ){

output countadd 1 to count

}

Format string

Page 18: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

18

#include <stdio.h>

/* Print out numbers 0 to 9 */int main(){

int count;

count = 0;while ( count < 10 ){

printf(“%d\n”, count);

count++;}

return 0;}

Example -- Count to 10 (cont)

Print out numbers 0 to 9

set count to 0while ( count is less than 10 ){ output count

add 1 to count}

same as

count = count + 1;

Page 19: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

19

#include <stdio.h>

/* Print out numbers 0 to 9 */int main(){

int count;

count = 0;while ( count < 10 ){

printf(“%d\n”, count);

count++;}

return 0;}

Example -- Count to 10 (cont)

Print out numbers 0 to 9

set count to 0while ( count is less than 10 ){

output countadd 1 to count

}

Page 20: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

20

Determine the sign of a number

output “Enter a number”input num

if (num is less than 0)then{

output num “ is -’ve”}else{

output num “ is +’ve”}

#include <stdio.h>

/* Determine the sign of a number */ int main(){

float num;

printf(“Enter a number: “);scanf(“%f”, &num);

if ( num < 0 ){ printf(“%f is -’ve\n”, num);}else{ printf(“%f is +’ve\n”, num);}

return 0;}

Example -- What’s your sign?

Page 21: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

21

Determine the sign of a number

output “Enter a number”input num

if (num is less than 0)then{

output num “ is -’ve”}else{

output num “ is +’ve”}

#include <stdio.h>

/* Determine the sign of a number */ int main(){

float num;

printf(“Enter a number: “);scanf(“%f”, &num);

if ( num < 0 ){ printf(“%f is -’ve\n”, num);}else{ printf(“%f is +’ve\n”, num);}

return 0;}

Example -- What’s your sign? (cont)

Page 22: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

22

Determine the sign of a number

output “Enter a number”input num

if (num is less than 0)then{

output num “ is -’ve”}else{

output num “ is +’ve”}

#include <stdio.h>

/* Determine the sign of a number */ int main(){

float num;

printf(“Enter a number: “);scanf(“%f”, &num);

if ( number < 0 ){ printf(“%f is -’ve\n”, num);}else{ printf(“%f is +’ve\n”, num);}return 0;

}

Example -- What’s your sign? (cont)

Page 23: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

23

Determine the sign of a number

output “Enter a number”input num

if (num is less than 0)then{

output num “ is -’ve”}else{

output num “ is +’ve”}

#include <stdio.h>

/* Determine the sign of a number */ int main(){

float num;

printf(“Enter a number: “);scanf(“%f”, &num);

if ( num < 0 ){ printf(“%f is -’ve\n”, num);}else{ printf(“%f is +’ve\n”, num);}return 0;

}

Example -- What’s your sign? (cont)

Page 24: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

24

Determine the sign of a number

output “Enter a number”input num

if (num is less than 0)then{

output num “ is -’ve”}else{

output num “ is +’ve”}

#include <stdio.h>

/* Determine the sign of a number */ int main(){

float num;

printf(“Enter a number: “);scanf(“%f”, &num);

if ( num < 0 ){ printf(“%f is -’ve\n”, num);}else{ printf(“%f is +’ve\n”, num);}return 0;

}

Example -- What’s your sign? (cont)

Page 25: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

25

Topics

Structure of a C program

• Values and variables

• Expressions

• Function calls

• Comments

Page 26: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

26

Values and Variables

• Basic Types:

– Integers

– Floating point numbers

– Characters

– Character Strings

Page 27: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

27

Basic Types: int and float

• Integers (int)0 1 1000 -1 -10 666

• Floating point numbers (float)1.0 .1 1.0e-1 1e1

Page 28: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

28

Basic Types: char

• Characters (char)’a’ ’z’ ’A’ ’Z’ ’?’ ’@’ ’0’ ’9’

- Special Characters: preceded by \’\n’ ’\t’ ’\0’ ’\’’ ’\\’ etc.

Page 29: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

29

Basic Types: character string

• Character Strings (a string of char-s)

• Examples:– ”Hi there!”– ”Line 1\nLine 2\nLine 3”– ””– ”\”\””

Page 30: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

30

Topics

Structure of a C programValues and variables

• Expressions

• Function calls

• Comments

Page 31: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

31

Expressions

• Combine values using operators and function calls

• Return a value of a known type

Page 32: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

32

Arithmetic Expressions• Expressions which take arithmetic

(numerical) values and return an arithmetic (numerical) value

• Are composed using the following operators:- (unary minus; a.k.a. “negation”)* (multiplication)/ (division or quotient)% (modulus or remainder)+ (addition)- (subtraction)

Page 33: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

33

Precedence in Expressions

• Defines the order in which an expression is evaluated

Page 34: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

34

Precedence in Expressions -- Example

1 + 2 * 3 - 4 / 5 =

B stands for brackets, O for Order (exponents), D for division, M for multiplication, A for addition, and S for subtraction.

B.O.D.M.A.S.1 + (2 * 3) - (4 / 5)

Page 35: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

35

Precedence in Expressions – Example (cont)

6.2

1 + 2 * 3 - 4 / 5 =

1 + (2 * 3) - (4 / 5)

Page 36: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

36

Precedence in Expressions –Example (cont)

6.2

1 + 2 * 3 - 4 / 5 =

1 + (2 * 3) - (4 / 5)

Page 37: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

37

Precedence in Expressions – Example (cont)

Integer division results in integer quotient

1 + 2 * 3 - 4 / 5 =

1 + (2 * 3) - (4 / 5)

Page 38: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

38

Precedence in Expressions – Example (cont)

= 0

7

1 + 2 * 3 - 4 / 5 =

1 + (2 * 3) - (4 / 5)

Page 39: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

39

Precedence in Expressions – Example (cont)

7

1 + 2 * 3 - 4 / 5 =

1 + (2 * 3) - (4 / 5)

Page 40: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

40

int-s and float-s

• float is a “communicable” type

• Example:

1 + 2 * 3 - 4.0 / 5

= 1 + (2 * 3) - (4.0 / 5)

= 1 + 6 - 0.8

= 6.2

Page 41: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

41

int-s and float-s – Example 2

(1 + 2) * (3 - 4) / 5

= ((1 + 2) * (3 - 4)) / 5

= (3 * -1) / 5

= -3 / 5

= 0

Page 42: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

42

int-s and float-s – Example 2 (cont)

(1 + 2.0) * (3 - 4) / 5

= ((1 + 2.0) * (3 - 4)) / 5

= (3.0 * -1) / 5

= -3.0 / 5

= -0.6

Page 43: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

43

int-s and float-s – Example 3

(1 + 2.0) * ((3 - 4) / 5)

= (1 + 2.0) * (-1 / 5)

= 3.0 * 0

= 0.0

Page 44: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

44

Evaluate a series of expressions

set result to 1 + 2 * 3 - 4 / 5output result

set result to (1 + 2) * (3 - 4) / 5output result

set result to ((((1 + 2) * 3) - 4) / 5)

output result

set result to (1 + (2 * (3 - (4 / 5))))

output result

#include <stdio.h>

Example -- Simple Expressions

Page 45: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

45

Evaluate a series of expressions

set result to 1 + 2 * 3 - 4 / 5output result

set result to (1 + 2) * (3 - 4) / 5output result

set result to ((((1 + 2) * 3) - 4) / 5)

output result

set result to (1 + (2 * (3 - (4 / 5))))

output result

#include <stdio.h>

/* Evaluate a series of expressions */

Example -- Simple Expressions (cont)

Page 46: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

46

Evaluate a series of expressions

set result to 1 + 2 * 3 - 4 / 5output result

set result to (1 + 2) * (3 - 4) / 5output result

set result to ((((1 + 2) * 3) - 4) / 5)

output result

set result to (1 + (2 * (3 - (4 / 5))))

output result

#include <stdio.h>

/* Evaluate a series of expressions */

int main(){

return 0;}

Example -- Simple Expressions (cont)

Page 47: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

47

Evaluate a series of expressions

set result to 1 + 2 * 3 - 4 / 5output result

set result to (1 + 2) * (3 - 4) / 5output result

set result to ((((1 + 2) * 3) - 4) / 5)

output result

set result to (1 + (2 * (3 - (4 / 5))))

output result

#include <stdio.h>

/* Evaluate a series of expressions */

int main(){

float result;

return 0;}

Example -- Simple Expressions (cont)

Page 48: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

48

Evaluate a series of expressions

set result to 1 + 2 * 3 - 4 / 5output result

set result to (1 + 2) * (3 - 4) / 5output result

set result to ((((1 + 2) * 3) - 4) / 5)

output result

set result to (1 + (2 * (3 - (4 / 5))))

output result

#include <stdio.h>

/* Evaluate a series of expressions */

int main(){

float result;

result = 1 + 2 * 3 - 4 / 5;

return 0;}

Example -- Simple Expressions (cont)

Page 49: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

49

Evaluate a series of expressions

set result to 1 + 2 * 3 - 4 / 5output result

set result to (1 + 2) * (3 - 4) / 5output result

set result to ((((1 + 2) * 3) - 4) / 5)

output result

set result to (1 + (2 * (3 - (4 / 5))))

output result

#include <stdio.h>

/* Evaluate a series of expressions */

int main(){

float result;

result = 1 + 2 * 3 - 4 / 5;printf(“%f\n”, result);

return 0;}

Example -- Simple Expressions (cont)

Page 50: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

50

Evaluate a series of expressions

set result to 1 + 2 * 3 - 4 / 5output result

set result to (1 + 2) * (3 - 4) / 5output result

set result to ((((1 + 2) * 3) - 4) / 5)

output result

set result to (1 + (2 * (3 - (4 / 5))))

output result

#include <stdio.h>

/* Evaluate a series of expressions */

int main(){

float result;

result = 1 + 2 * 3 - 4 / 5;printf(“%f\n”, result);

result = (1 + 2) * (3 - 4) / 5;printf(“%f\n”, result);

return 0;}

Example -- Simple Expressions (cont)

Page 51: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

51

Evaluate a series of expressions

set result to 1 + 2 * 3 - 4 / 5output result

set result to (1 + 2) * (3 - 4) / 5output result

set result to ((((1 + 2) * 3) - 4) / 5)

output result

set result to (1 + (2 * (3 - (4 / 5))))

output result

#include <stdio.h>

/* Evaluate a series of expressions */

int main(){

float result;

result = 1 + 2 * 3 - 4 / 5;printf(“%f\n”, result);

result = (1 + 2) * (3 - 4) / 5;printf(“%f\n”, result);

result = ((((1 + 2) * 3) - 4) / 5);printf(“%f\n”, result);

result = (1 + (2 * (3 - (4 / 5))));printf(“%f\n”, result);

return 0;}

Example -- Simple Expressions (cont)

Page 52: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

52

Evaluate a series of expressions

set result to 1 + 2 * 3 - 4 / 5output result

set result to (1 + 2) * (3 - 4) / 5output result

set result to ((((1 + 2) * 3) - 4) / 5)

output result

set result to (1 + (2 * (3 - (4 / 5))))

output result

#include <stdio.h>

/* Evaluate a series of expressions */

int main(){

float result;

result = 1 + 2 * 3 - 4 / 5;printf(“%f\n”, result);

result = (1 + 2) * (3 - 4) / 5;printf(“%f\n”, result);

result = ((((1 + 2) * 3) - 4) / 5);printf(“%f\n”, result);

result = (1 + (2 * (3 - (4 / 5))));printf(“%f\n”, result);

return 0;}

Example -- Simple Expressions (cont)

Page 53: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

53

Topics

Structure of a C programValues and variablesExpressions

• Function calls

• Comments

Page 54: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

54

Function Calls

• Tell the computer to execute a series of C commands and (maybe) return a value– In algorithm-speak: An invocation of a named

sequence of instructions

• Example: Solve Quadratic Equation

Page 55: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

55

Solve a quadratic equation: Ax2 + Bx + C = 0

output "Enter coefficients: "

input A B C

set x1 to result of SolveQuadratic1(A,B,C)

set x2 to result of SolveQuadratic2(A,B,C)

output x1 " or " x2

Example -- Solve Quadratic Equation

We assume that SolveQuadratic1 returns the value of:

Page 56: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

56

Solve a quadratic equation: Ax2 + Bx + C = 0

output "Enter coefficients: "

input A B C

set x1 to result of SolveQuadratic1(A,B,C)

set x2 to result of SolveQuadratic2(A,B,C)

output x1 " or " x2

We assume that SolveQuadratic2 returns the value of:

Example -- Solve Quadratic Equation (cont)

Page 57: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

57

Solve a quadratic equation: Ax2 + Bx + C = 0

output "Enter coefficients: "

input A B C

set x1 to result of SolveQuadratic1(A,B,C)

set x2 to result of SolveQuadratic2(A,B,C)

output x1 " or " x2

#include <stdio.h>#include “Quadratic.h”

We assume the actual instructions for SolveQuadratic1 and SolveQuadratic2 are in a function library that we have created

Example -- Solve Quadratic Equation (cont)

Page 58: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

58

Solve a quadratic equation: Ax2 + Bx + C = 0

output "Enter coefficients: "

input A B C

set x1 to result of SolveQuadratic1(A,B,C)

set x2 to result of SolveQuadratic2(A,B,C)

output x1 " or " x2

#include <stdio.h>#include “Quadratic.h”

/* Solve for x: Axx + Bx + C = 0 */

Example -- Solve Quadratic Equation (cont)

Page 59: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

59

Solve a quadratic equation: Ax2 + Bx + C = 0

output "Enter coefficients: "

input A B C

set x1 to result of SolveQuadratic1(A,B,C)

set x2 to result of SolveQuadratic2(A,B,C)

output x1 " or " x2

#include <stdio.h>#include “Quadratic.h”

/* Solve for x: Axx + Bx + C = 0 */

int main(){

return 0;}

Example -- Solve Quadratic Equation (cont)

Page 60: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

60

Solve a quadratic equation: Ax2 + Bx + C = 0

output "Enter coefficients: "

input A B C

set x1 to result of SolveQuadratic1(A,B,C)

set x2 to result of SolveQuadratic2(A,B,C)

output x1 " or " x2

#include <stdio.h>#include “Quadratic.h”

/* Solve for x: Axx + Bx + C = 0 */

int main(){

float A, B, C, x1, x2;

return 0;}

Example -- Solve Quadratic Equation (cont)

Page 61: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

61

Solve a quadratic equation: Ax2 + Bx + C = 0

output "Enter coefficients: "

input A B C

set x1 to result of SolveQuadratic1(A,B,C)

set x2 to result of SolveQuadratic2(A,B,C)

output x1 " or " x2

#include <stdio.h>#include “Quadratic.h”

/* Solve for x: Axx + Bx + C = 0 */

int main(){

float A, B, C, x1, x2;printf(“Enter coefficients: “);scanf(“%f %f %f”, &A, &B, &C);

return 0;}

Example -- Solve Quadratic Equation (cont)

Page 62: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

62

#include <stdio.h>#include “Quadratic.h”

/* Solve for x: Axx + Bx + C = 0 */

int main(){

float A, B, C, x1, x2;printf(“Enter coefficients: “);scanf(“%f %f %f”, &A, &B, &C);

x1 = SolveQuadratic1(A, B, C);

return 0;}

Solve a quadratic equation: Ax2 + Bx + C = 0

output "Enter coefficients: "

input A B C

set x1 to result of SolveQuadratic1(A,B,C)

set x2 to result of SolveQuadratic2(A,B,C)

output x1 " or " x2

Example -- Solve Quadratic Equation (cont)

Function Call

Page 63: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

63

#include <stdio.h>#include “Quadratic.h”

/* Solve for x: Axx + Bx + C = 0 */

int main(){

float A, B, C, x1, x2;printf(“Enter coefficients: “);scanf(“%f %f %f”, &A, &B, &C);

x1 = SolveQuadratic1(A, B, C);

x2 = SolveQuadratic2(A, B, C);

return 0;}

Solve a quadratic equation: Ax2 + Bx + C = 0

output "Enter coefficients: "

input A B C

set x1 to result of SolveQuadratic1(A,B,C)

set x2 to result of SolveQuadratic2(A,B,C)

output x1 " or " x2

Example -- Solve Quadratic Equation (cont)

Page 64: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

64

#include <stdio.h>#include “Quadratic.h”

/* Solve for x: Axx + Bx + C = 0 */

int main(){

float A, B, C, x1, x2;printf(“Enter coefficients: “);scanf(“%f %f %f”, &A, &B, &C);

x1 = SolveQuadratic1(A, B, C);

x2 = SolveQuadratic2(A, B, C);

printf(“%f or %f\n”, x1, x2);

return 0;}

Solve a quadratic equation: Ax2 + Bx + C = 0

output "Enter coefficients: "

input A B C

set x1 to result of SolveQuadratic1(A,B,C)

set x2 to result of SolveQuadratic2(A,B,C)

output x1 " or " x2

Example -- Solve Quadratic Equation (cont)

Page 65: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

65

Topics

Structure of a C programValues and variablesExpressionsFunction calls

• Comments

Page 66: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

66

Comments• Essential for documenting programs

• Run from a /* to the next */• Examples:

/* THIS IS A COMMENT */

/* So isthis */

/*** ...and this.***/

Page 67: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

67

Comments (cont)• Comments do not “nest”

/* Comments start with a “/*”and end with a “*/”but they don’t nest! */

Page 68: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

68

Topics

Structure of a C programValues and variablesExpressionsFunction callsComments

Page 69: 1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)

69

Reading

• D&D: Chapter 2– Sections 2.1 to 2.5