Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: 10 15 4 25 17 3 12 36 48 32 9 21 Desired output: 3 4 9 10 12 15.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

C Language.
Programming and Data Structure
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
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.
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
O-1 University of Washington Computer Programming I Lecture 14: Arrays © 2000 UW CSE.
Kernighan/Ritchie: Kelley/Pohl:
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
Enumerated Types 4 Besides the built-in types, ANSI C allows the definition of user-defined enumerated types –To define a user-define type, you must give.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
O-1 University of Washington Computer Programming I Lecture 14: Arrays © 2000 UW CSE.
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.
©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.
Loops – While, Do, For Repetition Statements Introduction to Arrays
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
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.
Arrays.
CS0007: Introduction to Computer Programming Introduction to Arrays.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays and Strings
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
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:
CS 161 Introduction to Programming and Problem Solving Chapter 19 Single-Dimensional Arrays Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied.
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
Iterations Very Useful: Ability to repeat a block of code Example:
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
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.
Computer Programming for Engineers
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
© 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.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
Arrays. Arrays are objects that help us organize large amounts of information.
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.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Data Storage So far variables have been able to store only one value at a time. What do you do if you have many similar values that all need to be stored?
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
Revision Lecture
Arrays, Part 1 of 2 Topics Definition of a Data Structure
The while Looping Structure
EKT150 : Computer Programming
The while Looping Structure
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
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Lecture 14: Problems with Lots of Similar Data
The while Looping Structure
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
The while Looping Structure
Presentation transcript:

Structuring Data: Arrays ANSI-C

Representing multiple homogenous data Problem: Input: Desired output: How can this be done? If we had lots of variables we could store each input in a variable. But think about what the program would be like. Is there a better way?

Representing multiple homogenous data double grade1, grade2, grade3, grade4, grade5, grade6, grade7, total ; /* initialize grades somehow...*/ total = grade1 + grade2 + grade3 + grade4 + grade5 + grade6 + grade7 ; printf( “average = %f \n”, total / 7.0) ; What if we had 500 grades to add up instead of 7?

Representing multiple homogenous data Problem: Input: Two desired outputs: 1. average of those numbers. int number; int sum = 0; int count = 0; double average = 0.0; printf(“Enter a number of ^Z return to end>>”); while ((scanf (“%d”, &number)!= EOF){ sum = sum + number; count = count + 1; printf(“Enter a number of ^Z return to end>>”); } if (count > 0) average = (float) sum/count;

Representing multiple homogenous data Problem: Input: Two desired outputs: 2. Number of elements read greater than the average found. How to do that? Would need to declare as many variables as input data to store all values. After computing average, compare with each of those variables

Representing multiple homogenous data Problem: –Need to declare multiple variables of the same type –These variables will represent similar data –They all will undergo same computations Solution: Array –Built-in type –Allows for the declaration of multiple variables all of the same homogenous type –All variables will have same prefix for the name.

Array attributes An array is said to provide the simplest representation of a list of values. An array has a type: representing the type of all the variables it will contain. An array has a size: the number of entries in the array. Each entry will represent one variable. Each variable in the array will be uniquely identified by its index in the array; this index is indicated by an integer value, variable or expression. This index is also refered as the subscript. The bounds of the array index are between 0 and size of array - 1

Syntax: Array declaration For a declaration of an array you must give: –Name of the array –Type of the array –Size of the array Having given the size the subscript to an array will range from 0..size-1 Examples: int table[10]; double column[15] Type name size

Syntax: Array declaration Examples: int table[10]; double column[15] table contains 10 variables column contains 15 named table[0], table[1], variables named: table[2], table[3], column[0], … table[8], table[9] column[1], column[2], … column[13], column[14] Type name size

Array names are identifiers Therefore: They follow the all usual rules for C –identifiers (start with a letter, etc.) –They must be declared before they are used If you see table[y] in program, then you know that – table should be the name of an array – y must be an integer expression with an integer value y >= 0 and y <= size - 1

Index Rule Rule: An array index must evaluate to an int between 0 and n-1, where n is the number of elements in the array. No exceptions! Example: given the declaration int grades[7]; grades[i+3+k] /* OK as long as 0<=i+3+k <= 6 */ The index may be very simple grade[0] or incredibly complex grade[(int) (3.1 * fabs(sin (2.0*PI*sqrt(29.067))))]

Syntax: Array initialization To initialize an array you must give each entry in the array an specific value. Explicit initialization: int table[5] = { 1, 2, 3, 4, 5}; int table[5] = { 0, 0, 0, 0, 0}; Restriction: can only use this form of initialization at declaration time. Syntax relief: int table[] = { 1, 2, 3, 4, 5}; int table[] = { 0, 0, 0, 0, 0};

Array Initializers int w[4] = {1, 2, 30, -4}; /*w has size 4, all 4 are initialized */ char vowels[6] = {‘a’, ‘e’, ‘i’, ‘o’, ‘u’}; /*vowels has size 6, only 5 initializers */ /* vowels[5] is uninitialized */ Cannot use this notation in assignment statement: w = {1, 2, 30, -4}; /*SYNTAX ERROR */

Incomplete Array Size double x[ ] = {1.0, 3.0, -15.0, 7.0, 9.0}; /*x has size 5, all 5 are initialized */ But: double x[ ]; /* ILLEGAL */ But double x[5]; /* Legal */

Loops and arrays In general to manipulate an array you will use a while loop. Let’s find the smallest of 5 values placed in an array of size 5. int table[5] = { 5, 8, 11, 3, 9}; int min = table[0]; if (min > table[1]) min = table[1]; if (min > table[2]) min = table[2]; if (min > table[3]) min = table[3]; if (min > table[4]) min = table[4]; We have the following pattern: if (min > table[i]) min = table[i]; for i = 1, 2, 3, 4 So we can use a while loop where the body of the loop contains the above code, and the loop variable varies from 1 to 4.

int table[5] = { 5, 8, 11, 3, 9}; int min = table[0]; int i = 0; while (i < 5){ if (min > table[i]) min = table[i]; i = i + 1; }

Loops and arrays In general to manipulate an array you will use a while loop. A variable for the loop will be the subscript variable. Example: int table[10]; int i = 0; while (i < 10){ table[i] = i*i; i = i + 1; } for(i = 0; i < 10; i++) table[i] = i*i;

Bug: array bounds check int table[10]; int i = 0; while( i <= 10){ table[i] = i*i; i = i + 1; } It will initialize table[0], table[1], table[2]… table[8], table[9], table[10] Bug: variable table[10] does not exist C will not detect this as an error!!!!! C will write something somewhere in memory: in the variable right next to variable[9]. C does not detect array out of bounds errors.

Reading user’s data in an array Simplest form: the user provides as many data values as entries in the array. Example: int table[10]; int i = 0; while( i < 10){ printf(“Enter value for entry %d”, i); scanf(“%d”, &table[i]); i = i + 1; }

Reading user’s data in an array Common form: user does not provide as many values as there are entries in array: –Provides less entries –Or tries to provide more entries. So we need to have a count on the number of entries that were actually entered by user; that count variable we call length of the array. Since user can provide less, we need to have a way for the user to indicate that there is no more data: –A sentinel value: special value indicating end of data. This must be a value outside the range of values for the array. –^Z : we know it indicates no more data. See: readArray.c

Things You Can and Can’t Do You can’t –use = to assign one entire array to another. You can’t –use == to directly compare entire arrays You can’t –directly scanf or printf entire arrays But you can do these things on array elements! And you can write functions to do them