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

Slides:



Advertisements
Similar presentations
1 C Fundamentals - I Math 130 Lecture # 2 B Smith: Sp05: With discussion on labs, this took 53 minutes. Score 3. Many caveats. B Smith: Sp05: With discussion.
Advertisements

CMSC 104, Version 9/011 Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class Project Incremental.
Lecture 2 Introduction to C Programming
Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
1 CSE1301 Computer Programming Lecture 5 C Primitives 2.
CSE1301 Computer Programming Lecture 4: C Primitives I.
Lab 5 rC language Elements rVariables Declarations and Data Types rGeneral Form of a C Program rArithmetic Expressions rFormatting Numbers rExercises Note:
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
1 Introduction to Computers and Programming Class 3 Introduction to C Professor Avi Rosenfeld.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
Basic Input/Output and Variables Ethan Cerami New York
The C Programming Lecture 24.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
COMPUTER SCIENCE I C++ INTRODUCTION
Chapter 2 Getting Started in C Programming
A First Book of ANSI C Fourth Edition
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
CSE1301 Computer Programming Lecture 5: C Primitives 2 Corrections.
Input, Output, and Processing
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1) Linda M c Iver.
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Operators in Python. Arithmetic operators Some operators in Python will look familiar (+, -, *, /) Others are new to you (%, //, **) All of these do work.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
CMSC 104, Section 301, Fall Lecture 17, 11/04/02 Homework 4a and 4b Topics Go over Homework 4a Problems , Problems Go over.
Khalid Rasheed Shaikh Computer Programming Theory 1.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 1 Scott Marino.
C is a high level language (HLL)
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
CSE1301 Computer Programming Lecture 2: Introduction to C Joselito (Joey) Chua
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
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.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Lecture2.
CSCE 206 Structured Programming in C
Chapter Topics The Basics of a C++ Program Data Types
Basic Elements of C++.
Revision Lecture
Variables, Expressions, and IO
Basic Elements of C++ Chapter 2.
CSI 121 Structure Programming Language Lecture 10: Iteration (Part 1)
Building Java Programs Chapter 2
CSI 121 Structured Programming Language Lecture 5: C Primitives 2
Programming Funamental slides
Introduction to C Topics Compilation Using the gcc Compiler
Lecture3.
CS150 Introduction to Computer Science 1
Introduction to C Topics Compilation Using the gcc Compiler
Engineering Problem Solving with C++ An Object Based Approach
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Getting Started With Coding
Presentation transcript:

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

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

4 #include 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?

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?

6 Basic Structure of a C Program output “Hello World!” Algorithm: #include int main() { printf(“Hello World!”); return 0; } C Program: Example: Hello World

7 Basic Structure of a C Program (cont) #include int main() { printf(“Hello World!”); return 0; } C Program : Example: Hello world Includes standard input/output library of procedures. Read: “Hash-include”

8 Basic Structure of a C Program (cont) #include int main() { printf(“Hello World!”); return 0; } C Program: Example: Hello world Instruction (function call) to output “Hello World!”

9 int main() { return 0; } Example -- Count to 10 Print out numbers 0 to 9 set count to 0 while ( count is less than 10 ) { output count add 1 to count }

10 #include int main() { return 0; } Example -- Count to 10 (cont) Print out numbers 0 to 9 set count to 0 while ( count is less than 10 ) { output count add 1 to count }

11 #include /* Print out numbers 0 to 9 */ int main() { return 0; } Example -- Count to 10 (cont) Print out numbers 0 to 9 set count to 0 while ( count is less than 10 ) { output count add 1 to count } Comment

12 #include /* 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 0 while ( count is less than 10 ) { output count add 1 to count } Variable declaration

13 #include /* 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 0 while ( count is less than 10 ) { output count add 1 to count }

14 #include /* 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 0 while ( count is less than 10 ) { output count add 1 to count } Assignment of a value (right expression) to a variable (left).

15 #include /* 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 0 while ( count is less than 10 ) { output count add 1 to count } No semi- colon here!

16 #include /* 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 0 while ( count is less than 10 ) { output count add 1 to count }

17 #include /* 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 0 while ( count is less than 10 ) { output count add 1 to count } Format string

18 #include /* 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 0 while ( count is less than 10 ) { output count add 1 to count } same as count = count + 1;

19 #include /* 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 0 while ( count is less than 10 ) { output count add 1 to count }

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 /* 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?

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 /* 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)

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 /* 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)

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 /* 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)

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 /* 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)

25 Topics Structure of a C program Values and variables Expressions Function calls Comments

26 Values and Variables Basic Types: –Integers –Floating point numbers –Characters –Character Strings

27 Basic Types: int and float Integers ( int ) Floating point numbers ( float ) e-1 1e1

28 Basic Types: char Characters ( char ) ’a’ ’z’ ’A’ ’Z’ ’?’ ’0’ ’9’ - Special Characters: preceded by \ ’\n’ ’\t’ ’\0’ ’\’’ ’\\’ etc.

29 Basic Types: character string Character Strings (a string of char -s) Examples: –”Hi there!” –”Line 1\nLine 2\nLine 3” –”” –”\”\””

30 Topics Structure of a C program Values and variables Expressions Function calls Comments

31 Expressions Combine values using operators and function calls Return a value of a known type

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)

33 Precedence in Expressions Defines the order in which an expression is evaluated

34 Precedence in Expressions -- Example * / 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)

35 Precedence in Expressions – Example (cont) * / 5 = 1 + (2 * 3) - (4 / 5)

36 Precedence in Expressions – Example (cont) * / 5 = 1 + (2 * 3) - (4 / 5)

37 Precedence in Expressions – Example (cont) Integer division results in integer quotient * / 5 = 1 + (2 * 3) - (4 / 5)

38 Precedence in Expressions – Example (cont) = * / 5 = 1 + (2 * 3) - (4 / 5)

39 Precedence in Expressions – Example (cont) * / 5 = 1 + (2 * 3) - (4 / 5)

40 int -s and float -s float is a “communicable” type Example: * / 5 = 1 + (2 * 3) - (4.0 / 5) = = 6.2

41 int -s and float -s – Example 2 (1 + 2) * (3 - 4) / 5 = ((1 + 2) * (3 - 4)) / 5 = (3 * -1) / 5 = -3 / 5 = 0

42 int -s and float -s – Example 2 (cont) ( ) * (3 - 4) / 5 = (( ) * (3 - 4)) / 5 = (3.0 * -1) / 5 = -3.0 / 5 = -0.6

43 int -s and float -s – Example 3 ( ) * ((3 - 4) / 5) = ( ) * (-1 / 5) = 3.0 * 0 = 0.0

44 Evaluate a series of expressions set result to * / 5 output result set result to (1 + 2) * (3 - 4) / 5 output result set result to ((((1 + 2) * 3) - 4) / 5) output result set result to (1 + (2 * (3 - (4 / 5)))) output result #include Example -- Simple Expressions

45 Evaluate a series of expressions set result to * / 5 output result set result to (1 + 2) * (3 - 4) / 5 output result set result to ((((1 + 2) * 3) - 4) / 5) output result set result to (1 + (2 * (3 - (4 / 5)))) output result #include /* Evaluate a series of expressions */ Example -- Simple Expressions (cont)

46 Evaluate a series of expressions set result to * / 5 output result set result to (1 + 2) * (3 - 4) / 5 output result set result to ((((1 + 2) * 3) - 4) / 5) output result set result to (1 + (2 * (3 - (4 / 5)))) output result #include /* Evaluate a series of expressions */ int main() { return 0; } Example -- Simple Expressions (cont)

47 Evaluate a series of expressions set result to * / 5 output result set result to (1 + 2) * (3 - 4) / 5 output result set result to ((((1 + 2) * 3) - 4) / 5) output result set result to (1 + (2 * (3 - (4 / 5)))) output result #include /* Evaluate a series of expressions */ int main() { float result; return 0; } Example -- Simple Expressions (cont)

48 Evaluate a series of expressions set result to * / 5 output result set result to (1 + 2) * (3 - 4) / 5 output result set result to ((((1 + 2) * 3) - 4) / 5) output result set result to (1 + (2 * (3 - (4 / 5)))) output result #include /* Evaluate a series of expressions */ int main() { float result; result = * / 5; return 0; } Example -- Simple Expressions (cont)

49 Evaluate a series of expressions set result to * / 5 output result set result to (1 + 2) * (3 - 4) / 5 output result set result to ((((1 + 2) * 3) - 4) / 5) output result set result to (1 + (2 * (3 - (4 / 5)))) output result #include /* Evaluate a series of expressions */ int main() { float result; result = * / 5; printf(“%f\n”, result); return 0; } Example -- Simple Expressions (cont)

50 Evaluate a series of expressions set result to * / 5 output result set result to (1 + 2) * (3 - 4) / 5 output result set result to ((((1 + 2) * 3) - 4) / 5) output result set result to (1 + (2 * (3 - (4 / 5)))) output result #include /* Evaluate a series of expressions */ int main() { float result; result = * / 5; printf(“%f\n”, result); result = (1 + 2) * (3 - 4) / 5; printf(“%f\n”, result); return 0; } Example -- Simple Expressions (cont)

51 Evaluate a series of expressions set result to * / 5 output result set result to (1 + 2) * (3 - 4) / 5 output result set result to ((((1 + 2) * 3) - 4) / 5) output result set result to (1 + (2 * (3 - (4 / 5)))) output result #include /* Evaluate a series of expressions */ int main() { float result; result = * / 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)

52 Evaluate a series of expressions set result to * / 5 output result set result to (1 + 2) * (3 - 4) / 5 output result set result to ((((1 + 2) * 3) - 4) / 5) output result set result to (1 + (2 * (3 - (4 / 5)))) output result #include /* Evaluate a series of expressions */ int main() { float result; result = * / 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)

53 Topics Structure of a C program Values and variables Expressions Function calls Comments

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

55 Solve a quadratic equation: Ax 2 + 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:

56 Solve a quadratic equation: Ax 2 + 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)

57 Solve a quadratic equation: Ax 2 + 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 #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)

58 Solve a quadratic equation: Ax 2 + 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 #include “Quadratic.h” /* Solve for x: Axx + Bx + C = 0 */ Example -- Solve Quadratic Equation (cont)

59 Solve a quadratic equation: Ax 2 + 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 #include “Quadratic.h” /* Solve for x: Axx + Bx + C = 0 */ int main() { return 0; } Example -- Solve Quadratic Equation (cont)

60 Solve a quadratic equation: Ax 2 + 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 #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)

61 Solve a quadratic equation: Ax 2 + 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 #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)

62 #include #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: Ax 2 + 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

63 #include #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: Ax 2 + 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)

64 #include #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: Ax 2 + 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)

65 Topics Structure of a C program Values and variables Expressions Function calls Comments

66 Comments Essential for documenting programs Run from a /* to the next */ Examples: /* THIS IS A COMMENT */ /* So is this*/ /* **...and this. ** */

67 Comments (cont) Comments do not “nest” /*Comments start with a “/*” and end with a “*/” but they don’t nest! */

68 Topics Structure of a C program Values and variables Expressions Function calls Comments

69 Reading D&D: Chapter 2 –Sections 2.1 to 2.5