BCA SCHOOL
Programming Tutorials • Notes • Practical • Books
LATEST TUTORIALS

Understanding Single Dimensional Array (1D Array) in C | Unit 4 | BKNMU BCA

by BCA School •

BKNMU Junagadh  |  BCA Sem 1  |  Unit 4



Understanding Single Dimensional Array (1D Array) in C

Complete Guide to Linear Data Structures, Memory Allocation, Loop-based Input/Output, and Fundamental Operations

BKNMU Junagadh BCA Sem 1 Unit 4 1D Array Contiguous Memory Loop Processing
In simple words: The basic concept of an array is to store multiple items under one variable name. When we store these items in a single, simple linear row—like a single row of lockers—it is called a Single Dimensional Array (1D Array). In a 1D array, we access each element using only one subscript or index, making it the most fundamental data structure for handling homogeneous data lists. This complete tutorial is tailored specifically for BKNMU BCA Semester 1 (Unit 4).
📖 Core Properties of a 1D Array Structure

A 1D array is defined by four core mathematical and structural behaviors:

Single Subscript

1. One-Dimensional Access

Every element in the list is uniquely identified using only one index number (subscript) inside a single set of brackets arr[index].

Contiguous memory

2. Linear Memory Layout

Elements reside side-by-side in uninterrupted memory addresses, forming a simple linear list (locker row analogy).

sizeof(Type)

3. Byte-Step Access

Moving from index i to i+1 shifts the memory pointer by exactly sizeof(data_type) bytes (e.g., 2 or 4 bytes for int).

for loops

4. Optimized Traversals

Because elements are contiguous and indexed linearly, using simple single for loops is the most efficient method for input and output processing.

1️⃣ Master Table: Operations & Syntax Patterns (1D Array)

A complete reference of syntax, memory flow, and coding loops for fundamental 1D operations:

Operation Type Syntax / Pattern Examples Description & Memory Action
Declaration data_type arr_name[SIZE];
int marks[10];
Compiler reserves SIZE × sizeof(type) contiguous bytes. Holds garbage values.
Initialization int arr[3] = {10, 20, 30};
char vowels[] = {'A', 'E', 'I'};
Compiler sets memory contents during program loading. Contiguous blocks populated sequentially.
Accessing Element x = arr[2];
arr[4] = 99;
CPU fetches/writes data at address (Base_Addr + 2*sizeof(type)) in constant time $O(1)$.
Loop Input (scanf) for(i=0; i<SIZE; i++)
  scanf("%d", &arr[i]);
Sequential user data population. Pointer steps through the list address-by-address.
Loop Output (printf) for(i=0; i<SIZE; i++)
  printf("%d\t", arr[i]);
Sequential value retrieval and display. Pointer steps through the list value-by-value.
ℹ️ Total Memory Byte Calculation (Recap):
  • Formula: Total Bytes = Number of Elements × sizeof(Single Element Type)
  • Example: A float prices[50]; array on GCC (where float is 4 bytes) occupies 50 × 4 = 200 Bytes of contiguous RAM.
💻 Practical Code Example: Array Input, Traversal & Multiplication

The following program demonstrates initializing a 1D array using a loop, traversing it to multiply elements by a factor, and displaying the results:

#include <stdio.h>

int main() {
    // Declaration of 1D array of size 5
    int numbers[5];
    int i, factor;

    // 1. Initialize Array using for loop (populating squares)
    for (i = 0; i < 5; i++) {
        numbers[i] = (i + 1) * (i + 1); // index 0 stores 1, 1 stores 4...
    }

    // Display original array contents
    printf("Original numbers array:\n");
    for (i = 0; i < 5; i++) {
        printf("%d\t", numbers[i]);
    }

    // 2. Operation: Multiply each element by factor
    printf("\n\nEnter multiplication factor: ");
    scanf("%d", &factor);

    for (i = 0; i < 5; i++) {
        numbers[i] = numbers[i] * factor;
    }

    // 3. Traversal Output Loop
    printf("Updated numbers array:\n");
    for (i = 0; i < 5; i++) {
        printf("%d\t", numbers[i]);
    }
    printf("\n");

    return 0;
}
🔄 Step-by-Step Flow: Memory Pointer Stepping
1

Base Address Mapping

Execution begins. The name numbers holds the base address (e.g., 2000). The for loop counter i initializes to 0.

2

Sequential Pointer Advancement

Inside the scanf or assignment loop, the runtime CPU calculates Base_Addr + (i × 2) (for Turbo C) bytes to populating each sequential locker slot.

3

Loop Continuation and Offset Bound

The loop counter i increments, advancing the memory pointer by exactly 2 bytes per step. Loop terminates when i reaches SIZE, boundary logic ensures contiguous slots are used.

💡 Industry Term: Traversal: Traversing an array simply means accessing or visiting every single element in the list exactly once sequentially (usually using a loop) to read, display, or modify contents.
⚠️ Critical Exam Point: Out of Bounds Vulnerability: C does not provide automatic bounds checking. Writing past the declared size (e.g., numbers[5] = 100; when size is 5) is illegal but compile-able; it corrupts neighbor memory slots and causes segmentation faults. Always structure loop conditions as i < SIZE.
📝 Quick Revision — Key Exam Points
  • Structure: A single linear list of items using one subscript `arr[index]`.
  • Data Type: Derived homogeneous type; items must be identical types.
  • Indexing: Zero-based; range is 0 to Size - 1.
  • Memory: Occupies continuous sequential bytes in RAM ( locker row).
  • Loop Dependency: for loops are essential for streamlined 1D array I/O processing.