Size: px
Start display at page:

Transcription

1 C How to Program, 6/e by Pearson Education, Inc.

2 by Pearson Education, Inc.

3 by Pearson Education, Inc.

4 This chapter serves as an introduction to the important topic of data structures. Arrays are data structures consisting of related data items of the same type. In Chapter 10, we discuss C s notion of struct (structure) a data structure consisting of related data items of possibly different types. Arrays and structures are static entities in that they remain the same size throughout program execution. In Chapter 12, we introduce dynamic data structures such as lists, queues, stacks and trees that may grow and shrink as programs execute by Pearson Education, Inc.

5 An array is a group of memory locations related by the fact that they all have the same name and the same type. To refer to a particular location or element in the array, we specify the name of the array and the position number of the particular element in the array. Figure 6.1 shows an integer array called c. This array contains 12 elements. Any one of these elements may be referred to by giving the name of the array followed by the position number of the particular element in square brackets ([]) by Pearson Education, Inc.

6 The first element in every array is the zeroth element. Thus, the first element of array c is referred to as c[0], the second element of array c is referred to as c[1], the seventh element of array c is referred to as c[6], and, in general, the ith element of array c is referred to as c[i - 1]. Array names, like other variable names, can contain only letters, digits and underscores. Array names cannot begin with a digit. The position number contained within square brackets is more formally called a subscript (or index). A subscript must be an integer or an integer expression by Pearson Education, Inc.

7 If a program uses an expression as a subscript, then the expression is evaluated to determine the subscript. For example, if a = 5 and b = 6, then the statement c[ a + b ] += 2; adds 2 to array element c[11]. A subscripted array name is an lvalue it can be used on the left side of an assignment by Pearson Education, Inc.

8 Let s examine array c (Fig. 6.1) more closely. The array s name is c. Its 12 elements are referred to as c[0], c[1], c[2],, c[11]. The value stored in c[0] is 45, the value of c[1] is 6, the value of c[2] is 0, the value of c[7] is 62 and the value of c[11] is 78. To print the sum of the values contained in the first three elements of array c, we d write printf( "%d", c[ 0 ] + c[ 1 ] + c[ 2 ] ); To divide the value of the seventh element of array c by 2 and assign the result to the variable x, we d write by Pearson Education, Inc.

9 by Pearson Education, Inc.

10 by Pearson Education, Inc.

11 The brackets used to enclose the subscript of an array are actually considered to be an operator in C. They have the same level of precedence as the function call operator (i.e., the parentheses that are placed following a function name to call that function). Figure 6.2 shows the precedence and associativity of the operators introduced to this point in the text by Pearson Education, Inc.

12 by Pearson Education, Inc.

13 Arrays occupy space in memory. You specify the type of each element and the number of elements required by each array so that the computer may reserve the appropriate amount of memory. To tell the computer to reserve 12 elements for integer array c, use the definition int c[ 12 ]; by Pearson Education, Inc.

14 The following definition int b[ 100 ], x[ 27 ]; reserves 100 elements for integer array b and 27 elements for integer array x. Arrays may contain other data types. Character strings and their similarity to arrays are discussed in Chapter 8. The relationship between pointers and arrays is discussed in Chapter by Pearson Education, Inc.

15 Figure 6.3 uses for statements to initialize the elements of a 10-element integer array n to zeros and print the array in tabular format. The first printf statement (line 16) displays the column heads for the two columns printed in the subsequent for statement by Pearson Education, Inc.

16 by Pearson Education, Inc.

17 by Pearson Education, Inc.

18 The elements of an array can also be initialized when the array is defined by following the definition with an equals sign and braces, {}, containing a commaseparated list of initializers. Figure 6.4 initializes an integer array with 10 values (line 9) and prints the array in tabular format by Pearson Education, Inc.

19 by Pearson Education, Inc.

20 by Pearson Education, Inc.

21 If there are fewer initializers than elements in the array, the remaining elements are initialized to zero. For example, the elements of the array n in Fig. 6.3 could have been initialized to zero as follows: int n[ 10 ] = { 0 }; This explicitly initializes the first element to zero and initializes the remaining nine elements to zero because there are fewer initializers than there are elements in the array by Pearson Education, Inc.

