142 M -1 Arrays Chapter 8 Motivation: to deal with large amounts of data e.g sorting values: Input: 10, 15, 4, 15, 17, 3, 12, 36, 48, 32, 9, 21 Want the.

Slides:



Advertisements
Similar presentations
O-1 University of Washington Computer Programming I Lecture 14: Arrays © 2000 UW CSE.
Advertisements

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.
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.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
1 ICS103 Programming in C Lecture 12: Arrays I. 2 Outline Motivation for One-dimensional Arrays What is a One-dimensional Array? Declaring One-dimensional.
Arrays.
CS0007: Introduction to Computer Programming Introduction to Arrays.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
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 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
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.
Chapter 8: Arrays Introduction to arrays Declaring arrays Initializing arrays Examples using arrays Relationship with pointers Array passing to a function.
ICS103 Programming in C Lecture 11: Arrays I
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Computer Programming for Engineers
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
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?
Department of Computer Science Western Michigan University
Lecture2.
1-d Arrays.
Arrays Low level collections.
May 17th – Comparison Sorts
Chapter 9: Pointers.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Computer Programming BCT 1113
Today’s Material Arrays Definition Declaration Initialization
C Programming Tutorial – Part I
Chapter 7 Part 1 Edited by JJ Shepherd
New Structure Recall “average.cpp” program
Chapter 8 Arrays Objectives
Module 2 Arrays and strings – example programs.
Arrays Arrays are data structures that allow us to store data of the same type in contiguous memory locations. That was a pretty dense statement!
Object-Oriented Programming Using C++
Chapter 9: Pointers.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
One-Dimensional Array Introduction Lesson xx
Functions Chapter 3 of the text Motivation:
Chapter 9: Pointers.
Chapter 8 Arrays Objectives
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
EKT150 : Computer Programming
Declaration, assignment & accessing
Arrays .
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Starting Out with Programming Logic & Design
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays.
Initializing variables
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Announcements Lab 6 was due today Lab 7 assigned this Friday
Arrays I Handling lists of data.
Arrays ICS2O.
Chapter 8 Arrays Objectives
Arrays.
Lecture 14: Problems with Lots of Similar Data
Consider Write a program that prompts a user to enter the number of students and then, their names and grades. The program will then outputs the average.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
ICS103 Programming in C Lecture 12: Arrays I
ARRAYS ..
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Data Types and Arithmetic in C
Presentation transcript:

142 M -1 Arrays Chapter 8 Motivation: to deal with large amounts of data e.g sorting values: Input: 10, 15, 4, 15, 17, 3, 12, 36, 48, 32, 9, 21 Want the Output: 3, 4, 9, 10, 12, 15, 17, 21, 25, 32, 36, 48 averaging grades: with what we know double grade1, grade2, grade3; double grade4, grade5, grade6; double grade7, total;... /* initialize the grades */... total = grade1+grade2+grade3+grade4 +grade5+grade6+grade7; printf("average=%.2f\n",total/7.0); is there a better way?

Data Structure 142 M -2 We have functions to organize the program We also need a data structure to organize data large amounts of data variable amounts of data sets of data where the individual pieces of data have special relationships to one another e.g. data related to an employee age, SSN, duration of employment For this C offers: arrays structs combination of arrays and structs especially for

142M -3 Arrays Definition: A named, ordered, collection of identical type Example grades for 7 students Name the collection grade Number the elements of the collection start at 0 C expressions grade[0] is 3.0 grade[6] is *grade[3] is 4.0

142 M -4 Program example double grade[7], total; int i; /*initialize the grades*/ for (i=0; i<7; i++) { printf("Enter grade %i: ",i+1); scanf("%lf",&grade[i]); } /* average */ total = 0; for (i=0; i<7; i++) total = total + grade[i]; printf("\n average=%.2f",total/7.0); array declaration grade[i] is a regular double variable

142 M - 5 Array Terminology type name[size] array declaration size MUST be a constant double grade[7]; e.g. grade is of type array of double with size 7 grade[0], grade[1],..., grade[6] are the elements of the array grade. Each is of type double. 0, 1,...,6 are the indices of the array. Also called subscripts The bounds are the lowest and highest values of the subscripts (here 0 and 6)

142 M -6 Names of Arrays Array names are identifiers Use the same rules as for function and variable names names MUST start with a letter names MUST be declared before they are used... if you read in a program: x[y] x must be the name of an array y must have an integer value

Some rules(1) 142 M -7 Index rule: An array index must evaluate to an int between 0 and n-1 where n is the number of elements of the array No Exceptions Example: double grade[7];... grade[i+3+k] = 3.0; Can have complicated expressions: grade[(int)(3.1* *sin(2.*PI))] grade[i] OK if 0  i+3+k  6

Some rules(2) Element rule: An array element can be used wherever a simple variable of same type can be used No Exceptions Example: scanf("%lf",&grade[i]); grade[i]=sin(2.0*PI*sqrt(32.1)); grade[i] 142 M -8

142 M -9 Program Example Goal: Get the grades of a class (the class size may vary) Print the average grade Print the grades above the average How? Have an array double grade[CLASS_SIZE_MAX]; #define constant Ask for the size of the class: class_size Check that class_size<=CLASS_SIZE_MAX Get the grades ( for loop) compute the average ( for loop) display the average and the grades above average ( for loop)

142 M -10 Using several arrays Computing a course grade: Need to compute an average between the Midterm and the Final Use three arrays double midterm[CLASS_SIZE_MAX]; double final[CLASS_SIZE_MAX]; double course[CLASS_SIZE_MAX]; parallel arrays And to compute the weighted average: for(i=0; i < class_size; i++) { course[i] = MT_WEIGHT*midterm[i] + FINAL_WEIGHT*final[i]; } MT_WEIGHT and FINAL_WEIGHT are #define constants equal for instance to 0.3 and 0.7

Shifting elements of an array 142 M -11 Goal: Given an array x, shift x[0], x[1],..., x[n-1] by one position upward to make space for a new element at x[0] Insert the new value at x[0] Update the dimension n of the array How? Check that x is big enough to receive a new element. Move x[k] into x[k+1] (k=n-1, n-2,...,0) Get x[0] Update n

In C #include #define SIZE_MAX 20 int main(void) { int x[SIZE_MAX],size,k,done=0; x[0]=1; x[1]=2; x[2]=3; size=3; while(!done && size<=SIZE_MAX) { /* shift x */ for(k=size-1; k>=0; k--) x[k+1] = x[k]; /* get x[0] */ printf("new element: "); scanf("%i",&x[0]); size = size + 1; /* again? */ printf("stop? (yes=1) "); scanf("%i",&done); } for(k=0; k<size; k++) printf("x[%i]=%i\n",k,x[k]); return EXIT_SUCCESS; } 142 M -12

Pitfalls of Arrays 142 M -13 double array1[SIZE], array2[SIZE]; some #define constant previously defined Suppose array1 has been initialized: Cannot do: array2 = array1; Cannot do: if (array1 == array2)... Cannot do: use scanf to directly read an array scan("??",&array1); But can do this on array elements Write your own functions