After CT1 Pps
After CT1 Pps
After CT1 Pps
Kattankulathur
SCHOOLF OF COMPUTING
21CSS101J – PROGRAMMING FOR PROBLEM SOLVING
Unit : II
Session title: Introduction to Arrays -One Dimensional (1D) Array
Declaration and initialization - Accessing, Indexing and operations
with 1D Arrays -Array Programs – 1D
Session Outcome:
By the end of this session, learners will be able to:
i)Write programs using various forms of 1D dimensional array, Declaration and initialization,
Accessing, indexing and operation
ii)Demonstrate the differences and appropriate use cases for indexing and operation with 1D array
Declaration OF AN ARRAY
• Size of array has to be specified
• The array informs the compiler to allocate and reserve the specified memory location.
21CSS101J PROGRAMMING FOR PROBLEM SOLVING
07-08-2024
- UNIT II
One Dimensional (1D):
Types of arrays
• Linear arrays
The datatype can be any valid C data type, the arrayName is the name of the array, and the arraySize
is an integer constant.
Link : https://tinyurl.com/4nbvkf94
Default initialization: If not explicitly initialized, array may contain garbage values
21CSS101J PROGRAMMING FOR PROBLEM SOLVING
07-08-2024
- UNIT II
Accessing Element In 1D Arrays
• The element are accessed by specifying the array name and the index value within the square
brackets.
• Array indexing starts from 0 and end with size -1.
• Try access array element out of the range, the compiler with not show any error message, rather,
it will return some garbage value
Syntax
Link:
<arr_name>[index]; https://tinyurl.com/4waa3sxb
Example
int nums[5] = {0, 1, 2, 3, 4};
printf("%d", nums[0]); //Array element at index 0 is printed
printf("%d", nums[-1]); //Garbage value will be printed
int main() {
…
// Accessing elements of the array
printf(" Value of arr[%d]: %d\n", i, arr[i]);
}
int main() {
int arr[5]; // Declare a 1D array of size 5
int out = 0;
int i;
Guess
printf("Enter 5 integers:\n"); // Prompt the user to enter 5 integers
output… }
out += arr[i];
int main() {
// Declare and initialize a 1-D array of size 10
int arr[10] = {34, 7, 1, 32, 5, 62, 78, 8, 45, 12}; // Example values
int i;
int largest, smallest; // Variables to hold the largest and smallest values
Guess
printf("%d ", arr[i]); elements:\n");
}
for (i = 0; i < 10; i++) {
// Initialize largest and smallest
printf("%d ", arr[i]);
}
the
largest = arr[0];
smallest = arr[0];
printf("\n"); // New line for better readability
// Find the largest and smallest elements return 0; // End of the program
for (i = 1; i < 10; i++) { }
output…
if (arr[i] > largest) {
largest = arr[i];
}
if (arr[i] < smallest) {
smallest = arr[i];
}
}
Guess
printf("Element %d: ", i + 1);
int main() {
scanf("%d", &arr[i]); // Read user input into the array int arr[10];
} int evenSum = 0, oddSum = 0;
the
int evenProduct = 1, oddProduct = 1;
// Calculate the sum and product of odd and even elements int i;
for (i = 0; i < 10; i++) {
if (arr[i] % 2 == 0) { // Check if the element is even
output…
evenSum += arr[i];
evenProduct *= arr[i];
} else { // If the element is odd
oddSum += arr[i];
oddProduct *= arr[i];
}
}
sum
printf("\nSum of even elements: %d\n", evenSum);
printf("Product of even elements: %d\n", evenProduct);
printf("Sum of odd elements: %d\n", oddSum);
printf("Product of odd elements: %d\n", oddProduct);
int main() {
// Declare and initialize a 1-D array of size 10
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // You can change these values as needed
int i;
the }
printf("%d ", arr[i]);
reverse }
printf("%d ", arr[i]);
Unit : II
Session title: Two dimensional (2D) arrays in C
Session Outcome:
By the end of this session, learners will be able to:
i)Write programs using various forms of initializing and accessing 2D arrays and 2D pointers.
ii)Demonstrate appropriate use cases for 2D arrays.
Using 2d arrays, you can store so much data at one moment, which can be passed at
any number of functions whenever required.
11/12/2024 21CSS101J PROGRAMMING FOR PROBLEM SOLVING - UNIT II
Example
• A class consists of 4 students, and the class has to publish the result of all those
students. You need a table to store all those four students' names, subjects' names,
and marks. For that, it requires storing all information in a tabular form
comprising rows and columns. A row contains the name of subjects, and columns
contain the name of the students. That class consists of four subjects, namely
English, Science, Mathematics, and Hindi, and the names of the students are first,
second, third, fourth.
In the above example, the name of the 2d array is multi_dim consisting of 2 rows and
three columns of integer data types.
11/12/2024 21CSS101J PROGRAMMING FOR PROBLEM SOLVING - UNIT II
2D Array Conceptual Memory
Representation
• Example: The abc array with dimension of 5*4
The array has first subscript value as 5 and second subscript value as 4.
So the array abc[5][4] can have 5*4 = 20 elements.
11/12/2024 21CSS101J PROGRAMMING FOR PROBLEM SOLVING - UNIT II
Actual Memory representation of array
Method 2
int multi_dim[4][3]={{10,20,30},{40,50,60},{70,80,90},{100,110,120}};
Example 2: https://tinyurl.com/3ecs3jmb
For example, to declare a 2D array of integers with 3 rows and 5 columns, you would write:
int matrix[3][5];
11/12/2024 21CSS101J PROGRAMMING FOR PROBLEM SOLVING - UNIT II
Accessing Elements in a 2D Array Using
Pointers
Direct vs Indirect Element Access
• Direct element access involves using the array subscript notation (array[i][j]) to
directly access an element.
• Indirect access, on the other hand, involves calculating the address of the element
using pointer arithmetic and then dereferencing it.
• Example: Accessing the element at the first row and second column of a 2D array
named matrix can be done directly with matrix[0][1] or indirectly with *(*(matrix
+ 0) + 1).
Guess the
for(j=0;j<3;j++)
output…
printf("%d",[i][j]);
}
return 0;
}
• Used to access and manipulate the variable's data stored at that location.
data-type *p-var-name;
data-type *p-var-name;
int *p; /* pointer to an integer */
With the address of another variable using the address of (&) operator.
data-type *p-var-name;
p-var-name = &var-name;
POINTER Initialization
Example:
int x = 10;
ptr = &x;
Dereferencing a pointer
using the * operator
int main()
{
int i;
int a[5] = {1, 2, 3, 4, 5};
int *p = a; // (same as &a[0])
11/12/2024
Pointer to an array
#include <stdio.h>
int main()
{
int i;
int a[5] = {1, 2, 3, 4, 5};
int *p = a; // (same as &a[0])
11/12/2024
Pointer to an array
#include <stdio.h>
int main()
{
int i;
int a[5] = {1, 2, 3, 4, 5};
int *p = a; // (same as &a[0])
11/12/2024
Pointer to an array
#include <stdio.h>
int main()
{
int i;
int a[5] = {1, 2, 3, 4, 5};
int *p = a; // (same as &a[0])
11/12/2024
Pointer to an array
#include <stdio.h>
int main()
{
int i;
int a[5] = {1, 2, 3, 4, 5};
int *p = a; // (same as &a[0])
11/12/2024
Pointer to an array
#include <stdio.h>
int main()
{
int i;
int a[5] = {1, 2, 3, 4, 5};
int *p = a; // (same as &a[0])
11/12/2024
Pointer to an array
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50}; // Initialize an array
int *ptr = arr; // Point to the first element of the array
int length = sizeof(arr) / sizeof(arr[0]); // Calculate the number of elements
printf("\n");
return 0;
}
11/12/2024
Pointer to an array
#include <stdio.h>
11/12/2024
Pointer to an Array
#include <stdio.h>
// Read n integers into the array using the pointer
int main() { for (i = 0; i < n; i++) {
int *a; // Pointer to int printf("Enter element %d: ", i + 1);
int b[10] = {0}; // Array of 10 integers, initialized to 0 scanf("%d", a++);
int n; // Variable to hold the number of elements to read }
int i = 0; // Loop variable
// Reset pointer 'a' to point to the beginning of the array
// Point 'a' to the beginning of array 'b' a = b;
a = b;
// Print the elements of the array using the pointer
printf("The elements entered are:\n");
// Read the number of elements to input
for (i = 0; i < n; i++) {
printf("Enter the number of elements (max 10): ");
printf("%d\n", *a++);
scanf("%d", &n); }
// Ensure that n does not exceed the size of the array return 0; // Indicate successful completion
}
if (n > 10) {
printf("Please enter a number less than or equal to 10.\n");
}
11/12/2024
Array Memory Allocation
int arr[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} };
11/12/2024
Pointer to Multidimensional Array
#include <stdio.h>
// Print the elements of the 2D array using the pointer
printf("The elements of the 3x3 matrix are:\n");
int main() {
for (i = 0; i < r; i++) {
int *a; // Pointer to int
for (j = 0; j < c; j++) {
int b[4][4]={0}; // 2D array of integers
printf("%d\t", *a++); // Print the current element and increment the
int r = 3, c = 3; // Number of rows and columns pointer
int i, j; // Loop variables }
printf("\n"); // New line after each row
// Read values into the 2D array }
printf("Enter %d elements for a 3x3 matrix:\n", r * c);
for (i = 0; i < r; i++) { return 0; // Indicate successful completion
for (j = 0; j < c; j++) { }
scanf("%d", &b[i][j]);
}
}
11/12/2024
Pointer to Multidimensional Array
a B[0] 1 2 3 0
B[1] 4 5 6 0
B[2] 7 8 9 0
B[3] 0 0 0 0
Output
1 2 3
0 4 5
6 0 7
11/12/2024