Lecture_Week_7.1
Lecture_Week_7.1
and Programming
• Example:
• int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
Access the Elements of a 2D Array
• To access an element of a two-dimensional array, you must specify the
index number of both the row and column.
• This statement accesses the value of the element in the first row
(0) and third column (2) of the matrix array.
• Example
• int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
• The following example will change the value of the element in the first row
(0) and first column (0):
• Example
• int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
matrix[0][0] = 9;
printing
{ for (j=0;j<3;j++) {
printf("Enter a[%d][%d]: ",i,j);
elements at } }
scanf("%d",&arr[i][j]);
Use in
Storing
2D arrays for graphics,
collections of
matrices. tables, and
data.
buffers.
Array with pointer will be discussed later
Functions
Content
Function
Arguments, Mid-sem Paper Macro & Inline
Function with Discussion Functions
Arrays
Function basics & Motivations
float F2C(float f) {
float c= (f – 32.0) * (5.0 / 9.0);
return c;}
• The impact is even greater when the
operation has multiple statements.
return 0;
}
Function
that finds
max(m,n)
Where is
the
function
defined?
Function Arguments
int main() {
int num = 10;
changeValue(&num);
printf("%d", num); // Output: 20
}
#include <stdio.h>
// Function to swap two numbers using call by reference
void swapByReference(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
Example of }
printf("Inside swap: a = %d, b = %d\n", *a, *b);
Reference
int x = 10, y = 20;
printf("Before swap: x = %d, y = %d\n", x, y);
swapByReference(&x, &y); // Call by reference
printf("After swap: x = %d, y = %d\n", x, y);
return 0;}
Output: Before swap: x = 10, y = 20
Inside swap: a = 20, b = 10
After swap: x = 20, y = 10
▪ Example: void PrintGreetings() {
printf(“\nWelcome to SNU”);
printf(“\nSNU is an Institute of
Eminence\n”);
A Simple }