0% found this document useful (0 votes)
51 views13 pages

CS-323 Programming Fundamentals 4 (3-2) CS-323 Programming Fundamentals 4 (3-2)

The document discusses functions and arrays in C programming. It covers passing arrays into functions, calling arrays by reference, and two-dimensional arrays or matrices. It provides examples of declaring and initializing 2D arrays, passing 2D arrays to functions, and multiplying two matrices. It concludes with an assignment to write a program to multiply two matrices.

Uploaded by

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

CS-323 Programming Fundamentals 4 (3-2) CS-323 Programming Fundamentals 4 (3-2)

The document discusses functions and arrays in C programming. It covers passing arrays into functions, calling arrays by reference, and two-dimensional arrays or matrices. It provides examples of declaring and initializing 2D arrays, passing 2D arrays to functions, and multiplying two matrices. It concludes with an assignment to write a program to multiply two matrices.

Uploaded by

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

CS-323

CS-323 Programming
Programming Fundamentals
Fundamentals
4(3-2)
4(3-2)
Lecture#
Lecture#16
16
Last
Last Lecture
Lecture
• Arrays

Programming Fundamentals 2
Today’s
Today’s Lecture
Lecture
• Functions and Arrays

• Two Dimensional Arrays (Matrix)

Programming Fundamentals 3
Functions
Functions and
and Arrays
Arrays
• Sending Arrays into Another Functions
– Name of the array

– Size of the array

Declaration
char name [ 100 ] ;

Function Call
reverse ( name , 100 ) ;

Programming Fundamentals 4
Functions
Functions and
and Arrays…
Arrays…
Prototype
void reverse ( char [ ] , int ) ;

Definition
void reverse ( char characters [ ] , int arraySize)
{
reverse the character string;
}

Programming Fundamentals 5
Functions
Functions and
and Arrays…
Arrays…
main ( )

Char name [100];

int arraySize = 100;

cin >> name ;

reverse ( name, arraySize ) ;

cout << name ;

Programming Fundamentals 6
Functions
Functions and
and Arrays…
Arrays…
• Call by Reference
– In case of arrays , call by reference is default

Address Operator (&)

Pointer Operator (*)

Programming Fundamentals 7
Functions
Functions and
and Arrays…
Arrays…
void funcName ( int [ ] , int ) ; Conclusion
main ( )
•Whenever a variable is passed, it
{
is called passed by value
int numbers [ 100 ] ;
funcName ( numbers , 100) ; •Whenever you pass an array to
for ( int i = 0 ; i < 100 ; i ++) function, it is called by reference
cout << numbers [ i ] ;
}
void funcName (int x [ ] , int arraySize)
{
int i ;
for ( i = 0 ; i < arraySize ; i ++)
x[i]=i;
}

Programming Fundamentals 8
Two
Two Dimensional
Dimensional Array
Array (Matrix)
(Matrix)
Columns
• Two Dimensional Array
– Consists of Rows and Columns Rows
– For example; int matrix [ 2 ] [ 3 ] ;

int maxRows = 2;
int maxCols = 3 ;
int matrix [ 2] [ 3 ];
int row , col ;
for ( row = 0 ; row < maxRows ; row ++ )
{
for ( col = 0 ; col < maxCols ; col ++ )
{
cout << “Please enter value of ” << row << “ ” << col;
cin >> matrix [ row ] [ col ] ;
}
}

Your Task: Display the elements of 2D array.


Programming Fundamentals 9
Two
Two Dimensional
Dimensional Array
Array (Matrix)
(Matrix)
• Passing two dimensional arrays to function
#include <iostream> void sum(int a[][3],int b[][3], int R, int C)
#include <conio.h> {
#include <stdio.h> int S[R][C];
#include <conio.h> for (int r=0;r<R;r++)
#include<iomanip> {
using namespace std; for (int c=0;c<C;c++)
void sum(int [][3],int[][3], int, int); S[r][c]=a[r][c]+b[r][c];
int main() }
{
int A[2][3]={{1,2,3},{4,5,6}}; for (int r=0;r<R;r++)
int B[2][3]={{1,2,3},{3,4,5}}; {
for (int c=0;c<C;c++)
sum(A,B,2,3); cout<<setw(5)<<S[r][c];
getch(); cout << endl;
return 0; }
} }

Programming Fundamentals 10
Three
Three Dimensional
Dimensional Array
Array

int array3D [ ] [ ] [ ] ;

Programming Fundamentals 11
Programming
Programming Assignment
Assignment
1. Write a program to multiply two matrices A and B.

Note: For multiplication of two matrices (A x B),

No. of Columns of matrix A = No. of Rows of matrix B

Programming Fundamentals 12
THANK
THANK YOU
YOU

Programming Fundamentals 13

You might also like