0% found this document useful (0 votes)
12 views70 pages

Unit Three Update

Uploaded by

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

Unit Three Update

Uploaded by

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

UNIT THREE

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

• Macros in C are preprocessor directives that define symbolic

constants or perform text replacement in the code.

• They are defined using the #define directive and are typically used for

defining constants or creating simple functions-like constructs.


Example
• #include <stdio.h>
• #define PI 3.14159
• #define SQUARE(x) ((x) * (x))

• int main() {
• double radius = 5.0;
• printf("Area of the circle: %f\n", PI * SQUARE(radius));
• return 0;
•}
Array

• An array in C is a collection of elements of the same data type stored

in contiguous memory locations.

• Elements in an array can be accessed using an index. Arrays are

declared using square brackets [].


Example
• #include <stdio.h>

• int main() {
• int arr[5] = {1, 2, 3, 4, 5};
• printf("First element: %d\n", arr[0]);
• return 0;
•}
Length of Array

The length of an Array can be specified by any positive integer constant


expression.
Like

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

int arr[10] = {1,2,3,5,7,8};

int arr[10] = {1,2,3,5,7,8,0,0,0,0};


Designated • Designated initialization is a feature that
allows to initialize specific elements of an
Initialization array explicitly by specifying their indices
of arrays within braces {}.
int arr[10] = {1,0,0,0,0,4,5,0,0,0};

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

• Operations on array elements include accessing, modifying, and

performing computations on array elements using loops or other

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.

int arr[] = {5, 8, 3, 10, 6};


Solution
• #include <stdio.h>
• int main() {
• int arr[] = {5, 8, 3, 10, 6};
• int size = sizeof(arr) / sizeof(arr[0]);
• int max = arr[0];
• for (int i = 1; i < size; i++) {
• if (arr[i] > max) {
• max = arr[i];
• }
• }
• printf("Maximum element in the array: %d\n", max);
• return 0;
• }
Second Practice
• Write a program that reverses the elements in an array.

int arr[] = {1, 2, 3, 4, 5};


• #include <stdio.h>
• int main() {
• int arr[] = {1, 2, 3, 4, 5};
• int size = sizeof(arr) / sizeof(arr[0]);
• printf("Original array: ");
• for (int i = 0; i < size; i++) {
• printf("%d ", arr[i]);
• }
• printf("\nReversed array: ");
• for (int i = size - 1; i >= 0; i--) {
• printf("%d ", arr[i]);
• }
• printf("\n");
• return 0;
• }
Third Practice
• Write a program that counts the number of even elements in an array.

int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};


Solution
• #include <stdio.h>
• int main() {
• int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
• int size = sizeof(arr) / sizeof(arr[0]);
• int count = 0;
• for (int i = 0; i < size; i++) {
• if (arr[i] % 2 == 0) {
• count++;
• }
• }
• printf("Number of even elements in the array: %d\n", count);
• return 0;
• }
How many elements are there in these arrays?
sizeof() operator
sizeof(name_of_arr)
sizeof(name_of_arr[0]
To find the number of elements in array
Multidimensional Arrays

• 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

Strings are sequence of characters enclosed within double quotation.

• 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

different types of data under a single name.

• Structures allocate memory for each member individually, while

unions allocate memory that is the size of the largest member.


Problem Statement

• I have a garage and want to store


all the information about the
cars which are available in the
garage.
For each parts of the car, we need to create a variable and store them.
Suppose we have 100 cars so for each we need separate variable and it is time consuming
Structure is the best solution for
such problems
• Structure is user defined data type that can be used to group elements of different types into a
single type.
Example
Structure Tag
Structure tag is used to identify a particular kind of structure
Example
• // Create a structure called myStructure
struct myStructure {
int myNum;
char myLetter;
};
int main() {
// Create a structure variable of myStructure called s1
struct myStructure s1;
// Assign values to members of s1
s1.myNum = 13;
s1.myLetter = 'B';
// Print values
printf("My number: %d\n", s1.myNum);
printf("My letter: %c\n", s1.myLetter);
return 0;
}
Union
Union is a user defined data type but unlike structures, union members share same memory location.
Fact about Union
Accessing Members Using Pointers
END UNIT 3

You might also like