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;
}

        
        
    
Step 1:

Include the stdio.h header file.

  • Allows the use of input/output functions like scanf and printf.

Step 2:

Declare an integer variable n.

  • This will hold the number of floating-point elements the user wants to enter.

Step 3:

Prompt the user for input and read the value of n.

  • scanf stores the user's input in n.

Step 4:

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.

Step 5:

Take n floating-point inputs from the user.

  • A for loop runs from 0 to n-1, reading each element into arr[i].

Step 6:

Display the array elements.

  • Another for loop prints each element with two decimal precision using %.2f.

Step 7:

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;
}
  
        
    
Step 1:

Include the <stdio.h> header file.

  • Enables use of printf and scanf functions.

Step 2:

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.

Step 3:

Read the array size and elements from the user.

  • A for loop is used to take size inputs into the array.

Step 4:

Read the element and position to insert.

  • Inserts the new element at the specified 1-based position.

Step 5:

Check for valid position.

  • If position is out of bounds, display error and exit.

Step 6:

Shift elements to the right to make space.

  • Use a reverse for loop from size to position to shift elements one step right.

Step 7:

Insert the new element.

  • Assign array[position - 1] = element.

  • Increment size to reflect the new array length.

Step 8:

Print the updated array.

  • Loop through the updated array and print all elements.

Step 9:

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;
}     
        
    
Step 1:

Include the <stdio.h> header file.

  • Allows usage of printf and scanf for input/output.

Step 2:

Declare an integer array array[100] and an integer size.

Step 3:

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.

Step 5:

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.

Step 6:

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;
}
     
        
    
Step 1:

Include the <stdio.h> header file.

  • Provides input/output functions like printf and scanf.

Step 2:

Declare matrix variables: a, b, and sum as 2D arrays of size 50×50, and integers m and n for matrix dimensions.

Step 3:

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.

Step 5:

Read elements of the second matrix b similarly.

Step 6:

Compute the element-wise sum of matrices a and b and store it in matrix sum.

Step 7:

Print the resulting sum matrix using nested for loops.

Step 8:

End the program with return 0;.

Comments

Popular Posts