Unit Three Update
Unit Three Update
FUNDAMENTALS OF PROGRAMMING
Content
• Headers and library functions
• Macros.
• Array: Array representation
• Operations on array elements
• Using arrays
• Multidimensional arrays
• Strings, operations on strings
• Structures & Unions
• Declaration and usage of structures and Unions.
Headers and Library Functions
• Headers in C are files that contain declarations of functions, data
types, and macros.
• They provide interfaces to use functionalities defined in libraries.
• Library functions are predefined functions provided by C libraries that
can be used to perform various tasks.
• For example, stdio.h is a header file that contains declarations for
standard input/output functions like printf() and scanf().
Example
• #include <stdio.h>
• int main() {
• printf("Hello, world!\n");
• return 0;
•}
Macros
• They are defined using the #define directive and are typically used for
• int main() {
• double radius = 5.0;
• printf("Area of the circle: %f\n", PI * SQUARE(radius));
• return 0;
•}
Array
• int main() {
• int arr[5] = {1, 2, 3, 4, 5};
• printf("First element: %d\n", arr[0]);
• return 0;
•}
Length of Array
int arr[5];
int arr[5+5];
int arr[5*4];
int arr[-6];
int x;
int arr[x = 21/7];
SPECIFYING THE LENGTH OF ARRAY USING MACRO IS AN EXCELLENT PRACTICE.
#define N 10
int arr[N];
Example without macro
#include <stdio.h>
int main(){
int arr[10], i;
for(i = 0; i < 10; i++){
printf(“Enter the value for index %d: “, i);
scanf(“%d” , &arr[i]);
}
printf(“\n Array elements are as follows: \n”);
for(i = 0 ; i < 10; i++){
printf(“%d “, arr[i]);
}
return 0;
}
Example with macro
#include <stdio.h>
#define N 10
int main(){
int arr[N], i;
for(i = 0; i < N; i++){
printf(“Enter the value for index %d: “, i);
scanf(“%d” , &arr[i]);
}
printf(“\n Array elements are as follows: \n”);
for(i = 0 ; i < N; i++){
printf(“%d “, arr[i]);
}
return 0;
}
Initializing an array
If elements are lesser than length
We want:
1 in position 0
4 in position 5
5 in position 6
int arr[10] = { [0] = 1 , [5] = 4, [6] = 5};
• #include <stdio.h>
• int main() {
• // Array with designated initialization
• int arr[5] = {[1] = 10, [3] = 30};
• // Printing the elements of the array
• for (int i = 0; i < 5; i++) {
• printf("arr[%d] = %d\n", i, arr[i]);
• }
• return 0;
• }
Operations on Array Elements
techniques.
Example
• #include <stdio.h>
• int main() {
• int arr[5] = {1, 2, 3, 4, 5};
• int sum = 0;
• for (int i = 0; i < 5; i++) {
• sum += arr[i];
• }
• printf("Sum of array elements: %d\n", sum);
• return 0;
• }
Practice Examples
• Write a program that finds the maximum element in an array of
integers.
• Multidimensional arrays are arrays with more than one dimension. They can be thought
of as arrays of arrays.
Syntax for two-dimensional array
Visualizing two dimensional array
Combining
Size of an array
Initializing two dimensional array
Better approach
How to access elements in 2D array
How to print 2 D array
Practice Examples
• Write a program to find the transpose of a given 2D array.
#include <stdio.h>
#define ROWS 3
#define COLS 3
int main() {
int matrix[ROWS][COLS] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int transpose[COLS][ROWS];
// Finding transpose of the matrix
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
transpose[j][i] = matrix[i][j];
}
}
// Printing the transpose matrix
printf("Transpose of the matrix:\n");
for (int i = 0; i < COLS; i++) {
for (int j = 0; j < ROWS; j++) {
printf("%d\t", transpose[i][j]);
}
printf("\n");
}
return 0;
}
Second Practice
• Write a program to find the largest element in a 2D array and print its
position (row and column).
• #include <stdio.h>
• #define ROWS 3
• #define COLS 3
• int main() {
• int matrix[ROWS][COLS] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
• int max = matrix[0][0];
• int maxRow, maxCol;
• // Finding the largest element and its position
• for (int i = 0; i < ROWS; i++) {
• for (int j = 0; j < COLS; j++) {
• if (matrix[i][j] > max) {
• max = matrix[i][j];
• maxRow = i;
• maxCol = j;
• }
• }
• }
• // Printing the largest element and its position
• printf("Largest element in the matrix: %d\n", max);
• printf("Position: Row %d, Column %d\n", maxRow + 1, maxCol + 1);
• return 0;
• }
Third Practice
• Write a program to search for a specific element in a 2D array and
print its position (row and column) if found.
#include <stdio.h>
#include <stdbool.h>
#define ROWS 3
#define COLS 3
int main() {
int matrix[ROWS][COLS] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int searchElement = 5;
bool found = false;
int foundRow, foundCol;
// Searching for the element
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (matrix[i][j] == searchElement) {
found = true;
foundRow = i;
foundCol = j;
break;
}}
if (found) {
break; } }
// Printing the search result
if (found) {
printf("Element %d found at position: Row %d, Column %d\n", searchElement, foundRow + 1, foundCol +
1);
} else {
printf("Element %d not found in the matrix.\n", searchElement);
} return 0;
Syntax for three dimensional
array
data_type array_name[size1][size2][size3];
int cube[2][3][4] = { {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}, {{13, 14, 15, 16},
{17, 18, 19, 20}, {21, 22, 23, 24}}};
Example
• #include <stdio.h>
• int main() {
• // Declaration and initialization of a three-dimensional array
• int cube[2][3][4] = {
• {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}},
• {{13, 14, 15, 16}, {17, 18, 19, 20}, {21, 22, 23, 24}}
• };
• // Accessing and printing elements of the three-dimensional array
• printf("Elements of the three-dimensional array:\n");
• for (int i = 0; i < 2; i++) {
• for (int j = 0; j < 3; j++) {
• for (int k = 0; k < 4; k++) {
• printf("%d ", cube[i][j][k]);
• }
• printf("\n");
• }
• printf("\n");
• }
• return 0;
• }
Strings, Operations on Strings
• Like:
“Hello everybody”
%s placeholder to print string
How string literals store in memory
Example
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "World";
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
return 0;
}
Point to be noted
String literal cannot be modified. It causes undefined behavior.
String literal and character constant are not the same.
Always pass string literal to printf function
Structures & Unions
• Structures and unions are user-defined data types that allow storing