0% found this document useful (0 votes)
17 views7 pages

Arrays 2

Uploaded by

cs9490750
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)
17 views7 pages

Arrays 2

Uploaded by

cs9490750
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/ 7

1.

Write a C program to insert an element in a specified position in a given


array.

Template:

#include <stdio.h>
/* Include any headers here */

int main()
{

/* Put your declarations here */

int arr[100]; // Array with a maximum size of 100


int n, element, position;

/* --- Start your solution here --- */

/* --- End of the solution --- */


return 0;
}

Test Cases:

Input Output
4 Array after insertion:
10 20 30 40 5 10 20 30 40
5
1
5 Array after insertion:
12345 123456
6
6
5 Array after insertion:
2 4 6 8 10 2 4 7 6 8 10
7
3
3 Invalid position. Please enter a position
123 between 1 and 4.
5
0
3 Invalid position! Please enter a
357 position between 1 and 4.
9
5
5 Array after insertion:
10 20 30 40 50 10 20 -15 30 40 50
-15
3

2. Write a C program to delete an element from a specified position in a given array.

Template

#include <stdio.h>
/* Include any headers here */
int main()
{
/* Put your declarations here */
int arr[100]; // Array with a maximum size of 100
int n, position;
/* --- Start your solution here --- */
/* --- End of the solution --- */
return 0;
}

Test Cases:

Input Output
5 Array after deletion:
10 20 30 40 50 20 30 40 50
1
5 Array after deletion:
10 20 30 40 50 10 20 30 40
5
5 Array after deletion:
10 20 30 40 50 12356
5
4 Invalid position. Please enter a position
10 20 30 40 between 1 and 4.
0
4 Invalid position. Please enter a position
10 20 30 40 between 1 and 4.
6

3. Write a C program to sort array elements in ascending order using Bubble sort .

Template:

#include <stdio.h>
/* Include any headers here */
int main()
{
/* Put your declarations here */
int arr[100]; // Array with a maximum size of 100
int n, temp;
/* --- Start your solution here --- */

/* --- End of the solution --- */


return 0;
}

Test Cases:

Input Output
5 Array in ascending order:
52913 12359
5 Array in ascending order:
-3 -1 -7 -5 -2 -7 -5 -3 -2 -1
6 Array in ascending order:
3 -2 1 -5 4 -1 -5 -2 -1 1 3 4
7 Array in ascending order:
5383915 1335589
5 Array in ascending order:
44444 44444
-5 Invalid input. Array size must be
greater than 0.

4. Write a C program to sort array elements in descending order using


Selection sort.

Template:

#include <stdio.h>
/* Include any headers here */
int main()
{
/* Put your declarations here */
int arr[100]; // Array with a maximum size of 100
int n, temp;
/* --- Start your solution here --- */

/* --- End of the solution --- */


return 0;
}

Test Cases:
Input Output
5 Array in descending order:
52913 95321
5 Array in descending order:
-3 -1 -7 -5 -2 -1 -2 -3 -5 -7
6 Array in descending order:
3 -2 1 -5 4 -1 4 3 1 -1 -2 -5
7 Array in descending order:
5383915 9855331
5 Array in descending order:
44444 44444
-5 Invalid input. Array size must be
greater than 0.

5. Write a C program to search for a key in an array using Linear search.

Template:

#include <stdio.h>
/* Include any headers here */
int main()
{
/* Put your declarations here */
int arr[100]; // Array with a maximum size of 100
int n, key, found = 0;
/* --- Start your solution here --- */
/* --- End of the solution --- */
return 0;
}

Test Cases:

Input Output
5 Key 3 found at position 3.
12345
3
4 Key 5 not found in the array.
6789
5
1 Key 10 found at position 1.
10
10
4 Key -20 found at position 2.
-10 -20 -30 -40
-20
7 Key 10 found at position 2.
5 10 15 10 20 25 10
10

6. Write a C program to search for a key in an array using Binary search.

Template:

#include <stdio.h>
/* Include any headers here */
int main()
{
/* Put your declarations here */
int arr[100]; // Array with a maximum size of 100
int n, key, found = 0;
/* --- Start your solution here --- */
/* --- End of the solution --- */
return 0;
}

Test Cases:

Input Output
5 Key 3 found at position 3.
12345
3
4 Key 5 not found in the array.
6789
5
1 Key 10 found at position 1.
10
10
4 Key -20 found at position 2.
-10 -20 -30 -40
-20
7 Key 10 found at position 2.
5 10 10 10 20 25 30
10

Additional Questions
1. Write a C program to read and display elements of a 2D array.
2. Write a C program to add two matrices and display the resultant matrix.
3. Write a C program to find the sum of all elements of each row of a matrix.
4. Write a C program to multiply two matrices and display the resultant matrix.
5. Write a C program to interchange principal and secondary diagonal elements in a
matrix.

You might also like