1 MT258 Computer Programming and Problem Solving Tutorial 03.

Slides:



Advertisements
Similar presentations
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Advertisements

Kernighan/Ritchie: Kelley/Pohl:
Lecture 2 Introduction to C Programming
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
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
Structure of a C program
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Introduction to C Programming
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.
First Program in C With Output. C Language Keywords.
Chapter 2 Getting Started in C Programming
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
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.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CISC105 – General Computer Science Class 9 – 07/03/2006.
Week 1 Algorithmization and Programming Languages.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
CSCI 171 Presentation 2. Program Components main() #include Variable Definition Function Prototype Program Statements Function Call Function Definition.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
S CCS 200: I NTERACTIVE I NPUT Lect. Napat Amphaiphan.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Week 1 Lecture 2SE1SA51 Introducing C SE1SA5 Sue Walmsley.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Chapter # 2 Part 2 Programs And data
BASIC ELEMENTS OF A COMPUTER PROGRAM
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 2 - Introduction to C Programming
Arrays Declarations CSCI N305
Chapter 2 - Introduction to C Programming
Lecture2.
Programmazione I a.a. 2017/2018.
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
Introduction to the C Language
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
C Arrays.
Chapter 9 - Arrays Outline 6.1 Introduction 6.2 Arrays
Chapter 2 - Introduction to C Programming
INC 161 , CPE 100 Computer Programming
Chapter 2 - Introduction to C Programming
To refer to an element, specify
Chapter 2 - Introduction to C Programming
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
Presentation transcript:

1 MT258 Computer Programming and Problem Solving Tutorial 03

2 UNIT THREE

3 C programs and computer systems w Driver w System software w Software libraries

4 C programs and computer systems

5 Pre-processing Directives w # include command instructs the pre-processing stage to insert a file into the source file Example #include This is a header file contains information of standard input and output library. It tells how the concerned functions should be used. At compilation stage, this information is used to check whether they are used correctly.

6 Pre-processing Directives w # define Constant value unconditionally modifying program code Example #define rate 20 It replaces all occurrences of the macro name in the program with the specified text. Macr o Text (no ;)

7 Pre-processing Directives Example #define HI "Have a nice day!" It replaces all occurrences of the macro name in the program with the specified text. Example #define RECTANGLE_AREA(x, y) ( (x) * (y) ) rectArea = RECTANGLE_AREA(a+4, b+7) ; rectArea = ( (a+4) * (b+7) ) ; It replaces all occurrences of the macro name in the program with the specified text. Macr o Text Macr o Text

8 Commenting in C programs w Comments contain text that may describe the purposes of a program, the roles of variables, the operation of a section of code. w Comments are completely ignored by the compiler. w Comments in C are text enclosed by the symbols /* and */. Anything can appear between this pair including newling characters. w Please refer to the examples in the “Tutorial Notes on C Programming Style Guide”

9 Values and Variables Floating point type - Fractional part - Exponent part Primitive Type Integral Type Floating Point Type charint long short floatdouble - Integral type –Store digits Primitive type

10 Values and Variables w Some examples of conversion character %ccharacter %dsigned integer %ffloating point %eexponential %sstring w Example printf (“%d, %e”, variable1, variable2);

11 Range & Precision TypeMinimumMaximumPrecisionBytes char short int long float e e e -7 4 double e e e -16 8

12 Range & Precision Type Minimum Maximum Precision char CHAR_MIN CHAR_MAX 1 short SHRT_MIN SHRT_MAX 1 int INT_MIN INT_MAX 1 long LONG_MIN LONG_MAX 1 float FLT_MIN FLT_MAX FLT_EPSILON double DBL_MIN DBL_MAX DBL_EPSILON signed charSCHAR_MINSCHAR_MAX 1 unsigned shortUSHRT_MINUSHRT_MAX1 unsigned intUINT_MINUINT_MAX1 unsigned longULONG_MINULONG_MAX1

13 Range & Precision w Defined in limits.h & float.h w Report range limits of computer system w Different from computer to computer w Constant definitions w Sign signed+ / - unsigned+ only w Size measurement sizeof( )

14 Range & Precision w Impact on greater range & precision The program uses up more computer more memory The program requires more processing time w Types Floating point types requires longer processing time than integral types

