[go: up one dir, main page]

0% found this document useful (0 votes)
3 views9 pages

Arrays

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)
3 views9 pages

Arrays

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

1. Write a program to find the maximum and minimum elements in an array.

Example:
Input:
4
10 12 23 15

Output:
Maximum element = 23
Minimum element = 10

Template

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

int main()
{

/* Put your declarations here */

int n, i;
int max, min;

scanf("%d", &n);

int arr[n];

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

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


return 0;
}

Input Output
6 Maximum element = 89
34 67 89 45 12 66 Minimum element = 12

8 Maximum element = 9
6 0 -1 5 3 7 9 1 Minimum element = -1

10 Maximum element = 90
90 43 67 -10 88 23 6 0 12 65 Minimum element = -10

2. Implement a program to search for a specific element in an array. If found, print


the element found at index; otherwise, print a message indicating that the
element is not present.

Example 1:
Input:
5 //size
10 20 30 40 50 //array elements
30 //search element

Output:
Element 30 found at index 2

Example 2:
Input:
4
15 25 35 45
50

Output:
Element 50 is not present in the array.

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

int main()
{

/* Put your declarations here */


int n, i, searchElement, found = 0;

// Input the number of elements in the array


// printf("Enter the number of elements in the array: ");

scanf("%d", &n);

int arr[n];

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


/* --- End of the solution --- */
return 0;
}

Input Output
6 Element 90 is not present in the array.
34 23 12 45 89 76
90

4 Element 67 found at index 1


23 67 45 33
67

4 Element -1 found at index 0


-1 5 7 3
-1

3. Write a program to copy all elements of one array into another array.
Example:
Input:
4
10 12 23 15
Output:
10 12 23 15

Template

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

int main()
{

/* Put your declarations here */


int n, i;

// Input the number of elements in the array


// printf("Enter the number of elements in the array: ");
scanf("%d", &n);

int arr1[n], arr2[n];

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


/* --- End of the solution --- */
return 0;
}

Input: Output:
4 10 12 23 15
10 12 23 15

5 -1 5 6 2 3
-1 5 6 2 3

6 43 54 87 9 76 56
43 54 87 9 76 56
4. Write a program to count the number of even and odd elements in an integer array.

Example:
Input:
5
10 20 12 15 23

Output:
Number of even elements: 3
Number of odd elements: 2

5
6
3
4
8
5
Number of even elements: 3
Number of odd elements: 2

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

int main()
{

/* Put your declarations here */


int n, i, evenCount = 0, oddCount = 0;

// Input the number of elements in the array


// printf("Enter the number of elements in the array: ");
scanf("%d", &n);

int arr[n]; // Declare an array of size n

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


/* --- End of the solution --- */
return 0;
}

Input: Output:
4 Number of even elements: 2
10 12 23 15 Number of odd elements: 2

5 Number of even elements: 2


-1 5 6 2 3 Number of odd elements: 3

6 Number of even elements: 3


43 54 87 9 76 56 Number of odd elements: 3

5. Create a program to find the frequency of a specific element in an array.


Size of the array: int
Array elements: int[n]
Example:
Template

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

int main()
{

/* Put your declarations here */


int n, i, searchElement, frequency = 0;

// Input the number of elements in the array


// printf("Enter the number of elements in the array: ");
scanf("%d", &n);

int arr[n]; // Declare an array of size n

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


/* --- End of the solution --- */
return 0;
}

Input: Output:
4 Array Elements: 10 12 23 15
10 12 23 15 Frequency of element 15:1

5 Array Elements: 10 10 10 5 10
10 10 10 5 10 Frequency of element 10:4

6 Array Elements: -2 -2 5 6 -9 67
-2 -2 5 6 -9 67 Frequency of element 15:0
6. Write a C program that removes duplicate elements from an array.

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

int main()
{

/* Put your declarations here */


int n, i, searchElement, frequency = 0;

// Input the number of elements in the array


// printf("Enter the number of elements in the array: ");
scanf("%d", &n);

int arr[n]; // Declare an array of size n

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


/* --- End of the solution --- */
return 0;
}

Input: Output:
5 Original array:
10 12 13 10 12 10 12 13 10 12
Array after removing duplicates:
10 12 13

5 Original array:
10 10 10 5 10 10 10 10 5 10
Array after removing duplicates:
10 5

6 Original array:
-2 -2 5 6 -9 67 -2 -2 5 6 -9 67
Array after removing duplicates:
-2 5 6 -9 67

7. Implement a C program to insert an element at a specific position in an array.


8. Write a C program to merge two sorted arrays into a single sorted array.

You might also like