[go: up one dir, main page]

0% found this document useful (0 votes)
28 views19 pages

Check PAGE NUMBER 8 Onwards For Mandatory Exercises.: Things Every Programmer Should Know About Array

The document discusses various concepts related to arrays in C programming such as initialization of arrays at compile-time and run-time, multidimensional arrays, examples to store and print temperature data using 2D arrays and calculate sum of matrices using 2D arrays. It also provides examples of programs to perform operations like storing and printing elements, reversing an array, finding sum and copy of arrays, counting duplicates, merging sorted arrays etc.

Uploaded by

Ankitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views19 pages

Check PAGE NUMBER 8 Onwards For Mandatory Exercises.: Things Every Programmer Should Know About Array

The document discusses various concepts related to arrays in C programming such as initialization of arrays at compile-time and run-time, multidimensional arrays, examples to store and print temperature data using 2D arrays and calculate sum of matrices using 2D arrays. It also provides examples of programs to perform operations like storing and printing elements, reversing an array, finding sum and copy of arrays, counting duplicates, merging sorted arrays etc.

Uploaded by

Ankitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Check PAGE NUMBER 8 Onwards for Mandatory Exercises.

Things Every Programmer should know about Array


1. Array index starts at 0, not 1.
2. Array are mostly immutable data structure whose length cannot be changed once created, the
mutable array is called list.
3. The array needs a memory block for allocation, called consecutive memory location, this means even
if you have a memory you cannot allocate a big array if memory is scattered.
4. Searching by index in the array is O(1) but insert and delete is not easy because you may need to re-
arrange the array.
5. An array is mostly homogenous data structure this means you cannot store a string in an integer array
and vice-versa.
6. An array can be single dimension or multiple dimensions. A two-dimensional array is known as Matrix
and very useful in games to create 2D world using tiles.

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 Array initialization

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

float area[5] = {23.4, 6.8, 5.5} // float array initialization

int marks[4] = {19, 10, 8, 17, 9} // Compile time error

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

Runtime Array initialization


An array can also be initialized at runtime using scanf() function. This approach is usually used
for initializing large array, or to initialize array with user specified values. Example,

#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

C Programming Multidimensional Arrays


In C programming, you can create an array of arrays known as multidimensional array. For
example,

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.

Initialization of a two dimensional array.


// Different ways to initialize two dimensional array
int c[2][3] = {{1, 3, 0}, {-1, 5, 9}};

int c[][3] = {{1, 3, 0}, {-1, 5, 9}};

int c[2][3] = {1, 3, 0, -1, 5, 9};

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] = {

{ {3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2} },


{ {13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9} }
};

Example #1: Two Dimensional Array to store and display values


// C program to store temperature of two cities for a week and display it.
#include <stdio.h>

const int CITY = 2;


const int WEEK = 7;

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]);
}
}

printf("\nDisplaying values: \n\n");


for (int i = 0; i < CITY; ++i) {
for(int j = 0; j < WEEK; ++j)
{
printf("City %d, Day %d = %d\n", i+1, j+1, temperature[i][j]);
}
}
return 0;
}

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

Example #2: Sum of two matrices using Two dimensional arrays


C program to find the sum of two matrices of order 2*2 using multidimensional arrays.

#include <stdio.h>
int main()
{
float a[2][2], b[2][2], c[2][2];
int i, j;

// Taking input using nested for loop


printf("Enter elements of 1st matrix\n");
for(i=0; i<2; ++i)
for(j=0; j<2; ++j)
{
printf("Enter a%d%d: ", i+1, j+1);
scanf("%f", &a[i][j]);
}

// Taking input using nested for loop


printf("Enter elements of 2nd matrix\n");
for(i=0; i<2; ++i)
for(j=0; j<2; ++j)
{
printf("Enter b%d%d: ", i+1, j+1);
scanf("%f", &b[i][j]);
}

// adding corresponding elements of two arrays


for(i=0; i<2; ++i)
for(j=0; j<2; ++j)
{
c[i][j] = a[i][j] + b[i][j];
}

// Displaying the sum


printf("\nSum Of Matrix:");

for(i=0; i<2; ++i)


for(j=0; j<2; ++j)
{
printf("%.1f\t", c[i][j]);

if(j==1)
printf("\n");
}
return 0;
}

Output

Enter elements of 1st matrix


Enter a11: 2;
Enter a12: 0.5;
Enter a21: -1.1;
Enter a22: 2;
Enter elements of 2nd matrix
Enter b11: 0.2;
Enter b12: 0;
Enter b21: 0.23;
Enter b22: 23;
Sum Of Matrix:
2.2 0.5
-0.9 25.0

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.

1. Write a program in C to store elements in an array and print it.


Test Data :
Input 10 elements in the array :
element - 0 : 1
element - 1 : 1
element - 2 : 2
.......
Expected Output :
Elements in array are: 1 1 2 3 4 5 6 7 8 9

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

3. Write a program in C to find the sum of all elements of the array.


Test Data :
Input the number of elements to be stored in the array :3
Input 3 elements in the array :
element - 0 : 2
element - 1 : 5
element - 2 : 8
Expected Output :
Sum of all elements stored in the array is : 15

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

5. Write a program in C to count a total number of duplicate elements in an array.


Test Data :
Input the number of elements to be stored in the array :3
Input 3 elements in the array :
element - 0 : 5
element - 1 : 1
element - 2 : 1
Expected Output :
Total number of duplicate elements found in the array is : 1

6. Write a program in C to print all unique elements in an array.


