Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
Outline 2.1 Introduction 2.2 Basics of C Programs
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
CMT Programming Software Applications
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
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.
CS150 Introduction to Computer Science 1
Chapter 2 Data Types, Declarations, and Displays
Introduction to C Programming
String Escape Sequences
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
Objectives You should be able to describe: Data Types
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.
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming1 Introduction to C – Part 2.
C Tokens Identifiers Keywords Constants Operators Special symbols.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
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?
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Chapter 2: Using Data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
Lecture #5 Introduction to C++
C++ Programming: Basic Elements of C++.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Characters and tokens Characters are the basic building blocks in C program, equivalent to ‘letters’ in English language Includes every printable character.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Chapter 2 Variables.
Programming Fundamentals
By Mr. Muhammad Pervez Akhtar
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
UNIMAP Sem2-07/08EKT120: Computer Programming1 Week 2 – Introduction to Programming.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Chapter 2 Variables.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
By: Syed Shahrukh Haider
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 Variables.
Chapter 2 - Introduction to C Programming
Introduction to Java Applications
Engineering Problem Solving with C++ An Object Based Approach
Chapter 2 - Introduction to C Programming
Chapter 2 Variables.
Introduction to C Programming
Presentation transcript:

Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input of gross salary Input the first salesperson’s sales in dollars While the input is not -1 Calculate the salesperson’s wages for the week Print the salesperson’s wages for the week Input the next salesperson’s sales in dollars end while

Input the first salesperson’s sales in dollars While the input is not -1 //Calculate the salesperson’s wages for the week Wages = RM200 + (Sales * 9%) Print the salesperson’s wages for the week Input the next salesperson’s sales in dollars end while

Write an algorithms that inputs a series of 10 numbers, and determines and prints the largest of the numbers with 3 variable COUNTER- a counter to count to 10, keep track the number NUMBER - current input number LARGEST - largest number found so far Input the first number directly into the variable largest Increment counter to 2 While counter is less than or equal to 10 input a new variable into the variable number If number is greater than largest replace largest with number Increment counter Print the value of largest

Annual Interest = principal * rate * days /365 Develop an algorithm using pseudocode will input principal, rate, and days for several loans, and will calculate and display simple interest for each loan, using the formula Input the first loan principal in dollars While the input is not -1 Input the interest rate input the term of the loan in days calculate the simple interest for the loan Print the simple interest for the loan Input the loan principal for the next loan

The factorial of nonnegative integer n is written n! and defined as n!=n*(n-1)*(n-2)*…*1 for n greater than or equal to 1 and n!=1 for n=0. Write an algorithms that read a non -ve integer and computes and prints it factorial Do Input number positive while number is negative While number >=0 if number = 0 then factorial = 1 else factorial = factorial * n end if n=n-1 end while What wrong with this pseudocode ?

Introduction to C

C language character set & tokens n Reserved words n Identifiers n Constants n String constants n Punctuators n Operators

n Reserved words –keywords that identify language entities, such as statements, data types, and language element attributes –they have a special meaning to compiler –must appear in the correct location –C reserved word must be typed fully in lowercase –const, double, int

n Identifiers –when writing a program, we must come up with additional words to represent and reference certain program entities. –Needed for program variables, functions, and other program constructs –Rules for constructing identifiers n can consist of the capital letters A and Z, the lowercase letters a to z, the digits 0 to 9, and the underscore character n first character must be a letter or an underscore n virtually no length limitation, compiler only recognize first 32 character as significant n reserved word cannot be used n case sensitive

Constants n Four types of constants ; integer, floating point, character, and enumeration n Integer can be +ve or -ve numbers and can be decimal, octal or hexadecimal

/* File Name : prog1_1.c Definition : A simple program that displays a message. */ # include int main() { printf(“This is my first C program.\n”); return 0; } Output This is my first C program.

Parts of A C Program n Comments Lines –/*……..*/ –is used to comment your program while writing it n Library File –# include –# include –# indicates a preprocessor command –file with extension.h –stdio.h is a library file that contains standard I/O functions

