0% found this document useful (0 votes)
6 views

2D Array & String

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

2D Array & String

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

2D Arrays and

Strings
By Prof. Vimal Rathod
Assistant Professor,
Department of Information Technology,
Government Engineering College, Modasa
Topics to be covered
2-D Array basics
- searching element
- maximum element
- sum of two matrix
- multiplication of two Matrices
String
- String initialization
- String methods
-Palindrome Check
Arrays
• An array is a collection of elements having same datatype.
• An array is a data structure that stores a collection of elements of the
same data type in contiguous memory locations. It allows for efficient
storage and access of multiple values using a single variable name and
an index.
• Declaring an array: int myNumbers[] = {25, 50, 75, 100};
• Accessing elements like myNumbers[0], this will give the value 25.
• In an array element index starts from 0.
2 Dimensional Array
A 2D array is also known as a matrix (a table of rows and columns).
To create a 2D array of integers, take a look at the following example:
int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };

• The first dimension represents the number of rows [2], while the
second dimension represents the number of columns [3].
2D Array element searching
• To search an element we have to follow the steps below:
1. Start from the first row.
2. For each row:
Go through each element in that row one by one (from left to right).
3. For each element:
• Check if it is equal to the target element you're searching for.
• If it is, return the position (row number and column number).
4. If you reach the end of the array and didn't find the element, say that
the element is not found.
Program to search an element from a 2D array
Addition of Two Matrices
•Start
•Take two matrices of the same size (same number of rows and columns).
•Create a new matrix to store the result.
•For each row (from top to bottom):
•For each column (from left to right):
•Add the corresponding elements from both matrices.
•Store the result in the same position of the result matrix.
•After all elements are added, show the result matrix.
•End
Program of Matrices Addition
Example of Matrices Addition
Multiplication of Two Matrices
1. Start
2. Take two matrices:
1. Matrix A with size m × n
2. Matrix B with size n × p
3. ✅ Note: The number of columns in A must equal the number of rows in B.
4. Create a new matrix C with size m × p to store the result.
5. For each row i in Matrix A (from 0 to m−1):
1. For each column j in Matrix B (from 0 to p−1):
1. Set a variable sum = 0
2. For each element k from 0 to n−1:
1. Multiply the element at A[i][k] with the element at B[k][j]
2. Add the result to sum
3. Store sum in C[i][j]
6. Repeat until all elements in C are calculated.
7. Display the result matrix C.
8. End
Program of Matrices Multiplication
Example of Matrices
Multiplication
Example of Matrices
Multiplication
✅ Step-by-Step: Multiply A × B
We'll calculate each element of the result matrix C = A × B, which will also be 3×3.
🔹 First Row of A × Columns of B
• C[0][0] = (1×9) + (2×6) + (3×3) = 9 + 12 + 9 = 30
• C[0][1] = (1×8) + (2×5) + (3×2) = 8 + 10 + 6 = 24
• C[0][2] = (1×7) + (2×4) + (3×1) = 7 + 8 + 3 = 18
🔹 Second Row of A × Columns of B
• C[1][0] = (4×9) + (5×6) + (6×3) = 36 + 30 + 18 = 84
• C[1][1] = (4×8) + (5×5) + (6×2) = 32 + 25 + 12 = 69
• C[1][2] = (4×7) + (5×4) + (6×1) = 28 + 20 + 6 = 54
🔹 Third Row of A × Columns of B
• C[2][0] = (7×9) + (8×6) + (9×3) = 63 + 48 + 27 = 138
• C[2][1] = (7×8) + (8×5) + (9×2) = 56 + 40 + 18 = 114
• C[2][2] = (7×7) + (8×4) + (9×1) = 49 + 32 + 9 = 90
Example of Matrices
Multiplication
Maximum Element of the
Matrice
1.Start
2.Take a matrix (2D array) as input.
3.Assume the first element of the matrix is the maximum.
4.Go through each row one by one:
• For each row, go through each element:
• If the current element is greater than the current maximum, update the maximum.
5.After checking all elements, the maximum value is the largest number in
the matrix.
6.End
Program to find Maximum number of Matrice
Strings in C
Definition: A string is a sequence of characters belonging to a specific character
set.
• For example, "hello world" is a string containing five characters: "h," "e", "l",
"l", and "o". In C, a string is represented using a character array that ends with
the null character(\0).
• C does not have a String type to easily create string variables. Instead, you
must use the char type and create an array of characters to make a string in C:
char greetings[] = "Hello World!";
OR
char greetings[] = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!', '\0’};
printf("%s", greetings);
Strings in C
• To output the string, you can use the printf() function together with
the format specifier %s to tell C that we are now working with strings:
char greetings[] = "Hello World!";
printf("%s", greetings);
• To use string, you must include the <string.h> header file in your
program.
String Functions
• strlen()
• sizeof()
• strcat()
• strcpy()
• strcmp()
A program using String Functions.
Length of String
• To get the length of a string, you can use the strlen() function.You can
use it like below:
char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
printf("%d", strlen(alphabet));
• In the Strings chapter, we used sizeof to get the size of a string/array.
Note that sizeof and strlen behaves differently, as sizeof also includes
the \0 character when counting:
char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
printf("%d", strlen(alphabet)); // 26
printf("%d", sizeof(alphabet)); // 27
Concatenate Strings
• To concatenate (combine) two strings, you can use the strcat() function:
char str1[20] = "Hello ";
char str2[] = "World!";

// Concatenate str2 to str1 (result is stored in str1)


strcat(str1, str2);

// Print str1
printf("%s", str1);
Copy Strings
• To copy the value of one string to another, you can use the strcpy()
function:
char str1[20] = "Hello World!";
char str2[20];

// Copy str1 to str2


strcpy(str2, str1);

// Print str2
printf("%s", str2);
Compare Strings
• To compare two strings, you can use the strcmp() function.
• It returns 0 if the two strings are equal, otherwise a value that is not 0:
char str1[] = "Hello";
char str2[] = "Hello";
char str3[] = "Hi";

// Compare str1 and str2, and print the result


printf("%d\n", strcmp(str1, str2)); // Returns 0 (the strings are equal)

// Compare str1 and str3, and print the result


printf("%d\n", strcmp(str1, str3)); // Returns -4 (the strings are not
equal)
Whether the given string is a
palindrom of not?
Start
•Take a string as input.
•Set two pointers:
•One at the beginning of the string (let’s call it start)
•One at the end of the string (let’s call it end)
•While start is less than end, do the following:
•Compare the characters at start and end.
•If they are not equal, the string is not a palindrome → Stop.
•If they are equal, move start forward and end backward.
•If all characters matched, then the string is a palindrome.
End
Program to check if the string is palindrom or not.
End of Chapter

Any Questions?

You might also like