Day 1 of DSA in C
Write a program to create an array of floating point elements and display the element and the number of input must be user given.
#include <stdio.h>
int main() {
int n;
printf("Enter the number of floating point elements: ");
scanf("%d", &n);
float arr[n];
printf("Enter %d float values:\n", n);
for (int i = 0; i < n; i++) {
printf("Element %d: ", i + 1);
scanf("%f", &arr[i]);
}
printf("You entered:\n");
for (int i = 0; i < n; i++) {
printf("Element %d: %.2f\n", i + 1, arr[i]);
}
return 0;
}
Include the stdio.h
header file.
Allows the use of input/output functions like
scanf
andprintf
.
Declare an integer variable n
.
This will hold the number of floating-point elements the user wants to enter.
Prompt the user for input and read the value of n
.
scanf
stores the user's input inn
.
Declare a float array arr[n]
using Variable Length Array (VLA).
The size of the array is determined at runtime using the user's input.
Take n
floating-point inputs from the user.
A
for
loop runs from 0 ton-1
, reading each element intoarr[i]
.
Display the array elements.
Another
for
loop prints each element with two decimal precision using%.2f
.
End the program with return 0;
.
Write a program to insert an element in an array at a particular position.
#include <stdio.h>
int main() {
int array[100], size, position, element;
printf("Enter number of elements: ");
scanf("%d", &size);
printf("Enter %d elements:\n", size);
for (int i = 0; i < size; i++){
scanf("%d", &array[i]);
}
printf("Enter element and position: ");
scanf("%d %d", &element, &position);
if (position < 1 || position > size + 1) {
printf("Invalid position.\n");
return 1;
}
for (int i = size; i >= position; i--){
array[i] = array[i - 1];
}
array[position - 1] = element;
size++;
printf("Updated array:\n");
for (int i = 0; i < size; i++)
printf("%d ", array[i]);
return 0;
}
Include the <stdio.h>
header file.
Enables use of
printf
andscanf
functions.
Declare variables:
array[100]
: to store up to 100 integers.size
: number of existing elements in the array.position
: the index at which the new element will be inserted (1-based).element
: the new value to insert.
Read the array size and elements from the user.
A
for
loop is used to takesize
inputs into thearray
.
Read the element
and position
to insert.
Inserts the new element at the specified 1-based position.
Check for valid position.
If
position
is out of bounds, display error and exit.
Shift elements to the right to make space.
Use a reverse
for
loop fromsize
toposition
to shift elements one step right.
Insert the new element.
Assign
array[position - 1] = element
.Increment
size
to reflect the new array length.
Print the updated array.
Loop through the updated array and print all elements.
End the program with return 0;
.
Write a program to reverse the elements of an array without using a separate array.
#include <stdio.h>
int main() {
int array[100], size;
printf("Enter number of elements: ");
scanf("%d", &size);
printf("Enter %d elements:\n", size);
for (int i = 0; i < size; i++)
scanf("%d", &array[i]);
for (int i = 0; i < size / 2; i++)
array[i] = array[i] + array[size - 1 - i] - (array[size - 1 - i] = array[i]);
printf("Reversed array:\n");
for (int i = 0; i < size; i++)
printf("%d ", array[i]);
return 0;
}
Include the <stdio.h>
header file.
Allows usage of
printf
andscanf
for input/output.
Declare an integer array array[100]
and an integer size
.
Prompt and read the number of elements from the user.
Step 4:Use a for
loop to read size
number of integers into the array.
Reverse the array in-place using a swap trick.
Iterate only till
size/2
because we swap symmetric elements.Swap using the expression:
array[i] = array[i] + array[size - 1 - i] - (array[size - 1 - i] = array[i]);
This performs the swap without using a temporary variable.
Print the reversed array using a loop.
Step 7:End the program with return 0;
.
Write a program to add two m*n matrices and display the results.
#include <stdio.h>
int main() {
int m, n;
int a[50][50], b[50][50], sum[50][50];
printf("Enter number of rows and columns (m n): ");
scanf("%d %d", &m, &n);
printf("Enter elements of first matrix (%d × %d):\n", m, n);
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
scanf("%d", &a[i][j]);
printf("Enter elements of second matrix (%d × %d):\n", m, n);
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
scanf("%d", &b[i][j]);
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
sum[i][j] = a[i][j] + b[i][j];
printf("Sum of matrices:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
printf("%d ", sum[i][j]);
printf("\n");
}
return 0;
}
Include the <stdio.h>
header file.
Provides input/output functions like
printf
andscanf
.
Declare matrix variables: a
, b
, and sum
as 2D arrays of size 50×50, and integers m
and n
for matrix dimensions.
Prompt the user to enter the number of rows and columns of the matrices.
Step 4:Read elements of the first matrix a
using nested for
loops.
Read elements of the second matrix b
similarly.
Compute the element-wise sum of matrices a
and b
and store it in matrix sum
.
Print the resulting sum
matrix using nested for
loops.
End the program with return 0;
.
Comments