Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.

Slides:



Advertisements
Similar presentations
Arithmetic Calculations
Advertisements

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 1 Engineering Problem Solving.
Computer Programming w/ Eng. Applications
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.
Introduction to C Programming
Chapter 2 Introduction to C Programming
Friday, December 08, 2006 “Experience is something you don't get until just after you need it.” - Olivier.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
1 Engineering Problem Solving with C++ An Object Based Approach Chapter 2 Simple C++ Programs.
Introduction to C Programming
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Basic Elements of C++ Chapter 2.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
COMPUTER SCIENCE I C++ INTRODUCTION
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
ECE 264 Object-Oriented Software Development Instructor: Dr. Michael Geiger Spring 2009 Lecture 2: Basic C++ Programs.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 3: Introduction to C: Input & Output, Assignments, Math functions.
Simple C++ Programs Program Structure Constants and Variables
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
2/4/2016Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs.
Chapter 3 – Variables and Arithmetic Operations. First Program – volume of a box /************************************************************/ /* Program.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Simple C Programs.
Today Variable declaration Mathematical Operators Input and Output Lab
CSCE 206 Structured Programming in C
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1: Introduction to computers and C++ Programming
Chapter 2 Introduction to C++ Programming
INSPIRING CREATIVE AND INNOVATIVE MINDS
Chapter 2 - Introduction to C Programming
BIL 104E Introduction to Scientific and Engineering Computing
Basic Elements of C++.
Chapter 2 - Introduction to C Programming
Java Programming: From Problem Analysis to Program Design, 4e
Basic Elements of C++ Chapter 2.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
A First Book of ANSI C Fourth Edition
Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Presentation transcript:

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs

Etter/Ingber Program Structure

Etter/Ingber Program Structure - General Form preprocessing directives int main(void) { declarations statements }

Etter/Ingber Program Structure Comments begin with the characters /* and end with the characters */ Preprocessor directives give instructions to the compiler Every C program contains one function named main The body of the main function is enclosed by braces, { }

Etter/Ingber Program Structure - continued The main function contains two types of commands: declarations and statements Declarations and statements are required to end with a semicolon (;) Preprocessor directives do not end with a semicolon To exit the program, use a return 0; statement

Etter/Ingber Program Structure - First Program /******************************************************************/ /* Program chapter1 */ /* */ /* This program computes the sum two numbers */ #include int main(void) { /* Declare and initialize variables. */ double number1 = , number2 = 45.7, sum; /* Calculate sum. */ sum = number1 + number2; /* Print the sum. */ printf(The sum is %5.2f \n, sum); /* Exit program. */ return 0; } /***************************************************************************/

Etter/Ingber Constants and Variables

Etter/Ingber Constants and Variables A constant is a specific value A variable is a memory location that is assigned a name or an identifier An identifier is used to reference a memory location. Rules for selecting a valid identifier must begin with an alphabetic character or underscore may contain only letters, digits and underscore (no special characters) case sensitive can not use keywords as identifiers

Etter/Ingber C Data Types Integers short int long Floating-Point Values float double long double Characters char

Etter/Ingber Symbolic Constants 4 Defined with a preprocessor directive 4 Compiler replaces each occurrence of the directive identifier with the constant value in all statements that follow the directive 4 Example –#define PI

Etter/Ingber Assignment Statements

Etter/Ingber Assignment Statements 4 Used to assign a value to a variable 4 General Form: identifier = expression; 4 Example 1 double sum = 0;sum 4 Example 2 int x; x=5;x 4 Example 3 char ch; ch = a;a 0 5 a

Etter/Ingber Assignment Statements - continued 4 Example 3 int x, y, z; x=y=0; z=2;x y z 4 Example 4 y=z;y

Etter/Ingber Arithmetic Operators 4 Addition+ 4 Subtraction- 4 Multiplication* 4 Division/ 4 Modulus% –Modulus returns remainder of division between two integers –Example 5%2 returns a value of 1