Test Data :
Print all unique elements of an array:
------------------------------------------
Input the number of elements to be stored in the array: 4
Input 4 elements in the array :
element - 0 : 3
element - 1 : 2
element - 2 : 2
element - 3 : 5
Expected Output :
The unique elements found in the array are:
35

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

8. Write a program in C to count the frequency of each element of an array.


Test Data :
Input the number of elements to be stored in the array :3
Input 3 elements in the array :
element - 0 : 25
element - 1 : 12
element - 2 : 43
Expected Output :
The frequency of all elements of an array :
25 occurs 1 times
12 occurs 1 times
43 occurs 1 times

9. Write a program in C to find the maximum and minimum element in an array.


Test Data :
Input the number of elements to be stored in the array :3
Input 3 elements in the array :
element - 0 : 45
element - 1 : 25
element - 2 : 21
Expected Output :
Maximum element is : 45
Minimum element is : 21
10. Write a program in C to separate odd and even integers in separate arrays.
Test Data :
Input the number of elements to be stored in the array :5
Input 5 elements in the array :
element - 0 : 25
element - 1 : 47
element - 2 : 42
element - 3 : 56
element - 4 : 32
Expected Output :
The Even elements are :
42 56 32
The Odd elements are :
25 47

11. Write a program in C to sort elements of array in ascending order.


Test Data :
Input the size of array : 5
Input 5 elements in the array :
element - 0 : 2
element - 1 : 7
element - 2 : 4
element - 3 : 5
element - 4 : 9
Expected Output :
Elements of array in sorted ascending order:
24579

12. Write a program in C to sort elements of the array in descending order.


Test Data :
Input the size of array : 3
Input 3 elements in the array :
element - 0 : 5
element - 1 : 9
element - 2 : 1
Expected Output :
Elements of the array in sorted descending order:
951

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

15. Write a program in C to delete an element at desired position from an array.


Test Data :
Input the size of array : 5
Input 5 elements in the array in ascending order:
element - 0 : 1
element - 1 : 2
element - 2 : 3
element - 3 : 4
element - 4 : 5
Input the position where to delete: 3
Expected Output :
The new list is : 1 2 4 5

16. Write a program in C to find the second largest element in an array.


Test Data :
Input the size of array : 5
Input 5 elements in the array :
element - 0 : 2
element - 1 : 9
element - 2 : 1
element - 3 : 4
element - 4 : 6
Expected Output :
The Second largest element in the array is : 6

17. Write a program in C to find the second smallest element in an array.


Test Data :
Input the size of array : 5
Input 5 elements in the array (value must be <9999) :
element - 0 : 0
element - 1 : 9
element - 2 : 4
element - 3 : 6
element - 4 : 5
Expected Output :
The Second smallest element in the array is : 4

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

19. Write a program in C for addition of two Matrices of same size.


Test Data :
Input the size of the square matrix (less than 5): 2
Input elements in the first matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [1],[0] : 3
element - [1],[1] : 4
Input elements in the second matrix :
element - [0],[0] : 5
element - [0],[1] : 6
element - [1],[0] : 7
element - [1],[1] : 8
Expected Output :
The First matrix is :

12
34
The Second matrix is :

56
78
The Addition of two matrix is :

68
10 12

20. Write a program in C for subtraction of two Matrices.


Test Data :
Input the size of the square matrix (less than 5): 2
Input elements in the first matrix :
element - [0],[0] : 5
element - [0],[1] : 6
element - [1],[0] : 7
element - [1],[1] : 8
Input elements in the second matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [1],[0] : 3
element - [1],[1] : 4
Expected Output :
The First matrix is :

56
78
The Second matrix is :

12
34
The Subtraction of two matrix is :

44
44

21. Write a program in C for multiplication of two square Matrices.


Test Data :
Input the rows and columns of first matrix : 2 2
Input the rows and columns of second matrix : 2 2
Input elements in the first matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [1],[0] : 3
element - [1],[1] : 4
Input elements in the second matrix :
element - [0],[0] : 5
element - [0],[1] : 6
element - [1],[0] : 7
element - [1],[1] : 8
Expected Output :
The First matrix is :

12
34
The Second matrix is :

56
78
The multiplication of two matrix is :

19 22
43 50

22. Write a program in C to find transpose of a given matrix.


Test Data :
Input the rows and columns of the matrix : 2 2
Input elements in the first matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [1],[0] : 3
element - [1],[1] : 4
Expected Output :
The matrix is :

12
34

The transpose of a matrix is :


13
24

23. Write a program in C to find sum of right diagonals of a matrix.


Test Data :
Input the size of the square matrix : 2
Input elements in the first matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [1],[0] : 3
element - [1],[1] : 4
Expected Output :
The matrix is :
12
34
Addition of the right Diagonal elements is :5
Elements in array are:

24. Write a program in C to find the sum of left diagonals of a matrix.


Test Data :
Input the size of the square matrix : 2
Input elements in the first matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [1],[0] : 3
element - [1],[1] : 4
Expected Output :
The matrix is :
12
34
Addition of the left Diagonal elements is :5
25. Write a program in C to find sum of rows and columns of a Matrix.
Test Data :
Input the size of the square matrix : 2
Input elements in the first matrix :
element - [0],[0] : 5
element - [0],[1] : 6
element - [1],[0] : 7
element - [1],[1] : 8
Expected Output :
The First matrix is :
The matrix is :
56
78
The sum or rows and columns of the matrix is :
5 6 11
7 8 15

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.

You might also like