CISC105 – General Computer Science Class 2 – 6/7/2006.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
 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
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
The scanf Function The scanf function reads input from the standard input device into one or more variables Example: scanf(“%lf”, &miles); Reads a real.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
CS 201 Functions Debzani Deb.
1 CS 201 Introduction to C (2) Debzani Deb. 2 Overview C Arithmetic Expressions Formatting Numbers in Program Output Interactive Mode, Batch Mode, and.
Data types and variables
Introduction to C Programming
Software Development Method
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.
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
Chapter 2 Overview of C++ Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University Millersville, PA
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Chapter 2 Overview of C Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Computer Science 101 Introduction to Programming.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
Chapter 2: Using Data.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Overview of C. C—a high-level programming language developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories. We will discuss: –the elements of a.
1 ELE118 lecture 3 Dr. Mehmet Demirer Dr. Seniha Esen Yuksel.
© 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?
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
A.Abhari CPS1251 Topic 2: C Overview C Language Elements Variable Declaration and Data Types Statement Execution C Program Layout Formatting Output Interactive.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
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.
CSCE 206 Structured Programming in C
Topics Designing a Program Input, Processing, and Output
Chapter 2 - Introduction to C Programming
Revision Lecture
ICS103 Programming in C Lecture 3: Introduction to C (2)
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
INPUT & OUTPUT scanf & printf.
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Topics Designing a Program Input, Processing, and Output
Lecture3.
Chapter 2: Introduction to C++.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Chapter 2 - Introduction to C Programming
Variables in C Topics Naming Variables Declaring Variables
Introduction to C Programming
Presentation transcript:

CISC105 – General Computer Science Class 2 – 6/7/2006

User-Defined Identifiers (Variables) Syntax Rules: 1.An identifier can consist of only letters, digits and underscores 2.An identifier cannot begin with a digit 3.A C reserved word cannot be used as an identifier 4.An identifier defined in a C standard library should not be redefined (Advice – not a rule)

User-Defined Identifiers User-Defined Identifiers are case sensitive –Hello != hello != HELLO Accepted rule: –Constants use uppercase KMS_PER_MILE –Other identifiers in lowercase miles, kms –Separate words by using an underscore per_capita_income

Data Types int : whole numbers in the range to – Why is this limited? double : contains and integral and fractional part –Larger values use Scientific Notation –1.25E5 = 1.25e5 = 1.25 x 10 5 –Still have size limitation – all real numbers cannot be represented!

Data Types char : Represents an individual character – a letter, a digit or symbol –Represented by single quotes ‘1’, ‘a’, ‘A’, ‘ ‘ –Do not enter a char as ‘z’ at a prompt –char first_initial = ‘m’; –You can compare and add char data types but use with care! char letter = ‘A’ + 32; Will return ‘a’;

Executable Statements - scanf scanf is an input function call that usually follows a printf prompt scanf(“%lf”, &miles) & is the C address-of operator and tells scanf to where to find the variable to store the information into Using scanf for multiple input? Write scanf to input Bob?

Executable Statements – return(0) The return(0) statement returns control from the program back to the OS. The return value of 0 indicates an error free execution.

General Form of a C program

Program Style Do not write more than one command per line. Be consistent in use of spaces –/*This is a comment*/ Use program documentation to enhance the readability of your program

Program Style Every program should start with comments telling the reader: –The programmers name –Class and assignment info if appilicable –Date of the current version –Brief description of what the program does

Arithmetic Expressions + addition operator - subtraction operator * multiplication operator / division operator % remainder operator (modulus)

Arithmetic Expressions Mixed-type expressions: int + double = double Type cast – convert an expression to a different type by writing the desired type in paranthesis Unary operator (+, -) – requires one operand Binary operator – requires 2 operands

Arithmetic Expressions Rules for evaluating expressions Parentheses Operator precedence –Unary –*,/,% –+,- Associativity –Unary – right to left (right associativity) –Binary – left to right (left associativity)

Arithmetic Expressions

Writing Formulas in C Multiplication can be implied in a formula by writing the 2 items together (a=bc) –C requires the use of * operator a = b * c; Writing a formula for division we usually write the numerator and denominator on a separate line –C requires numerator and denominator on the same line m = (y – b) / (x – a); Note: use parentheses to clarify the numerator and denominator.

Formatting Numbers in Output Formatting int is easy! Just add the number of characters you wish to display before the ‘d’ –printf(“The value is: %3d.\n”, this_value); Displays this_value with a minimum of 3 characters if this_value is larger than 3 characters it will print the entire value.

Formatting Numbers in Output Formatting a double variable in output is done by indicating the total field width and the number of decimal places desired in the format %n.mf (n can be zero) –printf(“The value is : %6.2f\n”, this_value); –If this_value was the output would be (rounding the decimal)

Interactive Mode vs. Batch Mode Interactive Mode – a mode of execution where the user responds to prompts by entering data Batch Mode – a mode of execution where the program scans data from a previously prepared data file –Input Redirection is achived in Unix / MS-DOS by placing <filename at the end of the command line

A matter of Style

Output Redirection You can redirect output to file by using >filename You can use input and output redirection on the same command line: –myprog myoutput

Program Controlled – I/O Files

Common Programming Errors Syntax Errors: Violation of C grammar rules – detected by compiler as it attempts to translate your program Run-Time Errors: Detected and displayed by computer during execution of program Undetected Errors: An execution error that does not prevent successful run but can lead to incorrect results. Logic Errors: Program follows a faulty algorithm

Syntax Error

Run-Time Error

Undetected Error

Error due to omission

Functions We have already seen some functions From stdio.h – printf and scanf From math.h – sqrt and others (see table 3.1)

Functions Why use functions? Need to do a computation several times in a program Way to modularize a program –Big problem -> smaller problems –Top-down Design Code Reuse –Use previous program to solve a bigger/slightly different problem –Why reinvent the wheel – sqrt would be difficult to write every time you need it – just use the math.h library!

Functions Procedural Abstractions: main program only contains a list of sub-functions

Aspects of a Function Declarations –tells the name of the function –what inputs are required –the type of output to expect Note: Declarations are usually put in a.h file, but.h files are more advanced so we will have the declaration in same file as the main program.

Aspects of a Function Definition –Defines what the function does –Contains the actual code for whatever task the function needs to program Use –When a function is used in another place in a program –The same function can be used several times

House and Stick Figure What functions to write?

Structure Chart of Functions

Function Declaration and Main Program

Function Definitions

Function Use and Control Flow

Functions with Arguments Input Arguments: arguments used to pass information into a function program Actual Argument = Formal Parameter is rnum

Functions with Arguements Output Argument: arguments used to return results to the calling function What would be returned by the following? double result = scale(25.0, 4); Answer: 250,000

Function Notes Variables are local –Variables are not shared between functions –Variable names can be reused in functions Do not point to same memory space Should only be used when referencing the same information –Necessary variables must be passed a arguments Constant Variables are Global Values of constants are unique and can be accessed from all functions in the program

Function Notes Make sure you comment preconditions and postconditions –Precondition: condition assumed to be true prior to a function call N and m are defined, math.h is included –Postcondition: condition assumed to be true after a function call

Function Notes Number of arguments in a function call must be equal to the formal parameters in the function definition! Order of Arguments in the function call must correspond to the order required in the formal parameters in the function definition Each argument in the function call must be of the same data type as the formal parameter in the function definition