n Main() Function –indicates the logical beginning of the program –consist of function header and function body –Function Header int main() n int indicates the function returns an integer value. –Function Body n{n{n{n{ printf(“This is my first c program.\n”); printf(“This is my first c program.\n”); return 0; }

n Statement format, –every statement will end with ; n output function, printf() n Escape character, \n –\b, back space –\r, carriage return –\\, backslash –\”, double quotation –\t, tab

Examples Printf(“\\ Hello \\ \t “ Good bye \” \n “); OUTPUT \ Hello \ “ Good bye “

Data Types in C n Basic Data Type –characterchar –integer numberint –floating-point numberfloat –double precision numberdouble –typeless/valuelessvoid

Class of Data n Character constants n string constants n Integer constants n floating-point constants

Displaying Numbers n Printf(“control string”, arg1, arg2,…); n example printf(“A Simple Control String”); Output : A Simple Control String printf(“The sum of 1 and 2 is %d), 1+2); Conversion specification argument

Conversion Specification %c - single character printf(“%c”,’A’);A %d - signed decimal integer printf(“%d”,10);10 %f - floating-point num.,decimal notation printf(“%f”,5.6); %e - floating-point num., e notation printf(“%e”,52.6);5.26e1

Conversion Specificatiom %o octalprintf(“%o”,100) 120 %d decimal 100 %x hexadecimal 64 Floating point numbers printf(“[ 10.3f ]”, 21.12); [ ] 10.3 = display the number in a total field of 10 and 3 digit to the right of the decimal point.

Conversion Specification Flags - (minus sign) left justify + display the + or - sign of the number blank display blank if +ve and - if -ve Example printf(“This number is : %+.2f \n”, ); printf(“Next number is : %+d \n”, -123); OUTPUT This number is : Next number : is -123

DISPLAYING CHARACTERS n The character representation Storage “%c”A “%d”65

Displaying Formatted Strings printf(“[%-20s]”,”Pure C”); [Pure C ] [%20.4s] [ Pure] [-%20.4] [Pure ] [%.4s] [Pure]

#include #include int main() { printf(“[30s] \n”, “Universiti Malaysia Sarawak”); printf(“[%-30s] \n”, “Universiti Malaysia Sarawak”); printf(“[%-30s] \n”, “Universiti Malaysia Sarawak”); printf(“[%15.6s] \n”, “Universiti Malaysia Sarawak”); printf(“[%.6s] \n”, “Universiti Malaysia Sarawak”); printf(“[%.6s] \n”, “Universiti Malaysia Sarawak”); printf(“[%5s|] \n”, “Universiti Malaysia Sarawak”); printf(“[%5s|] \n”, “Universiti Malaysia Sarawak”); return 0; } [ Universiti Malaysia Sarawak] [ Univer] [Univer] [Universiti Malaysia Sarawak]

Variables Variable type declaration variable type variable list; variable type - must be valid C data type variable list - consists of one of more variable names separate by commas

Common Errors n Forgetting to declare variables before using them n placing variable declaration in the middle of the program n forgetting the data type or declaring a wrong data type n forgetting the ; at the end of the statement

Variable initializations n It is possible to assign an initial value to a variable when it is declared. int I, x; char ch = ‘A’ float f _number = 10.00; C does not automatically initialize declared variables

Variable Names/identifier n 1st character must be a letter or an underscore, rest can be combination of letters, underscores, or digit. n Case sensitive n number of character is compiler-dependent n whitespace character and special character (!,$,()) is not allowed within a name n no keyword and reserved word

Data Type Modifier n Integer modifiers - usually is 2 bytes Data Type Max Min Data Type Max Min short/short int short/short int int long/long int unsigned short2550 unsigned or unsigned int unsigned long or unsigned long int

Data Type Modifier n Floating-Point modifiers - usually is 8 bytes –only one modifier, long double long double long_double; /* declare a long double type variable */

Declaration & Initialization Statement long int LongNumber; long Lnum = 100L; /* 100 is fit in short but with this declaration will forces to store in 4 bytes */ will forces to store in 4 bytes */ long A_Long = 5l; unsigned positive=35353U; unsigned long count1; short counter; long double Dlarge; long double LD=1.1e+3000

What You Have Learn ? n /* */ comment line n #include directivepreprocessor command n int main () n{n{n{n{ n declaration statements n initialization statements n statements n return 0; n}n}n}n}

Operators n A symbol performs a specific mathematical or logical operation. n Arithmetic Operators + addition -substraction *Multiple /Division %Remainder

Mixed Operand n If all operand are integer type, then the result is an integer n If any operand is a floating-point or double precision type, then the result ia a double precision n Examples printf(%.2f”, 10.0/5);float/intfloating point2.00 printf(%.2f”, 10/5.0);int/floatfloating point2.00 printf(%.2f”, );float-intfloating point5.00

Assignment Operator Lvalue = Rvalue Variable Constant Expression Variable int x, y, z; char ch; float fnum x = 10; ch = ‘C’; fnum = ; y = x*2 z = y;

Thanks…will continue on next lecture