Introduction to Array:

An arrays is a collection of elements of the same data type stored in contiguous memory locations. It allows you to store multiple values under a single name, and you can access each value by its index number. It allows programmers to store multiple values of the same data type, like integers or strings, instead of declaring separate variables for each value.

In arrays, each value is called an element, and every element can be easily accessed by its index number, which always starts from zero and goes up to size minus one. For example, if you declare an array of size 8, the valid indexes are 0, 1, 2, 3,4,5,6, and 7.

arrays index

Declaration of an Array: 
An Array can be declared as
data_type array_name[size];
Examples: 
int numbers[5]; // array of 5 integers
float marks[10]; // array of 10 float values
char letters[26]; // array of 26 characters

Initializing an Array:
You can also assign values during declaration:

int numbers[5] = {10, 20, 30, 40, 50};
char vowels[5] = {‘a’, ‘e’, ‘i’, ‘o’, ‘u’};

Accessing Array Elements:

In C programming, array elements can be accessed using their index number. The index of an array starts from 0, which means the first element is at index , 0 and the last element of an array of size n is at index n-1. You can access a specific element by writing the array name followed by the index in square brackets, like array_name[index]. Loops, especially for Loops are commonly used to access all elements of an array efficiently.

Example

#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
// Accessing individual elements
printf("First element: %d\n", numbers[0]);
printf("Third element: %d\n", numbers[2]);
// Accessing all elements using a loop
printf("All elements: ");
for(int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
return 0;
}
Out put of program:
First element: 10
Third element: 30
All elements: 10 20 30 40 50

Access array elements by a for loop:

In C programming, you can access all elements of an array using a for loop. The loop variable usually represents the index of the array, starting from 0 up to size-1. This method is efficient when you want to process or display all elements of the array.

Example: Access Array Elements Using a for Loop:

#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
printf("Array elements are: ");
for(int i = 0; i < 5; i++) { // Loop from 0 to size-1
printf("%d ", numbers[i]); // Accessing each element using index
}
return 0;
}
out put of program
Array elements are: 10 20 30 40 50

Classification of an Array:


In C programming, arrays are classified based on their dimensions and structure. There are mainly three types of arrays:

Types of array
Types of an Array

One-Dimensional Array in C:

A one-dimensional array is a collection of elements of the same data type that are stored in contiguous memory locations. It is like a list of items that can be accessed using an index. The index of an array starts from 0 in C programming.

Basic Syntax and Declaration of a One-Dimensional Array

The syntax  and declaration of a one-dimensional array can be expressed as

data_type array_name[size];   // syntax
int numbers[5];               // Declares an integer array of size 5
float marks[10];              // Declares a float array of size 10
char letters[20];             // Declares a character array of size 20

Example Program in C

#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50}; // declaration & initialization
int i;
printf("Array elements are:\n");
for(i = 0; i < 5; i++) {
printf("%d ", numbers[i]); // accessing elements using index
}
return 0;
}
OUT PUT OF PROGRAM: 
Array elements are:
10 20 30 40 50

Two-Dimensional Array:

A two-dimensional array in C is an array of arrays. It can be visualized as a table with rows and columns, where data is stored in a grid form. Each element is accessed using two indices: one for the row and one for the column.

Syntax, Declaration, and Example 2-Dimensional array 

data_type array_name[rows][columns];
int matrix[3][4]; // integer array with 3 rows and 4 columns
float marks[2][5]; // float array with 2 rows and 5 columns
char letters[4][10]; // character array with 4 rows and 10 columns

Example in C

#include <stdio.h>
int main() {
// declaration & initialization
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
int i, j;
printf("Two-Dimensional Array elements are:\n");
// printing array elements using nested loop
for(i = 0; i < 2; i++) { // loop for rows
for(j = 0; j < 3; j++) { // loop for columns
printf("%d ", matrix[i][j]);
}
printf("\n"); // new line after each row
}
return 0;
}
put put of program:
Two-Dimensional Array elements are:
1 2 3
4 5 6

How can we access the elements of a 2-dimensional Array?

