TLDR;
Alright, so this video is a crash course on arrays in C programming, especially for college students. It starts with the basics – what arrays are, why we need them, and how to declare and initialise them. Then, it moves on to more complex stuff like taking input and giving output using loops, solving problems, and passing arrays to functions. There are also some predict-the-output questions, error-finding exercises, and homework problems to get you coding.
- Arrays are collections of similar data types stored in contiguous memory locations.
- Indexing in arrays starts from 0.
- Loops are essential for input, output, and manipulation of array elements.
Introduction [0:00]
The video is about to teach you all about arrays in C programming. This is the seventh video in the C programming series. The instructor addresses queries about the Java one-shot videos, clarifying that a comprehensive Java series is already ongoing. He also mentions that C videos can be helpful for understanding basic programming concepts like loops, even for Java learners. The C course will cover structures, strings, dynamic arrays, searching, and sorting, with videos released more frequently.
What is an Array? [3:34]
Arrays are introduced as a way to store multiple values of the same data type under a single variable name. The problem it solves is the inefficiency of declaring individual variables for each value, like storing the heights of 50 students. Instead of int h1, h2, ... h50, you can use int a[50] to create 50 integer boxes at once. Arrays are also the first data structure being taught in the course. Data structures are ways to store and organise data for efficient access and manipulation.
Syntax and Declaration [8:40]
The syntax for declaring an array is explained. int a[5] creates an array named 'a' that can hold 5 integers. To initialise the array with values, you can use curly braces: int a[5] = {2, 4, 6, 8, 1}. This creates five boxes in memory and stores the values in the specified order. The array elements are referred to as the first element, second element, and so on.
Indexing in Arrays [13:10]
Each element in an array has an index, starting from 0. So, in int a[5], the indices are 0, 1, 2, 3, and 4. The last index is always one less than the size of the array (n-1). These indices are used to access and modify array elements.
Accessing Array elements [16:08]
Array elements are accessed using their index within square brackets. For example, a[0] refers to the first element, a[1] to the second, and so on. You can print the value of an array element using printf("%d", a[2]), which would print the value stored at the third index. Array elements can also be updated by assigning a new value to a specific index, like a[4] = 100. Trying to access an index outside the array's bounds (e.g., a[10] in an array of size 5) results in an "index out of bounds" error. Arrays can be of different data types like float and char.
Output and Input using LOOPS [24:07]
The video explains how to take input for array elements from the user. You can treat each array element like a variable and use scanf to input values. However, this is inefficient for large arrays. A better approach is to use loops. A for loop can iterate through the array, prompting the user to enter values for each element. Similarly, loops can be used to print all the elements of an array. The video demonstrates how to print array elements in reverse order by starting the loop from the last index and decrementing it.
Prob 1 : Given array of marks, print marks less than 35 [38:49]
The problem is to print the roll numbers (indices) of students whose marks are less than 35. The video provides an array of marks and iterates through it using a for loop. Inside the loop, an if condition checks if the mark at the current index is less than 35. If it is, the index (roll number) is printed.
Prob 2 : Find the correct declarations [45:00]
The video presents three array declarations and asks which ones are correct.
int a(25)is incorrect because arrays use square brackets, not round brackets.int size = 10, b[size]is correct. You can declare a variablesizeand use it to define the size of the array.int c = {0, 1, 2}is incorrect because it's missing the array size declaration using square brackets.
Prob 3 : Indexing based question [48:47]
The question asks what num[4] refers to in an array named num. The answer is the fifth element of the array, as array indexing starts from 0.
Memory allocation in Arrays [49:39]
Arrays have continuous memory allocation, meaning all elements are stored next to each other in memory. An integer typically takes 4 bytes. Unlike individual variables that can be stored at random memory locations, array elements are stored contiguously. This contiguous storage allows for efficient access to array elements. The address of the first element of the array is considered the address of the entire array.
Prob 4 : Predict the output - 1 [1:00:48]
The code swaps the values of the first and last elements of an array. The array num is initialised with 26 elements. The first element (num[0]) is set to 100, and the last element (num[25]) is set to 200. A temporary variable temp is used to swap these values. Finally, the code prints the values of num[0] and num[25], which will be 200 and 100 respectively.
Prob 5 : Predict the output - 2 [1:03:10]
The code initializes an array sub of size 50. A for loop attempts to assign values to the array elements, but a semicolon after the for loop makes it an empty loop. Therefore, only i = 0 is executed. Then, sub[i] is assigned the value of i (which is 0), and the value of sub[i] is printed. The output will be 0.
Garbage Values [1:07:37]
If you declare an array without initialising its elements, the array will contain garbage values. These are random values that were previously stored in those memory locations.
Prob 6 : Find the error - 1 [1:09:04]
The code has no errors. It declares three integer variables (i, a, b) and an array arr of size 5. It then uses a for loop to take input for the array elements and print them.
Prob 7 : Find the error - 2 [1:12:13]
The code has an error: the variable i is not declared before being used in the for loop.
Prob 8 : Print the sum of elements in given array [1:13:17]
The code calculates the sum of all elements in an array. It initializes a variable sum to 0 and then iterates through the array using a for loop. In each iteration, the current element is added to the sum. Finally, the code prints the value of sum.
HW 1 : Print the product of elements in given array [1:18:14]
This is a homework problem. You need to calculate the product of all elements in a given array.
Prob 9 : Print the maximum element in given array [1:19:11]
The code finds the maximum element in an array. It initializes a variable max to the smallest possible integer value (INT_MIN from limits.h). It then iterates through the array, comparing each element with max. If an element is greater than max, max is updated to that element. Finally, the code prints the value of max.
HW 2 : Print the minimum element in given array [1:31:26]
This is a homework problem. You need to find the minimum element in a given array.
Prob 10 : MCQ - 1 [1:31:36]
The question asks for the difference between the two 5s in the expressions int num[5] and num[5] = 11. The answer is that the first 5 specifies the size of the array, while the second 5 refers to the fifth index (sixth element) of the array.
Prob 11 : MCQ - 2 [1:33:01]
The question asks what happens if you assign a value to an array element whose index exceeds the size of the array. The answer is that other data may be overwritten, leading to unpredictable behaviour.
Prob 12 : MCQ - 3 [1:34:56]
The question asks what actually gets passed when you pass an array as an argument to a function. The answer is the address of the first element of the array.
Passing ARRAYS to FUNCTIONS [1:35:14]
Arrays are passed to functions by reference. This means that if you modify an array inside a function, the changes will be reflected in the original array. The video demonstrates this by passing an array to a function and changing the value of one of its elements. The change is visible outside the function.
Prob 13 : State True or False [1:44:36]
The video presents four statements about the array int num[26] and asks whether they are true or false.
- The array has 26 elements: True.
num[1]designates the first element: False (it's the second element).- It is necessary to initialise the array at the time of declaration: False.
num[27]designates the 28th element: True.
Prob 14 : Multiply odd indexed elements by 2 and add 10 to the even elements [1:45:47]
The problem is to modify an array such that the elements at odd indices are multiplied by 2, and 10 is added to the elements at even indices. The video iterates through the array using a for loop. Inside the loop, an if condition checks if the current index is odd or even. If it's odd, the element is multiplied by 2. If it's even, 10 is added to the element.
HW 3 : Print elements greater than x [1:52:38]
This is a homework problem. Given an array and a number x, you need to print all the elements in the array that are greater than x.
Prob 15 : Print difference b/w odd indexed and even indexed elements [1:53:59]
The problem is to find the difference between the sum of elements at even indices and the sum of elements at odd indices. The video initializes two variables, sumEven and sumOdd, to 0. It then iterates through the array using a for loop. Inside the loop, an if condition checks if the current index is odd or even. If it's even, the element is added to sumEven. If it's odd, the element is added to sumOdd. Finally, the code prints the difference between sumEven and sumOdd.
Prob 16 : Find total number of pairs whose sum add up to x [1:58:12]
The problem is to find the total number of pairs in an array whose sum is equal to a given value x. The video uses nested loops to iterate through all possible pairs in the array. For each pair, it checks if their sum is equal to x. If it is, a counter variable is incremented. Finally, the code prints the value of the counter variable.
Prob 17 : Find total number of triplets whose sum add up to x [2:07:07]
The problem is to find the total number of triplets in an array whose sum is equal to a given value x. The video uses three nested loops to iterate through all possible triplets in the array. For each triplet, it checks if their sum is equal to x. If it is, a counter variable is incremented. Finally, the code prints the value of the counter variable.
Prob 18 : Find the second largest element in Array [2:12:50]
The code finds the second largest element in an array. It first finds the maximum element in the array. Then, it iterates through the array again, comparing each element with the maximum element. If an element is not equal to the maximum element and is greater than the current second largest element, the second largest element is updated. Finally, the code prints the value of the second largest element.
**Prob 19 : Find second largest element in single pass of array [2:19:28]
The code finds the second largest element in an array in a single pass. It initializes two variables, max and secondMax, to the smallest possible integer value (INT_MIN). It then iterates through the array, comparing each element with max. If an element is greater than max, the current max is assigned to secondMax, and the element is assigned to max. Otherwise, if the element is not equal to max and is greater than secondMax, the element is assigned to secondMax. Finally, the code prints the value of secondMax.
Prob 20 : WAP to copy elements of array to another array in reverse order [2:42:15]
The code copies the elements of one array to another array in reverse order. It creates two arrays, arr and brr, of the same size. It then iterates through the arr array using a for loop. In each iteration, the element at the current index is copied to the corresponding index in the brr array, but in reverse order. Finally, the code prints the elements of the brr array.
*Prob 21 : Reverse the Array without using extra array [2:46:06]
The code reverses an array without using an extra array. It uses two pointers, i and j, to point to the first and last elements of the array, respectively. It then iterates through the array, swapping the elements at the i and j indices. The i pointer is incremented, and the j pointer is decremented in each iteration. The loop continues until the i pointer is greater than or equal to the j pointer.
HW 4 : Check if given Array is Palindrome or not [2:53:35]
This is a homework problem. You need to check if a given array is a palindrome or not.
**Prob 22 : Rotate the given Array by ‘k’ steps [2:54:52]
The code rotates an array by k steps. It first calculates the effective value of k by taking the modulo of k with the size of the array. Then, it reverses the entire array. After that, it reverses the first k elements of the array and then reverses the remaining elements of the array.
Prob 23 : Check if the given element in present in Array or not [3:18:55]
The code checks if a given element is present in an array. It iterates through the array using a for loop. In each iteration, it checks if the current element is equal to the given element. If it is, a flag variable is set to true, and the loop is broken. Finally, the code checks the value of the flag variable. If it's true, it prints that the element is present in the array. Otherwise, it prints that the element is not present in the array.
HW 5 : Find the missing element in given range [3:35:42]
This is a homework problem. Given an array containing elements from 1 to 100, except for one missing element, you need to find the missing element.
Prob 24 : Find the duplicate element in given array [3:39:40]
The code finds the duplicate element in an array. It uses nested loops to iterate through all possible pairs in the array. For each pair, it checks if the elements are equal. If they are, the duplicate element is printed, and the loop is broken.
Prob 25 : Find unique number, where all other elements are duplicate [3:44:15]
The code finds the unique number in an array where all other elements are duplicated. It uses nested loops to iterate through all possible pairs in the array. For each element, it checks if there is another element in the array that is equal to it. If there is no other element that is equal to it, the element is printed as the unique number, and the loop is broken.