๐Ÿš€ DSA with C – Day 2: Arrays & Linear Search

 

๐ŸŽฏ Goal:

  • Understand arrays in C.

  • Learn how to use loops with arrays.

  • Implement Linear Search algorithm.

  • Practice multiple array-based programs.


๐Ÿง  Theory: What is an Array?

  • An array is a collection of elements stored in contiguous memory.

  • All elements must be of the same type.

  • Indexing starts from 0 in C.

๐Ÿ”ง Declaration and Initialization

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

๐Ÿ‘จ‍๐Ÿ’ป Basic Program: Input & Output in Arrays

#include <stdio.h>
int main() { int arr[5]; printf("Enter 5 numbers:\n"); for (int i = 0; i < 5; i++) { scanf("%d", &arr[i]); } printf("You entered:\n"); for (int i = 0; i < 5; i++) { printf("%d ", arr[i]); } return 0; }

๐Ÿ” Linear Search in C

➕ Problem: Given an array and a number x, find if x exists in the array.

✅ Code:


#include <stdio.h> int main() { int arr[100], n, x, found = 0; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter %d elements:\n", n); for (int i = 0; i < n; i++) scanf("%d", &arr[i]); printf("Enter element to search: "); scanf("%d", &x); for (int i = 0; i < n; i++) { if (arr[i] == x) { printf("Element found at index %d\n", i); found = 1; break; } } if (!found) printf("Element not found\n"); return 0; }

๐Ÿง  Time Complexity:

  • Best case: O(1)

  • Worst case: O(n)


๐Ÿงช Practice Problems:

  1. Find the maximum and minimum element in an array.

  2. Count how many times a given number appears in the array.

  3. Reverse an array.


๐Ÿ“˜ Homework for Day 2:

  • Write a program to copy one array to another.

  • Write a program to merge two arrays.

  • Try solving one problem from LeetCode Easy using arrays (optional but forward-thinking!).


๐Ÿ“… Day 2 Summary

  • ✅ Learned arrays

  • ✅ Implemented Linear Search

  • ✅ Practiced I/O and traversal


๐Ÿงญ Tomorrow: Sorting (Bubble Sort) + Binary Search

Would you like me to create a GitHub repo or planner to track your DSA progress daily?

Comments

Popular posts from this blog

Raster scan Vs Vector Scan

Inheritance

unit -1 Introduction of Image processing