Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

C Programming Week7

The document discusses multi-dimensional arrays, specifically two-dimensional arrays, and their operations including declaration, initialization, and matrix multiplication. It also covers input handling in C programming, including the use of scanf and getchar functions, and introduces the concept of Hailstone numbers through the Collatz problem. Additionally, it provides sample code for reading and writing matrices and insists on proper input validation.

Uploaded by

ABHISHEK GOUTAM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

C Programming Week7

The document discusses multi-dimensional arrays, specifically two-dimensional arrays, and their operations including declaration, initialization, and matrix multiplication. It also covers input handling in C programming, including the use of scanf and getchar functions, and introduces the concept of Hailstone numbers through the Collatz problem. Additionally, it provides sample code for reading and writing matrices and insists on proper input validation.

Uploaded by

ABHISHEK GOUTAM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

14/10/17

Multi-Dimensional Arrays
• Arrays with two or more dimensions can be
defined
A[4][3]
CS1100 0 1 2 0 1
B[2][4][3]
2 0 1 2
Introduction to Programming 0
0

Matrix Operations 1 1

Madhu Mutyam 2 2
Department of Computer Science and Engineering
Indian Institute of Technology Madras 3
3

Course Material – SD, SB, PSK, NSN, DK, TAG – CS&E, IIT M 1 0 1
SD, PSK, NSN, DK, TAG – CS&E, IIT M 2

Two Dimensional Arrays Matrix Operations


• Declaration: int A[4][3] : 4 rows and • An m-by-n matrix M: m rows and n columns
3 columns, 4×3 array • Rows: 1, 2, … , m and Columns: 1, 2, … , n
• Elements: A[i][j] - element in row i A[4][3] • M(i, j): element in ith row, jth col., 1≤i≤m, 1≤j≤n
and column j of array A 0 1 2
• Array indexes in C language start with 0
• Rows/columns numbered from 0 0
• Use (m+1)×(n+1) array and ignore cells (0,i), (j,0)
• Storage: row-major ordering 1 • Programs can use natural convention – easier to
– elements of row 0, elements of row 1, understand
etc 2
• Functions: matRead(a,int,int), matWrite(a,int,int),
• Initialization: 3 matAdd(a,b,c,int,int), matMult(a,b,c,int,int,int);
int B[2][3]={{4,5,6},{0,3,5}};
SD, PSK, NSN, DK, TAG – CS&E, IIT M 3 SD, PSK, NSN, DK, TAG – CS&E, IIT M 4

Using Matrix Operations Reading and Writing a Matrix


main( ){ void matRead(int mat[ ][11], int rows, int cols){
for (int i = 1; i <= rows; i++) For the compiler to figure
int a[11][11], b[11][11], c[11][11]; /*max size: 10 by 10 */ for (int j = 1; j <= cols; j++) out the address of mat[i][j],
int aRows, aCols, bRows, bCols, cRows, cCols; scanf("%d", &mat[i][j]); the first dimension value is
Address of the (0,0) } not necessary. (Why?)
scanf("%d%d", &aRows, &aCols);
element of the array
matRead(a, aRows, aCols); void matWrite(int mat[ ][11], int rows, int cols){
scanf("%d%d", &bRows, &bCols); Remember
for (int i = 1; i <= rows; i++){
bRows=aCols
matRead(b, bRows, bCols); for (int j = 1; j <= cols; j++) /* print a row */
printf ("%d ", mat[i][j]); /* notice missing \n */
matMult(a, b, c, aRows, aCols, bCols);
printf ("\n"); /* print a newline at the end a row */
cRows = aRows; cCols = bCols; }
matWrite(c, cRows, cCols); }
5 6
}
SD, PSK, NSN, DK, TAG – CS&E, IIT M SD, PSK, NSN, DK, TAG – CS&E, IIT M

1
14/10/17

Matrix Multiplication Matrix Multiplication

void matMult(int mat1[ ][11], int mat2[ ][11],


N
Multiply two numbers
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
N program. It could have been done in this
Sum of N products function as well – probably a better idea.
SD, PSK, NSN, DK, TAG – CS&E, IIT M 7 SD, PSK, NSN, DK, TAG – CS&E, IIT M 8

scanf and getchar Input Buffer


• getchar( ) reads and returns one character • Your input line is first stored in a buffer
• scanf – formatted input, stores in variable • If you are reading a number with scanf (%d) and
– scanf returns an integer = number of inputs it enter 1235ZZZ, scanf will read 1235 into the
managed to convert successfully variable and leave ZZZ in the buffer
• The next read statement will get ZZZ and may
printf ("Input 2 numbers: "); ignore the actual input!
if (scanf ("%d%d", &i, &j) == 2) • One may need to write a statement to clear the
printf ("You entered %d and %d\n", i, j); buffer…
else printf ("You failed to enter two numbers\n"); while (getchar( ) != '\n');
This reads and ignores input till the end of line
from <http://cprogramming.com>
SD, PSK, NSN, DK, TAG – CS&E, IIT M 9 SD, PSK, NSN, DK, TAG – CS&E, IIT M 10

Program to Insist on One Number Only Experiments with Numbers


#include <stdio.h> • The Collatz problem asks if iterating
int main(void){
int temp; exit if one number ½ αn-1 for αn-1 even
printf ("Input your number: "); αn =
3αn-1 + 1 for αn-1 odd
while (scanf("%d", &temp) != 1)
{
clear buffer before always returns to 1 for positive α. The members
while (getchar( ) != '\n'); reading again
printf ("Try again: "); of the sequence produced by the Collatz problem
} are sometimes known as hailstone numbers.
printf ("You entered %d\n", temp);
return(0);
} From Wolfram Mathworld
http://mathworld.wolfram.com/CollatzProblem.html
SD, PSK, NSN, DK, TAG – CS&E, IIT M 11 SD, PSK, NSN, DK, TAG – CS&E, IIT M 12

2
14/10/17

Mathematical Recreations
Hailstone Numbers http://users.swing.be/TGMSoft/hailstone.htm
• 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 ….
Exercise : Write a program to accept an input and count the number of
• 909, 2726, 1364, 682, 341, 1024, 512, 256, 128, 64, iterations needed to get to 1, and the highest number reached. Generate a table
32, 16, 8, 4, 2, 1, 4, 2, 1… 210 of results…
SD, PSK, NSN, DK, TAG – CS&E, IIT M 13 SD, PSK, NSN, DK, TAG – CS&E, IIT M 14

You might also like