Introduction to C Programming CE00312-1 Lecture 9 Data Structures Arrays.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Introduction to C Programming CE
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.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
C++ for Engineers and Scientists Third Edition
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
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.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Arrays, Strings, and Pointers CSE 2451 Rong Shi. Arrays Store many values of the same type in adjacent memory locations Declaration [ ] Examples: – int.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays and Strings
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
ARRAY Prepared by MMD, Edited by MSY1.  Introduction to arrays  Declaring arrays  Initializing arrays  Examples using arrays  Relationship with pointers.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Chapter 8: Arrays Introduction to arrays Declaring arrays Initializing arrays Examples using arrays Relationship with pointers Array passing to a function.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
POINTERS.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Pointers *, &, array similarities, functions, sizeof.
1. 1. Introduction to Array 2. Arrays of Data 3. Array Declaration 4. Array Initialization 5. Operations on Array 6. Multidimensional Arrays 7. Index.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
CCSA 221 Programming in C CHAPTER 7 WORKING WITH ARRAYS 1.
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.
CHAPTER 7: Arrays CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (Feb 2012)
Prepared by MMD, Edited by MSY1 CHAPTER 4 ARRAY. Prepared by MMD, Edited by MSY2 Arrays  Introduction to arrays  Declaring arrays  Initializing arrays.
Array Declarations Arrays contain a fixed number of variables of identical type Array declaration and allocation are separate operations Declaration examples:
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Arrays. C++ Style Data Structures: Arrays(1) An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation.
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.
Windows Programming Lecture 03. Pointers and Arrays.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Objectives You should be able to describe: One-Dimensional Arrays
Arrays in C. What is Array? The variables we have used so far can store a single value. Array is a new type of variable capable of storing many values.
Computer Programming BCT 1113
EKT150 : Computer Programming
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
7 Arrays.
Presentation transcript:

Introduction to C Programming CE Lecture 9 Data Structures Arrays

Arrays in C have a very similar look and feel to arrays in other languages such as Java, although in terms of implementation they are very different. An array is a structured data type An array is: A collection of data objects (all of the same type) referred to collectively by a specific name An array is of a fixed size

The objects within the collection can be accessed independently by an index since they are consecutively stored in memory under a single name. e.g. int myarray[60]; myarray[10]=6; char code[4]; code[3]=’t’;

Array Subscripts i.e. An item can be accessed by its position number (subscript) within the array. The array name is a constant pointer – it is the name given to the address of the first item in the array (more about pointers and how they are used with arrays in lectures 6 and 7).

Order of Arrays Single-Dimensional a.k.a Vector Two-Dimensional a.k.a. Matrix or Table marks[0][2] int smarks[3][6] marks[2] int marks[6]

Multi-Dimensional Array 2 or more Dimensions 2D case referred to as matrix In theory limitless dimensions Hard to visualise beyond 3-dimensions

Single-dimensional Arrays or Vector A one dimension array (i.e. an array only requiring one subscript to specify any element) is declared in C as follows: int example[10]; This declares and allocates the memory space for an array called example, to contain 10 elements. Array subscripts always start at zero and so the example array consists of the elements example[0] to example[9] inclusive. e.g. example[0]=12; sum=example[0]+example[3];

Subscript Range Checks Consider the following code fragment: int a[10], i; scanf(“%d”, &i); printf(“%d\n”, a[i]); impossible for the compiler to safety check the array subscript NO range checking to ensure that any given subscripts are within the declared bounds of the array. Consequently, the following code will compile and run without even a warning on most systems: int b[10]; b[13]=1234; Result of the above code. Address=base+(subscript*element_size) This formula is used irrespective of the subscript supplied.

Initialising Arrays usually done at the start of a program or function Can be done with a set of explicit statements e.g. example[0]=0; example[1]=0; etc.. Simpler notation is available e.g. int x[4]={1, 2, 3, 4};

Array Size What happens if the initialising list is smaller than the size of the array? e.g. int a[3]={1, 0, 0}; int a[3]={1}; are equivalent statements What happens if the initialising list is larger than the size of the array? Error A useful extra rule in C allows you to omit the array size specifier when declaring an array with an initialising list. e.g. int d[]={1, 2, 3, 4, 5, 6}; d[0] – d[5] double price[]={9.5, 10.2, 11.6};

Character Strings char codes[]={‘a’, ‘b’, ‘c’}; char codes=’abc’; == char codes[]={‘a’, ‘b’, ‘c’, ‘\0’}; The codes array in this example actually has 4 elements: where \0 i.e. null character is added as a String terminator (more about Strings and String terminators in lectures 6 and 7). ‘a’‘b’‘c’‘\0’

Example 1 Passing an array as a parameter to a function. #include char code[4]; /*entervalues function If the array size were omitted in the array parameter declaration, it would have to be passed in as an extra function parameter */ entervalues(char myarray[4]) { int i; char dummy; for(i=0;i<4;i++) { printf(“Enter the grade: “); scanf(“%c%c”, &myarray[i], &dummy); } int main(void) { entervalues(code); /* The address of the first location of the array is passed as an array parameter to the function entervalues */ return 0; }

Manipulating Arrays In functions that manipulate arrays,size information is not always included (more on this in lectures 6 and 7). Consider a function which totals all of the numbers in an array of type int and returns the result Second parameter will have to be passed to the function to say how big the array is: int total(int array[], int arraySize); Consider the following usage of the total() function: int a[5], tot1, tot2, tot3; tot1=total(a, 5); tot2=total(a, 2); tot3=total(a, 200); Advantage: This makes total(), and indeed the C programming language, more versatile Disadvantage: As it is possible to lie about the array size, it is also possible to make a mistake i.e. C is more error prone.

Multi-dimensional Arrays Arrays with more than one dimension are also available in C. Individual elements in a multi-dimensional array are accessed using as many subscripts as there are dimensions i.e. 2 for a 2- dimensional array, n for an n-dimensional array. e.g. int twoDim[3][4]; int twoDim[3][4]= { (1, 2, 3, 4), ( 5, 6, 7, 8), (9, 10, 11, 12) }; int twoDim[3][4]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; int twoDim[][4]= { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };

Example 2 #include void display(int nums[3][4]) /* display function; prints the contents of the array */ { int row, col; for (row=0; row<3;row++) { for (col=0;col<4;col++) printf(“%4d\n”, nums[row][col]); printf(“\n”); } int main(void) { int val[3][4] = { 8, 6, 4, 5, 3, 15, 5, 7, 12, 5, 6, 7}; display(val) return 0; }

Example 3 Problem: Given the following function prototype, write a function which swaps two elements in an array of type int; void swap(int array[], int, int, int);

Code: #include int swap(int array[], int, int, int); /* prototype */ void main(void) { int arr[5]={1, 2, 3, 4, 5}, element, result, el1, el2; printf(“Which two elements would you like to swap?\n”); scanf(“%d%d”, &el1, &el2); result=swap(arr, 5, el1, el2); if (result==0) for(element=0;element<5;element++) printf(“%d “, arr[element]); else printf(“Error!”); } /* swap function declaration */ int swap(int arr[], int size, int x, int y) { int temp; if (x>size||y>size||x<1||y<1) return 1; else { temp=arr[x-1]; arr[x-1]=arr[y-1]; arr[y-1]=temp; return 0; }