Arrays y Templates. Prof. Ing. José María Sola. 1. Array. 2. Array of Array. 3. Matrix Template. 4. Matrix Template Module. 5. Listados Completos

Similar documents
UEE1303(1070) S12: Object-Oriented Programming Constant Pointer and Class

Comp151. Generic Programming: Container Classes

UEE1303(1070) S12: Object-Oriented Programming Constructors and Destructors

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

Graphs. directed and undirected graphs weighted graphs adjacency matrices. abstract data type adjacency list adjacency matrix

6.S096 Lecture 4 Style and Structure

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

Lists. linking nodes. constructors. chasing pointers. MCS 360 Lecture 11 Introduction to Data Structures Jan Verschelde, 17 September 2010.

CS 247: Software Engineering Principles. C++ Templates. Reading: Eckel, Vol. 2 Ch. 5 Templates in Depth. U Waterloo CS247 (Spring 2017) p.

CSCE 110 PROGRAMMING FUNDAMENTALS

Defining Class Functions.

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

C++ For Science and Engineering Lecture 15

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

Operator overloading

C++ Programming Lecture 8 Software Engineering Group

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

CS 7B - Spring Final Exam

C++ Programming Lecture 13 Software Engineering Group

COP4530 Data Structures, Algorithms and Generic Programming Recitation 4 Date: September 14/18-, 2008

Elevate your Code to Modern C++ with Automated Tooling. Peter Sommerlad

PIC 10A. Lecture 17: Classes III, overloading

6.096 Introduction to C++ January (IAP) 2009

Lambda functions. Zoltán Porkoláb: C++11/14 1

Introduction to C++ Day 2

UEE1302 (1102) F10: Introduction to Computers and Programming

CSCE 110 PROGRAMMING FUNDAMENTALS

Advanced C++ 4/13/2017. The user. Types of users. Const correctness. Const declaration. This pointer and const.

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

C++ Programming Lecture 6 Software Engineering Group

September 19,

Propedéutico de Programación

C++14 Reflections Without Macros, Markup nor External Tooling

Introduction to Programming

PIC 10A 1C Week 5b Problems. TA: Eric. [Solutions]

Object-Oriented Programming in C++

ADTs in C++ In C++, member functions can be defined as part of a struct

Lecture 7. Log into Linux New documents posted to course webpage

EE 152 Advanced Programming LAB 7

PENN STATE UNIVERSITY Department of Economics

EL2310 Scientific Programming

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

CSI33 Data Structures

PIC 10A Objects/Classes

C++ assign array values. C++ assign array values.zip

Circle all of the following which would make sense as the function prototype.

Intermediate Programming, Spring 2017*

COMP 2355 Introduction to Systems Programming

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

C++ Exception Handling 1

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Lesson 11: Generic Programming. EE3490E: Programming S1 2018/2019 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

C C C C++ 2 ( ) C C++ 4 C C

Lab 2: ADT Design & Implementation

Sol. Sol. a. void remove_items_less_than(int arr[], int size, int value) #include <iostream> #include <ctime> using namespace std;

Function Overloading

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference

Exercise 1.1 Hello world

Separate Compilation of Multi-File Programs

Integer Data Types. Data Type. Data Types. int, short int, long int

Programmazione. Prof. Marco Bertini

From python to C++ part 2. Jon Macey

TDDE18 & 726G77. Functions

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Review: C++ Basic Concepts. Dr. Yingwu Zhu

Intermediate Programming, Spring 2017*

Resumen. Una sola cámara Robot móvil Visión computacional Resultados experimentales

C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15

C++ Structures Programming Workshop 2 (CSCI 1061U)

Object-Oriented Programming for Scientific Computing

Queue Implementations

C++ Tutorial AM 225. Dan Fortunato

CS302 - Data Structures using C++

9. C++ advanced (II): Templates

A<int,60> array[100]; 100*60*sizeof(int)

Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name:

Programming. C++ Basics

9. C++ advanced (II): Templates

Variables. Data Types.

CS 376b Computer Vision

1d: tests knowing about bitwise fields and union/struct differences.