22 It s important to remember that arrays are not automatically initialized to zero. You must at least initialize the first element to zero for the remaining elements to be automatically zeroed. This method of initializing the array elements to 0 is performed at compile time for static arrays and at runtime for automatic arrays by Pearson Education, Inc.

23 by Pearson Education, Inc.

24 The array definition int n[ 5 ] = { 32, 27, 64, 18, 95, 14 }; causes a syntax error because there are six initializers and only five array elements by Pearson Education, Inc.

25 by Pearson Education, Inc.

26 If the array size is omitted from a definition with an initializer list, the number of elements in the array will be the number of elements in the initializer list. For example, int n[] = { 1, 2, 3, 4, 5 }; would create a five-element array by Pearson Education, Inc.

27 Figure 6.5 initializes the elements of a 10-element array s to the values 2, 4, 6,, 20 and prints the array in tabular format. The values are generated by multiplying the loop counter by 2 and adding by Pearson Education, Inc.

28 by Pearson Education, Inc.

29 by Pearson Education, Inc.

30 The #define preprocessor directive is introduced in this program. Line 4 #define SIZE 10 defines a symbolic constant SIZE whose value is 10. A symbolic constant is an identifier that is replaced with replacement text by the C preprocessor before the program is compiled by Pearson Education, Inc.

31 When the program is preprocessed, all occurrences of the symbolic constant SIZE are replaced with the replacement text 10. Using symbolic constants to specify array sizes makes programs more scalable. In Fig. 6.5, we could have the first for loop (line 13) fill a 1000-element array by simply changing the value of SIZE in the #define directive from 10 to If the symbolic constant SIZE had not been used, we d have to change the program in three separate places to scale the program to handle 1000 array elements by Pearson Education, Inc.

32 by Pearson Education, Inc.

33 If the #define preprocessor directive in line 4 is terminated with a semicolon, all occurrences of the symbolic constant SIZE in the program are replaced with the text 10; by the preprocessor. This may lead to syntax errors at compile time, or logic errors at execution time by Pearson Education, Inc.

34 by Pearson Education, Inc.

35 by Pearson Education, Inc.

36 by Pearson Education, Inc.

37 by Pearson Education, Inc.

38 Figure 6.6 sums the values contained in the 12-element integer array a. The for statement s body (line 16) does the totaling by Pearson Education, Inc.

39 by Pearson Education, Inc.

40 Our next example uses arrays to summarize the results of data collected in a survey. Consider the problem statement. Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 means awful and 10 means excellent). Place the 40 responses in an integer array and summarize the results of the poll. This is a typical array application (see Fig. 6.7). We wish to summarize the number of responses of each type (i.e., 1 through 10) by Pearson Education, Inc.

41 The array responses (line 17) is a 40-element array of the students responses. We use an 11-element array frequency (line 14) to count the number of occurrences of each response. We ignore frequency[0] because it s logical to have response 1 increment frequency[1] rather than frequency[0]. This allows us to use each response directly as the subscript in the frequency array by Pearson Education, Inc.

42 by Pearson Education, Inc.

43 by Pearson Education, Inc.

44 by Pearson Education, Inc.

45 by Pearson Education, Inc.

46 by Pearson Education, Inc.

47 The for loop (line 24) takes the responses one at a time from the array responses and increments one of the 10 counters (frequency[1] to frequency[10]) in the frequency array. The key statement in the loop is line 25 ++frequency[ responses[ answer ] ]; which increments the appropriate frequency counter depending on the value of responses[answer] by Pearson Education, Inc.

48 When the counter variable answer is 0, responses[answer] is 1, so ++frequeoncy[ responses[answer]]; is interpreted as ++frequency[ 1 ]; which increments array element one. When answer is 1, responses[answer] is 2, so ++frequency[responses[answer]]; is interpreted as ++frequency[ 2 ]; which increments array element two by Pearson Education, Inc.

49 When answer is 2, responses[answer] is 6, so ++frequency[responses[answer]]; is actually interpreted as ++frequency[ 6 ]; which increments array element six, and so on. Regardless of the number of responses processed in the survey, only an 11-element array is required (ignoring element zero) to summarize the results. If the data contained invalid values such as 13, the program would attempt to add 1 to frequency[13]. This would be outside the bounds of the array by Pearson Education, Inc.

