Unit-1 Array – C Programming | BCA 2nd Sem
Unit-1 Array – C Programming | BCA 2nd Sem- Hello everyone welcome to the pencilchampions.com website. This website provide Unit-1 Array – C Programming | BCA 2nd Sem CCS University Notes. Thankyou for visiting.
Unit-1
Array
Meaning of Array
- An array is a data structure that can hold a collection of elements. It’s like a container where you can store multiple values, like numbers or strings. The cool thing about arrays is that each element has an index, which is like its address in the array. You can access and manipulate the elements using these indices. Arrays are super useful for organizing and working with data efficiently.
Read more-Â https://pencilchampions.com/best-operator-unit-2-c-programming-in-bca-1st-year-2023-2024/
Declaration and Initialization of Array
- Declaring and initializing an array is like giving it a name and filling it up with values. It’s pretty straightforward, so let me break it down for you.
- To declare an array, you first need to decide what type of elements you want it to hold. It could be numbers, strings, or even objects. Once you know the type, you can give your array a name. For example, let’s say we want to create an array to store a list of fruits:
C
var fruits = [];
- In this case, fruits is the name of our array, and we’ve declared it by using the var keyword.
- Next, we need to initialize the array by assigning values to its elements. You can do this in a couple of ways. One way is to specify the values directly when declaring the array:
C
var fruits = [“apple”, “banana”, “orange”];
- Here, we’ve initialized the fruits array with three elements: “apple”, “banana”, and “orange”. Each element is enclosed in double quotes and separated by commas.
- Another way to initialize an array is by assigning values to its elements one by one:
Wikipedia-Â https://en.wikipedia.org/wiki/C_(programming_language)
C
var fruits = [];
fruits[0] = “apple”;
fruits[1] = “banana”;
fruits[2] = “orange”;
- In this case, we first declare the fruits array as an empty array. Then, we assign values to its elements using indices. The first element is assigned to fruits[0], the second element to fruits[1], and so on.
You can also declare and initialize an array in a single line using the new keyword:
C
var fruits = new Array(“apple”, “banana”, “orange”);
- This achieves the same result as the previous examples.
- Remember, arrays are zero-indexed, which means the first element is at index 0, the second element is at index 1, and so on. So, if you want to access or modify a specific element in the array, you use its corresponding index.
- That’s the basic idea of declaring and initializing arrays. It’s like creating a container and filling it up with the values you want. Arrays are super versatile and can be used in various programming languages to solve all sorts of problems.
Accessing Array
- In C, you can access elements in an array using their index, just like in other programming languages. Let’s say we have an array called fruits with some fruit names:
C
Char fruits[] = { “apple”, “banana”, “orange” };
- To access a specific element in the array, you use square brackets [] and provide the index of the element you want to access. The index starts from 0 for the first element. For example, to access the first element, which is “apple”, you would use the index 0:
C
Char* first Fruit = fruits[0];
- In this case, first Fruit will now point to the value “apple”. Similarly, to access the second element, which is “banana”, you would use the index 1:
C
Char* second Fruit = fruits[1];
- Now, second Fruit will point to the value “banana”. You can continue this pattern to access any element in the array by using its corresponding index.
- It’s important to note that in C, arrays are zero-based, which means the first element is at index 0, the second element is at index 1, and so on. If you try to access an index outside the bounds of the array, it can lead to unexpected behavior or errors.
- You can also modify the value of an element in the array by accessing it using its index. For example, if you want to change the second element to “grape”, you can do it like this:
C
fruits[1] = “grape”;
- Now, the second element in the fruits array will be “grape” instead of “banana”.
- That’s the basic idea of accessing elements in an array in C. It allows you to retrieve and modify specific values stored in the array based on their positions.
Displaying Array
- In C, arrays are a way to store multiple values of the same type. To display an array, you would typically use a loop to iterate over each element and print its value.
- Let’s say we have an array called numbers that contains some integers:
C
int numbers[] = { 1, 2, 3, 4, 5 };
- To display this array, you can use a for loop. The loop will iterate from the first element to the last element of the array, and in each iteration, you can print the value of the current element.
- Here’s an example of how you can display the numbers array:
C
for (int i = 0; i < sizeof(numbers) / sizeof(numbers[0]); i++) {
   printf(“%d “, numbers[i]);
}
- In this loop, the variable i is used as the index to access each element of the array. The loop starts from 0 and continues until i reaches the length of the array. The sizeof operator is used to calculate the total number of bytes occupied by the array, and dividing it by the size of a single element gives us the number of elements in the array.
- Inside the loop, printf is used to print the value of each element. The %d format specifier is used to indicate that we are printing an integer.
- When you run this code, you will see the numbers 1, 2, 3, 4, 5 displayed on the screen.
- It’s important to note that the loop condition i < sizeof(numbers) / sizeof(numbers[0]) ensures that the loop doesn’t go beyond the bounds of the array. By dividing the total size of the array by the size of a single element, we get the number of elements in the array.
- That’s the basic idea of displaying an array in C using a loop. It allows you to iterate over each element and print its value.
Sorting array and Functions
- Sorting arrays is a common task in programming, and it allows you to arrange the elements of an array in a specific order, such as ascending or descending. There are various sorting algorithms available, but one of the most commonly used ones is the “bubble sort” algorithm.
- The bubble sort algorithm works by repeatedly swapping adjacent elements if they are in the wrong order. This process is repeated until the array is sorted. Here’s an example of how you can implement the bubble sort algorithm in C:
C
void bubbleSort(int arr[], int size) {
   for (int i = 0; i < size – 1; i++) {
       for (int j = 0; j < size – i – 1; j++) {
           if (arr[j] > arr[j + 1]) {
               // Swap arr[j] and arr[j + 1]
               int temp = arr[j];
               arr[j] = arr[j + 1];
               arr[j + 1] = temp;
           }
       }
   }
}
- In this code, the bubbleSort function takes an array arr and its size as parameters. It uses nested loops to iterate over the array and compare adjacent elements. If the elements are in the wrong order, they are swapped.
- To use this function, you can create an array and pass it to the bubbleSort function like this:
C
int numbers[] = { 5, 2, 8, 1, 6 };
int size = size of(numbers) / size of(numbers[0]);
bubble Sort(numbers, size);
- After calling the bubble Sort function, the numbers array will be sorted in ascending order.
- Functions in programming are reusable blocks of code that perform a specific task. They allow you to organize your code, make it more modular, and avoid repetition. Functions can have parameters and return values.
- Here’s an example of a simple function that calculates the sum of two numbers:
C
int sum(int a, int b) {
   return a + b;
}
- In this code, the sum function takes two parameters a and b of type int and returns their sum. You can use this function to calculate the sum of two numbers like this:
C
int result = sum(3, 4);
Two Dimensional array
- In programming, a two-dimensional array is a data structure that allows you to store and manipulate data in a grid-like format. It is essentially an array of arrays, where each element in the main array is itself an array. This allows you to represent data in rows and columns.
- To declare a two-dimensional array, you specify the number of rows and columns it should have. Here’s an example in C:
C
int matrix[3][4];
- In this example, we declare a two-dimensional array called matrix with 3 rows and 4 columns. Each element in the array can be accessed using two indices, one for the row and one for the column. The indices start from 0, so the first element in the array would be matrix[0][0], and the last element would be matrix[2][3].
- To initialize a two-dimensional array, you can use nested loops to assign values to each element. Here’s an example:
C
int matrix[3][4];
for (int i = 0; i < 3; i++) {
   for (int j = 0; j < 4; j++) {
       matrix[i][j] = i + j;
   }
}
- In this code, we use nested loops to iterate over each element in the array and assign a value to it. The value assigned in this example is the sum of the row index and the column index.
- You can also access and modify individual elements of a two-dimensional array using the row and column indices. For example, to access the element in the second row and third column, you would use matrix[1][2]. You can then assign a new value to it or use it in calculations.
- Two-dimensional arrays are useful for representing grids, matrices, tables, and other structured data. They provide a convenient way to organize and manipulate data in a row-column format.
Memory Representations of array
- In memory, an array is a contiguous block of memory locations that can store multiple elements of the same data type. Each element in the array is stored in a specific memory location, and the elements are stored sequentially.
- Let’s take the example of a one-dimensional array of integers called my Array, which has 5 elements: [10, 20, 30, 40, 50].
- When this array is stored in memory, the elements are placed one after another in consecutive memory locations. The memory representation would look like this:
Memory Address  |  Value
—————————-
1000Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 10
1004Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 20
1008Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 30
1012Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 40
1016Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 50
- In this example, each element of the array occupies 4 bytes of memory (assuming an integer takes 4 bytes). The memory addresses are shown in increments of 4, indicating the number of bytes occupied by each element.
- To access a specific element in the array, the computer uses the memory address of the first element and the size of each element to calculate the address of the desired element. For example, to access the third element (30) in the array, the computer would calculate the address as address_of_first_element + (index_of_element * size_of_each_element). In this case, it would be 1000 + (2 * 4) = 1008.
- Now, let’s consider a two-dimensional array called myMatrix, which has 3 rows and 4 columns: [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]].
- The memory representation of this two-dimensional array is similar, but the elements are stored row by row. It would look like this:
Memory Address  |  Value
—————————-
2000Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 1
2004Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 2
2008Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 3
2012Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 4
2016Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 5
2020Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 6
2024Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 7
2028Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 8
2032Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 9
2036Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 10
2040Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 11
2044Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 12
Row Major System
- In computer programming, the Row Major system is a way to store multi-dimensional arrays in memory. It determines the order in which the elements of the array are stored.
- In the Row Major system, elements of a two-dimensional array are stored row by row. Let’s take an example of a 3×3 matrix called my Matrix, where each element is represented by its row and column number:
My Matrix = [[1, 2, 3],
           [4, 5, 6],
           [7, 8, 9]]