In C programming, the elements of a two-dimensional array are accessed using two indices: the first index represents the row number and the second index represents the column number. For example, if we have an array matrix[2][3], the element at the first row and first column is accessed asmatrix[0][0], and the element at the second row and third column is accessed as matrix[1][2]. Generally, nested loops are used to access all the elements of a 2D array because the outer loop runs through the rows, while the inner loop runs through the columns.

2 dimension array

Example:

#include <stdio.h>
int main() {
int matrix[2][3] = {
{10, 20, 30},
{40, 50, 60}
};
int i, j;
printf("2D Array Elements:\n");
for(i = 0; i < 2; i++) { 
for(j = 0; j < 3; j++) { 
printf("%d ", matrix[i][j]); 
}
printf("\n"); 
}
return 0;
}
out put of program:
2D Array Elements:
10 20 30
40 50 60

multidimensional array:

In C programming, a multidimensional array is an array that contains one or more arrays as its elements. Essentially, it is an array of arrays. The most common type is the two-dimensional (2D) array, which can be thought of as a table with rows and columns. However, C supports arrays with even more dimensions (3D, 4D, etc.).

Syntax: 
data_type array_name[size1][size2]...[sizeN];
Declaration
int arr[2][3][4]; // 3D array: 2 blocks, 3 rows, 4 columns

What is a String in C?

In C programming, a string is simply a sequence of characters stored in a character array and terminated by a null character (\0).

Ways to Initialize a String in C

In C programming, a string is a sequence of characters stored in a character array and terminated with a null character (\0). Initializing a string can be done in several ways. The most common way is using a string literal, where the compiler automatically adds the null character at the end, for example:

1. Using String Literal 

char str1[] = "Hello";  The compiler automatically adds the null character \0 at the end.

2. Specifying Size

char str2[10] = "Hello";
 The array has extra unused space after "Hello" (from index 6 to 9).

3. Character by Character with Null Character

char str3[] = {'H', 'e', 'l', 'l', 'o', '\0'};
 Here we manually include the null character. If we forget \0, it won’t be a valid string.

4. Without Null Character (Not a String)

char str4[] = {'H', 'e', 'l', 'l', 'o'};
 This is just a character array, not a proper string, because it does not end with \0.

5. Empty String Initialization

char str5[20] = "";
 This creates a string with the first character as \0, meaning it’s an empty string.

Write a program to show string initialization.

Here’s a simple C program to show different ways of string initialization:

#include <stdio.h>
int main() {
// String initialization using string literal
char str1[] = "Hello";
// String initialization with specified size
char str2[10] = "World";
// String initialization character by character with null character
char str3[] = {'C', 'o', 'd', 'e', '\0'};
// Character array without null character (not a proper string)
char str4[] = {'T', 'e', 's', 't'};
// Empty string initialization
char str5[20] = "";
// Displaying initialized strings
printf("String 1: %s\n", str1);
printf("String 2: %s\n", str2);
printf("String 3: %s\n", str3);
printf("String 4: %s (may print garbage after this since no \\0)\n", str4);
printf("String 5: %s (empty string)\n", str5);
return 0;
}
out put: 
String 1: Hello
String 2: World
String 3: Code
String 4: Test (may print garbage after this since no \0)
String 5: (empty string)

Write a program to read the names of 5 different people using an array of strings and display them

In C, we can use a 2D character array to store multiple strings (names). Each row stores one string (one name), and each column stores the characters of that name. Here’s a program to read and display the names of 5 people:

#include <stdio.h>
int main() {
char names[5][50]; // 5 strings, each with max length 49 characters (+1 for '\0')
int i;
// Reading names from user
printf("Enter the names of 5 people:\n");
for (i = 0; i < 5; i++) {
printf("Name %d: ", i + 1);
scanf("%s", names[i]); // read string (no need for & with arrays)
}
// Displaying names
printf("\nThe names you entered are:\n");
for (i = 0; i < 5; i++) {
printf("Name %d: %s\n", i + 1, names[i]);
}
return 0;
}
out put:
Enter the names of 5 people:

Name 1: Ram
Name 2: Sita
Name 3: Hari
Name 4: Gita
Name 5: Shyam

The names you entered are:
Name 1: Ram
Name 2: Sita
Name 3: Hari
Name 4: Gita
Name 5: Shyam
 

Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!
Scroll to Top