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.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Chapter 7: Arrays In this chapter, you will learn about
One Dimensional Arrays
Programming and Data Structure
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Week 8 Arrays Part 2 String & Pointer
Kernighan/Ritchie: Kelley/Pohl:
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
 2003 Prentice Hall, Inc. All rights reserved. 7.1 Introduction Arrays –Data structures which reference one or more value –All items must have same data.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Lesson 6 - Pointers Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators Calling Functions by Reference Using the const.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer Variable Declarations and Initialization 7.3Pointer.
 2003 Prentice Hall, Inc. All rights reserved Introduction Arrays –Structures of related data items –Static entity (same size throughout program)
© 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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring Arrays 6.4Examples Using Arrays 6.5Passing.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
 2006 Pearson Education, Inc. All rights reserved Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Arrays Dale Roberts, Lecturer
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
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 CS105 C++ Lecture 2 Arrays. Parameter passing  2 types  Pass-by-value  Pass-by-reference 2.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
 2006 Pearson Education, Inc. All rights reserved 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.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
Chapter 8: Arrays.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
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 Functions Pepper. Objectives Create functions Function prototypes Parameters – Pass by value or reference – Sending a reference Return values Math functions.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify.
C Lecture Notes 1 Arrays Lecture 6. C Lecture Notes 2 6.1Introduction Arrays –Structures of related data items –Static entity – same size throughout program.
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:
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 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.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
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.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
© 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.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Objectives You should be able to describe: One-Dimensional Arrays
C++ Programming Lecture 18 Pointers – Part II The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
CNG 140 C Programming (Lecture set 8)
Arrays Kingdom of Saudi Arabia
Arrays Outline Introduction Arrays Declaring Arrays
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Data Structures (CS212D) Week # 2: Arrays.
Arrays Week 2.
Arrays.
Presentation transcript:

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 size_t – starting at (myarray[2])

Defining Arrays Tell the compiler the size and type and give it a name: – int b[10] makes 10 boxes, from 0 to 9 Initialize later or upon creation: – int b[5] = {33,22,10,11,5}; – int a[] = {1,2,3}; - compiler figures out 3 elements You can create a variable length array by using variable in place of a number as the length.

Array Structure Array variable name holds the starting address Array can tell where its own self ends You can tell how many if you know the size of the variable type

Array Access Sample program to initialize and print all Changes to make: – Initialize too few b[10] – Initialize too many b[2] – See no crash #include int main (void) { int b[5] = { 33, 22, 10,15,5 }; size_t c; for (c = 0; c < 5; c++) { printf ("value at c[%zd] = %d\n", c, b[c]); }

© by Pearson Education, Inc. All Rights Reserved. Sum an Array

Bubble Sort Work from left to right moving one element over until it is lower than one next to it. Repeat for as many passes as elements Once sorted, can do binary search

Bubble Sort © by Pearson Education, Inc. All Rights Reserved.

Bubble Sort © by Pearson Education, Inc. All Rights Reserved.

Bubble Sort

Tricky – use Array item as subscript Count frequency of survey responses © by Pearson Education, Inc. All Rights Reserved.

Survey cont. © by Pearson Education, Inc. All Rights Reserved.

Static Array created inside a function Static keyword on array – values will persist if you return to the function Static int arrays initialize to 0 upon creation #include void arrayInit (void); int main (void) { puts ("first array use"); arrayInit (); puts ("second array use"); arrayInit (); } void arrayInit (void) { static int array1[3]; //int array1[3] = {0,0,0}; size_t i; for (i = 0; i < 3; i++) { printf ("array1[ %zd ] = %d\n", i, array1[i] += 5); }

Get Array Size It knows where it starts and where it ends. Ask array for its total size : – it tells you how many bytes – Divide the number of bytes by the array type sizeof(myarray) returns total bytes sizeof(int) returns size of one integer Total array elements is then: – sizeof(myarray) / sizeof(myarray[0])

Strings – Character Arrays Initialize as a string: – char string1[] = { 'f', 'i', 'r', 's', 't', '\0' }; – char string1[] = "first"; Access full string as array name – printf(“%s”,string1); Access portion with index – printf(“%s”,string1[2]); // will print ‘r’ The array name does hold an address, so no need for & operator on scanf – char string2[ 20 ]; // remember 1 for ‘\0’ – scanf( "%19s", string2 ); – Scanf will overwrite the bounds of the array – String2 knows it ends because of the null, but if write over null, array is confused.

Passing Arrays to Functions Passes Array by reference without & or * – Because array name holds the address – Send the size over as well because it cannot figure out the size Passes Array element (scalars) by value – B[1] holds an a variable value, not an address Function header: – void arrayfunc (int b[], size_t arrSize) Call to function: – arrayfunc(b,10); Const If no changes allowed to array elements – void arrayfunc (const int b[], size_t arrSize)

Sample Array pass to function © by Pearson Education, Inc. All Rights Reserved.

Multi-dimension Arrays Second subscript : b[4][3] Initialize: – int b[2][3] = {{1,2,3}, {4,5,6}}; Function header needs the second number of elements: – void arrayfunc (int b[][3], size_t arrSize)

Ex: Total multidimensional array Sum = 0 // loop through rows of grades for ( i = 0; i < pupils; ++i ) { // loop through columns of grades for ( j = 0; j < tests; ++j ) { sum = sum + grades[i][j]; } // end inner for } // end outer for

Comparison to Java FeatureCJava array declarations int a[5]int[] a = new int[N]; array size arrays don't know their own size, but you can call sizeof () to get their memory size and divide by the size of one element. Only works on local arrays. a.length

Summary Define an array single or multi subscript Access an array element Pass an array as a function – by reference Get an array size Special character array Static keyword for array