Two-Dimensional Arrays Lesson xx

Slides:



Advertisements
Similar presentations
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Advertisements

 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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 4: Continuing with C++ I/O Basics.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
1 CS161 Introduction to Computer Science Topic #3.
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
CS Class 03 Topics  Sequence statements Input Output Assignment  Expressions Read pages Read pages 40 – 49 for next time.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
 for loop  while loop  do-while loop for (begin point; end point ; incrementation ) { //statements to be repeated }
EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers.
Arrays.
Module 1: Array ITEI222 - Advance Programming Language.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Chapter Topics The Basics of a C++ Program Data Types
Popping Items Off a Stack Using a Function Lesson xx
User-Written Functions
I/O Streams File I/O 2-D array review
Basics (Variables, Assignments, I/O)
What Actions Do We Have Part 1
EGR 2261 Unit 10 Two-dimensional Arrays
Two Dimensional Array Mr. Jacobs.
Basic Elements of C++.
Objectives Identify the built-in data types in C++
EGR 115 Introduction to Computing for Engineers
CSC 113: Computer Programming (Theory = 03, Lab = 01)
Arrays Part-1 Armen Keshishian.
Introduction to C++ October 2, 2017.
Student Book An Introduction
C++ Arrays.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
void Pointers Lesson xx
Basic Elements of C++ Chapter 2.
Arrays & Functions Lesson xx
Structures Lesson xx In this module, we’ll introduce you to structures.
Chapter 2 Elementary Programming
Engineering Problem Solving with C++, Etter/Ingber
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
Returning Structures Lesson xx
Basics (Variables, Assignments, I/O)
Linked List Lesson xx   In this presentation, we introduce you to the basic elements of a linked list.
One-Dimensional Array Introduction Lesson xx
File I/O with Records Lesson xx
Passing Structures Lesson xx
Popping Items Off a Stack Lesson xx
Programming Funamental slides
Value returning Functions
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Arrays Kingdom of Saudi Arabia
Programming Funamental slides
Functions Pass By Value Pass by Reference
Multidimensional array
Alternate Version of STARTING OUT WITH C++ 4th Edition
Programs written in C and C++ can run on many different computers
Engineering Problem Solving with C++ An Object Based Approach
Capitolo 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Arrays Arrays A few types Structures of related data items
What Actions Do We Have Part 1
Capitolo 1 – Introduction C++ Programming
Pointers and dynamic objects
Chapter 3 Arrays Dr. A. PHILIP AROKIADOSS Assistant Professor
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Presentation transcript:

Two-Dimensional Arrays Lesson xx  In this presentation, we’ll talk about two-dimensional arrays.

Objective We will cover the following topics that deal with two-dimensional array: Declaration Subscripts Initialization Operations Application in a program In this module, we learned the following about two-dimensional arrays: 1. Declaration 2. Subscripts 3. Initialization 4. Operations 5. Application in a program.

Two Dimensional Array Declaration int m [3] [4]; col 0 col 1 col 2 col 3 row 0 row 1 A 2-dimensional array allows you to store items in table form in the computer. Here is how you declare a 2-dimensional array: int m[3] [4]; This means that array m has 3 rows and 4 columns. The rows are labeled row 0-2 and the columns are from 0-3. row 2

More Examples of 2-D Arrays float taxTable [10] [15]; double d [50][50]; char nameList [100] [20]; int t [5] [6]; Here are more examples of two-dimensional array declarations: float taxTable [10][5]; this declares an array call taxTable that has 10 rows and 15 columns. The array contains floating point numbers. double d [50] [50]; declares an array of doubles. Basically, you may declare an array with any data type.

Subscripts int m [3] [4]; col 0 col 1 col 2 col 3 row 0 row 1 In order to access one element of a 2-dimensional array, we use subscripts. Shown in red is how you refer to the item in the 3rd row, 2nd column, m [2][1]. The row subscript always goes first and then the column subscript. Row and column numbers start with 0. Shown in green is another example of using subscripts. row 2 m [2] [1]

Initialization with { } int m[2][3] = {{3, 6, 7}, {4, 7, 5}};     3 7 6 4 5 Here is an example of declaration and initialization of a two-dimensional array. We have array m that has 2 rows and 3 columns. The 1st row is initialized with 3, 6 and 7. The 2nd row is initialized with 4, 7 and 5. The inner {} are optional in this example. However, they are useful when you want to initialize partial rows which we’ll demonstrate in the next slide.

Initializing Partial Rows   int m[2][3] = {{3, 6 }, {4} };     6 4 3 6 4 3 In the top example declaration, we have array m that has 2 rows and 3 columns. The 1st row is initialized with 3, 6 and 0. The 2nd row is initialized with 4, 0 and 0. The inner {}s allow us to initialize partial rows. In the bottom example, we have the same statement except that we have removed the { }s. Notice that all the #s go into the 1st row.   int m[2][3] = { 3, 6 , 4 };

Two-Dimensional Array Operations int m[4][5] = {0};   int x = 0;   int a = 1;   m[2][3] = 17;   m[3][3] = a + m[2][3];   if (m[1][2] >= x)      cin >> m[1][3];   cout << m[1][0] << endl;   Basically, any operation that can be performed on scalar variables or one-dimensional arrays can be done with two-dimensional arrays. Here are some examples, you can declare and initialize an array as in: int m[4][5] = {0}; This sets up an array called m with 4 rows and 5 columns. The entire array contains zeros.

Two-Dimensional Array Operations int m[4][5] = {0};   int x = 0;   int a = 1;   m[2][3] = 17;   m[3][3] = a + m[2][3];   if (m[1][2] >= x)      cin >> m[1][3];   cout << m[1][0] << endl;   You can do assignment with the statement: m[2][3] = 17; Arithmetic operation can be performed on the different elements of the array. Shown here is the example: m[3][3] = a + m[2][3]; At the bottom of the slide, you can see that we can use input, output and conditional statements in conjunction with two-dimensional arrays .

Program Description Write a program for a shipping company that: 1. Reads in an origination and destination zone. 2. Reads in the weight of the package. 3. Looks up the price/lb for shipping the package. 4. Computes the cost for shipping the package.   A common use of two-dimensional arrays is table look up. Let’s write a program that does this. We’ll write a program for a shipping company that does the following: 1. reads in an origination and destination zone. 2. Reads in the weight of the package. 3. Looks up the price/lb for shipping the package 4. Computes the cost for shipping the package

Table .50 1.00 1.2 5 2.00 .89 .47 1.25 .82 .69 .44 Zone 1 2 3 4 1 Here is the chart with the price/lb for shipping a package from one zone to another. To send a package from zone 1 to zone 2, it costs 1.00 / lb. In order to ship a package from zone 4 to zone 3, the price is 82 cents/lb.

Program Listing #include <iostream> using std::cin; using std::cout; using std::endl; int main() {   double chart[4][4] = {{0.5, 1.0, 1.25, 2.0},    {1.0, 0.5, 0.89, 0.47},     {1.25, 0.82, 0.5, 0.69},     {2.0, 0.44, 0.82, 0.5}};   int o, d; float w;   cout << "Enter origination zone, destination zone & weight: ";   cin >> o >> d>> w;   cout << "The cost for sending your package is "     <<( chart[o-1][d-1 ] ) * w << endl;   return 0; }   Here is the entire listing of the program: the table set-up, the input and the computation.

Table int main() {   double chart[4][4] = {{0.5, 1.0, 1.25, 2.0},    {1.0, 0.5, 0.89, 0.47},     {1.25, 0.82, 0.5, 0.69},     {2.0, 0.44, 0.82, 0.5}}; chart .50 1.00 1.2 5 2.00 .89 .47 1.25 .82 .69 .44 1 2 3 double chart [4] [4]; sets ups a two-dimensional array of doubles. We have also initialized the array with the values from our chart. Notices that the row and column # are off by 1 digit from the zone #s.

Declaration and Input float w;   int o, d; float w;   cout << "Enter origination zone, destination zone & weight: ";   cin >> o >> d>> w; In this code, we have declared 3 variables, o, d & w. o is the origination zone, d is the destination zone and w is the weight of the package. We prompt the user for the 3 items of input with the cout statement and we read in the values via keyboard using the cin statement.

Compute Cost cout << "The cost for sending your package is "     <<( chart[o-1][d-1 ] ) * w << endl;   return 0; } This is the important part of the program. We calculate and print the cost of shipping a package in the cout statement. chart [o-1] [d-1] contains the cost/lb for sending an item from zone o to zone d. We need to subtract 1 because the array is offset by 1 from the actual table.

Summary In this module, we learned the following about two-dimensional arrays: Declaration Subscripts Initialization Operations Application in a program In this module, we learned the following about two-dimensional arrays: 1. Declaration 2. Subscripts 3. Initialization 4. Operations 5. Application in a program.