- In memory, the Row Major system stores the elements of myMatrix as follows:
Memory Address  |  Value
—————————-
1000Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 1
1004Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 2
1008Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 3
1012Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 4
1016Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 5
1020Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 6
1024Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 7
1028Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 8
1032Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 9
- As you can see, the elements are stored row by row in consecutive memory locations. The first row [1, 2, 3] is stored first, followed by the second row [4, 5, 6], and finally the third row [7, 8, 9].
- To access a specific element in the array, the computer uses the formula (row_index * number_of_columns) + column_index. For example, to access the element at row 2, column 1 (which is 4), the computer would calculate the address as (2 * 3) + 1 = 7. This means that the element 4 is stored at memory address 1012.
- The Row Major system is commonly used in programming languages like C and C++, where arrays are stored in a contiguous block of memory. It allows for efficient traversal of the array, as elements in the same row are stored close to each other.
Column Major System
- In contrast to the Row Major system, the Column Major system is another way to store multi-dimensional arrays in memory. It determines the order in which the elements of the array are stored.
- In the Column Major system, elements of a two-dimensional array are stored column by column. Let’s take the same example of a 3×3 matrix called my Matrix:
My Matrix = [[1, 2, 3],
           [4, 5, 6],
           [7, 8, 9]]
- In memory, the Column Major system stores the elements of myMatrix as follows:
Memory Address  |  Value
—————————-
1000Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 1
1004Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 4
1008Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 7
1012Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 2
1016Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 5
1020Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 8
1024Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 3
1028Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 6
1032Â Â Â Â Â Â Â Â Â Â Â Â |Â Â 9
- As you can see, the elements are stored column by column in consecutive memory locations. The first column [1, 4, 7] is stored first, followed by the second column [2, 5, 8], and finally the third column [3, 6, 9].
- To access a specific element in the array using the Column Major system, the computer uses the formula (column_index * number_of_rows) + row_index. For example, to access the element at column 2, row 1 (which is 4), the computer would calculate the address as (2 * 3) + 1 = 7. This means that the element 4 is stored at memory address 1004.
- The Column Major system is used in some programming languages and libraries like Fortran and OpenGL. It can be beneficial for certain operations and algorithms that require column-wise access to the elements.
Multidimensional array
- A multidimensional array is a data structure that can store values in multiple dimensions, such as rows and columns. It’s like having a table with rows and columns, where each cell can hold a value.
- Let’s start with a simple example of a two-dimensional array. Imagine a grid with rows and columns. Each cell in the grid can hold a value. For instance, let’s say we have a 3×3 grid:
Grid = [[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]]
- In this example, we have three rows and three columns. Each cell in the grid holds a number from 1 to 9. We can access a specific value in the array by specifying its row and column index. For example, to access the value 5, we would use the row index 1 and the column index 1.
- Multidimensional arrays can have more than two dimensions. For example, a three-dimensional array can be thought of as a cube. Each cell in the cube can hold a value. Here’s an example of a three-dimensional array:
Cube = [[[1, 2], [3, 4]],
       [[5, 6], [7, 8]],
       [[9, 10], [11, 12]]]
- In this example, we have three layers, each containing a 2×2 grid. Each cell in the cube holds a number from 1 to 12. We can access a specific value in the array by specifying its layer, row, and column index.
- Multidimensional arrays are used in various applications. They are particularly useful when dealing with data that has multiple dimensions, such as images, matrices, or geographic data. For example, in image processing, a two-dimensional array can represent the pixels of an image, where each pixel holds color information.
- To work with multidimensional arrays, programming languages provide syntax and functions specifically designed for array manipulation. You can perform operations like accessing specific elements, modifying values, iterating over the array, and more.
- Understanding multidimensional arrays is essential for solving complex problems that involve organizing and manipulating data in multiple dimensions. It allows you to represent and work with structured data efficiently.
Discover more from Pencil Champions
Subscribe to get the latest posts sent to your email.