1D Arrays in C

A 1D Array (One-Dimensional Array) is a collection of elements of the same data type stored in consecutive memory locations. It is used to store multiple values in a single variable name.

Why Use Arrays?

Array Declaration

data_type array_name[size];
Example:
int marks[5];

Array Initialization

int marks[5] = {90, 85, 92, 80, 88};
Or assign later:
marks[0] = 90;
marks[1] = 85;

Practical Example 1 – Print Array Elements

#include <stdio.h>

int main() {

    int a[5] = {10, 20, 30, 40, 50};

    for(int i = 0; i < 5; i++){
        printf("%d ", a[i]);
    }

    return 0;
}

Practical Example 2 – Sum of Array Elements

#include <stdio.h>

int main() {

    int arr[5] = {4, 7, 2, 9, 3};
    int sum = 0;

    for(int i = 0; i < 5; i++){
        sum += arr[i];
    }

    printf("Sum = %d", sum);

    return 0;
}

Practical Example 3 – Largest Element

#include <stdio.h>

int main() {

    int a[6] = {12, 5, 29, 8, 19, 3};
    int max = a[0];

    for(int i = 1; i < 6; i++){
        if(a[i] > max)
            max = a[i];
    }

    printf("Largest = %d", max);

    return 0;
}

Practical Example 4 – Smallest Element

#include <stdio.h>

int main() {

    int arr[6] = {10, 3, 22, 7, 15, 2};
    int min = arr[0];

    for(int i = 1; i < 6; i++){
        if(arr[i] < min)
            min = arr[i];
    }

    printf("Smallest = %d", min);

    return 0;
}

Practical Example 5 – Count Even and Odd

#include <stdio.h>

int main() {

    int arr[7] = {12, 9, 20, 15, 6, 3, 8};
    int even = 0, odd = 0;

    for(int i = 0; i < 7; i++){
        if(arr[i] % 2 == 0)
            even++;
        else
            odd++;
    }

    printf("Even = %d\nOdd = %d", even, odd);

    return 0;
}

Practical Example 6 – Search an Element

#include <stdio.h>

int main() {

    int arr[5] = {5, 10, 15, 20, 25};
    int key = 15, found = 0;

    for(int i = 0; i < 5; i++){
        if(arr[i] == key){
            found = 1;
            break;
        }
    }

    if(found)
        printf("Element found");
    else
        printf("Not found");

    return 0;
}

Practical Example 7 – Reverse the Array

#include <stdio.h>

int main() {

    int arr[5] = {2, 4, 6, 8, 10};

    for(int i = 4; i >= 0; i--){
        printf("%d ", arr[i]);
    }

    return 0;
}

Practical Example 8 – Copy Array Elements

#include <stdio.h>

int main() {

    int a[5] = {7, 14, 21, 28, 35};
    int b[5];

    for(int i = 0; i < 5; i++){
        b[i] = a[i];
    }

    for(int i = 0; i < 5; i++){
        printf("%d ", b[i]);
    }

    return 0;
}

Practical Example 9 – Count Positive and Negative

#include <stdio.h>

int main() {

    int arr[8] = {5, -4, 7, -9, 3, -1, 8, 2};
    int pos = 0, neg = 0;

    for(int i = 0; i < 8; i++){
        if(arr[i] >= 0)
            pos++;
        else
            neg++;
    }

    printf("Positive: %d\nNegative: %d", pos, neg);

    return 0;
}

Practical Example 10 – Calculate Average

#include <stdio.h>

int main() {

    int arr[5] = {50, 60, 70, 80, 90};
    int sum = 0;

    for(int i = 0; i < 5; i++){
        sum += arr[i];
    }

    float avg = sum / 5.0;

    printf("Average = %.2f", avg);

    return 0;
}

Practice Questions

  1. Store and print 10 numbers entered by user.
  2. Find the largest and smallest number in an array.
  3. Reverse an array without using another array.
  4. Count prime numbers in an array.
  5. Search element using linear search.

Practice Task

Write a program to store 15 student marks, and print: ✔ Total ✔ Average ✔ Highest ✔ Lowest ✔ Number of marks above 75