Arrays One-Dimensional initialize & display Arrays as Arguments Two-dimensional initialize & display Part I.

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved Introduction Arrays –Structures of related data items –Static entity (same size throughout program)
Advertisements

1 Lecture 21:Arrays and Strings(cont.) Introduction to Computer Science Spring 2006.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
 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.
Multiple-Subscripted Array
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
C++ for Engineers and Scientists Third Edition
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.
Arrays Structured Programming 256 Chapter 10 © 2000 Scott S Albert Arrays One-Dimensional initialize & display Arrays as Arguments Two-dimensional initialize.
Arrays One-Dimensional initialize & display Arrays as Arguments Part I.
Arrays Multi-dimensional initialize & display Sample programs Sorting Searching Part II.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Chapter 8 Arrays and Strings
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify –Array name –Position number Format: arrayname.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Data Structures (Terminology) Static Set at compile time Dynamic Set at run time.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
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.
Arrays Multi-dimensional initialize & display Sorting Part II.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
1 Arrays and Strings Lecture: Design Problem l Consider a program to calculate class average Why?? ?
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
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.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
An Introduction to Programming with C++ Sixth Edition Chapter 12 Two-Dimensional Arrays.
Arrays.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Module 1: Array ITEI222 - Advance Programming Language.
Opening Input/Output Files ifstream infile; ofstream outfile; char inFileName[40]; char outFileName[40]; coutinFileName;
© 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.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Arrays Chapter 12. One-Dimensional Arrays If you wanted to read in 1000 ints and print them in reverse order, it would take a program that’s over 3000.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
L what is a void-function? l what is a predicate? how can a predicate be used? l what is program stack? function frame? l what’s call-by-value? l what’s.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Two-Dimensional Data Class of 5 students Each student has 3 test scores Store this information in a two- dimensional array First dimension: which student.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
1 Programming in C++ Dale/Weems/Headington Chapter 11 One-Dimensional Arrays.
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)
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
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?
Objectives You should be able to describe: One-Dimensional 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 An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Chapter 8: Arrays Starting Out with C++ Early Objects Ninth Edition
Array Data Structure Chapter 6
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Arrays Kingdom of Saudi Arabia
Multidimensional Arrays
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Array Data Structure Chapter 6
Arrays Arrays A few types Structures of related data items
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
C++ Array 1.
Presentation transcript:

Arrays One-Dimensional initialize & display Arrays as Arguments Two-dimensional initialize & display Part I

Data Types a simple or atomic a structured * char, int, float, double array, struct, union, class

Structured Data Type - Array An array is a collection of data storage locations, each of which holds the same type of data. Each storage location is called an element of the array. Each element is referred to as an indexed or subscripted variable.

Array Declaration [] Syntax dataType arrayName [ ConstIntExpression ] The number of elements in an array is [ ] stated within square brackets, [ ]. Examples double angle [4];constant POLY = 8; int testScore [12];double angle [POLY]; char password [8];

Array Storage Each array has sufficient memory reserved to hold the number of data items of the given type. Initializing an array sets up the address of the first element. Addresses of all other elements are offsets from the starting address.

Array Element Access Syntax arrayName [ indexExpression ] Program access to each of the array elements is by referring to an offset from the array name. Array elements are counted beginning with zero.

Array Element Access double angle[4];// declaration Example angle [0] = 6.21; angle [1] = 15.89; angle [2] = 7.5; angle [3] = -45.7; angle sub zero = 6.21 angle sub one = angle sub two = 7.5 angle sub three = * * *

Using Array Elements  write the contents of an array element: cout << angle[2];  assign values to an array element: cin >> angle[3]; angle[6] = pow(axis,4);  use it as a parameter: y = sqrt(angle[0]);  use it in an expression: x = 2.5 * angle[1] + 64; * *

Array Component Access indexindex index index for(index=0; index < arraySize; index++) myArray[index] = 0.0; 100 * Zero out an entire array. (Initialize every element to 0.0.)

