Multi-dimensional arrays in C

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming
Advertisements

One Dimensional Arrays
Arrays. INTRODUCTION TO ARRAYS Just as with loops and conditions, arrays are a common programming construct and an important concept Arrays can be found.
Engineering Problem Solving with C++ An Object Based Approach Chapter 7 Two-Dimensional Arrays and Matrices.
Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers.
EXAMPLE 1 Add and subtract matrices
ECE 103 Engineering Programming Chapter 23 Multi-Dimensional Arrays Herbert G. Mayer, PSU CS Status 6/24/2014 Initial content copied verbatim from ECE.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
1 Example of how C programming is usually done. 2 Three basic techniques Use of files for data Use of separate source code files Use of library functions,
Explain Declaration,Initialization of Array Explain Types of Array One Dimensional,Two Dimensional and Multi Dimensional Array Explain Arrays.
MAHENDRAN. Session Objectives Explain Declaration,Initialization of Array Explain Types of Array One Dimensional,Two Dimensional and Multi Dimensional.
MULTI-DIMENSION ARRAY STRING Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
Department of Computer Science Western Michigan University
Array in C# Array in C# RIHS Arshad Khan
(Numerical Arrays of Multiple Dimensions)
Linear Algebra review (optional)
EKT120 COMPUTER PROGRAMMING
Chapter 7 Matrix Mathematics
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
© 2016 Pearson Education, Ltd. All rights reserved.
EKT120 : Computer Programming
Matrix Operations.
C/C++: type sizes in memory pointers
Dynamic Array Multidimensional Array Matric Operation with Array
EGR 115 Introduction to Computing for Engineers
MULTI-DIMENSIONAL ARRAY
Matrix Operations Monday, August 06, 2018.
Matrix Operations.
Numeric Arrays Numeric Arrays Chapter 4.
Array 9/8/2018.
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Lecture 7 – Arrays (1) PGT 106 : C PROGRAMMING.
Module 2 Arrays and strings – example programs.
Programmazione I a.a. 2017/2018.
Arrays in C.
Arrays C provides the option to the user to combine similar data types into a single entity It followed contiguous.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Chapter 8 Repetition Statements
WarmUp 2-3 on your calculator or on paper..
Multidimensional Arrays
EKT150 : Computer Programming
25. Basic matrix operations
EKT120: Computer Programming
2.2 Introduction to Matrices
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Dr Tripty Singh Arrays.
Lets Play with arrays Singh Tripty
Homework Continue with K&R Chapter 5 Skipping sections for now
Multi-Dimensional Arrays
Arrays.
Arrays C provides the option to the user to combine similar data types into a single entity It followed contiguous.
Linear Algebra review (optional)
What is the dimension of the matrix below?
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Dale Roberts, Lecturer IUPUI
ICS103 Programming in C Lecture 12: Arrays I
EECE.2160 ECE Application Programming
Arrays, Part 1 of 2 Topics Definition of a Data Structure
EECE.2160 ECE Application Programming
ICS103: Programming in C Searching, Sorting, 2D Arrays
Presentation transcript:

Multi-dimensional arrays in C They do not exist (Really!) However, they are simulated using arrays whose elements are themselves arrays Example: float arr[20][10]; This is an array with 20 elements, each of which is an array with 10 elements, each of type float

One-dimensional arrays in C, revisited float arr[10]; declares an array of 10 floats stored contiguously and allocates space for it float arr[]; declares an array of floats - the array is of unknown size

Multi-dimensional arrays in C –what’s allowed float arr[20][10]; This array has 20 elements, each of which is an array with 10 elements, of type float The first 6 elements of the array arr are illustrated = this is 6 * 10, or 60, floats

Multi-dimensional arrays in C –what’s allowed float arr[][10]; OK This is an array with an unknown number of elements, each of which is an array with 10 elements, each of type float The first 6 elements of the array arr are illustrated = this is 6 * 10, or 60, floats

