C PROGRAMS : ARRAYS

C PROGRAM : Arrays
6. Program to find Minimum element in an array
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable a with size five to store five elements
 int a[5];
 
 // Declaring variable "i" to iterate loop
 int i;
 
 // Declaring variable min=minimum
 int min;
 
 // Inputting Element of an array
 printf("Enter five element in an array : \n");
 for(i=0;i<5;i++)
 {
  scanf("%d",&a[i]);
 }
 
 // Initialising min with first element assuming it to be minimum
 min=a[0];
 
 // Determining minimum element
 for(i=0;i<5;i++)
 {
 if(min>a[i+1])
 min=a[i+1];
 }
 
 // Displaying minimum element
 printf("Minimum Element = %d",min);
 
 getch(); // Linux user - Remove this

 return 0;
}

7. Program to find highest minimum(-) temperature and lowest maximum(+) temperature
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 int i,j;
 int counternegative=0, counterpositive=0;
 int temp[12];
 int max=0;
 int min=0;
 printf("Enter temperature of 12 months : " );
 
 for(i=0;i<12;i++)
 {
  scanf("%d",&temp[i]); 
 }
 
 for(i=0;i<12;i++)
 {
  if(temp[i]>0 && counterpositive==0)
  {
  min=temp[i];
  counterpositive=1;
  }
  
  else if(temp[i]<0 && counternegative==0)
  {
  max=temp[i];
  counternegative=1;
  }
 }
 
 for(i=0;i<12;i++)
 {
    if(temp[i]<0)
    {
     if(max<temp[i])
     {
     max=temp[i];
     }
    }
    
    else if(temp[i]>0)
    {
     if(min>temp[i])
     {
      min=temp[i];
     }
    }
  }
 
 printf("Max negative temperature : %d \n",max);
 printf("Min positive temperature : %d ",min);
 
 getch(); // Linux user - Remove this

 return 0;
}

8. Program to accept 10 numbers and print first five numbers in original order and print last five numbers in reverse order
# include <stdio.h>
# include <conio.h> // Linux user - Remove this

int main( )
{ 

// Declaring variabel i = for iteration of loop
int i,a[10];

// Inputting elements
printf("Enter 10 elements : ");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}

// Printing first 5 numbers in original order
printf("Printing first 5 numbers in original order : ");
for(i=0;i<=4;i++)
printf("%d ",a[i]);

// Printing last five numbers in reverse order
printf("\nPrinting last 5 numbers in reverse order : ");
for(i=9;i>=5;i--)
printf("%d ",a[i]);

getch( ); // Linux user - Remove this

return 0;
}


9. Program showing Passing array to function. Program finds the average of 5 marks input by user
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

/* We have declared return type as float since value to be returned 
i.e "average" is a float value. */
float avg(float x[5])
{
      // Declaring variable sum to hold the sum of five elements entered
   float sum=0;
   
   // Declaring variable "i" to iterate loop
   int i;
   
   // Calculating sum 
      for(i=0;i<5;i++)
      {
              sum=sum+x[i];
      }
      
      // Returning average i.e sum/5
      return sum/5;
}
int main()
{
    // Declaring array variale marks with maximum size "5"
    float marks[5];
    
     // Declaring variable "i" to iterate loop
     int i;
    
    // Inputting elements or marks
    printf("Enter marks for five subject : ");
    for(i=0;i<5;i++)
    {
            scanf("%f",&marks[i]);
    }
 
 // Calling function and printing the returned resutl ( i.e average )
 printf("Average marks = %f",avg(marks));

 getch(); // Linux user - Remove this

 return 0;
}

10. Program to initialize a character array and display the it in reverse order
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable names of size 5 and initializing it
 char names[5]={'h','e','l','l','o'};
 
 // Declaring variable "i" to iterate loop
        int i;
    
        // Printing in reverse order
        printf("Initialized characters in reverse order : ");
  for(i=4;i>=0;i--)
  {
  printf("%c ",names[i]);
 }

 getch(); // Linux user - Remove this

 return 0;
}

Share this

Related Posts

Previous
Next Post »