1
Arrays:
One Dimensional Array
Two Dimensional Array
Dr THANGA MARIAPPAN L,
Department of Smart Computing,
School of Computer Science Engineering and Information Systems
(SCORE)
Vellore Institute of Technology, Vellore
Recap
•What is an array?
•Types of array
•Single Dimensional array
•Declaration
•Initialization
•Accessing 1D Array
•Example
•Two Dimensional array
•Declaration
•Initialization
•Accessing 2D Array
•Example
Array
Find Maximum Element in Array
You are given an array of N integers. Your task is to find the maximum element in the array.
Input
The first line contains an integer N (1 ≤ N ≤ 1000) — the size of the array.
The second line contains N integers separated by spaces, A1, A2, ..., AN (1 ≤ Ai ≤ 1000) —
the elements of the array.
Output
•Print a single integer — the maximum element in the array.
Array
Find Maximum Element in Array
Example
Input: Input: Input:
5 4 3
31254 8372 123
Output: 8 Output: 3
Output: 5
#include <stdio.h>
// Find the maximum element
int main() for (int i = 1; i < n; i++) {
{ if (arr[i] > max) {
max = arr[i]; } }
int n;
// Output the maximum element
// Read the size of the array printf("%d\n", max);
scanf("%d", &n); int arr[n]; return 0; }
// Read the array elements
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]); }
// Initialize max to the first element of the array
int max = arr[0];
Array
Count of Positive Numbers in an Array
You are given an array of N integers. Your task is to count how many of the numbers are
positive.
Input
The first line contains an integer N (1 ≤ N ≤ 1000) — the size of the array.
The second line contains N integers separated by spaces, A1, A2, ..., AN (−1000 ≤ Ai ≤
1000) — the elements of the array.
Output
Print a single integer — the number of positive elements in the array.
Array
Input: 5
1 -2 3 -4 5
Output: 3
Input: 3
234
Input: 4 Output: 3
-1 -2 -3 -4
Output: 0
#include <stdio.h>
// Increase count if the element is positive
int main() if (arr[i] > 0) {
{ count++; }
}
int n, count = 0; /
// Output the count of positive numbers
/ Read the size of the array printf("%d\n", count); return 0; }
scanf("%d", &n);
int arr[n];
// Read the array elements
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
Array
Sum of Diagonal Elements in a Matrix
You are given a square matrix of size N x N. Your task is to calculate the sum of the
elements on both the main diagonal and the secondary diagonal.
Input
The first line contains an integer N (1 ≤ N ≤ 100) — the size of the matrix.
The next N lines contain N integers separated by spaces, representing the elements of the
matrix.
Output
Print a single integer — the sum of the elements on both diagonals.
Array
Input: 3 Input: 2
1 2 3 1 2
4 5 6
3 4
7 8 9
Output: 25 Output: 10
In the first example, the sum of the main diagonal elements (1, 5, 9) is 1 + 5 + 9 = 15.
The sum of the secondary diagonal elements (3, 5, 7) is 3 + 5 + 7 = 15. Since the
middle element (5) is common to both diagonals, it is counted only once. So, the total
sum is 15 + 10 = 25.
#include <stdio.h>
// Add elements from the secondary diagonal
int main() { if (i + j == n - 1 && i != j) {
int n, sum = 0; sum += matrix[i][j]; }
}}
// Read the size of the matrix
// Output the sum of diagonal elements
scanf("%d", &n); printf("%d\n", sum);
int matrix[n][n]; return 0; }
// Read the matrix elements
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
// Add elements from the main diagonal
if (i == j) {
sum += matrix[i][j]; }
Array
Row-Wise Maximum in a Matrix
You are given a matrix of size N x M. Your task is to find the maximum element in each
row and print the result.
Input
The first line contains two integers N and M (1 ≤ N, M ≤ 100) — the number of rows and
columns of the matrix.
The next N lines contain M integers separated by spaces, representing the elements of the
matrix
Output
Print N integers — the maximum element from each row.
Array
Input: 3 3 Input: 2 4
1 2 3 1 4 2 3
4 5 6 8 6 5 7
7 8 9 Output: 4 8
Output: 3 6 9
#include <stdio.h>
int main() {
int n, m;
// Read the number of rows and columns
scanf("%d %d", &n, &m);
// Find and print the maximum element from each row
int matrix[n][m];
for (int i = 0; i < n; i++) {
// Read the matrix elements int max = matrix[i][0];
for (int i = 0; i < n; i++) { // Initialize max as the first element in the row
for (int j = 1; j < m; j++) {
for (int j = 0; j < m; j++) {
if (matrix[i][j] > max) {
scanf("%d", &matrix[i][j]); } } max = matrix[i][j]; } }
printf("%d ", max); // Print the max element of the row
}
return 0; }
Array
Search for an Element in a Matrix
You are given a matrix of size N x M and an integer X. Your task is to determine whether
the element X exists in the matrix. If it does, print "Found", otherwise print "Not Found".
Input
The first line contains two integers N and M (1 ≤ N, M ≤ 100) — the number of rows and
columns in the matrix.
The next N lines contain M integers separated by spaces, representing the elements of the
matrix.
The last line contains a single integer X, the element to be searched.
Output
Print "Found" if the element exists in the matrix, otherwise print "Not Found".
Array
Input: 3 3 Input: 2 2
1 2 3 1 2
4 5 6 3 4
10
7 8 9 Output: Not Found
5
Output: Found
#include <stdio.h> for (int i = 0; i < n; i++) {
int main() { for (int j = 0; j < m; j++) {
if (matrix[i][j] == x) {
int n, m, x, found = 0; found = 1;
// Read the number of rows and columns break; // Exit inner loop if element is found
scanf("%d %d", &n, &m); }
}
int matrix[n][m]; if (found) {
// Read the matrix elements break; // Exit outer loop if element is found }
}
for (int i = 0; i < n; i++) {
// Output the result
for (int j = 0; j < m; j++) { if (found)
scanf("%d", &matrix[i][j]); } } {
printf("Found\n");
// Read the element to search for
}
scanf("%d", &x); else {
// Search for the element using break printf("Not Found\n"); } return 0; }
Summary
•What is an array?
•Types of array
•Single Dimensional array
•Declaration
•Initialization
•Accessing 1D Array
•Example
•Two Dimensional array
•Declaration
•Initialization
•Accessing 2D Array
•Example
THANK YOU