Lecture 14: Problems with Lots of Similar Data

Slides:



Advertisements
Similar presentations
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
Advertisements

1 ICS103 Programming in C Lecture 3: Introduction to C (2)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
 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.
 2007 Pearson Education, Inc. All rights reserved. 1 C Arrays.
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.
Systems Programming Concepts
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
 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.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
C Arrays Systems Programming. Systems Programming: Arrays 22 ArraysArrays  Arrays  Defining and Initializing Arrays  Array Example  Subscript Out-of-Range.
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
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.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
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.
Arrays.
Arrays An array is an indexed data structure which is used to store data elements of the same data type. An array is an indexed data structure which is.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
1 Lecture 4: Part1 Arrays 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 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.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
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.
Chapter 6 Arrays in C++ 2nd Semester King Saud University
Computer Science 210 Computer Organization
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
Lecture 7: Repeating a Known Number of Times
EKT120 : Computer Programming
Arrays Declarations CSCI N305
ICS103 Programming in C Lecture 3: Introduction to C (2)
Lecture 7 – Arrays (1) PGT 106 : C PROGRAMMING.
Computer Science 210 Computer Organization
Arrays, For loop While loop Do while loop
C Arrays.
C Arrays Systems Programming.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Lecture 18: The Elegant, Abstract World of Computing
C Arrays.
7 Arrays.
Chapter 9 - Arrays Outline 6.1 Introduction 6.2 Arrays
CS-161 Computer Programming Lecture 14: Arrays I
EKT150 : Computer Programming
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Declaration, assignment & accessing
Lecture 18 Arrays and Pointer Arithmetic
Arrays Outline Introduction Arrays Declaring Arrays
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
EKT120: Computer Programming
6 C Arrays.
CISC181 Introduction to Computer Science Dr
7 Arrays.
To refer to an element, specify
Arrays Arrays A few types Structures of related data items
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

Lecture 14: Problems with Lots of Similar Data

What is an Array? An array is a data structure consisting of related data items of the same type. Stored in a group of memory locations Defining arrays int arrayName[ 100 ]; Examples int a[5]; float b[120], x[24]; … a[0] 5 memory 10 15 3 23 a[1] a[2] a[3] a[4] Name of the array Specifying the type of each element The number of the elements

What is an Array? Defining arrays (cont.) Using a symbolic constant for the size of an array. Making programs more scalable. #define SIZE 100 int a[ SIZE ]; float b[ SIZE ]; Using only uppercase letters for symbolic constant names. Not a variable, cannot assign it a value in an executable statement.

Referring to Array Elements Formally called a subscript or an index Format arrayName[ position number ] First element at position 0 The i-th element of array a is referred to as a[i-1] A subscript must be an integer or an integer expression. Ex. (k = 2, j = 1) a[k+j] += 12; The expression is evaluated to determine the subscript. Name of array (all elements of this array have the same name a) … a[0] 5 memory 10 15 3 23 a[1] a[2] a[3] a[4] subscript of the element within array a

Referring to Array Elements An array a of size of n has the following elements: a[0], a[1], a[2], …, a[n-1] Array elements are like normal variables printf(“%d”, a[0]*a[3]); x = (a[2]+a[3])/2; For a defined array without initialization, the values of the elements are undefined. Avoid to referring to an element outside the array bounds. … a[0] 5 memory 10 15 3 23 a[1] a[2] a[3] a[4] C has NO array bounds checking to prevent the computer from referring to an element that does not exist.

Initializing the Array Using a loop to initialize the array’s elements Initialized to a uniform value Initialized to different values - using scanf Initializing an array in a definition with an initializer list Defining an array followed by an equals sign and braces, { }, containing a comma-separated list of initializers. int n[5] = {1, 2, 3, 4, 5}; If not enough initializers, rightmost elements become 0. int n[5] = {1, 2} int n[5] = {0} --- all elements are 0. If too many initializers, a syntax error occurs If size omitted, # of initializers determine the size int n[ ] = {1, 2, 3, 4, 5, 6}; 6 initializers, therefore 6 element array.

Practice Question Q. What is the output of the following program? #include<stdio.h> int aFunc( int ); int x = 1; /* global variable */ int main( void ) { int y = 0; x = 10; y = aFunc( x ); printf(”%d %d\n", x, y); return 0; } int aFunc( int x) x *= 10; return x; 100 100 10 100 10 10 100 10 Solution: B

Practice Question Q. What is the output of the following code fragment? #define SIZE 5 int aray[SIZE] = {2, 3, 4}; printf(“%d\n”, aray[1] + aray[2] + aray[3]); 2 5 9 7 Solution: D

Practice Question Q. What is the value of aray[5]? 4 Not sure 5 #define SIZE 5 int aray[SIZE] = {2, 3, 4, 5}; Q. What is the value of aray[5]? 4 Not sure 5 Solution: C Q. What is the value of aray[1]? 2 Not sure 3 Solution: D

Practice Question Q. To initialize an array to be {1, 2, 3, 4, 0}, which of the following is WRONG? int ary[ ] = {1, 2, 3, 4, 0}; int ary[5] = {0}; int k; for (k = 0; k < 4; k++) ary[k] = k+1; int ary[5]; ary = {1, 2, 3, 4, 0}; int ary[5] = {1, 2, 3, 4}; Solution: C