Check PAGE NUMBER 8 Onwards For Mandatory Exercises.: Things Every Programmer Should Know About Array
Check PAGE NUMBER 8 Onwards For Mandatory Exercises.: Things Every Programmer Should Know About Array
The key to solving array-based questions is having a good knowledge of array data structure as well as basic
programming constructors such as loop, recursion, and fundamental operators.
Compile time initialization of array elements is same as ordinary variable initialization. The
general form of initialization of array is,
type array-name[size]= {list of values};
int marks[4] = {19, 10, 8, 17} // integer array initialization
One important thing to remember is that when you will give more initializer(array elements) than declared
array size than the compiler will give an error.
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
int arr[]={2,3,4}; //Compile time array initialization//Static
for(i=0 ; i<3 ; i++) {
printf("%d\t",arr[i]);
}
getch();
}
Output
2 3 4
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[4];
int i, j;
printf("Enter array element");
for(i=0;i<4;i++)
{
scanf("%d",&arr[i]); //Run time array initialization
}
for(j=0;j<4;j++)
{
printf("%d\n",arr[j]);
}
getch();
}
// Program to find the average of n (n < 10) numbers using arrays
#include <stdio.h>
int main()
{
int marks[10], i, n, sum = 0, average;
printf("Enter n: ");
scanf("%d", &n);
for(i=0; i<n; ++i)
{
printf("Enter number%d: ",i+1);
scanf("%d", &marks[i]);
sum += marks[i];
}
average = sum/n;
printf("Average marks = %d", average);
return 0;
}
Output
Enter n: 5
Enter number1: 45
Enter number2: 35
Enter number3: 38
Enter number4: 31
Enter number5: 49
Average = 39
Here, x is a two-dimensional (2d) array. The array can hold 12 elements. You can think the
array as table with 3 row and each row has 4 column.
Similarly, you can declare a three-dimensional (3d) array. For example,
float y[2][4][3];
Here,The array y can hold 24 elements.
You can think this example as: Each 2 elements have 4 elements, which makes 8 elements
and each 8 elements can have 3 elements. Hence, the total number of elements is 24.
How to initialize a multidimensional array?
There is more than one way to initialize a multidimensional array.
Above code are three different ways to initialize a two dimensional arrays.
Initialization of a three dimensional array.
You can initialize a three dimensional array in a similar way like a two dimensional array.
Here's an example,
int test[2][3][4] = {
int main()
{
int temperature[CITY][WEEK];
for (int i = 0; i < CITY; ++i) {
for(int j = 0; j < WEEK; ++j) {
printf("City %d, Day %d: ", i+1, j+1);
scanf("%d", &temperature[i][j]);
}
}
Output
City 1, Day 1: 33
City 1, Day 2: 34
City 1, Day 3: 35
City 1, Day 4: 33
City 1, Day 5: 32
City 1, Day 6: 31
City 1, Day 7: 30
City 2, Day 1: 23
City 2, Day 2: 22
City 2, Day 3: 21
City 2, Day 4: 24
City 2, Day 5: 22
City 2, Day 6: 25
City 2, Day 7: 26
Displaying values:
City 1, Day 1 = 33
City 1, Day 2 = 34
City 1, Day 3 = 35
City 1, Day 4 = 33
City 1, Day 5 = 32
City 1, Day 6 = 31
City 1, Day 7 = 30
City 2, Day 1 = 23
City 2, Day 2 = 22
City 2, Day 3 = 21
City 2, Day 4 = 24
City 2, Day 5 = 22
City 2, Day 6 = 25
City 2, Day 7 = 26
#include <stdio.h>
int main()
{
float a[2][2], b[2][2], c[2][2];
int i, j;
if(j==1)
printf("\n");
}
return 0;
}
Output
Learn first and then write and Dry Run on the Note Book I have directed to
maintain. Monday, I shall check, 1-25 Programs with Dry Run.
2. Write a program in C to read n number of values in an array and display it in reverse order.
Test Data :
Input the number of elements to store in the array :3
Input 3 number of elements in the array :
element - 0 : 2
element - 1 : 5
element - 2 : 7
Expected Output :
The values store into the array are :
257
The values store into the array in reverse are :
752
4. Write a program in C to copy the elements of one array into another array.
Test Data :
Input the number of elements to be stored in the array :3
Input 3 elements in the array :
element - 0 : 15
element - 1 : 10
element - 2 : 12
Expected Output :
The elements stored in the first array are :
15 10 12
The elements copied into the second array are :
15 10 12
7. Write a program in C to merge two arrays of same size sorted in decending order.
Test Data :
Input the number of elements to be stored in the first array :3
Input 3 elements in the array :
element - 0 : 1
element - 1 : 2
element - 2 : 3
Input the number of elements to be stored in the second array :3
Input 3 elements in the array :
element - 0 : 1
element - 1 : 2
element - 2 : 3
Expected Output :
The merged array in decending order is :
332211
13. Write a program in C to insert New value in the array (sorted list )..
Test Data :
Insert New value in the sorted array :
-----------------------------------------
Input the size of array : 5
Input 5 elements in the array in ascending order:
element - 0 : 2
element - 1 : 5
element - 2 : 7
element - 3 : 9
element - 4 : 11
Input the value to be inserted : 8
The exist array list is :
2 5 7 9 11
After Insert the list is :
2 5 7 8 9 11
--------------------------------
Process exited after 39.33 seconds with return value 10
Press any key to continue . . .
14. Write a program in C to insert New value in the array (unsorted list ).
Test Data :
Input the size of array : 4
Input 4 elements in the array in ascending order:
element - 0 : 1
element - 1 : 8
element - 2 : 7
element - 3 : 10
Input the value to be inserted : 5
Input the Position, where the value to be inserted :2
Expected Output :
The current list of the array :
1 8 7 10
After Insert the element the new list is :
1 5 8 7 10
18. Write a program in C for a 2D array of size 3x3 and print the matrix.
Test Data :
Input elements in the matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [0],[2] : 3
element - [1],[0] : 4
element - [1],[1] : 5
element - [1],[2] : 6
element - [2],[0] : 7
element - [2],[1] : 8
element - [2],[2] : 9
Expected Output :
The matrix is :
123
456
789
12
34
The Second matrix is :
56
78
The Addition of two matrix is :
68
10 12
56
78
The Second matrix is :
12
34
The Subtraction of two matrix is :
44
44
12
34
The Second matrix is :
56
78
The multiplication of two matrix is :
19 22
43 50
12
34
12 14
To be sure that you know most about arrays….. Try these below also (For
Placement Purpose) ….. Not Mandatory….. But try so that you won’t regret later.
1. Write a C program to read and print elements of array. - using recursion.
2. Write a C program to print all negative elements in an array.
3. Write a C program to find sum of all array elements. - using recursion.
4. Write a C program to find maximum and minimum element in an array. - using recursion.
5. Write a C program to find second largest element in an array.
6. Write a C program to count total number of even and odd elements in an array.
7. Write a C program to count total number of negative elements in an array.
8. Write a C program to copy all elements from an array to another array.
9. Write a C program to insert an element in an array.
10. Write a C program to delete an element from an array at specified position.
11. Write a C program to count frequency of each element in an array.
12. Write a C program to print all unique elements in the array.
13. Write a C program to count total number of duplicate elements in an array.
14. Write a C program to delete all duplicate elements from an array.
15. Write a C program to merge two arrays to third array.
16. Write a C program to find reverse of an array.
17. Write a C program to put even and odd elements of array in two separate arrays.
18. Write a C program to search an element in an array.
19. Write a C program to sort array elements in ascending or descending order.
20. Write a C program to sort even and odd elements of array separately.
21. Write a C program to left rotate an array.
22. Write a C program to right rotate an array.