50 C has no array bounds checking to prevent the program from referring to an element that does not exist. Thus, an executing program can walk off the end of an array without warning. You should ensure that all array references remain within the bounds of the array by Pearson Education, Inc.

51 by Pearson Education, Inc.

52 by Pearson Education, Inc.

53 by Pearson Education, Inc.

54 Our next example (Fig. 6.8) reads numbers from an array and graphs the information in the form of a bar chart or histogram each number is printed, then a bar consisting of that many asterisks is printed beside the number. The nested for statement (line 20) draws the bars. Note the use of printf( "\n" ) to end a histogram bar (line 24) by Pearson Education, Inc.

55 by Pearson Education, Inc.

56 by Pearson Education, Inc.

57 Roll a single six-sided die 6000 times to test whether the random number generator actually produces random numbers. An array version of this program is shown in Fig by Pearson Education, Inc.

58 by Pearson Education, Inc.

59 by Pearson Education, Inc.

60 We now discuss storing strings in character arrays. So far, the only string-processing capability we have is outputting a string with printf. A string such as "hello" is really a static array of individual characters in C. A character array can be initialized using a string literal. For example, char string1[] = "first"; initializes the elements of array string1 to the individual characters in the string literal "first" by Pearson Education, Inc.

61 In this case, the size of array string1 is determined by the compiler based on the length of the string. The string "first" contains five characters plus a special string-termination character called the null character. Thus, array string1 actually contains six elements. The character constant representing the null character is '\0'. All strings in C end with this character. A character array representing a string should always be defined large enough to hold the number of characters in the string and the terminating null character. Character arrays also can be initialized with individual character constants in an initializer list by Pearson Education, Inc.

62 The preceding definition is equivalent to char string1[] = { 'f', 'i', 'r', 's', 't', '\0' }; Because a string is really an array of characters, we can access individual characters in a string directly using array subscript notation. For example, string1[0] is the character 'f' and string1[3] is the character 's'. We also can input a string directly into a character array from the keyboard using scanf and the conversion specifier %s by Pearson Education, Inc.

63 For example, char string2[ 20 ]; creates a character array capable of storing a string of at most 19 characters and a terminating null character. The statement scanf( "%s", string2 ); reads a string from the keyboard into string2. The name of the array is passed to scanf without the preceding & used with nonstring variables. The & is normally used to provide scanf with a variable s location in memory so that a value can be stored there by Pearson Education, Inc.

64 In Section 6.5, when we discuss passing arrays to functions, we ll see that the value of an array name is the address of the start of the array; therefore, the & is not necessary. Function scanf will read characters until a space, tab, newline or end-of-file indicator is encountered. The string should be no longer than 19 characters to leave room for the terminating null character. If the user types 20 or more characters, your program may crash! For this reason, use the conversion specifier %19s so that scanf does not write characters into memory beyond the end of the array s by Pearson Education, Inc.

65 It s your responsibility to ensure that the array into which the string is read is capable of holding any string that the user types at the keyboard. Function scanf reads characters from the keyboard until the first white-space character is encountered it does not check how large the array is. Thus, scanf can write beyond the end of the array by Pearson Education, Inc.

66 by Pearson Education, Inc.

67 A character array representing a string can be output with printf and the %s conversion specifier. The array string2 is printed with the statement printf( "%s\n", string2 ); Function printf, like scanf, does not check how large the character array is. The characters of the string are printed until a terminating null character is encountered by Pearson Education, Inc.

68 Figure 6.10 demonstrates initializing a character array with a string literal, reading a string into a character array, printing a character array as a string and accessing individual characters of a string by Pearson Education, Inc.

69 by Pearson Education, Inc.

70 by Pearson Education, Inc.

71 Figure 6.10 uses a for statement (line 22) to loop through the string1 array and print the individual characters separated by spaces, using the %c conversion specifier. The condition in the for statement, string1[i]!= '\0', is true while the terminating null character has not been encountered in the string by Pearson Education, Inc.

