Skip to main content

Introduction to Arrays

 Arrays

Definition:
An array is a data structure that stores a collection of elements of the same type. It is a sequential collection of elements where each element can be accessed using an index. Arrays provide an efficient way to store and access multiple elements of data under a single name.

Types of Arrays:

  1. One-dimensional Array: A one-dimensional array, also known as a linear array, is a collection of elements stored in a single row or column. Each element is accessed using a single index value.

  2. Multi-dimensional Array: A multi-dimensional array is an array of arrays. It can be visualized as a table with rows and columns. The elements are accessed using multiple indices corresponding to each dimension.

    • Two-dimensional Array: A two-dimensional array has two indices to access its elements, typically representing rows and columns.

    • Multi-dimensional Arrays: Arrays with more than two dimensions are also possible. For example, a three-dimensional array can be thought of as a cube, with elements accessed using three indices.

Code in C:

1. One-dimensional Array:


#include <stdio.h>
int main() 
{ // Declaring an array of integers 
int numbers[5] = {10, 20, 30, 40, 50}; // Accessing elements of the array printf("Elements of the array:\n");
for (int i = 0; i < 5; i++)
 { 
printf("%d ", numbers[i]);
 } 
printf("\n"); 
return 0; }

2. Two-dimensional Array:

c
#include <stdio.h> 
int main() 
{ // Declaring a 2D array of integers 
int matrix[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; // Accessing elements of the 2D array 
printf("Elements of the 2D array:\n");
for (int i = 0; i < 3; i++)
 { for (int j = 0; j < 3; j++) 
{ printf("%d ", matrix[i][j]); } 
printf("\n"); }
return 0
}

Notes:

Arrays are fundamental data structures in programming languages like C. They provide a convenient way to store and manipulate collections of data elements. In C, arrays are of fixed size, meaning once declared, their size cannot be changed during runtime. However, they offer efficient memory access and are widely used in various applications ranging from simple data storage to complex algorithms like sorting and searching. Understanding arrays is essential for mastering data structures and algorithms in C programming.

Comments

Popular posts from this blog

Header Files and their Examples in C

  What Is A Header File In C? A header file in C language is a text file containing definitions of functions, variables, and macros that are shared among multiple source files. It provides an interface to the variety of functions and data structures that can be used by other parts of the program. Essentially, a header file serves as a contract that specifies what features/ operations are available to the rest of the program without exposing the implementation details. Standard Header File In C & Their Uses In C, there are many standard header files, each of which has a unique collection of utilities and methods. Naturally, there are a few standard C header files that are most typically used in applications/ C code.  We have compiled a detailed explanation of all 25 standard header files in C. The <stdio.h> Header File In C The name stands for  standard input/output header , and it contains functions for standard input and output operations such as...

Recursive Functions in C

  Lecture Notes: Recursive Functions in C Introduction to Recursive Functions: Recursive functions are functions that call themselves directly or indirectly to solve a problem by dividing it into smaller instances of the same problem. They are a powerful programming technique used to solve problems that can be broken down into simpler subproblems. Definition: A recursive function is a function that calls itself either directly or indirectly during its execution. Types of Recursive Functions: Direct Recursion: In direct recursion, a function calls itself directly. c Copy code // Example of direct recursion void countdown ( int n) { if (n > 0 ) { printf ( "%d " , n); countdown(n - 1 ); // Recursive call } } Indirect Recursion: In indirect recursion, a function calls another function, which in turn calls the first function, creating a cycle of function calls. c Copy code // Example of indirect recursion void func1 ( int n) ; void func2 ( ...

Introduction to Pointers in C

  Introduction to  Pointers Introduction: In C programming, a pointer is a special variable that stores the memory address of another variable. Pointers are powerful tools that allow direct manipulation of memory and enable dynamic memory allocation. They play a crucial role in tasks such as dynamic memory allocation, accessing hardware, implementing data structures, and optimizing code performance. In simple terms pointers are the variables which store addresses of another variable. It can also store addresses of functions, arrays, structures and other pointers along with primitive data type variables. But the question arises “ why do we need pointers? “. Let’s find out the answer for the above question. Declaration of pointers Declaration of pointer variables is almost same as declaring normal variable, only difference is that we add ‘ * (asterisk) ‘ sign before the pointer variable. Syntax data_type *pointer_variable_name; E.g. int *num;  // pointer variable which can...