Arrays 2(Chapter 8.1,8.5) Solution 5 Solution 6 Multidimensional arrays Dynamic Array Problems.

Slides:



Advertisements
Similar presentations
Starting Out with C++, 3 rd Edition 1 Chapter 10 – Characters, Strings, and the string Class.
Advertisements

#include using namespace std; void main() { int a[3]={10,11,23}; for(int i=0;i
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
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.
 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
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Pointers 1 Pointers Pointers  In chapter three we looked at passing parameters to functions using call by value, and using call by reference, using reference.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
 Write a program that uses a one dimension to do a table look-up  Learn about parallel arrays.
 Review structures  Program to demonstrate a structure containing a pointer.
February 11, 2005 More Pointers Dynamic Memory Allocation.
C to C++ © Bruce M. Reynolds & Cliff Green, C++ Programming Certificate University of Washington Cliff Green.
Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS];
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
SNPL1 Woochang Lim C+OOP = C++ C (non OOP)  C++ (non OOP+OOP)  Java (OOP) Object-Oriented Design  Object-Oriented Programming Programming with C++
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Multi-dimensional Array 1 Multi-dimensional array refers to an array with more than one index. It is a logical representation. On physical storage, the.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
4.1Introduction Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based arrays (C-like) –Arrays.
Stallings, Wireless Communications & Networks, Second Edition, © 2005 Pearson Education, Inc. All rights reserved C++ Programming: Arrays.
Pointers and Dynamic Arrays
Chapter 7 User-Defined Methods.
Chapter 7 Pointers and C-Strings
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.
Two Dimensional Array Mr. Jacobs.
Introduction to Programming
Command Line Arguments
Array An “average.cpp” program
Multi-dimensional Array
Chapter 7: Arrays.
CSCE 210 Data Structures and Algorithms
C++ Arrays.
Parallel Arrays Parallel array =>Two or more arrays with the same number of elements used for storing related information about a collection of data. Example:
group work #hifiTeam
CS 1430: Programming in C++.
Chapter 2 Elementary Programming
Popping Items Off a Stack Lesson xx
Constant pointers and pointers to constants
Arrays Kingdom of Saudi Arabia
Arrays November 8, 2017.
4.9 Multiple-Subscripted Arrays
Chapter 12 Pointers and Memory Management
Can store many of the same kind of data together
Introduction to Programming
Multidimensional Arrays
Pointers Lecture 2 Tue, Jan 24, 2006.
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.
7 Arrays.
C++ Pointers and Strings
CHAPTER 2 Arrays and Vectors.
Suggested self-checks: Section 7.11 #1-11
Capitolo 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Arrays Arrays A few types Structures of related data items
Fundamental Programming
CHAPTER 2 Arrays and Vectors.
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.
Arrays.
C++ Programming Lecture 20 Strings
Chapter 3 Arrays Dr. A. PHILIP AROKIADOSS Assistant Professor
Structure (i.e. struct) An structure creates a user defined data type
C++ Pointers and Strings
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

Arrays 2(Chapter 8.1,8.5) Solution 5 Solution 6 Multidimensional arrays Dynamic Array Problems

Multidimensional Arrays Storing scores on 5 tests for a class of 30 students Storing a 200x200 picture Storing the words in a paragraph Storing data on 2000 employees

Tables of Numbers int test[5][30]; //allocate five arrays of 30 integers Test[2][20] is the score of the 20 th student on test two double average; for (i=0; i<30; i++){ average=0; for (j=0; j<5; j++) average += test[j][i]; cout << “The test average for student << i << “is “ << average/5; }

Array Storage double p[5][10] //5 arrays of 10 doubles p[0],p[1]...,p[4] are the arrays p[0] p[3] char p[3][2]={{'a','b'},{'c','e'},{'w','A'}}; Double smallest(double p[][10]); void myfunction( char p[][2] );

Storing an Image char w[5][10][3] allocates a 5x10 table of 3 charaters W[3][1] Total 5x10x3 elements. Can also be viewed as 5 groups of 10x3 arrays What is the location of the character w[3][2][1]?

Words in a Paragraph string words[200]; cout > word[i]; cout <endl << “The last letter of each word is” <<endl for (i=0;i<200;i++) cout << word[i][word[i].length()-1];

Array of class objects class Baby { public: Baby(){}; Baby(int age,int weight, string name) { itsAge = age; itsWeight=weight; itsName=name; } ~Baby() {} int GetAge() { return itsAge; } int GetWeight() { return itsWeight; } void SetAge(int age) { itsAge = age; } void SetWeight(int weight) { itsWeight = weight; } void SetName(string name) { itsName = name; } private: int itsAge; int itsWeight; string itsName; };

Array of class objects int main() { int age, weight; string name; Baby newbabies[2]; int i; for (i = 0; i <2; i++) { cin >> age; cin >> weight; cin >> name; newbabies[i].SetAge(age); newbabies[i].SetWeight(weight); newbabies[i].SetName(name); } for (i = 0; i < 2; i++) std::cout << "#" << i << ": " << newbabies[i].GetAge() << std::endl; return 0; } Complete Program

Dynamic Array Problems In general multidimesional arrays cannot be dynamic. That is one cannot define an array as int myarray[dim1][dim2] Static arrays are hard to pass as function parameters as dimenstion must be specified. void myfunction( char p[][2] ); Solution use Boost LibraryBoost Library Examples gnplot.h, c1_graphB.cppgnplot.hc1_graphB.cpp