72 A static local variable exists for the duration of the program, but is visible only in the function body. We can apply static to a local array definition so the array is not created and initialized each time the function is called and the array is not destroyed each time the function is exited in the program. This reduces program execution time, particularly for programs with frequently called functions that contain large arrays by Pearson Education, Inc.

73 by Pearson Education, Inc.

74 Arrays that are static are initialized once at compile time. If you do not explicitly initialize a static array, that array s elements are initialized to zero by the compiler. Figure 6.11 demonstrates function staticarrayinit (line 22) with a local static array (line 25) and function automaticarrayinit (line 44) with a local automatic array (line 47). Function staticarrayinit is called twice (lines 12 and 16). The local static array in the function is initialized to zero by the compiler (line 25). The function prints the array, adds 5 to each element and prints the array again by Pearson Education, Inc.

75 The second time the function is called, the static array contains the values stored during the first function call. Function automaticarrayinit is also called twice (lines 13 and 17). The elements of the automatic local array in the function are initialized with the values 1, 2 and 3 (line 47). The function prints the array, adds 5 to each element and prints the array again. The second time the function is called, the array elements are initialized to 1, 2 and 3 again because the array has automatic storage duration by Pearson Education, Inc.

76 by Pearson Education, Inc.

77 by Pearson Education, Inc.

78 by Pearson Education, Inc.

79 by Pearson Education, Inc.

80 by Pearson Education, Inc.

C How to Program, 7/e This chapter serves as an introduction to data structures. Arrays are data structures consisting of related data items of the same type. In Chapter 10, we discuss C s notion of

### Fundamentals of Programming Session 14

Fundamentals of Programming Session 14 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2013 These slides have been created using Deitel s slides Sharif University of Technology Outlines

### Programming for Engineers Arrays

Programming for Engineers Arrays ICEN 200 Spring 2018 Prof. Dola Saha 1 Array Ø Arrays are data structures consisting of related data items of the same type. Ø A group of contiguous memory locations that

### C Arrays. O b j e c t i v e s In this chapter, you ll:

www.thestudycampus.com C Arrays O b j e c t i v e s In this chapter, you ll: Use the array data structure to represent lists and tables of values. Define an array, initialize an array and refer to individual

### C: How to Program. Week /Apr/23

C: How to Program Week 9 2007/Apr/23 1 Review of Chapters 1~5 Chapter 1: Basic Concepts on Computer and Programming Chapter 2: printf and scanf (Relational Operators) keywords Chapter 3: if (if else )

### C++ Programming. Arrays and Vectors. Chapter 6. Objectives. Chiou. This chapter introduces the important topic of data structures collections

C++ Programming Chapter 6 Arrays and Vectors Yih-Peng Chiou Room 617, BL Building (02) 3366-3603 3603 ypchiou@cc.ee.ntu.edu.tw Photonic Modeling and Design Lab. Graduate Institute of Photonics and Optoelectronics

### C: How to Program. Week /Apr/16

C: How to Program Week 8 2006/Apr/16 1 Storage class specifiers 5.11 Storage Classes Storage duration how long an object exists in memory Scope where object can be referenced in program Linkage specifies

### Fundamentals of Programming Session 4

Fundamentals of Programming Session 4 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2011 These slides are created using Deitel s slides, ( 1992-2010 by Pearson Education, Inc).

Java How to Program, 10/e Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. Data structures Collections of related data items. Discussed in depth in Chapters 16 21. Array objects Data

### C Arrays. Group of consecutive memory locations Same name and type. Array name + position number. Array elements are like normal variables

1 6 C Arrays 6.2 Arrays 2 Array Group of consecutive memory locations Same name and type To refer to an element, specify Array name + position number arrayname[ position number ] First element at position

### Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program)

Chapter - Arrays 1.1 Introduction 2.1 Introduction.2 Arrays.3 Declaring Arrays. Examples Using Arrays.5 Passing Arrays to Functions.6 Sorting Arrays. Case Study: Computing Mean, Median and Mode Using Arrays.8

C How to Program, 6/e 1992-2010 by Pearson Education, Inc. An important part of the solution to any problem is the presentation of the results. In this chapter, we discuss in depth the formatting features

### Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program) A few types

