"Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Computer Programming w/ Eng. Applications
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
True or false A variable of type char can hold the value 301. ( F )
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 Introduction to Computers and Programming Class 3 Introduction to C Professor Avi Rosenfeld.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
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 Input/Output and Variables Ethan Cerami New York
1 Lecture 3  Lexical elements  Some operators:  /, %, =, +=, ++, --  precedence and associativity  #define  Readings: Chapter 2 Section 1 to 10.
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C/C++ Programming This lecture has major focus on programming, compilation.
CPS120: Introduction to Computer Science Lecture 8.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
10/8/2015 CSI Chapter 02 1 Statements A statement in C++ is the basic unit for building C++ programs.  A statement performs an action.  A complete.
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.
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
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Chapter 2: Using Data.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
Object Oriented Programming with C++ Diploma in Computer System Design.
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Fundamental Programming: Fundamental Programming 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.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Chapter 2 Variables.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Ajmer Singh PGT(IP) Programming Fundamentals. Ajmer Singh PGT(IP) Java Character Set Character set is a set of valid characters that a language can recognize.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Introduction to Programming Lecture 4. Key Words of C main main if if else else while while do do for for.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Introduction to C Programming
Introduction to Programming Lesson 3. #include #include main ( ) { cout
1 2. Program Construction in Java. 01 Java basics.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
A Sample Program #include using namespace std; int main(void) { cout
Chapter 2 Introduction to C++ Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Programming Fundamental
Variables, Expressions, and IO
Introduction to C++.
Introduction to C++ Programming
Programming Funamental slides
CS111 Computer Programming
Introduction to C++ Programming
Expressions and Assignment
Comments Any string of symbols placed between the delimiters /* and */. Can span multiple lines Can’t be nested! Be careful. /* /* /* Hi */ is an example.
Primitive Types and Expressions
Unit 3: Variables in Java
Lexical Elements & Operators
Introduction to Programming
Programming Fundamental-1
Presentation transcript:

"Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password like your toothbrush. Don't let anybody else use it, and get a new one every six months." — Clifford Stoll — Clifford Stoll Monday 03, 2008

Introduction to C Programming Lecture 3

Today’s Lecture Include Data Types Data Types Arithmetic Operations Arithmetic Operations Tokens Tokens Numeric Constants Numeric Constants Punctuations Punctuations Operators Operators Keywords of C Keywords of C

Data Types 1. int 2. short 3. long 4. float 5. double 6. char

Arithmetic operators Plus+ Minus- Multiply* Divide/ Modulus%

Arithmetic operators i + j x * y a / b a % b

% = Remainder 5 % 2 = 1 2 % 2 = 0

4 / 2 = 2 5 / 2 = ?

Precedence Highest:( ) Highest:( ) Next:*, /, % Next:*, /, % Lowest:+, - Lowest:+, -

Another C++ Program /***************************************** ** File: proj1.c ** Author: Joe Student ** Date: 9/15/01 ** SSN: ** Section: 0304 ** ** ** This program prompts the user for two integer values then displays ** their product. ** ***********************************************/

Another C Program (con’t) #include main( ) { int value1, value2, product ; cout<<“Enter two integer values: ; cin>>value1 cin>>value2; //cin>>value1>>value2 product = value1 * value2 ; cout<<“Product = ”<<product ; return 0 ; }

Tokens The smallest element in the C language is the token. The smallest element in the C language is the token. It may be a single character or a sequence of characters to form a single item. It may be a single character or a sequence of characters to form a single item.

Tokens are: Tokens can be: Tokens can be: –Numeric constants –Character constants –String constants –Keywords –Names (identifiers) –Punctuation –Operators

Numeric Constants Numeric constants are an uninterrupted sequence of digits (and may contain a period). They never contain a comma. Numeric constants are an uninterrupted sequence of digits (and may contain a period). They never contain a comma. Examples: Examples: –123 –98.6 –

