Search Here

C program for 1D Array



/* C program to perform simple operations on 1D Array */

void main()
{
    int arr[5],i,Min,Max,sum,flag,item,location;
    
    printf("____ 1D Array ____\n\n");
    printf("Enter 5 integers: ");
    for(i=0;i<5;i++)
        scanf("%d",&arr[i]);
        
    // Displaying elements in FORWARD order
    printf("\nThe numbers in FORWARD order are:");
    for(i=0;i<5;i++)
        printf("\n%d",arr[i]);
    
    // Displaying elements in BACKWARD order
    printf("\nThe numbers in BACKWARD order are:");
    for(i=4;i>=0;i--)
        printf("\n%d",arr[i]);
    
    // Displaying MINIMUM number
    Min=arr[0];
    for(i=1;i<5;i++)
    {
        if(Min>arr[i])
            Min=arr[i];
    }
    printf("\nThe MINIMUM number is %d",Min);
    
    // Displaying MAXIMUM number
    Max=arr[0];
    for(i=1;i<5;i++)
    {
        if(Max<arr[i])
            Max=arr[i];
    }
    printf("\nThe MAXIMUM number is %d",Max);
    
    // Calculating SUM of elements
    sum=0;
    for(i=0;i<5;i++)
        sum=sum+arr[i];
    printf("\nThe SUM of the elements is %d",sum);
    
    // Searching an item in the array using linear search
    flag=0;
    printf("\nEnter an item to search\n");
    scanf("%d",&item);
    for(i=0;i<5;i++)
    {
        if(item==arr[i])
        {
            flag=1;
            location=i;
        }
    }
    if(flag==1)
        printf("\nThe is found at location arr[%d]",location);
    else
        printf("\n%d not found",item);
}


Output

No comments:

Post a Comment