Etter/Ingber Integer Division 4 Division between two integers results in an integer. 4 The result is truncated, not rounded 4 Example: 5/3 is equal to 1 3/6 is equal to 0

Etter/Ingber Priority of Operators 1 ParenthesesInner most first 2 Unary operatorsRight to left (+ -) 3 Binary operatorsLeft to right (* / %) 4 Binary operatorsLeft to right (+ -)

Etter/Ingber Increment and Decrement Operators 4 Increment Operator ++ post incrementx++; pre increment++x; 4 Decrement Operator - - post decrementx- -; pre decrement- -x;

Etter/Ingber Abbreviated Assignment Operator operatorexampleequivalent statement +=x+=2; x=x+2; -=x-=2;x=x-2; *=x*=y;x=x*y; /=x/=y;x=x/y; %=x%=y;x=x%y;

Etter/Ingber

Standard Input and Output

Etter/Ingber Standard Output 4 printf Function –prints information to the screen –requires two arguments control string conversion specifier 4 Example double angle = 45.5; printf(Angle = %.2f degrees \n, angle); Output: Angle = degrees

Etter/Ingber Standard Input 4 scanf Function –inputs values from the keyboard –required arguments control string memory locations that correspond to the specifiers in the control string 4 Example: double distance; char unit_length; scanf("%1f %c", &distance, &unit_length); X It is very important to use a specifier that is appropriate for the data type of the variable

Etter/Ingber

Practice! printf("Sum = %5i; Average = %7.1f \n", sum, average); printf("Sum = %4i \n Average = %8.4f \n", sum, average); printf("Sum and Average \n\n %d %.1f \n", sum, average); printf("Character is %c; Sum is %c \n", ch, sum); printf("Character is %i; Sum is %i \n", ch, sum); Assume that the integer variable sum contains the value 65, the double variable average contains the value and that the char variable ch contains the value 'b'. Show the output line (or lines) generated by the following statements.

Etter/Ingber Library Functions

Etter/Ingber Math Functions fabs(x)Absolute value of x. sqrt(x)Square root of x, where x>=0. pow(x,y)Exponentiation, x y. Errors occur if x=0 and y<=0, or if x<0 and y is not an integer. ceil(x)Rounds x to the nearest integer toward (infinity). Example, ceil(2.01) is equal to 3. floor(x)Rounds x to the nearest integer toward - (negative infinity). Example, floor(2.01) is equal to 2. exp(x)Computes the value of e x. log(x)Returns ln x, the natural logarithm of x to the base e. Errors occur if x<=0. log10(x)Returns log10x, logarithm of x to the base 10. Errors occur if x<=0.

Etter/Ingber Trigonometric Functions sin(x)Computes the sine of x, where x is in radians. cos(x)Computes the cosine of x, where x is in radians tan(x)Computes the tangent of x, where x is in radians. asin(x)Computes the arcsine or inverse sine of x, where x must be in the range [-1, 1]. Returns an angle in radians in the range [- /2, /2]. acos(x)Computes the arccosine or inverse cosine of x, where x must be in the range [-1, 1]. Returns an angle in radians in the range [0, ]. atan(x)Computes the arctangent or inverse tangent of x. The Returns an angle in radians in the range [- /2, /2]. atan2(y,x) Computes the arctangent or inverse tangent of the value y/x. Returns an angle in radians in the range [-, ].

Etter/Ingber Character Functions toupper(ch)If ch is a lowercase letter, this function returns the corresponding uppercase letter; otherwise, it returns ch isdigit(ch)Returns a nonzero value if ch is a decimal digit; otherwise, it returns a zero. islower(ch)Returns a nonzero value if ch is a lowercase letter; otherwise, it returns a zero. isupper(ch)Returns a nonzero value if ch is an uppercase letter; otherwise, it returns a zero. isalpha(ch)Returns a nonzero value if ch is an uppercase letter or a lowercase letter; otherwise, it returns a zero. isalnum(ch)Returns a nonzero value if ch is an alphabetic character or a numeric digit; otherwise, it returns a zero.