ICS 2024 Sem1 Array
ICS 2024 Sem1 Array
}
Array
What if we try to store more
Elements than 4?
Int main()
In GCC, warning with garbage.
{
Output may change based on
int a[4] = {10, 21, 4 , 0, 17}; // declaring and initializing an
Compilers.
array
C does not give error, so be careful.
Array bounds are not checked.
a[2] = 18;
}
Tell me the output
Int main()
{
float a[4] = {10.3, 21, 4 , 0}; // declaring and initializing an array 10.300000
a[7] = 18; 21.000000
4.000000
printf ("%f\n", a[0]); 0.000000
printf ("%f\n", a[1]); Segmentation fault
printf ("%f\n", a[2]);
printf ("%f\n", a[3]);
}
Segmentation Fault
• A Segmentation Fault in C, also known as a segfault, is an error that
occurs when a program tries to access memory that it is not allowed
to access. In C programming language, a Segmentation Fault in C
occurs when a program attempts to read or write to a memory
location that has not been allocated to it.
2D array
Sum of matrices 1
2
Sum of matrices 1
Sum
Sum of matrices 1
Sum
2
Sum of matrices 1
Sum
2 4
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
Sum of matrices printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");
for (i = 0; i < r; ++i)
Input by the user: for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
• No of Rows
scanf("%d", &a[i][j]);
• No of Cols }
• Enter each element in 1st Matrix printf("Enter elements of 2nd matrix:\n");
• Enter each element in 2nd Matrix for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
// adding two matrices
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}
// printing the result
printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n"); }}}
Practice Questions
• count negative elements in array
•
#define MAX_SIZE 100 // Maximum array size
int main()
Practice Questions {
int arr[MAX_SIZE]; // Declares array of size 100
int i, size, count = 0;
• In C programming, #define is a preprocessor directive that is // Defining parameterized macros with expression
used to define macros. The macros are the identifiers defined #define CIRCLE_AREA(r) (3.14 * r * r)
by #define which are replaced by their value before compilation #define SQUARE_AREA(s) (s * s)
• #define MACRO_NAME value int main()
• #define MACRO_NAME (expression within brackets) {
• #define MACRO_NAME(ARG1, ARG2,..) (expression within int radius = 21;
brackets) int side = 5;
int area;
// Using macros to calculate areas by passing argument
#include <stdio.h> area = CIRCLE_AREA(radius);
#define PI 3.14159265359 printf("Area of Circle of radius %d: %d \n", radius,
int main() area);
{
int radius = 21; area = SQUARE_AREA(side);
int area; printf("Area of square of side %d: %d", side, area);
area = PI * radius * radius;
printf("Area of Circle of radius %d: %d", radius, area); return 0;
return 0; }
}
C program to insert an element in array
• Input array elements: 10, 20, 30, 40, 50
• Input element to insert: 25
• Input position where to insert: 3
• Elements of array are: 10, 20, 25, 30, 40, 50
/**
* C program to insert an element in array at specified position /* If position of element is not valid */
*/ if(pos > size+1 || pos <= 0)
{
#include <stdio.h> printf("Invalid position! Please enter position between 1 to %d", size);
#define MAX_SIZE 100 }
else
int main() {
{ /* Make room for new array element by shifting to right */
int arr[MAX_SIZE]; for(i=size; i>=pos; i--)
int i, size, num, pos; {
arr[i] = arr[i-1];
/* Input size of the array */ }
printf("Enter size of the array : ");
scanf("%d", &size); /* Insert new element at given position and increment size */
arr[pos-1] = num;
/* Input elements in array */ size++;
printf("Enter elements in array : ");
for(i=0; i<size; i++) /* Print array after insert operation */
{ printf("Array elements after insertion : ");
scanf("%d", &arr[i]); for(i=0; i<size; i++)
} {
printf("%d\t", arr[i]);
/* Input new element and position to insert */ }
printf("Enter element to insert : "); }
scanf("%d", &num);
printf("Enter the element position : "); return 0;
scanf("%d", &pos); }
/**
* C program to insert an element in array at specified position /* If position of element is not valid */
*/ if(pos > size+1 || pos <= 0)
{
#include <stdio.h> printf("Invalid position! Please enter position between 1 to %d", size);
#define MAX_SIZE 100 }
else
int main() {
{ /* Make room for new array element by shifting to right */
int arr[MAX_SIZE]; for(i=size; i>=pos; i--)
int i, size, num, pos; {
arr[i] = arr[i-1];
/* Input size of the array */ }
printf("Enter size of the array : ");
scanf("%d", &size); /* Insert new element at given position and increment size */
What is the limitation here? arr[pos-1] = num;
/* Input elements in array */ size++;
printf("Enter elements in array : ");
for(i=0; i<size; i++) /* Print array after insert operation */
{ printf("Array elements after insertion : ");
scanf("%d", &arr[i]); for(i=0; i<size; i++)
} {
printf("%d\t", arr[i]);
/* Input new element and position to insert */ }
printf("Enter element to insert : "); }
scanf("%d", &num);
printf("Enter the element position : "); return 0;
scanf("%d", &pos); }
For a matrix, compute transpose
int main() { // computing the transpose
int a[10][10], transpose[10][10], r, c; for (int i = 0; i < r; ++i)
printf("Enter rows and columns: "); for (int j = 0; j < c; ++j) {
scanf("%d %d", &r, &c); transpose[j][i] = a[i][j];
}
// asssigning elements to the matrix
printf("\nEnter matrix elements:\n"); // printing the transpose
for (int i = 0; i < r; ++i) printf("\nTranspose of the matrix:\n");
for (int j = 0; j < c; ++j) { for (int i = 0; i < c; ++i)
printf("Enter element a%d%d: ", i + 1, j + 1); for (int j = 0; j < r; ++j) {
scanf("%d", &a[i][j]); printf("%d ", transpose[i][j]);
} if (j == r - 1)
printf("\n");
// printing the matrix a[][] }
printf("\nEntered matrix: \n"); return 0;
for (int i = 0; i < r; ++i) }
for (int j = 0; j < c; ++j) {
printf("%d ", a[i][j]);
if (j == c - 1)
printf("\n");
}
Do it yourself Program
• Write a C program to read elements in a matrix and interchange elements of
primary(major) diagonal with secondary(minor) diagonal.
int main() {
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int loop, largest;
largest = array[0];
return 0;
}
•
Sorting arrays
How you would sort n-elements?
• Different methods
• We will discuss one here
This is called bubble sort
Key idea
1.for (i = 0; i < n - 1; i++) {
2. for (j = 0; j < n - i - 1; j++) {
3. if (arr[j] > arr[j + 1]) {
4. int temp = arr[j];
5. arr[j] = arr[j + 1];
6. arr[j + 1] = temp;
7. }
8. }
9. }
10.
3D Array
Multidimensional arrays
Character array
int main()
{
char a[5];
int i;
a[0] = 'H';
a[1] = 'E';
a[2] = 'L';
a[3] = 'L';
a[4] = 'O';
39
No string data type is directly available
int main()
{
char a[5];
int i;
a[0] = 'H';
a[1] = 'E';
a[2] = 'L';
a[3] = 'L';
a[4] = 'O';
special null character, denoted by '\0' a[5] = '\0'; //This should be explicitly inserted to treat it like a string
for (i = 0; i < 5; ++i)
{
printf("%c", a[i]);
}
printf("\n%c",a[i]);
printf("\n%s", a);
return 0;
}
40
int main()
{
char a[6]="hello"; // make it 6 for safe side.
char b[] = "Welcome IITJ students"; // This is also possible
printf("%s\n", a);
printf("%s\n", b);
return 0;
}
41
Output?
int main()
{
char b[] = “This is ICS course";
int count = 0,i = 0;
count = strlen(b);
printf("The length is : %d\n", count);
for (i = 0; i < count; ++i)
{
a[i] = b[i];
}
a[i] = '\0';
strcpy(c, b); // (des, src);
if(strcmp(a, c) == 0)
printf("Both same\n");
return 0;
}