C Programming Lecture 14 Arrays. What is an Array? b An array is a sequence of data items that are: all of the same typeall of the same type –a sequence.

Slides:



Advertisements
Similar presentations
UNIT IV.
Advertisements

Introduction to C Programming
Kernighan/Ritchie: Kelley/Pohl:
Chapter 10.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
Enumerated Types 4 Besides the built-in types, ANSI C allows the definition of user-defined enumerated types –To define a user-define type, you must give.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
5 5 Arrays. OBJECTIVES In this lecture we will learn:  Case switch  To use the array data structure to represent a set of related data items.  To declare.
1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
1 CS 201 Passing Function as Parameter & Array Debzani Deb.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
 2006 Pearson Education, Inc. All rights reserved Arrays.
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 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
1 Chapter 8 Functions, Pointers, and Storage Classes  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Arrays One-Dimensional initialize & display Arrays as Arguments Part I.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
 2006 Pearson Education, Inc. All rights reserved Arrays.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Chapter 8 Arrays and Strings
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Chapter 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
Lecture 15: Projects Using Similar Data. What is an Array? An array is a data structure consisting of related data items of the same type. Stored in a.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
1. 1. Introduction to Array 2. Arrays of Data 3. Array Declaration 4. Array Initialization 5. Operations on Array 6. Multidimensional Arrays 7. Index.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Computer Programming for Engineers
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
Objectives You should be able to describe: One-Dimensional Arrays
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
User-Written Functions
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
7 Arrays.
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Lecture 18 Arrays and Pointer Arithmetic
7 Arrays.
Presentation transcript:

C Programming Lecture 14 Arrays

What is an Array? b An array is a sequence of data items that are: all of the same typeall of the same type –a sequence of integers, or a sequence of characters, or a sequence of floats, etc. indexibleindexible –you can indicate or select the first element or second element or... stored contiguously in memorystored contiguously in memory –each element of the sequence is stored in memory immediately after the previous element

One-Dimensional Arrays b Declaration of a one-dimensional array. int grade[3];int grade[3]; –Provides space in memory to store three grades (as in grades on three of your quizzes) b Example of Assignments grade[0] = 95; /* 1st element index is 0 */ grade[1] = 88; /* 2nd element index is 1 */ grade[2] = 97; /* 3rd element index is 2 */

Accessing Array Elements b The following code segment will print the three grades stored in the array on the previous slide (assume i has been declared as an int variable): for (i = 0; i < 3; ++i) printf(“Grade %d is %d\n”, i, grade[i]); printf(“Grade %d is %d\n”, i, grade[i]); b Output Grade 0 is 95 Grade 1 is 88 Grade 2 is 97

Arrays and Pointers b An array name is also the name of a pointer constant to the base address (beginning) of the array in memory. b Using “made up” addresses (as we have before in discussing pointers): grade < addresses in memory < index of array element values stored in each element The address stored in grade would be 1000

Using a Symbolic Constant as the Size of the Array b It is considered good programming practice to define the size of an array as a symbolic constant: In the next example, if the size of a class changes then the symbolic constant at the top of the program is easily changed.In the next example, if the size of a class changes then the symbolic constant at the top of the program is easily changed. The program would then be recompiled so it can be used with the larger array of grades.The program would then be recompiled so it can be used with the larger array of grades.

Example of Entering Values into an Array, then Displaying the Values #include #define N 20 /* Number of students in class */ int main(void) { int grade[N]; /* declare array with N elements */ int i; /* Enter the grades for a quiz */ for (i = 0; i < N; ++i) { printf(“Enter quiz grade %d: “, i + 1); scanf(“%d”, &grade[i]); } /* display the grades that were entered */ printf(“Here are the grades that were entered\n”); for (i = 0; i < N; ++i) printf(“Grade %d is %d”, i + 1, grade[i]); } Note that the user is shown grades starting with grade 1, but what the user thinks of as grade 1 is stored in grade[0] in the array.

Initialization of Arrays As with simple variables, arrays can be initialized within a declaration. An array initializer is a sequence of initializing values written as a brace- enclosed, comma-separated list.An array initializer is a sequence of initializing values written as a brace- enclosed, comma-separated list. float x[4] = {-1.1, 0.2, 33.0, 4.4}; float x[4] = {-1.1, 0.2, 33.0, 4.4}; x[0] is initialized to -1.1 x[1] is initialized to 0.2 etc.

More on Initialization of Arrays b If a list of initializers is shorter than the number of array elements, the remaining elements are initialized to zero. b If an external or static array is not initialized, then the system automatically initializes all elements to zero. b Uninitialized automatic and constant arrays start with garbage values -- whatever happens to be in memory when the array is allocated.

Declaring and Initializing an Array Without a Size b If an array is declared without a size and is initialized to a series of values, it is implicitly given the size of the number of initializers. int a[] = {3, 4, 5, 6}; int a[] = {3, 4, 5, 6}; is equivalent to is equivalent to int a[4] = {3, 4, 5, 6}; int a[4] = {3, 4, 5, 6};

Range of Values Allowed for a Subscript b Assume a declaration: int i, a[size]; int i, a[size]; b To access an element of the array, we can write a[expr], where expr is an integral expression. b expr is called a subscript and the value of the subscript must lie in the range 0 to size - 1.

“Subscript Out of Bounds” b If a subscript from the previous example lies outside the range 0 to size - 1 then: A run-time error occurs.A run-time error occurs. The system will stop executing your program and display a message such as “subscript out of bounds”.The system will stop executing your program and display a message such as “subscript out of bounds”. Sometimes no error message is displayed, but your program will begin to produce unexpected results.Sometimes no error message is displayed, but your program will begin to produce unexpected results.

Relationship Between Arrays and Pointers b An array name represents the address of the beginning of the array in memory. When an array is declared, the compiler must allocate a base address and a sufficient amount of storage (RAM) to contain all the elements of the array.When an array is declared, the compiler must allocate a base address and a sufficient amount of storage (RAM) to contain all the elements of the array. The base address of the array is the initial location in memory where the array is stored.The base address of the array is the initial location in memory where the array is stored. –It is the address of the first element (index 0) of the array.

Passing Arrays to Functions b In a function definition, a formal parameter that is declared as an array is actually a pointer. When an array is passed, its base address is passed call-by-value.When an array is passed, its base address is passed call-by-value. The array elements themselves are not copied.The array elements themselves are not copied. As a notational convenience, the compiler allows array bracket notation to be used in declaring pointers as parameters.As a notational convenience, the compiler allows array bracket notation to be used in declaring pointers as parameters.

Example of Passing an Array to a Function int sum(int a[], int n) /* n is size of the array */ { int i, s = 0; for (i = 0; i < n; ++i) s += a[i]; return s; } alternative function definition: int sum(int *a, int n) /* n is size of the array */ { int i, s = 0; for (i = 0; i < n; ++i) s += a[i]; return s; } Notice -- no size here. That was provided in the original declaration of the array.

Equivalence of int a[]; and int *a; b As part of the header of a function definition the declaration int a[]; is equivalent to int *a; int a[]; is equivalent to int *a; b Within the body of a function, these are not equivalent. The first will create a constant pointer (and no storage for an array).The first will create a constant pointer (and no storage for an array). The second will create a pointer variable.The second will create a pointer variable.