Chapter 4 - Arrays 1 4.1 Introduction 4.2 Arrays 4.3 Declaring Arrays 4.4 Examples Using Arrays 4.5 Passing Arrays to Functions 4.6 Sorting Arrays 4.7 Case Study: Computing Mean, Median and Mode Using

1 6 C Arrays 2 Now go, write it before them in a table, and note it in a book. Isaiah 30:8 To go beyond is as wrong as to fall short. Confucius Begin at the beginning, and go on till you come to the end:

### Programming for Engineers Introduction to C

Programming for Engineers Introduction to C ICEN 200 Spring 2018 Prof. Dola Saha 1 Simple Program 2 Comments // Fig. 2.1: fig02_01.c // A first program in C begin with //, indicating that these two lines

### C: How to Program. Week /Mar/05

1 C: How to Program Week 2 2007/Mar/05 Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers

### Fundamentals of Programming. Lecture 3: Introduction to C Programming

Fundamentals of Programming Lecture 3: Introduction to C Programming Instructor: Fatemeh Zamani f_zamani@ce.sharif.edu Sharif University of Technology Computer Engineering Department Outline A Simple C

### Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

1 History C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC

### Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 1 ARRAYS Arrays 2 Arrays Structures of related data items Static entity (same size

### Chapter 2 - Introduction to C Programming

Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers 2.4 Memory Concepts 2.5 Arithmetic

### Full file at C How to Program, 6/e Multiple Choice Test Bank

