13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

Structure.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
©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.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
Chapter 8 Arrays and Strings
Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
Prepared by MURLI MANOHAR PGT (COMPUTER SCIENCE) KV,B.E.G., PUNE.
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
Chapter 8 Arrays and Strings
Crypto Project Sample Encrypted Data: –Turing: CS Public\CryptoProjectData Crypto_Short_Keys_P_U.out Crypto_Long_Keys_O_R.out Bonus +10 points on.
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 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
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.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
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.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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.
CHAPTER 7: Arrays CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (Feb 2012)
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Arrays.
1. Define an array 1 Create reference arrays of objects in Java program 2 Initialize elements of arrays 3 Pass array to methods 4 Return array to methods.
1 CS161 Introduction to Computer Science Topic #15.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 7. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
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.
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 CSC103: Introduction to Computer and Programming Lecture No 17.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 11 Structures, Unions and Typedef 11.1 Structures Structures allow us to group related data items of different types under a common name. The individual.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-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)
Java Programming Language Lecture27- An Introduction.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
C Programming Lecture 15 Two Dimensional Arrays. Two-Dimensional Arrays b The C language allows arrays of any type, including arrays of arrays. With two.
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.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Array Data Structure Chapter 6
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
EKT150 : Computer Programming
Introduction To Programming Information Technology , 1’st Semester
Arrays Week 2.
CS150 Introduction to Computer Science 1
Array Data Structure Chapter 6
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
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.
Structures, Unions, and Enumerations
Presentation transcript:

13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns  We specify the number of columns in a two dimensional array in the same way that we specify the number of rows  int matrix[3][5];

23/10/2016CS150 Introduction to Computer Science 1 Initializing two dimensional arrays  int matrix[3][5] = {{ 1, 2, 3, 4, 5}, { 6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}};  How could you read in the input for the array from the user?

33/10/2016CS150 Introduction to Computer Science 1 Problem  Write the program segment to sum all the elements in a two-dimensional array  Write the program segment to output all the elements of the array to the screen. Display the output as a matrix with the same number of rows and columns as the array

43/10/2016CS150 Introduction to Computer Science 1 Passing Two-Dimensional Arrays to Functions  When passing two-dimensional arrays to functions you need to specify the number of columns in the formal argument list

53/10/2016CS150 Introduction to Computer Science 1 Problem  Write the function definition of (sumRow) that is passed a two-dimensional array and returns the sum of all the rows placed in a one-dimensional array.

63/10/2016CS150 Introduction to Computer Science 1 Arrays and Data Types  Useful for storing a collection of data elements of the same data type (float, int, string). char myName[5]; //All elements chars float salaries[NUM_EMP]; //All elements floats char vowels[]={ ‘ A ’, ’ E ’, ’ I ’, ’ O ’, ’ U ’ };  What about storing a collection of data elements of different data types?

73/10/2016CS150 Introduction to Computer Science 1 Data with Different Data Types  For example, what if we wanted to keep the following information on a particular employee: employee id SS# number of children salary citizen  The elements have different data types, so we can’t conveniently use array. Instead we will use a struct ( short for structure).

83/10/2016CS150 Introduction to Computer Science 1 Structure Definition To store this information: employee id SS# number of children salary citizen We would begin by defining a structure : struct employ { int id int ssnum; int numchild; float salary; bool citizen; };

93/10/2016CS150 Introduction to Computer Science 1 Struct Terminology For this struct: struct employ { int id int ssnum; int numchild; float salary; bool citizen; };  employ is the identifier name and a NEW data type.  The individual components id, ssnum, etc. are called members.

103/10/2016CS150 Introduction to Computer Science 1 Struct Declaration  As with all data types, in order to use our new data type employ we must allocate storage space by declaring variables of this data type: employ engineer,tech;  This will allocate space for two variables called engineer and tech with the previously described members id, ssnum, etc.

113/10/2016CS150 Introduction to Computer Science 1 Member Access Operator  To access a struct member, we use the member access operator (period between struct variable name and member name).  In the variable engineer of data type employ we can make the assignments: engineer.id = 12345; engineer.ssnum = ; engineer.numchild = 2; engineer.salary = ; engineer.citizen = true;  How do we access the data in arrays?

123/10/2016CS150 Introduction to Computer Science 1 Example One  Write a C++ struct data type realnum that will have members number, realpart, and intpart.  Declare a variable numinfo of that type.  Place the value in the field number.

133/10/2016CS150 Introduction to Computer Science 1 Example One Solution struct realnum { float number; int realpart; int intpart; }; realnum numinfo; numinfo.number= ; Define struct realnum Declare variable numinfo Assign member number

143/10/2016CS150 Introduction to Computer Science 1 Structs as function arguments  Structs can be passed to functions by reference or value in the same manner that other data types have been passed.  Generally, passing structs by reference is preferred since passing by value requires a local copy of the struct be created within the function’s variables.

153/10/2016CS150 Introduction to Computer Science 1 Example Two  Write a C++ function split that accepts a variable of type realnum.  Assign the integer part of the number to the member variable intpart and the real part of the number to the member variable realpart.

163/10/2016CS150 Introduction to Computer Science 1 Example Two Solution  Function prototype: void split(realnum &);  Function call: split (numinfo);  Function definition: void split(realnum & numberinfo) { // Use numberinfo.number,numberinfo.intpart, // and numberinfo.realpart. }

173/10/2016CS150 Introduction to Computer Science 1 Example Three Consider the following struct data type: struct info { int num; int divisors[10]; int howmany; }; Write a C++ function compute that accepts a variable of type info and returns all the divisors greater than 1 of the variable num in the array divisors and the number of divisors in the variable howmany.