Off Set memory addresses starting address off set by one unit off set by two units off set by three units [ 0 ] [ 1 ] [ 2 ] [

Out-of-Bounds Array Index memory addresses angle[4] = 135; cout << angle[5]; [ 0 ] [ 1 ] [ 2 ] [

Declare an Array Syntax type arrayName[index]; Example double dailyHigh[23] ; int quizGrade[132] ; char ASURiteID[5] ; *

Initialize an Array Element Syntax arrayName[index] = value; Example dailyHigh[18] = 16.7; quizGrade[2] = 15; ASURiteID[3] = ‘d’; *

Initialize an Array double angle[4] = {16.21, 15.89, 7.5, -45.7}; double ATtoCG[5] = {.64,.89,.76,.83,.65}; int scores[12] = {210, 198, 203, 188, 200, 224, 220, 217, 211, 194, 197, 189}; double iona[8] = {0.0}; *

Initialize an Array int scores[ ] = {210, 198, 203, 188, 200, 224, 220, 217, 211, 194, 197, 189}; char name[4] = {‘Z’, ‘o’, ‘l’, ‘a’}; char name[4] = “Zola”;// no braces or, char phrase [ ] = “Hello World”; *

Initialize an Array double angle[4];// declaration Example angle [0] = 6.21; angle [1] = 15.89; angle [2] = 7.5; angle [3] = -45.7; angle sub zero = 6.21 angle sub one = angle sub two = 7.5 angle sub three = *

Sequencing Through an Array Use the for statement to sequence through an array. Total the contents of an array: sum = 0; for(index=0; index < 7; index++) sum = sum + grades[index];

Loading an Array double grade[10]; int index; for(index=0; index < 10; index++) { cout<<“Enter a grade “; cin >> grade[index]; }

Finding the Max/Min Value Set the max or min to element zero. Use a for loop to cycle through the array. Compare each element to the max/min. If necessary, assign a new value to max/min. How would you do this? *

Finding the Maximum Value double find_max(int temp[30]) { max = temp[0]; for(index=0; index max) max = temp[index]; return (max); } *

Finding the Minimum Value double find_min(int temp[30]) { min = temp[0]; < for(index = 1; index < 30; index++) if (temp[index] < min) min = temp[index]; return ( min ); } *

Aggregate Assignment - NOT! There are no aggregate assignments with arrays. That is, you may not assign one array to another. int x[5] = {11, 22, 33, 44, 55}; int y[5]; y = x;y[ ] = x[ ];

Arrays as Arguments temp[ ] void find_max(int temp[ ]) { max = temp[0]; for(index = 1; index max) max = temp[index]; } temp[ ] double find_max(int temp[ ]) * return max;

Passing an Array/Element temp find_max(temp); // no [ ] pricequantityamount inventory(price, quantity, amount); // 3 arrays cuplet words(cuplet); temp[3] find_max(temp[3]); pricequantity[4]amount[12] inventory(price, quantity[4], amount[12]); cuplet words(cuplet); *

Passing an Array formal parameter formal parameter declaration fordeclaration for parameterPass-by-ValuePass-by-Reference simple var.int costint& price arrayimpossibleint myArray[ ] arrayconst int source[ ]

1-D Array Review An array is a structured data type 1st element is “ arrayName subzero ” Array Declaration int myArray [9]; Array Initialization int myArray [9 ] = { 9, 9, 9, 8, 7, 6, 5, 4, 3}; Array Element Reference myArray [7];// value = 4 * 9

1-D Array Review Arrays are always passed by reference, unless const added to the formal parameter. Array in a Function Prototype void myFunction(int [9]); Array in a Function Call myFunction(myArray); Array in a Function Header void myFunction(int anyArrayName[9]) optional

Two Dimensional Arrays a everything about one dimensional arrays applies a sometimes referred to as a table a has rows and columns ex. multiplication table

2-D Array Declaration Syntax [] [] dataType arrayName [ ConstIntExpression ] [ ConstIntExpression ]  Example int highTemp [52] [7]; double matrix [8] [8]; // checker board int roomSchedule [237] [13];

2-D Array Initialization int nums [3] [2] = {7, 4, 1, 8, 5, 2} int roomsPSF[3] [7] = { {17, 18,19, 110, 111, 112, 113}, {27, 28, 29, 210, 211,212, 213}, {37, 38, 39, 310, 311, 312, 313} };

2-D Array Initialization double ATtoCG[2] [3] = {.74,.79,.76,.83,.65,.89}; } double ATtoCG[2] [3] = { {.74,.79,.76}, {.83,.65,.89} }; int scores[4] [3] = {210, 198, 203, 188, 200, 224, 220, 217, 211, 194, 197, 189};

2-D Array Initialization You can assign values to individual elements: ATtoGC [0] [0] = 0.74; ATtoGC [0] [1] = 0.79; ATtoGC [0] [2] = 0.76; ATtoGC [1] [0] = 0.83; ATtoGC [1] [1] = 0.65; * ATtoCG [1] [2] = 0.89;

Accessing a 2-D Array a very similar to creating a multiplication table. a use nested loops. outer for loop was for the rows inner for loop was for the columns

Loading a 2-D Array a The Multiplication Table for (row =1; row <=10; row++) { cout <<setw(5)<<row<<" "; for for (column=1; column <= 10; column++) cout << setw(5)<< column*row; cout << endl; }

Loading a 2-D Array int scores [4][3]; for(row=0; row<4; row++) for(col=0; col<3; col++) { cout<<"Enter the value :"; cin>>scores[row][col]; }

Loading a 2-D Array scores [0][0][2][0] [0][1] [2][1] [0][2][2][2] [1][0][3][0] [1][1][3][1] [1][2][3][2] [row][col] = somevalue;

Displaying a 2-D Array int scores [4] [3]; for(row=0; row<4; row++) { for(col=0; col<3; col++) cout << setw(6) << scores[row][col]; cout << endl; // new line for each row }

Displaying a 2-D Array Output

Functions and 2-D Arrays int test[7][19]; // declaration find_min(test); // function call int find_min(int num [7][19])// funct. header * * char code[26][10]; obtain(code); char obtain(char key [26][10])

Local vs. Global No difference

“One might as well say he has sold when no one has bought as to say he has taught when no one has learned.” John Dewey