Array Practice
Part-1: Basics of Array
Declaring and initializing 1D arrays
Accessing array elements
Basic operations (traverse, update, input/output)
1. Input and Print N elements: Take N as input and read N integers into
an array. Print them using a loop.
#include <stdio.h>
int main() {
int arr[100], n;
// Read number of elements
printf("Enter number of elements: ");
scanf("%d", &n);
// Read elements into the array
printf("Enter %d elements:\n", n);
// Write code to Read elements into the array
// TODO: Print all elements
printf("Array elements are:\n");
// Write code to PRINT elements of the array
return 0;
}
2. Count Even and Odd: Given an array of integers, count how many
are even and how many are odd.
#include <stdio.h>
int main() {
int arr[100], n, even = 0, odd = 0;
printf("Enter number of elements: ");
// TODO: Read value of n
printf("Enter elements:\n");
// TODO: Read array elements and count even and odd
numbers
printf("Even: %d, Odd: %d\n", even, odd);
return 0;
}
3. Largest and Smallest in an array: Given an array of integers, find
largest and smallest in array.
#include <stdio.h>
int main() {
int arr[100], n;
int max, min;
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]);
}
max = min = arr[0]; // Initialize both to first element
for(int i = 1; i < n; i++) {
//TODO: Write code to find maximum and
minimum
}
printf("Largest element: %d\n", max);
printf("Smallest element: %d\n", min);
return 0;
}
4. Insert element at given position: Given an array and a position, insert
a new element at that position. (Shift elements to make space).
#include <stdio.h>
int main() {
int arr[100], n, pos, value;
// TODO: Read number of elements
printf("Enter %d elements:\n", n);
// TODO: Read array elements
printf("Enter position to insert (0 to %d): ", n);
scanf("%d", &pos);
printf("Enter value to insert: ");
scanf("%d", &value);
// Shift elements to right
// Write code to place the new element at
correct position
printf("Array after insertion:\n");
// Print Array Elements
printf("\n");
return 0;
}
/*
Test Case 1:
Input: 5
12345
2
99
Output:
Array after insertion:
1 2 99 3 4 5
Test Case 2:
Input:
3
10 20 30
0
5
Output:
Array after insertion:
5 10 20 30
*/
Part 2: Arrays and Functions
Passing arrays to functions
Receiving arrays in functions
Array as function parameters
Limitation: Array size must be passed
5. Function to Calculate Average. Write a function that takes an array
and its size and returns the average.
#include <stdio.h>
// Function to calculate average of array elements
float calculateAverage(int arr[], int size) {
int sum = 0;
// TODO: Write a loop to add all elements of the
array to 'sum'
// HINT: Use a for loop, and add arr[i] to sum
// TODO: After calculating sum, calculate average
and return it
// HINT: Average = (float)sum / size;
//Example of Typecasting
return 0; // Replace this with the correct return
//statement
}
int main() {
int arr[100], n;
// Step 1: Read the size of the array
printf("Enter the number of elements: ");
scanf("%d", &n);
// Step 2: Read the array elements
printf("Enter %d elements:\n", n);
for(int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Step 3: Call the function and print the average
float avg = calculateAverage(arr, n);
printf("Average = %.2f\n", avg);
return 0;
}
6. Reverse Array in Function. Pass an array to a function and print it in
reverse order.
#include <stdio.h>
// Function to print array in reverse order
void printReverse(int arr[], int size) {
// TODO: Use a loop to print elements from last index
to 0
// Inside loop: print arr[i]
}
int main() {
int arr[100], n;
// Step 1: Read number of elements
printf("Enter number of elements: ");
scanf("%d", &n);
// Step 2: Read array elements
printf("Enter %d elements:\n", n);
// Step 3: Call the function to print reverse
return 0;
}
Part 3: Search in Array
Linear Search
Bubble Sort (basic sorting logic)
Sorting in ascending/descending order
7. Linear Search. Search for an element in the array and return its index
or -1 if not found.
#include <stdio.h>
// Function to perform linear search
int linearSearch(int arr[], int size, int key) {
// TODO: Use a loop to compare each element with the key
// If the loop ends without finding, return -1
return -1; // Replace this with correct return in loop
}
int main() {
int arr[100], n, key;
// Step 1: Read number of elements
printf("Enter the number of elements: ");
scanf("%d", &n);
// Step 2: Read array elements
printf("Enter %d elements:\n", n);
// Step 3: Read the key to search
printf("Enter the element to search: ");
scanf("%d", &key);
// Step 4: Call linearSearch and store result
int index = linearSearch(arr, n, key);
// Step 5: Print result
if(index == -1) {
printf("Element not found.\n");
} else {
printf("Element found at index %d.\n", index);
}
return 0;
}
8. Binary Search. Efficiently search for an element in a sorted array.
Binary Search Working:
1. Start with a sorted array (ascending order).
2. Set two values:
o low = 0
o high = n - 1
3. Repeat while low <= high:
o Calculate middle index: mid = (low + high) / 2
o If arr[mid] == key, you found it!
o If key < arr[mid], search in the left half: high = mid - 1
o If key > arr[mid], search in the right half: low = mid + 1
4. If the loop ends, the element is not present.
#include <stdio.h>
//Write Function definition
{
// TODO: Initialize low and high
// TODO: Use loop to perform binary search
// TODO: Compare key with arr[mid]
// TODO: Return index if found, else -1
}
int main() {
int arr[100], n, key;
printf("Enter size: ");
scanf("%d", &n);
printf("Enter %d sorted elements:\n", n);
for(int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter element to search: ");
scanf("%d", &key);
int result = binarySearch(arr, n, key);
if(result == -1)
printf("Element not found.\n");
else
printf("Element found at index %d\n", result);
return 0;
}
9. Count Element Frequency: Given an array of integers and a specific
number, write a program to count how many times that number
appears in the array.
#include <stdio.h>
// Function to count occurrences of a number in array
int countOccurrences(int arr[], int size, int target) {
int count = 0;
// TODO: Write a loop that iterates from all the
elements
// Inside the loop, check if arr[i] == target
// If true, increment count
return count; // This should be the final count
}
int main() {
int arr[100], n, target;
// Step 1: Read number of elements
printf("Enter the number of elements: ");
scanf("%d", &n);
// Step 2: Read array elements
printf("Enter %d elements:\n", n);
// Step 3: Read the number to count
printf("Enter the number to count: ");
// scan the number into a variable
// Step 4: Call the function and display result
int count = //Call to function
printf("The number %d appears %d time(s) in the array.\n", target,
count);
return 0;
}
10. Array Left Rotation by One: Write a program that takes an array of
integers and its size. Rotate the array elements one position to the
left. The first element becomes the last element. Print the modified
array.
#include <stdio.h>
// Function to perform one left rotation on the array
void leftRotateByOne(int arr[], int size) {
// TODO: Step 1: Initialize local variables if needed
// TODO: Step 2- Shift all elements one position to the left
// TODO: Step 3: Place the first element at the last position
}
int main() {
int arr[100], n;
// Step 1: Input the size
printf("Enter the number of elements: ");
//scan the variable
// Step 2: Input the array elements
printf("Enter %d elements:\n", n);
// Step 3: Call the rotation function
// Step 4: Print the modified array
printf("Array after left rotation:\n");
for(int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
11. Write a function void greet() that prints:
Hello! This is your Xth visit.
Where X is the number of times the function has been called. Do not use
any Global variable.
12. Login System with Limited Attempts
🔹 Objective: Design and implement a basic login system in C that allows
a user to enter the correct password within a limited number of attempts
(maximum 3). If the password is entered incorrectly three times, the
system should lock the user out and not allow further attempts.
🔹 Requirements:
1. Create a function named login() to handle the password input and validation.
2. Use a static variable to track the number of attempts made by the user.
3. The correct password is "admin123".
4. If the password is correct, display:
Login successful!
5. If the password is incorrect, display the number of remaining attempts:
Incorrect password. Attempts left: X
6. After 3 failed attempts, display:
Account locked. Too many attempts.
and do not allow further inputs.
🔹 Constraints:
The password should be read using scanf.
The login() function can be called multiple times (e.g., from main()).
Do not use any global variables.