Character Constants Singular! Singular! One character defined character set. One character defined character set. Surrounded on the single quotation mark. Surrounded on the single quotation mark. Examples: Examples: –‘A’ –‘a’ –‘$’ –‘4’

String Constants A sequence characters surrounded by double quotation marks. A sequence characters surrounded by double quotation marks. Considered a single item. Considered a single item. Examples: Examples: –“UMBC” –“I like ice cream.” –“123” –“CAR” –“car”

Keywords Sometimes called reserved words. Sometimes called reserved words. Are defined as a part of the C language. Are defined as a part of the C language. Can not be used for anything else! Can not be used for anything else! Examples: Examples: –int –while –for

Names Sometimes called identifiers. Sometimes called identifiers. Can be of anything length, but on the first 31 are significant (too long is as bad as too short). Can be of anything length, but on the first 31 are significant (too long is as bad as too short). Are case sensitive: Are case sensitive: –abc is different from ABC Must begin with a letter and the rest can be letters, digits, and underscores. Must begin with a letter and the rest can be letters, digits, and underscores. Must follow the standards for this course! Must follow the standards for this course!

Punctuation Semicolons, colons, commas, apostrophes, quotation marks, braces, brackets, and parentheses. Semicolons, colons, commas, apostrophes, quotation marks, braces, brackets, and parentheses. ; :, ‘ “ [ ] { } ( ) ; :, ‘ “ [ ] { } ( )

Operators There are operators for: There are operators for: –assignments –mathematical operations –relational operations –Boolean operations –bitwise operations –shifting values –calling functions –subscripting –obtaining the size of an object –obtaining the address of an object –referencing an object through its address –choosing between alternate subexpressions

Key Words of C main main if if else else while while do do for for

x = ; = 6 ; = 6 ; Memory x 6

Memory x = a + b ; ab x

#include <iostream.h> main ( ) { int age1, age2, age3, age4, age5, age6, age7, age8, age9, age10 ; int TotalAge ; int AverageAge ; cout << “ Please enter the age of student 1: “ ; cin >> age1 ; cout << “ Please enter the age of student 2: “ ; cin >> age2 ; : : TotalAge = age1+ age2 + age3+ age4+ age5+age6+ age7+ age8+age9 + age10 ; AverageAge = TotalAge / 10 ; cout<< “The average age of the class is :” << AverageAge ; }

Quadratic Equation In algebra y = ax 2 ax 2 + bx + c In C y = a * x * x + b * x + c

a*b%c +d

a*(b%c) = a*b%c ?

Discriminant b2 - 2a = b*b - 4*a*c /2 *a Incorrect answer Solution = (b*b - 4*a*c) /(2 *a) Correct answer 4c

No expression on the left hand side of the assignment Integer division truncates fractional part Liberal use of brackets/parenthesis

Interesting Problem Given a four-digit integer, separate and print the digits on the screen Given a four-digit integer, separate and print the digits on the screen

Analysis Number = 1234 Number = 1234 Take the remainder of the above number after dividing by 10 Take the remainder of the above number after dividing by 10 Eg 1234 / 10 gives remainder % 10 = 4 Remove last digit Remove last digit –1234/10 = –123(Truncation due to Integer Division) 123 %10 gives %10 gives 3 Remove last digit Remove last digit –123/10 = 12.3 –12 (Truncation due to Integer Division) 12 % 10 gives remainder 2 12 % 10 gives remainder 2 Remove last digit Remove last digit –12/10 = 1.2 –1 (Truncation due to Integer Division) Final digit remains Final digit remains

Code #include #include main ( ) { int number; int digit; cout << “Please enter a 4 digit integer : ”; cin >> number; digit = number %10; cout <<“The digit is: “ << digit << ‘\n’; // first digit; and then << ‘\n’ number = number / 10; digit = number % 10; cout <<“The digit is: “ << digit << ‘\n’; number = number / 10; digit = number % 10; cout <<“The digit is: “ << digit << ‘\n’; number = number / 10; digit = number % 10; cout <<“The digit is: “ << digit; }