Lab 5 rC language Elements rVariables Declarations and Data Types rGeneral Form of a C Program rArithmetic Expressions rFormatting Numbers rExercises Note:

Slides:



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

Computer Programming w/ Eng. Applications
11-2 Identify the parts of the “main” function, which include Preprocessor Directives main function header main function body which includes Declaration.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
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
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
1 Introduction to Computers and Programming Class 3 Introduction to C Professor Avi Rosenfeld.
Introduction to C Programming
Basic Input/Output and Variables Ethan Cerami New York
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Chapter 2 Getting Started in C Programming
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1) Linda M c Iver.
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.
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Khalid Rasheed Shaikh Computer Programming Theory 1.
Chapter 2 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?
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
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++
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.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
From Algorithms to Programs Both are sets of instructions on how to do a task Algorithm: –talking to humans, easy to understand –in plain (English) language.
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.
Lecture2.
CSCE 206 Structured Programming in C
Chapter 1: Introduction to computers and C++ Programming
Chapter 2 Introduction to C++ Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chapter 2 - Introduction to C Programming
Yanal Alahmad Java Workshop Yanal Alahmad
ICS103 Programming in C Lecture 3: Introduction to C (2)
Chapter 2 - Introduction to C Programming
Java Programming: From Problem Analysis to Program Design, 4e
Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Chapter 2: Basic Elements of Java
CSCE 206 Lab Structured Programming in C
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Lecture3.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Chapter 2 - Introduction to C Programming
CSCE 206 Lab Structured Programming in C
Introduction to C Programming
Presentation transcript:

Lab 5 rC language Elements rVariables Declarations and Data Types rGeneral Form of a C Program rArithmetic Expressions rFormatting Numbers rExercises Note: Look at String.h (Appendix B AP16), time.h (Appendix B AP16). Be Careful Read Chapter 4(All Self-Check exercises ). C KEYWORDS and format must be learned by heart

Hello, World 1./* Hello, world program */ 2.#include 3. 4.int main(void) 5.{ 6. printf("hello, "); 7. printf("world\n"); 8. return 0; 9.}

Basic Programming Concepts Comment To explain what the program is for and why it is the way it is. Preprocessing Done before the program is compiled. To include header files containing information about C libraries Statement A line of code that tells computer to perform some action. Always ended with a semicolon(;). Function A module often consisting of a set of statements. Two functions in the hello world program: main, printf

Arithmetic Operators To form expressions Many statements are merely expressions. Normal order of operations is followed. Parentheses can be used. +Addition -Subtraction *Multiplication /Division % Modulus (remainder) ++Increment --Decrement

Arithmetic Operators: An Example 1./* Arithmetic operators */ 2.#include 3. 4.int main(void) 5.{ 6. printf("7+3=%d\n",7+3); 7. printf("7-3=%d\n",7-3); 8. printf("7*3=%d\n",7*3); 9. printf("7/3=%d\n",7/3); 10. printf("7%3=%d\n",7%3); 11. printf("7.0/3.0=%f\n",7.0/3.0); 12. return 0; 13.}

Variables Variable Name for a memory object. Variable names must start with letters and contain letters, digits, and underscores. a, b, i, j, counter, number_of_points, c1234, … Type What the variable represents. Could be of integer, floating point number, character, string, and many others. int, float, double, char, … Declaration Tells compiler about variables and their types. int i,j; float sum;

Numeric Types intinteger floatfloating point charcharater short or short intshort integer long or long intlong integer doublelong floating point long doublevery long floating point

Rules of Evaluating Expressions A-Parentheses rules: all expressions in parentheses must be evaluated separately. Nested parenthesized expressions must be evaluated from the inside out. B-Operator Precedence: unary+, - *, /,% binary +,- C-Associativity rule: Unary operators are evaluated right to left. Binary operators are evaluated left to right. Exp: x*y*z +a /b –c*d area=PI*radius*radius

Reading Inputs 1.#include 2.int main(void) 3.{ 4. float a,b; 5. printf("Please input the first number:\n"); 6. scanf("%f",&a); 7. printf("Please input the second number:\n"); 8. scanf("%f",&b); 9. printf("a+b=%f\n",a+b); 10. printf("a-b=%f\n",a-b); 11. printf("a*b=%f\n",a*b); 12. printf("a/b=%f\n",a/b); 13. return 0; 14.}

Assignment 1./* Arithmetic operators */ 2.#include 3. 4.int main() 5.{ 6. int a,c; 7. int b=4; 8. a=3; 9. c=a+b; 10. printf(“Sum: %d + %d -> %d\n”,a,b,c); 11. a++;b--; 12. printf(“Now a=%d and b=%d\n”,a,b); 13. return 0; 14.}

Printf Function printf(format string, print list) Exp: printf(“I am %d years old and my gpa is %f \n”,age,gpa); Placeholder: is a symbol beginning with % that indicates where and how to display the output value. (%c char, %d int, %f float and %lf for double). Exr1: if letter_1 holds ‘E’, letter_2 holds ‘A’ (they are of type char) and age of type int holds 24, what should be displayed by the following: printf(“Hi %c%c- your age is %d \n”, letter_1, letter_2, age);

Scanf Function scanf (format string, input list) Exp: scanf (“%c%d”,&first_initial,&age); Each input is preceded by ampersand &. Commas are used to separate variable names. The order of the placeholders must correspond to the order of variables in the input list. Exr2:Write a C program that asks the user to enter the radius of a circle and then displays the area and the circumference of the circle.PI is the constant macro (use #define)

Formatting numbers Format Value Displayed Output %6d 234 %1d 234 %6d -234 %1d -234 %5.1f %

Other Exercises rThe New-Wave Computer Company sells its product, the NW-PC for 7000 Dhs. In addition, it sells memory extension cards for 750 Dhs, disk drives for 2000 Dhs, and software for 350 Dhs. Given the number of memory cards, disk drives, and software packages desired by a customer purchasing an NW-PC, write a C to print out a bill of sale. rWe would like to develop an algorithm that produces a student’s interim reports for a computer science class. To solve this problem you need to know the following: l There are 3 tests, each worth 10% l All the Quizzes are worth 10% l Projects and assignments are worth 30% l The Final Exam is worth 30% Write a C program that given the grades of all of the above components calculates the final average grade.