Arrays in C.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

One Dimensional Arrays
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
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.
Topic 9A – Arrays as Function Arguments. CISC105 – Topic 9A Arrays as Function Arguments There are two ways to use arrays as function arguments: Use an.
1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
CS 61C L03 C Arrays (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #3: C Pointers & Arrays
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.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Introduction to C Programming CE Lecture 9 Data Structures Arrays.
Arrays, Strings, and Pointers CSE 2451 Rong Shi. Arrays Store many values of the same type in adjacent memory locations Declaration [ ] Examples: – int.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Systems Programming Concepts
C Static Arrays Pepper. What is an array? Memory locations – same type – next to each other (contiguous) – Same name – Indexed by position number of type.
Array.
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
Computer programming Lecture 5. Lecture 5: Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character arrays.
ARRAY Prepared by MMD, Edited by MSY1.  Introduction to arrays  Declaring arrays  Initializing arrays  Examples using arrays  Relationship with pointers.
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
C Arrays Systems Programming. Systems Programming: Arrays 22 ArraysArrays  Arrays  Defining and Initializing Arrays  Array Example  Subscript Out-of-Range.
Chapter 8: Arrays Introduction to arrays Declaring arrays Initializing arrays Examples using arrays Relationship with pointers Array passing to a function.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
Lecturer: Omid Jafarinezhad Sharif University of Technology Department of Computer Engineering 1 Fundamental of Programming (C) Lecture 6 Array and String.
FUNCTION Dong-Chul Kim BioMeCIS UTA 12/7/
Review of Lectures 12, 13, 14 -Functions -Arrays -Strings.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
1. 1. Introduction to Array 2. Arrays of Data 3. Array Declaration 4. Array Initialization 5. Operations on Array 6. Multidimensional Arrays 7. Index.
Computer programming Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
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.
Prepared by MMD, Edited by MSY1 CHAPTER 4 ARRAY. Prepared by MMD, Edited by MSY2 Arrays  Introduction to arrays  Declaring arrays  Initializing arrays.
Arrays, Part 2 We have already learned how to work with arrays using subscript notation. Example: float myData[] = {3.5, 4.0, 9.34}; myData[0] += 2; printf("myData[0]
Pointers and Arrays An array's name is a constant whose value is the address of the array's first element. For this reason, the value of an array's name.
Arrays.
Computer Programming for Engineers
Advanced Pointer Topics. Pointers to Pointers u A pointer variable is a variable that takes some memory address as its value. Therefore, you can have.
CSE 251 Dr. Charles B. Owen Programming in C1 Intro to Arrays Storing List of Data.
Multi-dimensional Arrays and other Array Oddities Rudra Dutta CSC Spring 2007, Section 001.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
Lecture 7: Arrays BJ Furman 06OCT2012. The Plan for Today Announcements Review of variables and memory Arrays  What is an array?  How do you declare.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
Pointers. Pointer Fundamentals  When a variable is defined the compiler (linker/loader actually) allocates a real memory address for the variable. –int.
1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays.
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.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
CS 2130 Lecture 6 Pointers and Arrays. Pointers are like jumps, leading wildly from one part of the data structure to another. Their introduction into.
Windows Programming Lecture 03. Pointers and Arrays.
Pointers and Arrays Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
1-d Arrays.
Functions Dr. Sajib Datta
Array 9/8/2018.
Arrays in C.
Pointers and Arrays S.Bhuvaneshwari Assistant Professor/CSE
C Arrays Systems Programming.
Lecture 10 Arrays.
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
Outline Defining and using Pointers Operations on pointers
To refer to an element, specify
Lecture 14: Problems with Lots of Similar Data
Presentation transcript:

Arrays in C

Single Dimensional Arrays Generic declaration: typename variablename[size] typename is any type variablename is any legal variable name size is a number the compiler can figure out For example int a[10]; Defines an array of ints with subscripts ranging from 0 to 9 There are 10*sizeof(int) bytes of memory reserved for this array. You can use a[0]=10; x=a[2]; a[3]=a[2]; etc. You can use scanf("%d",&a[3]); a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]

Using Constants to define Arrays It is useful to define arrays using constants: #define MONTHS 12 int array [MONTHS]; However, in ANSI C, you cannot int n; scanf(“%d”, &n); int array[n]; In GNU C, the variable length array is allowed. In ANSI C, the handling of variable length array is more complicated.

Bound Checking C, unlike many languages, does NOT check array bounds subscripts during: Compilation (some C compilers will check literals) Runtime (bounds are never checked) If you access off the ends of any array, it will calculate the address it expects the data to be at, and then attempts to use it anyways may get “something…” may get a memory exception (segmentation fault, bus error, core dump error) It is the programmer’s responsibility to ensure that their programs are correctly written and debugged! This does have some advantages but it does give you all the rope you need to hang yourself!

Initializing Arrays Initialization of arrays can be done by a comma separated list following its definition. For example: int array [4] = { 100, 200, 300, 400 }; This is equivalent to: int array [4]; array[0] = 100; array[1] = 200; array[2] = 300; array[3] = 400; You can also let the compiler figure out the array size for you: int array[] = { 100, 200, 300, 400};

Example #include <stdio.h> int main() { float expenses[12]={10.3, 9, 7.5, 4.3, 10.5, 7.5, 7.5, 8, 9.9, 10.2, 11.5, 7.8}; int count,month; float total; for (month=0, total=0.0; month < 12; month++) { total+=expenses[month]; } for (count=0; count < 12; count++) printf ("Month %d = %.2f K$\n", count+1, expenses[count]); printf("Total = %.2f K$, Average = %.2f K$\n", total, total/12); return 0;

Multidimensional Arrays Arrays in C can have virtually as many dimensions as you want. Definition is accomplished by adding additional subscripts when it is defined. For example: int a [4] [3] ; defines a two dimensional array a is an array of int[3]; Logically, four rows and three columns

Initializing Multidimensional Arrays The following initializes a[4][3]: int a[4] [3] = { {1, 2, 3} , { 4, 5, 6} , {7, 8, 9} , {10, 11, 12} }; Also can be done by: int a[4] [3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; is equivalent to a[0][0] = 1; a[0][1] = 2; a[0][2] = 3; a[1][0] = 4; ... a[3][2] = 12;

Example #include <stdio.h> #include <stdlib.h> int main () { int random1[8][8]; int a, b; for (a = 0; a < 8; a++) for (b = 0; b < 8; b++) random1[a][b] = rand()%2; { printf ("%c " , random1[a][b] ? 'x' : 'o'); printf("\n"); } return 0;

The Value of the Array name #include <stdio.h> int main(){ int i; int a[3] = { 1, 2, 3 }; printf( "a ? %d\n", a); printf( "a[0] ? %d\na[1] ? %d\na[2] ? %d\n", a[0], a[1], a[2]); printf( "&a[0] ? %d\n&a[1] ? %d\n&a[2] ? %d\n", &a[0], &a[1], &a[2]); printf( "\na[0] <- 4 \n"); a[0] = 4; printf( "&a[0] ? %d\n&a[1] ? %d\n&a[2] ? %d\n\n", &a[0], &a[1], &a[2]); for (i=0; i<3; i++) { printf( "a[%d] <- ",i); scanf( "%d", &a[i]); } printf( "a ? %d\n", a); printf( "a[0] ? %d\na[1] ? %d\na[2] ? %d\n", a[0], a[1], a[2]); printf( "&a[0] ? %d\n&a[1] ? %d\n&a[2] ? %d\n", &a[0], &a[1], &a[2]); When the array name is used alone, its value is the address of the array (a pointer to its address). &a has no meaning if used in this program.

Array as Function Parameters In this program, the array addresses (i.e., the values of the array names), are passed to the function inc_array(). This does not conflict with the rule that “parameters are passed by values”. void inc_array(int a[ ], int size) { int i; for(i=0;i<size;i++) a[i]++; } void inc_array(int a[ ],int size); main() { int test[3]={1,2,3}; int ary[4]={1,2,3,4}; int i; inc_array(test,3); for(i=0;i<3;i++) printf("%d\n",test[i]); inc_array(ary,4); for(i=0;i<4;i++) printf("%d\n",ary[i]); return 0; }

Example - Sorting void mysort(int a[ ],int size) { int i,j,x; for(i=0; i<size; i++) for(j=i; j>0; j--) if(a[ j ] < a[ j-1]) { /* Change the order of a[ j ] and a[ j-1] */ x=a[ j ];a[ j ]=a[ j-1]; a[j-1]=x; } int main() { int i; int tab[10] = {3,6,3,5,9,2,4,5,6,0}; for(i=0;i<10;i++) printf("%d ",tab[i]); printf("\n"); mysort(tab,10); return 0; }