CS1100 Computational Engineering

Slides:



Advertisements
Similar presentations
Chapter 7: Arrays In this chapter, you will learn about
Advertisements

An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
1 ICS103 Programming in C Lecture 16: 2-Dimensional Arrays.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
Computer programming Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
CS 161 Introduction to Programming and Problem Solving Chapter 17 Nested Loops Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
Strings C supports strings using one-dimensional character arrays. A string is defined as a null-terminated character array. In C, a null is 0. You must.
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
ARRAYS.
Lesson #5 Repetition and Loops.
Programming application CC213
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Lecture 7 Arrays 1. Concept of arrays Array and pointers
© 2016 Pearson Education, Ltd. All rights reserved.
Lesson #5 Repetition and Loops.
EKT120 : Computer Programming
Quiz 11/15/16 – C functions, arrays and strings
ICS103 Programming in C Lecture 3: Introduction to C (2)
Numeric Arrays Numeric Arrays Chapter 4.
Array 9/8/2018.
Lecture 7 – Arrays (1) PGT 106 : C PROGRAMMING.
Module 2 Arrays and strings – example programs.
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Lecture 9 : Array Acknowledgment : Lecture notes from Ohio Supercomputing Center.
Looping.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Lesson #5 Repetition and Loops.
CNG 140 C Programming (Lecture set 8)
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Chapter 2 - Introduction to C Programming
EKT150 : Computer Programming
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
CS111 Computer Programming
EKT120: Computer Programming
IPC144 Introduction to Programming Using C Week 8 – Lesson 1
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Chapter 7 Arrays PROGRAMMING IN ANSI C.
Chapter 2 - Introduction to C Programming
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
UMBC CMSC 104 – Section 01, Fall 2016
Arrays, Part 1 of 2 Topics Definition of a Data Structure
ECE 103 Engineering Programming Chapter 19 Nested Loops
Introduction to C Programming
Multi-Dimensional Arrays
Chapter 2 - Introduction to C Programming
More Loops Topics Counter-Controlled (Definite) Repetition
Arrays.
ECE 103 Engineering Programming Chapter 18 Iteration
Lesson #5 Repetition and Loops.
Functions Department of Computer Science-BGU יום שישי 26 אפריל 2019.
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
EECE.2160 ECE Application Programming
Arrays, Part 1 of 2 Topics Definition of a Data Structure
EECE.2160 ECE Application Programming
More Loops Topics Counter-Controlled (Definite) Repetition
ICS103: Programming in C Searching, Sorting, 2D Arrays
Presentation transcript:

CS1100 Computational Engineering Matrix Operations N.S. Narayanaswamy

Multi-Dimensional Arrays Arrays with two or more dimensions can be defined 1 2 3 A[4][3] 1 2 3 B[2][4][3]

Two Dimensional Arrays Declaration: int A[4][3] : 4 rows and 3 columns, 43 array Elements: A[i][j] - element in row i and column j of array A Rows/columns numbered from 0 Storage: row-major ordering elements of row 0, elements of row 1, etc Initialization: int B[2][3]={{4,5,6},{0,3,5}}; 1 2 3 A[4][3]

Matrix Operations An m-by-n matrix M: m rows and n columns Rows: 1, 2, … , m and Columns: 1, 2, … , n M(i, j): element in ith row, jth col., 1im, 1jn Array indexes in C language start with 0 Use (m+1)(n+1) array and ignore cells (0,i), (j,0) Programs can use natural convention – easier to understand Functions: matRead(a,int,int), matWrite(a,int,int), matAdd(a,b,c,int,int), matMult(a,b,c,int,int,int);

Address of the (0,0) element of the array Using Matrix Operations main( ){ int a[11][11], b[11][11], c[11][11]; /*max size: 10 by 10 */ int aRows, aCols, bRows, bCols, cRows, cCols; scanf("%d%d", &aRows, &aCols); matRead(a, aRows, aCols); scanf("%d%d", &bRows, &bCols); matRead(b, bRows, bCols); matMult(a, b, c, aRows, aCols, bCols); cRows = aRows; cCols = bCols; matWrite(c, cRows, cCols); } Address of the (0,0) element of the array Remember bRows=aCols

Reading and Writing a Matrix void matRead(int mat[ ][11], int rows, int cols){ for (int i = 1; i <= rows; i++) for (int j = 1; j <= cols; j++) scanf("%d", &mat[i][j]); } void matWrite(int mat[ ][11], int rows, int cols){ for (int i = 1; i <= rows; i++){ for (int j = 1; j <= cols; j++) /* print a row */ printf ("%d ", mat[i][j]); /* notice missing \n */ printf ("\n"); /* print a newline at the end a row */ For the compiler to figure out the address of mat[i][j], the first dimension value is not necessary. (Why?)

Matrix Multiplication Multiply two numbers N Sum of N products

Matrix Multiplication void matMult(int mat1[ ][11], int mat2[ ][11], int mat3[ ][11], int m, int n, int p){ for (int i =1; i <= m; i++) for (int j = 1; j <= p; j++) for (int k = 1; k <= n; k++) mat3[i][j] += mat1[i][k]*mat2[k][j]; } Remember it was initialized to zero in the main program. It could have been done in this function as well – probably a better idea.

from <http://cprogramming.com> scanf and getchar getchar( ) reads and returns one character scanf – formatted input, stores in variable scanf returns an integer = number of inputs it managed to convert successfully printf ("Input 2 numbers: "); if (scanf ("%d%d", &i, &j) == 2) printf ("You entered %d and %d\n", i, j); else printf ("You failed to enter two numbers\n"); from <http://cprogramming.com>

Input Buffer Your input line is first stored in a buffer If you are reading a number with scanf (%d) and enter 1235ZZZ, scanf will read 1235 into the variable and leave ZZZ in the buffer The next read statement will get ZZZ and may ignore the actual input! One may need to write a statement to clear the buffer… while (getchar( ) != '\n'); This reads and ignores input till the end of line

clear buffer before reading again Program to Insist on One Number Only #include <stdio.h> int main(void){ int temp; printf ("Input your number: "); while (scanf("%d", &temp) != 1) { while (getchar( ) != '\n'); printf ("Try again: "); } printf ("You entered %d\n", temp); return(0); exit if one number clear buffer before reading again

Experiments with Numbers The Collatz problem asks if iterating always returns to 1 for positive α. The members of the sequence produced by the Collatz problem are sometimes known as hailstone numbers. From Wolfram Mathworld http://mathworld.wolfram.com/CollatzProblem.html 3αn-1 + 1 for αn-1 odd ½ αn-1 for αn-1 even αn =

A Hailstone Sequence is generated by a simple algorithm: Hailstone Numbers A Hailstone Sequence is generated by a simple algorithm: Start with an integer N. If N is even, the next number in the sequence is N/2. If N is odd, the next number in the sequence is (3*N)+1 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1, 4, 2, 1, ... repeats 12, 6, 3, 10, 5, 16, 8, 4, 2, 1, 4, 2, 1 …. 909, 2726, 1364, 682, 341, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1, 4, 2, 1… 210

Mathematical Recreations http://users.swing.be/TGMSoft/hailstone.htm Exercise : Write a program to accept an input and count the number of iterations needed to get to 1, and the highest number reached. Generate a table of results…