Multi-dimensional arrays in C –what’s allowed float arr[10][]; Not OK This can’t be an array with 10 elements of any type, because we don’t know where the lowest level elements are Cannot tell where array elements begin or end

Multi-dimensional arrays in C The most important thing to remember is the pattern [][], [][][], etc. for indices An element in a 2-D array is accessed by two indices: array_name[i][j] Don’t use commas array_name[i, j] wrong, wrong, wrong, wrong

Multi-dimensional arrays in C There can be 2- dimensional arrays, 3- dimensional arrays, 4- -dimensional arrays, etc. As with 2-dimensional arrays, only the leftmost size limit can be empty int a[][10]; int b[20][10]; float c[20][300]; float d[][2]; double e[][10][1] are all OK float f[10][]; char g[][10][][1] are not

Two-dimensional arrays in C The same patterns apply to multi-dimensional arrays as one-dimensional ones: iteration and accumulation C is a good language for engineering because multi-array elements are stored contiguously Usually, two different indices are needed to access multi-dimensional arrays – we only show 2-D arrays in our examples

Initializing a 2-D array, nested loops #define SIZE 3 #include <stdio.h> int main( int argc, char * argv[]) { int i, j; double m1[SIZE][SIZE]; /* initialize array */ for( i =0 ; i < SIZE; i++) for (j = 0; j < SIZE; j++) m1[i][j] = 2.0 + i;

Printing the array – nested loops /* Echo the input arrays */ printf("The input array is:\n"); for (i = 0; i < SIZE; i++) { for (j = 0; j < SIZE; j++) printf("%6.2lf", m1[i][j]); printf("\n"); } } /* end of program (for now) */

Example – linear algebra package /* Routines for operations on square matrices */ #define SIZE 3 #include <stdio.h> int main( int argc, char * argv[]) { int i, j, k; char choice; double m1[SIZE][SIZE]; double m2[SIZE][SIZE]; double output[SIZE][SIZE]; double scalar;

puts("Welcome to the cheap vector algebra package."); /* initialize array */ for( i =0 ; i < SIZE; i++) for (j = 0; j < SIZE; j++) { m1[i][j] = 2.0 + i; m2[i][j] = 3.0 * j + i; output[i][j] = 0; }

/* Echo the input arrays */ printf("The first input array is:\n"); for (i = 0; i < SIZE; i++) { for (j = 0; j < SIZE; j++) printf("%6.2lf", m1[i][j]); printf("\n"); } printf("\nAnd the second input array is:\n");

for (i = 0; i < SIZE; i++) { for (j = 0; j < SIZE; j++) printf("%6.2lf", m2[i][j]); printf("\n"); } /* Now ask user for input: add, subtract two vectors, multiple by a scalar, computer dot product */

Guess what comes next! puts("Enter a + for addition, a - for subtraction,"); puts("a * for multiplication by another square matrix"); puts("or . for multiplication by a scalar"); scanf("%c", &choice);

switch (choice) { case '+': for ( i = 0; i < SIZE; i++) for (j = 0; j < SIZE; j++) output[i][j] = m1[i][j] + m2[i][j]; break; case '-': output[i][j] = m1[i][j] - m2[i][j];

case '*': for ( i = 0; i < SIZE; i++) for (j = 0; j < SIZE; j++) { output[i][j] = 0; for (k = 0; k < SIZE; k++) output[i][j] += m1[i][k] * m2[k][i]; } break; case '.': puts("Enter the scalar you wish to multiply by"); scanf("%lf", &scalar); output[i][j] = scalar * m1[i][j];

default: puts("Enter your choice again"); scanf("%c", &choice); } /* end switch */ printf("Your selection was %c ", choice); printf(" and the output is:\n"); for (i = 0; i < SIZE; i++) { for (j = 0; j < SIZE; j++) printf("%6.2lf", output[i][j]); printf("\n"); } } /* end main */