Iterators. node UML diagram implementing a double linked list the need for a deep copy. nested classes for iterator function objects

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Implementing an ADT with a Class

Fast Introduction to Object Oriented Programming and C++

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Suppose that you want to use two libraries with a bunch of useful classes and functions, but some names collide:

Lesson 12 - Operator Overloading Customising Operators

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

OBJECT ORIENTED PROGRAMMING USING C++

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

Functions and Recursion

Introduction to C++ Systems Programming

Lecture 2 More C++ Jon Macey

Chapter 12 - Templates

Structuur van Computerprogramma s 2

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Transcription:

Prof. Ing. José María Sola. Introducción a Arrays y Templates Por Ing. José María Sola 1. Array Array 2. Array of Array Array of Array 3. Matrix Template Matrix Template 4. Matrix Template Module Figura 1. Conceptual Figura 2. Implementación 5. Listados Completos 5.1. Array Array.cpp. /* Arrays, template functions, 1

* range-for, size_t, sizeof, auto, intializazation, const, parameters (in-out-inout), type inference. * 201304-201608 * José María Sola */ int main(){ void TestArrayVariables(); TestArrayVariables(); void TestNonGenericArrayFunctions(); TestNonGenericArrayFunctions(); void TestGenericLengthArrayFunctions(); TestGenericLengthArrayFunctions(); void TestGenericTypeAndLengthArrayFunctions(); TestGenericTypeAndLengthArrayFunctions(); #include <cstddef> using std::size_t; #include <array> using std::array; #include <iostream> using std::cout; #include <string> void TestArrayVariables(){ cout << func << '\n'; // Imprime el nombre de la función. array<int,7> x; // Declaración, sin incialización. x.at(1) = 42; // Set/Write. cout << x.at(1) << '\n'; // Get/Read. array<int,3> a = {{10, 20, 30; // Declaración e inicialización. // Un array es un struct con un data member, por eso las dobles llaves. cout << a.at(0) << '\t' // Muestra el primer elemento, << a.at(2) << '\n' // y el último de forma particular << a.at(a.size()-1) << '\n'; // y genéricamente. 2

// El tamaño de un arreglo es igual al tamaño del tipo de sus elementos por la cantidad de elementos del arreglo. cout << "sizeof a : " << sizeof a << '\n' << "sizeof(array<int,3>) : " << sizeof(array<int,3>) << '\n' << "sizeof a.at(0) : " << sizeof a.at(0) << '\n' << "sizeof(int) : " << sizeof(int) << '\n' << "a.size() : " << a.size() << '\n' << "sizeof a / sizeof a.at(0) : " << sizeof a / sizeof a.at(0) << '\n' << "sizeof(array<int,3>) / sizeof(int): " << sizeof(array<int,3>) / sizeof(int) << '\n'; // Iterar arreglo para get con for (for general). for(size_t i=0; i < 3; ++i) // size_t. cout << a.at(i) << '\t'; // Iterar arreglo para set con for. for(size_t i=0, n=a.size(); i < n; ++i) // n. a.at(i) *= 10; // Iterar arreglo para get con range-for (for-auto). for(auto e : a) // auto. /* También for(const auto& e : x)... pero para tipos simples no es necesario, hasta es menos performante. */ // Iterar arreglo para set con range-for. for(auto& e : a) // auto&. e /= 10; for(auto e : a) // Array de otros tipos 3

// Array de doubles array<double,7> ad = {{0.1, 2.3, 4.5, 6.7; // 4:0.0, 5:0.0, 6:0.0 for(auto e : ad) // Array de strings array<std::string,3> as = {{"C++", "Moderno"; // 2:"" for(auto e : as) // Non-Generic functions declarations. void PrintArrayInt5(const array<int,5>& x); // in array. void MultiplyArrayInt5(array<int, 5>& x, int k); // inout array. array<int, 5> AddArrayInt5(const array<int, 5>& x, const array<int, 5>& y); // return array. void TestNonGenericArrayFunctions(){ cout << func << '\n'; array<int, 5> a = {{1,2,3,4,5; cout << "a :\t"; PrintArrayInt5(a); auto b = a; cout << "b :\t"; PrintArrayInt5(b); MultiplyArrayInt5(a,10); cout << "a*10 :\t"; PrintArrayInt5(a); auto c = AddArrayInt5(a,b); // Variable auxiliar. cout << "c=a+b:\t"; PrintArrayInt5(c); cout << "a+b :\t"; PrintArrayInt5(AddArrayInt5(a,b)); // Composición. // Non-Generic functions definitions. void PrintArrayInt5(const array<int,5>& x){ for(auto e : x) 4

void MultiplyArrayInt5(array<int, 5>& x, int k){ for(auto& e : x) e *= k; array<int, 5> AddArrayInt5(const array<int, 5>& x, const array<int, 5>& y){ array<int, 5> z; for(size_t i=0; i < 5; ++i) z.at(i) = x.at(i) + y.at(i); return z; /* Otra forma: auto z = x; for(auto i=0; i < 5; ++i) z.at(i) += y.at(i); return z; */ // Length-generic functions declarations. template <size_t n> void PrintArrayInt(const array<int, n>& x); // in array. template <size_t n> void MultiplyArrayInt(array<int, n>& x, int k); // inout array. template <size_t n> array<int, n> AddArrayInt(const array<int, n>& x, const array<int, n>& y); // return array. void TestGenericLengthArrayFunctions(){ cout << func << '\n'; array<int, 5> a = {{1,2,3,4,5; cout << "a :\t"; PrintArrayInt(a); auto b = a; cout << "b :\t"; PrintArrayInt(b); MultiplyArrayInt(a,10); cout << "a*10 :\t"; PrintArrayInt(a); 5

auto c = AddArrayInt(a,b); // Variable auxiliar. cout << "c=a+b:\t"; PrintArrayInt(c); cout << "a+b :\t"; PrintArrayInt(AddArrayInt(a,b)); // Composición. // Length-generic functions definitions. template <size_t n> void PrintArrayInt(const array<int, n>& x){ for(auto e : x) template <size_t n> void MultiplyArrayInt(array<int, n>& x, int k){ for(auto& e : x) e *= k; template <size_t n> array<int, n> AddArrayInt(const array<int, n>& x, const array<int, n>& y){ auto z = x; for(size_t i=0; i < n; ++i) z.at(i) += y.at(i); return z; // Type-and-length generic functions declarations. template <typename T, size_t n> void PrintArray(const array<t, n>& x); // in array. template <typename T, size_t n> void MultiplyArray(array<T, n>& x, int k); // inout array. template <typename T, size_t n> array<t, n> AddArray(const array<t, n>& x, const array<t, n>& y); // return array. void TestGenericTypeAndLengthArrayFunctions(){ cout << func << '\n'; 6

array<int, 5> a = {{1,2,3,4,5; cout << "a :\t"; PrintArray(a); auto b = a; cout << "b :\t"; PrintArray(b); MultiplyArray(a,10); cout << "a*10 :\t"; PrintArray(a); auto c = AddArray(a,b); // Variable auxiliar. cout << "c=a+b:\t"; PrintArray(c); cout << "a+b :\t"; PrintArray(AddArray(a,b)); // Composición. // Type-and-length generic functions definitions. template <typename T, size_t n> void PrintArray(const array<t, n>& x){ for(auto e : x) template <typename T, size_t n> void MultiplyArray(array<T, n>& x, int k){ for(auto& e : x) e *= k; template <typename T, size_t n> array<t, n> AddArray(const array<t, n>& x, const array<t, n>& y){ auto z = x; for(size_t i=0; i < n; ++i) z.at(i) += y.at(i); return z; Array output. TestArrayVariables 42 10 30 30 sizeof a : 12 7

sizeof(array<int,3>) : 12 sizeof a.at(0) : 4 sizeof(int) : 4 a.size() : 3 sizeof a / sizeof a.at(0) : 3 sizeof(array<int,3>) / sizeof(int): 3 10 20 30 100 200 300 10 20 30 0.1 2.3 4.5 6.7 0 0 0 C++ Moderno TestNonGenericArrayFunctions a : 1 2 3 4 5 b : 1 2 3 4 5 a*10 : 10 20 30 40 50 c=a+b: 11 22 33 44 55 a+b : 11 22 33 44 55 TestGenericLengthArrayFunctions a : 1 2 3 4 5 b : 1 2 3 4 5 a*10 : 10 20 30 40 50 c=a+b: 11 22 33 44 55 a+b : 11 22 33 44 55 TestGenericTypeAndLengthArrayFunctions a : 1 2 3 4 5 b : 1 2 3 4 5 a*10 : 10 20 30 40 50 c=a+b: 11 22 33 44 55 a+b : 11 22 33 44 55 5.2. Array of Array ArrayOfArray.cpp. /* Arrays-of-Arrays, matrixes, template functions, * range-for, size_t, sizeof, auto, intializazation, const, parameters (in-out-inout), type inference. * 201304-201608 * José María Sola */ int main(){ void TestArrayOfArrayVariables(); TestArrayOfArrayVariables(); 8

void TestNonGenericArrayOfArrayFunction(); TestNonGenericArrayOfArrayFunction(); void TestGenericArrayOfArrayFunctions(); TestGenericArrayOfArrayFunctions(); #include <cstddef> using std::size_t; #include <array> using std::array; #include <iostream> using std::cout; void TestArrayOfArrayVariables(){ cout << func << '\n'; // Imprime el nombre de la función. array<array<int,3>,5> x; // Declaración, sin incialización. x.at(3).at(2) = 42; // Set/Write. cout << x.at(3).at(2) << '\n'; // Get/Read. array<array<int,3>,5> m = {{ // Declaración e inicialización. {{ 1, 2, 3, {{ 4, 5, 6, {{ 7, 8, 9, {{10, 11, 12, {{13, 14, 15 ; // Un array es un struct con un data member, por eso las dobles llaves. cout << m.at(0).at(0) << '\t' // Muestra el primer elemento, << m.at(4).at(2) << '\t' // y el último de forma particular << m.at(m.size()-1).at(m.at(m.size()- 1).size()- 1) << '\n'; // y genéricamente. // El tamaño de un arreglo es igual al tamaño del tipo de sus elementos por la cantidad de elementos del arreglo. cout << "sizeof m : " << sizeof m << '\n' 9

<< "sizeof(array<array<int,3>,5>): " << sizeof(array<array<int,3>,5>) << '\n'; // Iterar arreglo para get con for (for general). for(size_t i=0; i < 5; ++i){ // size_t. for(size_t j=0; j < 3; ++j) cout << m.at(i).at(j) << '\t'; // Iterar arreglo para set con for. for(size_t i=0, n=m.size(); i < n; ++i) // n. for(size_t j=0, n=m.at(i).size(); j < n; ++j) m.at(i).at(j) = i*10+j; // Iterar arreglo para get con range-for (for-auto). for(auto row : m){ // auto. for(auto e : row) // Iterar arreglo para set con range-for. for(auto& row : m) // auto& for(auto& e : row) e *= 10; for(auto row : m){ for(auto e : row) void TestNonGenericArrayOfArrayFunction(){ cout << func << '\n'; // Imprime el nombre de la función. array<array<int,2>,3> m = {{ {{0, 1, {{2, 3, {{4, 5 ; void PrintMatrixInt3x2(const array<array<int,2>,3>& x); cout << "m:\n"; PrintMatrixInt3x2(m); 10

void PrintMatrixInt3x2(const array<array<int,2>,3>& x){ for(auto row : x){ for(auto e : row) // Template functions declarations template <typename T, size_t rows, size_t columns> void PrintMatrix(const array<array<t, columns>, rows>& x); // in. template <typename T, size_t rows, size_t columns> void MultiplyMatrix(array<array<T, columns>, rows>& x, int k); // inout. template <typename T, size_t rows, size_t columns> array<array<t, columns>, rows> // return. AddMatrix( const array<array<t, columns>, rows>& x, const array<array<t, columns>, rows>& y ); void TestGenericArrayOfArrayFunctions(){ cout << func << '\n'; array<array<int,3>,5> a = {{ {{11,12,13, {{21,22,23, {{31,32,33, {{41,42,43, {{51,52,53 ; cout << "a :\n"; PrintMatrix(a); auto b = a; cout << "b :\n"; PrintMatrix(b); MultiplyMatrix(a,10); cout << "a*10 :\n"; PrintMatrix(a); auto c = AddMatrix(a,b); // Variable auxiliar. cout << "c=a+b:\n"; PrintMatrix(c); 11

cout << "a+b :\n"; PrintMatrix(AddMatrix(a,b)); // Composición. // Template functions definitions template <typename T, size_t rows, size_t columns> void PrintMatrix(const array<array<t, columns>, rows>& x){ for(auto row : x){ for(auto e : row) template <typename T, size_t rows, size_t columns> void MultiplyMatrix(array<array<T, columns>, rows>& x, int k){ for(auto& row : x) for(auto& e : row) e *= k; template <typename T, size_t rows, size_t columns> array<array<t, columns>, rows> AddMatrix( const array<array<t, columns>, rows>& x, const array<array<t, columns>, rows>& y ){ auto z = x; for(size_t i=0; i < rows; ++i) for(size_t j=0; j < columns; ++j) z.at(i).at(j) += y.at(i).at(j); return z; Array output. TestArrayOfArrayVariables 42 1 15 15 sizeof m : 60 sizeof(array<array<int,3>,5>): 60 1 2 3 4 5 6 7 8 9 10 11 12 12

13 14 15 0 1 2 10 11 12 20 21 22 30 31 32 40 41 42 0 10 20 100 110 120 200 210 220 300 310 320 400 410 420 TestNonGenericArrayOfArrayFunction m: 0 1 2 3 4 5 TestGenericArrayOfArrayFunctions a : 11 12 13 21 22 23 31 32 33 41 42 43 51 52 53 b : 11 12 13 21 22 23 31 32 33 41 42 43 51 52 53 a*10 : 110 120 130 210 220 230 310 320 330 410 420 430 510 520 530 c=a+b: 121 132 143 231 242 253 341 352 363 451 462 473 561 572 583 a+b : 121 132 143 231 242 253 341 352 363 451 462 473 13

561 572 583 5.3. Matrix Template MatrixTemplate.cpp. /* Type template, Arrays-of-Arrays, matrixes, template functions, * range-for, size_t, sizeof, auto, intializazation, const, parameters (in-out-inout), type inference. * 201304-201608 * José María Sola */ // Template type declaration #include <array> // array #include <cstddef> // size_t using Matrix = std::array< // array std::array< // of arrays T, // of T columns // of columns elements >, rows // of rows elements >; int main(){ void TestMatrixVariables(); TestMatrixVariables(); void TestMatrixFunctions(); TestMatrixFunctions(); #include <iostream> // cout using std::cout; void TestMatrixVariables(){ cout << func << '\n'; Matrix<int, 5, 3> x; // Declaración, sin incialización. x.at(3).at(2) = 42; // Set/Write. cout << x.at(3).at(2) << '\n'; // Get/Read. 14

cout << x.at(0).at(0) << '\t' // Muestra el primer elemento, << x.at(4).at(2) << '\t' // y el último de forma particular << x.at(x.size()-1).at(x.at(x.size()- 1).size()- 1) << '\n'; // y genéricamente. Matrix<int, 5, 3> m = {{ // Declaración e inicialización. {{ 1, 2, 3, {{ 4, 5, 6, {{ 7, 8, 9, {{10, 11, 12, {{13, 14, 15 ; // Un array es un struct con un data member, por eso las dobles llaves. // El tamaño de un arreglo es igual al tamaño del tipo de sus elementos por la cantidad de elementos del arreglo. cout << "sizeof m : " << sizeof m << '\n' << "sizeof(matrix<int, 5, 3>): " << sizeof(matrix<int, 5, 3> ) << '\n'; // Iterar arreglo para get con for (for general). for(size_t i=0; i < 5; ++i){ // size_t. for(size_t j=0; j < 3; ++j) cout << m.at(i).at(j) << '\t'; // Iterar arreglo para set con for. for(size_t i=0, n=m.size(); i < n; ++i) // n. for(size_t j=0, n=m.at(i).size(); j < n; ++j) m.at(i).at(j) = i*10+j; // Iterar arreglo para get con range-for (for-auto). for(auto row : m){ // auto. for(auto e : row) // Iterar arreglo para set con range-for. for(auto& row : m) // auto& 15

for(auto& e : row) e *= 10; for(auto row : m){ for(auto e : row) // Template functions declarations void PrintMatrix(const Matrix<T, rows, columns>& x); // in. void MultiplyMatrix(Matrix<T, rows, columns>& x, int k); // inout. Matrix<T, rows, columns> // return. AddMatrix( const Matrix<T, rows, columns>& x, const Matrix<T, rows, columns>& y ); void TestMatrixFunctions(){ cout << func << '\n'; // Imprime el nombre de la función. Matrix<int, 5, 3> a = {{ {{11,12,13, {{21,22,23, {{31,32,33, {{41,42,43, {{51,52,53 ; cout << "a :\n"; PrintMatrix(a); auto b = a; cout << "b :\n"; PrintMatrix(b); MultiplyMatrix(a,10); cout << "a*10 :\n"; PrintMatrix(a); auto c = AddMatrix(a,b); // Variable auxiliar. cout << "c=a+b:\n"; PrintMatrix(c); 16

cout << "a+b :\n"; PrintMatrix(AddMatrix(a,b)); // Composición. // Template functions definitions void PrintMatrix(const Matrix<T, rows, columns>& x){ for(auto row : x){ for(auto e : row) std:: std:: void MultiplyMatrix(Matrix<T, rows, columns>& x, int k){ for(auto& row : x) for(auto& e : row) e *= k; Matrix<T, rows, columns> AddMatrix( const Matrix<T, rows, columns>& x, const Matrix<T, rows, columns>& y ){ auto z = x; for(size_t i=0; i < rows; ++i) for(size_t j=0; j < columns; ++j) z.at(i).at(j) += y.at(i).at(j); return z; Matrix Template output. TestMatrixVariables 42 0 16 16 sizeof m : 60 sizeof(matrix<int, 5, 3>): 60 1 2 3 4 5 6 7 8 9 17

10 11 12 13 14 15 0 1 2 10 11 12 20 21 22 30 31 32 40 41 42 0 10 20 100 110 120 200 210 220 300 310 320 400 410 420 TestMatrixFunctions a : 11 12 13 21 22 23 31 32 33 41 42 43 51 52 53 b : 11 12 13 21 22 23 31 32 33 41 42 43 51 52 53 a*10 : 110 120 130 210 220 230 310 320 330 410 420 430 510 520 530 c=a+b: 121 132 143 231 242 253 341 352 363 451 462 473 561 572 583 a+b : 121 132 143 231 242 253 341 352 363 451 462 473 561 572 583 18

5.4. Matrix Template Module MatrixTemplateModule.cpp. /* Modularization, headers, include guards, type template, * Arrays-of-Arrays, matrixes, template functions, * range-for, size_t, sizeof, auto, intializazation, const, parameters (in-out-inout), type inference. * 201304-201608 * José María Sola */ int main(){ void TestMatrixVariables(); TestMatrixVariables(); void TestMatrixFunctions(); TestMatrixFunctions(); #include "Matrix.h" // Matrix. Include interface, public part. #include <iostream> // cout using std::cout; void TestMatrixVariables(){ cout << func << '\n'; Matrix<int, 5, 3> x; // Declaración, sin incialización. x.at(3).at(2) = 42; // Set/Write. cout << x.at(3).at(2) << '\n'; // Get/Read. cout << x.at(0).at(0) << '\t' // Muestra el primer elemento, << x.at(4).at(2) << '\t' // y el último de forma particular << x.at(x.size()-1).at(x.at(x.size()- 1).size()- 1) << '\n'; // y genéricamente. Matrix<int, 5, 3> m = {{ {{ 1, 2, 3, {{ 4, 5, 6, {{ 7, 8, 9, {{10, 11, 12, // Declaración e inicialización. 19

{{13, 14, 15 ; // Un array es un struct con un data member, por eso las dobles llaves. // El tamaño de un arreglo es igual al tamaño del tipo de sus elementos por la cantidad de elementos del arreglo. cout << "sizeof m : " << sizeof m << '\n' << "sizeof(matrix<int, 5, 3>): " << sizeof(matrix<int, 5, 3> ) << '\n'; // Iterar arreglo para get con for (for general). for(size_t i=0; i < 5; ++i){ // size_t. for(size_t j=0; j < 3; ++j) cout << m.at(i).at(j) << '\t'; // Iterar arreglo para set con for. for(size_t i=0, n=m.size(); i < n; ++i) // n. for(size_t j=0, n=m.at(i).size(); j < n; ++j) m.at(i).at(j) = i*10+j; // Iterar arreglo para get con range-for (for-auto). for(auto row : m){ // auto. for(auto e : row) // Iterar arreglo para set con range-for. for(auto& row : m) // auto& for(auto& e : row) e *= 10; for(auto row : m){ for(auto e : row) void TestMatrixFunctions(){ cout << func << '\n'; // Imprime el nombre de la función. Matrix<int, 5, 3> a = {{ 20

{{11,12,13, {{21,22,23, {{31,32,33, {{41,42,43, {{51,52,53 ; cout << "a :\n"; PrintMatrix(a); auto b = a; cout << "b :\n"; PrintMatrix(b); MultiplyMatrix(a,10); cout << "a*10 :\n"; PrintMatrix(a); auto c = AddMatrix(a,b); // Variable auxiliar. cout << "c=a+b:\n"; PrintMatrix(c); cout << "a+b :\n"; PrintMatrix(AddMatrix(a,b)); // Composición. #include "Matrix.cpp" // Include definitions, private part of the implementation. Because there's no separate compilation for templates, this strategy is used, copied from The C++ Programming Language, 4th Edition, Chapter 23. Matrix Template Moudule output. TestMatrixVariables 42 0 16 16 sizeof m : 60 sizeof(matrix<int, 5, 3>): 60 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 1 2 10 11 12 20 21 22 30 31 32 40 41 42 0 10 20 100 110 120 21

200 210 220 300 310 320 400 410 420 TestMatrixFunctions a : 11 12 13 21 22 23 31 32 33 41 42 43 51 52 53 b : 11 12 13 21 22 23 31 32 33 41 42 43 51 52 53 a*10 : 110 120 130 210 220 230 310 320 330 410 420 430 510 520 530 c=a+b: 121 132 143 231 242 253 341 352 363 451 462 473 561 572 583 a+b : 121 132 143 231 242 253 341 352 363 451 462 473 561 572 583 Matrix.h. /* Matrix.h * Interface. Module implemantation public part. Template declarations. * 201304-201608 * José María Sola */ #ifndef MATRIX_H_INLCUDED #define MATRIX_H_INLCUDED 22

#include <array> // array #include <cstddef> // size_t using Matrix = std::array< // array std::array< // of arrays T, // of T columns // of columns elements >, rows // of rows elements >; void PrintMatrix(const Matrix<T, rows, columns>& x); // in. void MultiplyMatrix(Matrix<T, rows, columns>& x, int k); // inout. Matrix<T, rows, columns> // return. AddMatrix( const Matrix<T, rows, columns>& x, const Matrix<T, rows, columns>& y ); #endif Matrix.cpp. /* Matrix.cpp * Module implemantation private part. Template function definitios. * 201304-201608 * José María Sola */ #include "Matrix.h" // Include to check definitions match declarations. #include <iostream> // cout void PrintMatrix(const Matrix<T, rows, columns>& x){ for(auto row : x){ for(auto e : row) 23

std:: std:: void MultiplyMatrix(Matrix<T, rows, columns>& x, int k){ for(auto& row : x) for(auto& e : row) e *= k; Matrix<T, rows, columns> AddMatrix( const Matrix<T, rows, columns>& x, const Matrix<T, rows, columns>& y ){ auto z = x; for(size_t i=0; i < rows; ++i) for(size_t j=0; j < columns; ++j) z.at(i).at(j) += y.at(i).at(j); return z; 24