15 Integer and floating-point division w Integer Division If both operands are integer values, it is integer division. The result is the integer part of the original result. w Floating Point Division If either one of the operands is float or double, it is floating point division. The result is floating point type. w Type Casting Result = (float) 5 / 2;

16 Array w An array is an orderly arrangement of items. w It consists of a fixed number of elements. w The elements are all of the same base type under the same name in programming. w Start at index of 0 w Syntax : type identifier[number of elements]; example float Prices[25];

17 Declaring Arrays w When declaring arrays, specify Name Type of array Number of elements arrayType arrayName[ numberOfElements ]; Examples: int c[ 10 ]; float myArray[ 3284 ]; w Declaring multiple arrays of same type Format similar to regular variables Example: int b[ 100 ], x[ 27 ];

18 Array w Array elements are like normal variables c[ 0 ] = 3; printf( "%d", c[ 0 ] ); Perform operations in subscript. If x equals 3 c[ ] == c[ 3 ] == c[ x ]

19 Arrays w Operations each elements depending on type of elements whole array no operations no assignment no comparison

20 Examples Using Arrays w We could give values during declaration. Example : int cat[5] = {5,63,175,103,10}; w We can also initialize all cells to the same value. Example : Float vec[7] = 0.0; w If size omitted, initializers determine it int n[ ] = { 1, 2, 3, 4, 5 }; w Example of array initialisation int j; for (j=0; j<10; j++) intArray[j] = 0;

21 Array (activity) w For each value, declare a variable in C and initialize the variables : w i) ABC w ii) 13 w iii) 30 Good Shepherd St. w iv) 6.02 x w v)

22 Array (activity) w For each value, declare a variable in C and initialize the variables : w i) ABC char aString[] =“ABC”; w ii) 13 int x = 13; w iii) 30 Good Shepherd St. char aString[] = “30 Good Shepherd St. “; w iv) 6.02 x float cons = 6.02e23; w v) float pi= ;

23 Examples Using Arrays w Print the values of all the array int j; for (j=0; j<10; j++) printf(“%d “, intArray[j]); w Sum of all the array elements int j; for (j=0; j<10; j++) sum= sum + intArray[j];

24 Two Dimensional Arrays w Syntax : Type Arrayname[FirstIndexSize][SecondIndexSize] w Example char page[100][100]; w Please refer to the example in page 55 of unit 3.

25 Pointer w Definition of pointer A pointer is a memory cell, it’s content is used for storing an address of another memory cell and not for storing actual data.

26 Declaration of pointer : w int *a; w char *a; w float *a; w double *a; w a is an address pointer pointing to its data location

27 Declaration of pointer : w The below two declaration are the same. w int* a;

28 Declaration of pointer : w Please refer to examples in page in unit 3.

29 Activity 1 w Draw a memory location table for the following declarations and statements. w float x=20; w float *y, *z; w y= &x; w z=&x; w *y=30.5; w *z=40.5; w What is the content of x?

30 Activity 1 w Draw a memory location table for the following declarations and statements. w float x=20; w float *y, *z; w y= &x; w z=&x; w *y=30.5; w *z=40.5; w What is the content of x? w Awser : 40.5

31 Meaning of *, & and NULL w * means as at address w & means as the address of w NULL is defined in the stdio.h with a value of 0, it indicates that the pointer is not currently pointing at a location.

32 Activity 2 w Suppose a program contains the following statements: intnum, *ptr; w Write statements to print each of the following:.the address of num;.the content of num;.the address of ptr; the address of the location to which the ptr points.the integer value to which the ptr points.

33 Activity 2 w Answers : w (i)printf("the address of num is %d\n",&num); w (ii)printf("num = %d\n", num); w (iii) printf("the address of ptr is %d\n",&ptr); w (iv) printf("the address of the location to which the ptr w points %d\n ", ptr); w (v) printf("ptr points to value : %d", *ptr);

34 Activity 3 w Assume we declare an array as follows: w short int array[] = {3, 1997, 2000, 1776, 12888, 1, 9999} w If array is stored at memory location 1000, what is the value of each of the expression below: array[3] &array[2] array *array *(array + 2)

35 Activity 3 w (i)1776 w (ii)1004 w (iii)1000 w (iv)3 w (v)2000