Welcome !!

NICETECHVIDYA

Tutorials

Web Designs

You are welcome to Aptech Designers, designed by Pratik from Nepal.

Creativity is my passion. Logo describes your details about company.

Graphics Design needs vision to create yours unique digital concept.

Responsive and All Browser Compatible Dynamic Web Layouts our target.

Home Python C Language J A V A Our Courses
This WebSite is under Construction....

Thursday, 10 March 2022

37.array examples for loop

The first element a[0] is considered as both "min" and "max".

We will select element by element from the second element to n-1 (a[i])

If a[i] is less than "min" then it is stored in "min"

If a[i] is greater then "max" then it is store in "max"

Once the loop is completed then "min" has the minimum element and "max" has the maximum element.

 

Program:

#include<stdio.h>

void main()

{

int n,i,min,max,a[50];

 

printf("How many elements?");

scanf("%d",&n);

printf("Enter %d elements:\n",n);

for(i=0;i<n;i++)

  scanf("%d",&a[i]);

for(min=max=a[0],i=1;i<n;i++)

{

      if(a[i]<min)

     min=a[i];

   if(a[i]>max)

     max=a[i];

}  

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

 

}

 

Execution:

How many elements? 5

Enter 5 elements:

12   67   6   35   98

Minimum element 6

Maximum element 98

 

 Specification6:

Accept "n" elements into an array copy the element onto another array and print the resultant array.

 

Note: An array cannot be directly copied on to another array using assigning operator like normal variable.

We need to copy element by element using a loop.

 

Logic:

 

 

a

 
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Program:

#include<stdio.h>

void main()

{

int n,i,a[50],b[50];

 

printf("How many elements?");

scanf("%d",&n);

printf("Enter %d elements:\n",n);

for(i=0;i<n;i++)

   scanf("%d",&a[i]);

for(i=0;i<n;i++)

    b[i]=a[i];

printf("Resultant array elements are:\n");

for(i=0;i<n;i++)

   printf("%5d",b[i]);

 

}

 

Execution:

How many elements? 5

Enter 5 elements:

12   67   6   35   98

Resultant array elements are:

12   67   6   35   98


0 comments:

Post a Comment