Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data.

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
 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
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
CS 1400 Chapter 2 sections 1, 2, 4 – 6, 8,
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
CS1061 C Programming Lecture 5: Building Blocks of Simple Programs A. O’Riordan, 2004.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
 2002 Prentice Hall. All rights reserved. 1 Intro: Java/Python Differences JavaPython Compiled: javac MyClass.java java MyClass Interpreted: python MyProgram.py.
1 Introduction to Computers and Programming Class 3 Introduction to C Professor Avi Rosenfeld.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Introduction to C Programming
1  Ex: Declare a variable to store user’s age: int age; Prompt the user to enter his/her age: printf (“ How old are you? “); Read / scan the entered value.
Basic Input/Output and Variables Ethan Cerami New York
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
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.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
Agenda Sed Utility - Advanced –Using Script-files / Example Awk Utility - Advanced –Using Script-files –Math calculations / Operators / Functions –Floating.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Java Data Types. Primitive Data Types Java has 8 primitive data types: – char: used to store a single character eg. G – boolean: used to store true or.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
Lecture-2 Operators and Conditionals. Variables(again???) Type: Representation of “bits” in memory Variables: Name for a memory object. Starts with letters,
© 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.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Agenda Basic Logic Purpose if statement if / else statement
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Agenda  Take up homework  Loops - Continued –For loops Structure / Example involving a for loop  Storing Characters in variables  Introduction to Functions.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Primitive Data Types int is a primitive data type A primitive data type is one that stores only a single piece of data. TypeStorageDescription int 4 bytes+ve.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
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.
Making Interactive Programs with Visual Basic .NET
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.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
2.3 Output Formatting. Outputting Format Specify the number of spaces, “c”, used to print an integer value with specifier %cd, e.g., %3d, %4d. E.g. printf.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
Lecture2.
Arithmetic Expressions
Topics Designing a Program Input, Processing, and Output
Variables, Expressions, and IO
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Reading Input from the Keyboard
Chapter 2 - Introduction to C Programming
Conversion Check your class notes and given examples at class.
Lecture3.
Topics Designing a Program Input, Processing, and Output
Introduction to C Programming
Topics Designing a Program Input, Processing, and Output
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Presentation transcript:

Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data Types  Formatting of numbers (printf)

Commenting  Commenting your source code is very important:  Comments help user identify sections of a large program.  Comments help others understand structure of program and how program operates.  A some point, you will be required to read someone else’s C programming code!

Commenting  All comments within C source code must begin with /* symbol and end with */ symbol.  Examples:  /* This is a comment */  /* This is a comment over 2 lines */

Inputting Data from Keyboard  So far, we have only stored data in variables and displayed data, but a computer program should allow for data to be inputted (for example: the keyboard).  The scanf command is used in C to input data from the keyboard to be stored as a variable in the computer’s internal memory

Example of Program with scanf  #include main () { int age; printf (“Please enter your age: ”); scanf (“%d”, &age); printf (“you are %d years old.\n”, age); } You need to initialize a variable in order to store inputted data

Example of Program with scanf  #include main () { int age; printf (“Please enter your age: ”); scanf (“%d”, &age); printf (“you are %d years old.\n”, age); } A printf command is used to prompt user to enter data (note no newline!)

Example of Program with scanf  #include main () { int age; printf (“Please enter your age: ”); scanf (“%d”, &age); printf (“you are %d years old.\n”, age); } Since printf statement does NOT move down one line, provide a space for appearance

Example of Program with scanf  #include main () { int age; printf (“Please enter your age: ”); scanf (“%d”, &age); printf (“you are %d years old.\n”, age); } A format specifier is used to indicate the numeric data type. In this case “%d” represents an integer.

Example of Program with scanf  #include main () { int age; printf (“Please enter your age: ”); scanf (“%d”, &age); printf (“you are %d years old.\n”, age); } &age indicates the location in the computer’s internal memory that will hold inputted data

Example of Program with scanf  #include main () { int age; printf (“Please enter your age: ”); scanf (“%d”, &age); printf (“you are %d years old.\n”, age); } The format specifier is used to represent the value of the variable age in the display.

In Class Exercise  Write a C program to read your height (in centimeters - no decimals) and store in a variable called “height” and display the message: My height is x centimeters! (where x represents your height)

Arithmetic Operators  Now that numerical data can be stored in variables, we can perform mathematical calculations to convert data into information.  Arithmetic Operators include:  Multiplication (*), Division (/), Modulus (%)  Addition (+) and Subtraction (-)

Arithmetic Operators  Modulus (%) is used only with integers to determine the remainder of a number (e.g.. Examples:  7 % 3 = 4  20 % 3 = 2 (How the did you get that?!?)

Arithmetic Order of Operations  To avoid arithmetical errors, it is important to understand in what order the computer perform math calculations:  First:calculations in brackets ( )  Second:multiplication, division, modulus (in order of appearance)  Third:addition & subtraction (in order of appearance)

In Class Exercise  Calculate the results of the following expressions as if you were the computer: 3 * 4 / 2 =  * 10 / 2 =  (7 - 3) / 2 =  2 + ((15 / 3) * 7) =  4 % 2 =  100 % 33 =  % 16 =

Mixing Different Numeric Data Types  The type of numeric data type may have significant results on the outcome of a calculation!  You should choose the right numerical data type for the variable to prevent mathematical errors which would cause errors in your program!

Mixing Different Numeric Data Types  The following chart displays what data type to initialize for storing math result:  Double: When performing math on just doubles, both integers & doubles, or dividing two integers (otherwise, decimal values get “cut-off” or truncated)  Integer: Multiplying, adding, subtracting, or modulus of integers.  Note: Due to advancement of PC’s you are encouraged to use doubles instead of floats

In Class Exercise  What would be the best numerical data type for the following variables that store these calculations? (note: variables x & y are integers and variables a & b are doubles)  x + y + 1  x / (y + 3)  x + a - b  a * x + y / b

Formatting Numbers with printf  How do we make display of numbers look nice (“line –up”)?  To display the variable cars (which is a double) as currency ($ xx.xx): printf (“The sales amount is: $ %.2lf\n”, cars); %.2 lf tells printf to display only 2 decimal place for the variable (which is a double)

In Class Exercise –Write a program called minute_convert to prompt for and store the total number of minutes (assume the total number of minutes is greater than 60). Convert the number of minutes into hours and minutes. E.g. –120 minutes is the same as 2 hour(s) and 0 minutes. –75 minutes is the same as 1 hour(s) at 15 minutes.