2.1 Introduction 2.2 A Simple Program: Printing a Line of Text 2.1 Lines beginning with let the computer know that the rest of the line is a comment. (a) /* (b) ** (c) REM (d)

### Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

GATE- 2016-17 Postal Correspondence 1 C-Programming Computer Science & Information Technology (CS) 20 Rank under AIR 100 Postal Correspondence Examination Oriented Theory, Practice Set Key concepts, Analysis

### C++ PROGRAMMING SKILLS Part 4: Arrays

C++ PROGRAMMING SKILLS Part 4: Arrays Outline Introduction to Arrays Declaring and Initializing Arrays Examples Using Arrays Sorting Arrays: Bubble Sort Passing Arrays to Functions Computing Mean, Median

### Deitel Series Page How To Program Series

Deitel Series Page How To Program Series Android How to Program C How to Program, 7/E C++ How to Program, 9/E C++ How to Program, Late Objects Version, 7/E Java How to Program, 9/E Java How to Program,

### c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

2.1 Introduction (No questions.) 2.2 A Simple Program: Printing a Line of Text 2.1 Which of the following must every C program have? (a) main (b) #include (c) /* (d) 2.2 Every statement in C

### A Fast Review of C Essentials Part I

A Fast Review of C Essentials Part I Structural Programming by Z. Cihan TAYSI Outline Program development C Essentials Functions Variables & constants Names Formatting Comments Preprocessor Data types

### Introduction to C Programming

1 2 Introduction to C Programming 2.6 Decision Making: Equality and Relational Operators 2 Executable statements Perform actions (calculations, input/output of data) Perform decisions - May want to print

### B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Unit 1 Programming Language and Overview of C 1. State whether the following statements are true or false. a. Every line in a C program should end with a semicolon. b. In C language lowercase letters are

### Chapter 1 & 2 Introduction to C Language

1 Chapter 1 & 2 Introduction to C Language Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 1 & 2 - Introduction to C Language 2 Outline 1.1 The History

### Content. In this chapter, you will learn:

ARRAYS & HEAP Content In this chapter, you will learn: To introduce the array data structure To understand the use of arrays To understand how to define an array, initialize an array and refer to individual

### Outline. 7.1 Introduction. 7.2 Arrays. 7.2 Arrays

jhtp5_07.fm Page 279 Wednesday, November 20, 2002 12:44 PM 7 Arrays Objectives To introduce the array data structure. To understand the use of arrays to store, sort and search lists and tables of values.

### Function Call Stack and Activation Records

71 Function Call Stack and Activation Records To understand how C performs function calls, we first need to consider a data structure (i.e., collection of related data items) known as a stack. Students

### 1 Lexical Considerations

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2013 Handout Decaf Language Thursday, Feb 7 The project for the course is to write a compiler

### UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

UNIT - I Introduction to C Programming Introduction to C C was originally developed in the year 1970s by Dennis Ritchie at Bell Laboratories, Inc. C is a general-purpose programming language. It has been

### Data Types and Variables in C language

Data Types and Variables in C language Basic structure of C programming To write a C program, we first create functions and then put them together. A C program may contain one or more sections. They are

### UNIT- 3 Introduction to C++

UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . \$, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage

### Chapter 3 Structure of a C Program

Chapter 3 Structure of a C Program Objectives To be able to list and describe the six expression categories To understand the rules of precedence and associativity in evaluating expressions To understand

### 19 Much that I bound, I could not free; Much that I freed returned to me. Lee Wilson Dodd

19 Much that I bound, I could not free; Much that I freed returned to me. Lee Wilson Dodd Will you walk a little faster? said a whiting to a snail, There s a porpoise close behind us, and he s treading

### بسم اهلل الرمحن الرحيم

بسم اهلل الرمحن الرحيم Fundamentals of Programming C Session # 10 By: Saeed Haratian Fall 2015 Outlines Examples Using the for Statement switch Multiple-Selection Statement do while Repetition Statement

### IT 374 C# and Applications/ IT695 C# Data Structures

IT 374 C# and Applications/ IT695 C# Data Structures Module 2.1: Introduction to C# App Programming Xianrong (Shawn) Zheng Spring 2017 1 Outline Introduction Creating a Simple App String Interpolation

### Arrays. Lecture 9 COP 3014 Fall October 16, 2017

Arrays Lecture 9 COP 3014 Fall 2017 October 16, 2017 Array Definition An array is an indexed collection of data elements of the same type. Indexed means that the array elements are numbered (starting at

### CSC 211 Intermediate Programming. Arrays & Pointers

CSC 211 Intermediate Programming Arrays & Pointers 1 Definition An array a consecutive group of memory locations that all have the same name and the same type. To create an array we use a declaration statement.

### Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Introduction to C Programming Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline Printing texts Adding 2 integers Comparing 2 integers C.E.,

### AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

AN OVERVIEW OF C CSE 130: Introduction to Programming in C Stony Brook University WHY C? C is a programming lingua franca Millions of lines of C code exist Many other languages use C-like syntax C is portable

### Intro to Computer Programming (ICP) Rab Nawaz Jadoon

Intro to Computer Programming (ICP) Rab Nawaz Jadoon DCS COMSATS Institute of Information Technology Assistant Professor COMSATS IIT, Abbottabad Pakistan Introduction to Computer Programming (ICP) What

Two ways to pass arguments to functions in many programming languages are pass-by-value and pass-by-reference. When an argument is passed by value, a copy of the argument s value is made and passed (on

### Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA.

Arrays Defining arrays, declaration and initialization of arrays Introduction Many applications require the processing of multiple data items that have common characteristics (e.g., a set of numerical

### Fundamental of Programming (C)

Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 3 Constants, Variables, Data Types, And Operations Department of Computer Engineering

### Lab Session # 5 Arrays. ALQUDS University Department of Computer Engineering

2013/2014 Programming Fundamentals for Engineers Lab Lab Session # 5 Arrays ALQUDS University Department of Computer Engineering Objective: After completing this session, the students should be able to:

### 12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Java How to Program, 9/e Education, Inc. All Rights Reserved. } Java application programming } Use tools from the JDK to compile and run programs. } Videos at www.deitel.com/books/jhtp9/ Help you get started

### ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

ITC213: STRUCTURED PROGRAMMING Bhaskar Shrestha National College of Computer Studies Tribhuvan University Lecture 07: Data Input and Output Readings: Chapter 4 Input /Output Operations A program needs

Assoc. Prof. Dr. Marenglen Biba (C) 2010 Pearson Education, Inc. All rights reserved. Java application A computer program that executes when you use the java command to launch the Java Virtual Machine

### C Programming

204216 -- C Programming Chapter 3 Processing and Interactive Input Adapted/Assembled for 204216 by Areerat Trongratsameethong A First Book of ANSI C, Fourth Edition Objectives Assignment Mathematical Library

### Chapter 2: Introduction to C++

Chapter 2: Introduction to C++ Copyright 2010 Pearson Education, Inc. Copyright Publishing as 2010 Pearson Pearson Addison-Wesley Education, Inc. Publishing as Pearson Addison-Wesley 2.1 Parts of a C++

### Reserved Words and Identifiers

1 Programming in C Reserved Words and Identifiers Reserved word Word that has a specific meaning in C Ex: int, return Identifier Word used to name and refer to a data element or object manipulated by the

### Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture 05 I/O statements Printf, Scanf Simple

### C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C Language Part 1 (Minor modifications by the instructor) References C for Python Programmers, by Carl Burch, 2011. http://www.toves.org/books/cpy/ The C Programming Language. 2nd ed., Kernighan, Brian,

### INTRODUCTION 1 AND REVIEW

INTRODUTION 1 AND REVIEW hapter SYS-ED/ OMPUTER EDUATION TEHNIQUES, IN. Programming: Advanced Objectives You will learn: Program structure. Program statements. Datatypes. Pointers. Arrays. Structures.

### EC312 Chapter 4: Arrays and Strings

Objectives: (a) Describe how an array is stored in memory. (b) Define a string, and describe how strings are stored. EC312 Chapter 4: Arrays and Strings (c) Describe the implications of reading or writing

### Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Chapter 2: Introduction to C++ 2.1 Parts of a C++ Program Copyright 2009 Pearson Education, Inc. Copyright 2009 Publishing Pearson as Pearson Education, Addison-Wesley Inc. Publishing as Pearson Addison-Wesley

### Lexical Considerations

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Fall 2005 Handout 6 Decaf Language Wednesday, September 7 The project for the course is to write a

### Cpt S 122 Data Structures. Introduction to C++ Part II

Cpt S 122 Data Structures Introduction to C++ Part II Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Objectives Defining class with a member function

### 2.8. Decision Making: Equality and Relational Operators

Page 1 of 6 [Page 56] 2.8. Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. This section introduces a simple version of Java's if statement

### ET156 Introduction to C Programming

ET156 Introduction to C Programming g Unit 22 C Language Elements, Input/output functions, ARITHMETIC EXPRESSIONS AND LIBRARY FUNCTIONS Instructor : Stan Kong Email : skong@itt tech.edutech.edu General

### Lecture 04 FUNCTIONS AND ARRAYS

Lecture 04 FUNCTIONS AND ARRAYS 1 Motivations Divide hug tasks to blocks: divide programs up into sets of cooperating functions. Define new functions with function calls and parameter passing. Use functions

### CSE101-Lec#17. Arrays. (Arrays and Functions) Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

Arrays CSE101-Lec#17 (Arrays and Functions) Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU Outline To declare an array To initialize an array To pass an array to a function Arrays Introduction

1 2 Introduction to C++ Programming 2 What s in a name? that which we call a rose By any other name would smell as sweet. William Shakespeare When faced with a decision, I always ask, What would be the

### LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

LESSON 1 FUNDAMENTALS OF C The purpose of this lesson is to explain the fundamental elements of the C programming language. C like other languages has all alphabet and rules for putting together words

### Lexical Considerations

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2010 Handout Decaf Language Tuesday, Feb 2 The project for the course is to write a compiler

About Codefrux While the current trends around the world are based on the internet, mobile and its applications, we try to make the most out of it. As for us, we are a well established IT professionals

### Chapter 2 Basic Elements of C++

C++ Programming: From Problem Analysis to Program Design, Fifth Edition 2-1 Chapter 2 Basic Elements of C++ At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion

### 9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Overview The C Programming Language (with material from Dr. Bin Ren, William & Mary Computer Science) Motivation Hello, world! Basic Data Types Variables Arithmetic Operators Relational Operators Assignments

### Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring 2009 Topic Notes: C and Unix Overview This course is about computer organization, but since most of our programming is

### The SPL Programming Language Reference Manual

The SPL Programming Language Reference Manual Leonidas Fegaras University of Texas at Arlington Arlington, TX 76019 fegaras@cse.uta.edu February 27, 2018 1 Introduction The SPL language is a Small Programming

### Multiple-Subscripted Arrays

Arrays in C can have multiple subscripts. A common use of multiple-subscripted arrays (also called multidimensional arrays) is to represent tables of values consisting of information arranged in rows and

### 2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Chapter 2: Introduction to C++ 2.1 Parts of a C++ Program Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Parts of a C++ Program Parts of a C++ Program // sample C++ program

### ONE DIMENSIONAL ARRAYS

LECTURE 14 ONE DIMENSIONAL ARRAYS Array : An array is a fixed sized sequenced collection of related data items of same data type. In its simplest form an array can be used to represent a list of numbers

### Full file at

Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

### Language Reference Manual

TAPE: A File Handling Language Language Reference Manual Tianhua Fang (tf2377) Alexander Sato (as4628) Priscilla Wang (pyw2102) Edwin Chan (cc3919) Programming Languages and Translators COMSW 4115 Fall

### The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

The C Programming Language (with material from Dr. Bin Ren, William & Mary Computer Science) 1 Overview Motivation Hello, world! Basic Data Types Variables Arithmetic Operators Relational Operators Assignments

### C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

C++ Basics Lecture 2 COP 3014 Spring 2018 January 8, 2018 Structure of a C++ Program Sequence of statements, typically grouped into functions. function: a subprogram. a section of a program performing

### Chapter 3: Arrays and More C Functionality

Chapter 3: Arrays and More C Functionality Objectives: (a) Describe how an array is stored in memory. (b) Define a string, and describe how strings are stored. (c) Describe the implications of reading

### In this chapter you ll learn:

Much that I bound, I could not free; Much that I freed returned to me. Lee Wilson Dodd Will you walk a little faster? said a whiting to a snail, There s a porpoise close behind us, and he s treading on

### The C++ Language. Arizona State University 1

The C++ Language CSE100 Principles of Programming with C++ (based off Chapter 2 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University

### Fundamentals of Programming. Lecture 11: C Characters and Strings

1 Fundamentals of Programming Lecture 11: C Characters and Strings Instructor: Fatemeh Zamani f_zamani@ce.sharif.edu Sharif University of Technology Computer Engineering Department The lectures of this

### CS102: Variables and Expressions

CS102: Variables and Expressions The topic of variables is one of the most important in C or any other high-level programming language. We will start with a simple example: int x; printf("the value of

### 7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

KEY CONCEPTS COMP 10 EXPLORING COMPUTER SCIENCE Lecture 2 Variables, Types, and Programs Problem Definition of task to be performed (by a computer) Algorithm A particular sequence of steps that will solve

### CSE 374 Programming Concepts & Tools

CSE 374 Programming Concepts & Tools Hal Perkins Fall 2017 Lecture 8 C: Miscellanea Control, Declarations, Preprocessor, printf/scanf 1 The story so far The low-level execution model of a process (one

### UNIT-2 Introduction to C++

UNIT-2 Introduction to C++ C++ CHARACTER SET Character set is asset of valid characters that a language can recognize. A character can represents any letter, digit, or any other sign. Following are some

### C Functions. 5.2 Program Modules in C

1 5 C Functions 5.2 Program Modules in C 2 Functions Modules in C Programs combine user-defined functions with library functions - C standard library has a wide variety of functions Function calls Invoking

### Should you know scanf and printf?

C-LANGUAGE INPUT & OUTPUT C-Language Output with printf Input with scanf and gets_s and Defensive Programming Copyright 2016 Dan McElroy Should you know scanf and printf? scanf is only useful in the C-language,

### Basic Elements of C. Staff Incharge: S.Sasirekha

Basic Elements of C Staff Incharge: S.Sasirekha Basic Elements of C Character Set Identifiers & Keywords Constants Variables Data Types Declaration Expressions & Statements C Character Set Letters Uppercase

### [0569] p 0318 garbage

A Pointer is a variable which contains the address of another variable. Declaration syntax: Pointer_type *pointer_name; This declaration will create a pointer of the pointer_name which will point to the

### Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview

Computer Science 322 Operating Systems Mount Holyoke College Spring 2010 Topic Notes: C and Unix Overview This course is about operating systems, but since most of our upcoming programming is in C on a

### Objectives. In this chapter, you will:

Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates arithmetic expressions Learn about

### ANSI C Programming Simple Programs

ANSI C Programming Simple Programs /* This program computes the distance between two points */ #include #include #include main() { /* Declare and initialize variables */ double