1D Arrays
1D Arrays
What is an Array?
An array is a collection of elements of the same data type stored in contiguous memory locations. Instead of declaring 10 separate integer variables, you can use one array of size 10.
// Without array (bad): 5 separate variables int m1, m2, m3, m4, m5; // With array (good): one declaration int marks[5]; // Array of 5 integers
Key properties:
- All elements are of the same type
- Elements stored in contiguous memory
- Each element accessed using its index (starting from 0!)
- Size is fixed at declaration (in C89/C90)
For int marks[5], if the array starts at address 1000:
marks[0]=1000, marks[1]=1004, marks[2]=1008, marks[3]=1012, marks[4]=1016
(Each int = 4 bytes, so addresses differ by 4)
Declaration and Initialization
// Method 1: Declare only (garbage values) int arr[5]; // Method 2: Declare and initialize int marks[5] = {90, 85, 78, 92, 88}; // Method 3: Partial init (rest become 0) int nums[5] = {10, 20}; // {10, 20, 0, 0, 0} // Method 4: Size auto-detected from values int primes[] = {2, 3, 5, 7, 11}; // Size = 5 // Method 5: Initialize all to zero int zeros[10] = {0}; // All elements = 0 // Other types float temps[7] = {36.5f, 37.0f, 36.8f}; char grades[5] = {'A', 'B', 'A', 'C', 'B'};
Accessing Array Elements
Use the index inside square brackets. Index starts at 0, ends at size-1.
int marks[5] = {90, 85, 78, 92, 88}; printf("%d\\n", marks[0]); // 90 (first element) printf("%d\\n", marks[4]); // 88 (last element, index = size-1) marks[2] = 95; // Change element at index 2 printf("%d\\n", marks[2]); // Now prints 95
Accessing arr[5] when size is 5 is undefined behavior — it may crash, produce wrong output, or corrupt memory. C does NOT check bounds automatically! Always stay within 0 to size-1.
Array Input and Output
#include <stdio.h> int main() { int n; printf("How many elements? "); scanf("%d", &n); int arr[n]; // Input printf("Enter %d numbers:\\n", n); for (int i = 0; i < n; i++) { printf("arr[%d] = ", i); scanf("%d", &arr[i]); } // Output printf("Array: "); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\\n"); return 0; }
Array Operations
Finding Sum, Average, Max, Min
#include <stdio.h> int main() { int marks[] = {85, 92, 78, 96, 70}; int n = sizeof(marks) / sizeof(marks[0]); // Auto calculate size int sum = 0, max = marks[0], min = marks[0]; for (int i = 0; i < n; i++) { sum += marks[i]; if (marks[i] > max) max = marks[i]; if (marks[i] < min) min = marks[i]; } printf("Sum = %d\\n", sum); printf("Average = %.2f\\n", (float)sum / n); printf("Maximum = %d\\n", max); printf("Minimum = %d\\n", min); return 0; }
Linear Search
#include <stdio.h> int main() { int arr[] = {10, 25, 8, 40, 15}; int n = 5, key, found = 0; printf("Search for: "); scanf("%d", &key); for (int i = 0; i < n; i++) { if (arr[i] == key) { printf("Found at index %d\\n", i); found = 1; break; } } if (!found) printf("Not found\\n"); return 0; }
Bubble Sort
#include <stdio.h> int main() { int arr[] = {64, 34, 25, 12, 22}; int n = 5; for (int i = 0; i < n-1; i++) { for (int j = 0; j < n-i-1; j++) { if (arr[j] > arr[j+1]) { int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } printf("Sorted: "); for (int i = 0; i < n; i++) printf("%d ", arr[i]); return 0; }
Summary
- Array = collection of same-type elements in contiguous memory
- Index starts at 0, ends at size - 1
- Declaration:
type name[size]; - Use
sizeof(arr)/sizeof(arr[0])to get array length - Always stay within bounds — C doesn't check automatically!
- Use
forloops to traverse, search, sort arrays
Array क्या है?
Array same data type के elements का collection है जो contiguous memory locations में store होते हैं। 10 अलग integer variables declare करने की बजाय, size 10 का एक array use करें।
int marks[5] के लिए: marks[0], marks[1], marks[2], marks[3], marks[4] — सभी एक साथ memory में। Index हमेशा 0 से शुरू होता है!
Declaration और Initialization
int marks[5]; // Declare only int marks[5] = {90, 85, 78, 92, 88}; // Declare + Initialize int primes[] = {2, 3, 5, 7, 11}; // Size auto-detect int zeros[10] = {0}; // सब 0 initialize
Elements Access करना
int marks[] = {90, 85, 78, 92, 88}; printf("%d\\n", marks[0]); // 90 — पहला element printf("%d\\n", marks[4]); // 88 — आखिरी element marks[2] = 95; // index 2 का value बदला
Size 5 के array में arr[5] access करना undefined behavior है — program crash हो सकता है। C automatically bounds check नहीं करता! हमेशा 0 से size-1 के बीच रहें।
Array Input और Output
int n = 5, arr[5]; printf("5 numbers daalen:\\n"); for(int i=0; i<n; i++) scanf("%d", &arr[i]); // Input for(int i=0; i<n; i++) printf("%d ", arr[i]); // Output
Array Operations
int marks[] = {85, 92, 78, 96, 70}; int n = sizeof(marks)/sizeof(marks[0]); int sum=0, max=marks[0], min=marks[0]; for(int i=0; i<n; i++) { sum += marks[i]; if(marks[i]>max) max=marks[i]; if(marks[i]<min) min=marks[i]; } printf("Sum=%.d Avg=%.2f Max=%d Min=%d\\n",sum,(float)sum/n,max,min);
सारांश
- Array = same type के elements का contiguous memory में collection
- Index 0 से शुरू होता है, size - 1 पर खत्म
- Declaration:
type name[size]; - Array length:
sizeof(arr)/sizeof(arr[0]) - C automatically bounds check नहीं करता — सावधान रहें!
- Traverse, search, sort के लिए
forloops use करें