Class Notes
Date: 11.06.2025
ARRAYS
[Link] [Link] is the use of Arrays?
Answer:
To process large amounts of data, we need a data structure known as an [Link] is a linear
data structure where all elements are arranged sequentially. It is a collection of
elements of the same data type stored at contiguous memory locations. Arrays are
essential in C programming because they allow us to store and manage multiple elements of the
same data type using a single variable.
[Link] how a 1D array is declared and initialized with a syntax and example.
Answer:
1D Array Declaration Syntax
In declaration, we specify the name and the size of the 1d array.
data_type array_name[array_size];
2
EX: int marks[10];
1D Array Initialization Syntax
To assign values, we have to initialize an array.
elements_type array_name[array_size] = {value1, value2, ... };
EX: int marks[]={98,97,90};
[Link] a Program to read and print marks of n students.
Answer:
#include<stdio.h>
void main()
int i,n;
printf("enter the number of students");
scanf("%d",&n);
int mark[n];
for(i=0;i<n;i++)
printf("enter %d marks",i+1);
scanf("%d",&mark[i]);
for(i=0;i<n;i++)
{
3
printf("\n marks of %d student is : %d",i+1,mark[i]);
[Link] to add all the elements of the 1D array.
Answer:
#include<stdio.h>
void main()
int n,sum=0;
printf("enter size of array");
scanf("%d",&n);
int a[n];
printf("enter array elements");
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
for(int i=0;i<n;i++)
sum=sum+a[i];
printf("\n sum is %d",sum);
}
4
[Link] for inserting an element at a specific position in an array.
Answer:
#include<stdio.h>
void main()
int pos,n,num,i;
printf("enter the size of array :");
scanf("%d",&n);
int a[n];
printf("enter the elements : ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("enter number to be inserted : ");
scanf("%d",&num);
printf("enter position to insert : ");
scanf("%d",&pos);
for(i=n-1;i>=pos;i--)
a[i+1]=a[i];
a[pos]=num;
n++;
5
printf("\n The array after insertion is : ");
for(i=0;i<=n-1;i++)
printf(" %d \t",a[i]);
[Link] to demonstrate Binary search.
Answer:
#include<stdio.h>
void main()
int i, low, high, mid, n, key, array[100];
printf("Enter number of elements in the array");
scanf("%d",&n);
printf("Enter the array elements");
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to be searched ");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high)
{
6
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key)
printf("\n %d found at location %d", key, mid+1);
break;
else
high = mid - 1;
mid = (low + high)/2;
if(low > high)
printf("Not found! %d isn't present in the list", key);
[Link] to demonstrate Linear search.
Answer:
#include<stdio.h>
void main()
int a[100], key, i, n;
7
printf("Enter number of elements in array: ");
scanf("%d", &n);
printf("\n Enter elements of the array: ");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
printf("\n Enter a number to search: \n");
scanf("%d", &key);
for (i = 0; i < n; i++)
if (a[i] == key)
printf("%d is present at location %d \n", key, i+1);
break;
if (i == n)
printf("%d isn't present in the array\n",key);
8. Develop a C Program to find the Transpose a Matrix.
Answer:
#include <stdio.h>
8
int main()
int m, n, i, j, matrix[10][10],transpose[10][10];
printf("Enter rows and columns :\n");
scanf("%d%d", &m, &n);
printf("Enter elements of the matrix\n");
for (i= 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &matrix[i][j]);
for (i = 0;i < m;i++)
for (j = 0; j < n; j++)
transpose[j][i] = matrix[i][j];
printf("Transpose of the matrix:\n");
for (i = 0; i< n; i++)
for (j = 0; j < m; j++)
printf("%d\t", transpose[i][j]);
printf("\n");
return 0;
}
9
○