Computer Science 210 Computer Organization Arrays.

Slides:



Advertisements
Similar presentations
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Advertisements

Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
Kernighan/Ritchie: Kelley/Pohl:
Chapter 10 Introduction to Arrays
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
© The McGraw-Hill Companies, 2006 Chapter 5 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.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Computer Science 210 Computer Organization Pointers.
Computer Science 210 Computer Organization Strings in C.
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.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
Computer Science 210 Computer Organization Introduction to C.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
1 Chapter 4: Arrays and strings Taufik Djatna
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Problem: A company employs a group of fifty salespersons (with reference numbers ) who are paid commission if their individual sales exceeds two-thirds.
PASSING VALUE TO A FUNCTION # CALL BY VALUECALL BY VALUE # CALL BY REFERENCECALL BY REFERENCE STORAGE CLASS # AUTOAUTO # EXTERNALEXTERNAL # STATICSTATIC.
ARRAY Prepared by MMD, Edited by MSY1.  Introduction to arrays  Declaring arrays  Initializing arrays  Examples using arrays  Relationship with pointers.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Chapter 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Chapter 8: Arrays Introduction to arrays Declaring arrays Initializing arrays Examples using arrays Relationship with pointers Array passing to a function.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
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.
1 Arrays and Methods Java always passes arguments by value – that is a copy of the value is made in the called method and this is modified in the method.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CSE 251 Dr. Charles B. Owen Programming in C1 Intro to Arrays Storing List of Data.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
C Programming Tutorial – Part I
Module 2 Arrays and strings – example programs.
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Chapter 8 Arrays Objectives
Lecture 18 Arrays and Pointer Arithmetic
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Topics discussed in this section:
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Chapter 8 Arrays Objectives
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Suggested self-checks: Section 7.11 #1-11
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Presentation transcript:

Computer Science 210 Computer Organization Arrays

The Array Structure A linear sequence of memory cells, each accessible by an an integer index, ranging from 0 to the number of cells minus 1 Like Python lists and Java array lists, C arrays support random (constant-time) access to the cells

int integers[10]; // Cells for 10 ints char letters[5]; // Cells for 5 characters float matrix[3][2]; // A 3 by 2 matrix of floats int max = 100; // The number of cells in the next array double doubles[max]; // Cells for max doubles Declaring Array Variables The syntax [ ] declares an array variable and reserves memory for a definite number of cells (use more [] for more dimensions), each of which can store an element of the given type (arrays are statically typed) Each cell initially contains garbage!

Initializing and Processing int i, max = 10, array[max]; // Declare variables for (i = 0; i < max; i++) // Store 1..max in the array array[i] = i + 1; for (i = 0; i < max; i++) // Print the contents printf("%d\n", array[i]); Here we initialize all of the array’s cells and visit them all The loop is pretty standard

Initializing Arrays that Are Not Full int number, length = 0, max = 10, array[max]; printf("Enter a number or 0 to stop: "); scanf("%d", &number); while (number){ array[length] = number; length++; if (length == max){ printf("Maximum number of numbers entered\n"); break; } printf("Enter a number or 0 to stop: "); scanf("%d", &number); } The number of data values is tracked with a separate variable This length variable is also used in processing the array later

Processing Arrays that Are Not Full int number, length = 0, max = 10, array[max]; // Get the array’s data and length here // Print all of the currently available values int i; for (i = 0; i < length; i++) printf("%d\n", array[i]); The number of data values is tracked with a separate variable This length variable is also used in processing the array later

Arrays and Functions #include #include "numbers.h" // Use a library of array processing functions int main(){ int max = 100; int numbers[max]; int length = getNumbers(numbers, max); if (length > 0) printf("The average is %f\n", sum(numbers, length) / (double) length); else printf("No numbers were entered\n"); } An array can be passed as an argument to a function The function can access or replace values in the array cells

Example: Find the Minimum // Returns the index of the minimum value in the array int minIndex(int array[], int length){ int probe, minPos = 0; for (probe = 1; probe < length; probe++) if (array[probe] < array[minPos]) minPos = probe; return minPos; } The element type of the array parameter must be specified, but its physical size need not be Therefore, this function can process an integer array of any physical size The logical size ( length ) is passed too, because the array might not be full

How Parameters Are Passed to Functions Parameters of basic types ( char, int, float, and double ) are passed by value (a copy of the value of the argument is placed in temporary storage on the runtime stack) A copy of an array’s base address is also placed in temporary storage, so its cells can still be accessed or modified