Data Structures Practical 1

Question 1
Write a C program to find minimum and maximum element in an array
minmax.c
C
				#include <stdio.h>
#include <conio.h>

void main() 
{
    int arr[100];
    int n, i;
    int max, min;

    clrscr();

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

    printf("Enter %d elements:\n", n);
    for (i = 0; i < n; i++) 
	{
        printf("Element %d: ", i + 1);
        scanf("%d", &arr[i]);
    }

    max = arr[0];
    min = arr[0];

    for (i = 1; i < n; i++) 
	{
        if (arr[i] > max) 
		{
            max = arr[i];
        }
        if (arr[i] < min) 
		{
            min = arr[i];
        }
    }

    printf("\nMaximum element = %d", max);
    printf("\nMinimum element = %d", min);

    getch();
}

			
Output
Question 2
Write a C program to search a specific element in an array.
search.c
C
				#include <stdio.h>
#include <conio.h>

void main() 
{
    int arr[100], i, size, num, found = 0;

    clrscr();

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

    printf("Enter %d elements:\n", size);
    for(i = 0; i < size; i++) 
	{
        scanf("%d", &arr[i]);
    }

    printf("Enter the element you want to search: ");
    scanf("%d", &num);

    for(i = 0; i < size; i++) 
	{
        if(arr[i] == num) 
		{
			printf("\nElement %d found at position %d.", num, i + 1);
            found = 1;
            break; 
        }
    }

    if(found == 0) 
	{
        printf("\nElement %d is not present in the array.", num);
    } 
	
    getch();
}

			
Output

Leave a Comment

Your email address will not be published. Required fields are marked *