Government College of Engineering, Karad Programming for Problem Solving Lab
Experiment No. 6
Title: Program for finding smallest and largest numbers from given 3 numbers from array.
Outcome: Student will able to insert and search elements in array by using C programming
language and decision making statements.
Theory:
C programming language provides a data structure called the array, which can store a
fixed-size sequential collection of elements of the same type. An array is used to store a
collection of data, but it is often more useful to think of an array as a collection of variables
of the same type.
Instead of declaring individual variables, such as number0, number1, ..., and
number99, you declare one array variable such as numbers and use numbers[0], numbers[1],
and ..., numbers[99] to represent individual variables. A specific element in an array is
accessed by an index. All arrays consist of contiguous memory locations. The lowest address
corresponds to the first element and the highest address to the last element.
Declaration of array:
Data_type array_name [size];
int arr[5];
arr[0] arr[1] arr[2] arr[3] arr[4]
0 1 2 3 4 5
Figure 1. Continuous memory allocation in array
Initializing Arrays:
You can initialize array in C either one by one or using a single statement as follows:
int arr[5]= {1,2,3,4,5};
The number of values between braces { } cannot be larger than the number of elements that
we declare for the array between square brackets [ ].
Arrays can be accessed by any of looping statements, but generally for loops are used with
arrays. Array elements are compared with each other by using if statements to find largest
and the smallest element stored in an array.
Types of Arrays:
1. One dimensional
2. Two dimensional
3. Multi-dimensional
Department of Information Technology
Government College of Engineering, Karad Programming for Problem Solving Lab
Searching:
Searching is an operation or a technique that helps finds the place of a given element
or value in the list. Any search is said to be successful or unsuccessful depending upon
whether the element that is being searched is found or not.
Some of the standard searching technique that is being followed in data structure is
listed below:
1. Linear Search or Sequential Search
2. Binary Search
1. Linear search in C:
Linear search in C programming: The following code implements linear search
(Searching algorithm) which is used to find whether a given number is present in an array
and if it is present then at what location it occurs. It is also known as sequential search. It is
straightforward and works as follows: We keep on comparing each element with the element
to search until it is found or the list ends. Linear search in C language for multiple
occurrences and using function.
2. Binary Search
Binary search is a very fast and efficient searching technique. It requires the list to be
in sorted order. In this method, to search an element you can compare it with the present
element at the center of the list. If it matches, then the search is successful otherwise the list
is divided into two halves: one from the 0th element to the middle element which is the
center element (first half) another from the center element to the last element (which is the
2nd half) where all values are greater than the center element.
Analysis :
1.
2.
3.
4.
5.
List of similar programs: Assignment No. 6
1. Write a C program to update and display array of five integers.
2. Write a C program to calculate standard deviation.
3. Write a C program to add, subtract, multiply two arrays and store result in third array.
4. Write a C language program to find even nos. in array and display them with index.
5. Write a C language program to find odd nos. in array and display their sum.
6. Write a C program to find max number position from array of 10 integers.
7. Write a C program to find largest element in an array.
8. Write a C program to find smallest element in an array.
Program:
List of questions:
1. Define array. Explain compile time and run time initialization of arrays.
2. Explain types of arrays.
3. What is Searching explain with example.
4. What is Sorting explain with example.
1.Write a C program to update and display array of five integers.
SOURCE CODE:
#include<stdio.h>
int main()
{
int a[5],i;
printf(“Enter Five integers:\n”);
for(i=0;i<5;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<5;i++)
{
printf(“a[%d]=%d\n”,i,a[i]);
}
return 0;
}
OUTPUT:
2.Write a C program to calculate standard deviation.
SOURCE CODE:
#include<stdio.h>
int main()
{
int a[50],i,sum=0,n;
float mean, k, V=0,SD;
printf(“Enter the size of array:\n”,n);
scanf(“%d”,&n);
printf(“Enter Array Elements:\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
printf(“Sum=%d\n”,sum);
mean=sum/n;
printf(“Mean=%f\n”,mean);
for(i=0;i<n;i++)
{
k=k+(mean-a[i])*(mean-a[i]);
}
V=k/n;
SD=sqrt(V);
printf(“Variance=%f\n”,V);
printf(“Standard Deviation=%f”,SD);
return 0;
}
OUTPUT:
3.Write a C program to add, subtract, multiply two arrays and store result in third
array.
SOURCE CODE:
#include<stdio.h>
int main()
{
int a[5],b[5],add[5],sub[5],multi[5],i;
printf(“Enter Elements Of First Array:\n”);
for(i=0;i<5;i++)
{
scanf(“%d”,&a[i]);
}
printf(“Enter Elements Of Second Array:\n”);
for(i=0;i<5;i++)
{
scanf(“%d”,&b[i]);
}
printf(“Addition array of two array:add[5]\n”);
for(i=0;i<5;i++)
{
add[i]=a[i]+b[i];
printf(“add[%d]=%d\n”,i,add[i]);
}
printf(“Subtraction array of two array:sub[5]\n”);
for(i=0;i<5;i++)
{
sub[i]=a[i]-b[i];
printf(“sub[%d]=%d\n”,i,sub[i]);
}
printf(“Multiplication array of two array:multi[5]\n”);
for(i=0;i<5;i++)
{
multi[i]=a[i]*b[i];
printf(“multi[%d]=%d\n”,i,multi[i]);
}
return 0;
}
OUTPUT:
4.Write a C language program to find even nos. in array and display them with index.
SOURCE CODE:
#include<stdio.h>
int main()
{
int a[10],i;
printf(“Enter ten elements of an array:\n”);
for(i=0;i<10;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<10;i++)
{
if(a[i]%2==0)
{
printf( “Even number=%d\tIndex=%d\n”,a[i],i);
}
}
return0;
}
OUTPUT:
5.Write a C language program to find odd nos. in array and display their sum.
SOURCE CODE:
#include<stdio.h>
int main()
{
int a[10],i,sum=0;
printf(“Enter ten elements of an array:\n”);
for(i=0;i<10;i++)
{
scanf(“%d”,&a[i]);
}
printf(“Odd Numbers=”);
for(i=0;i<10;i++)
{
if (a[i]%2==1)
{
sum=sum+a[i];
printf(“%d\t”,a[i]);
}
}
printf(“\nSum of odd numbers=%d”,sum);
return 0;
}
OUTPUT:
6.Write a C program to find max number position from array of 10 integers.
SOURCE CODE:
#include<stdio.h>
int main()
{
int a[10],i,n, position,max;
printf(“Enter ten elements of an array:\n”,n);
for(i=0;i<10;i++)
{
scanf(“%d”,&a[i]);
}
max=a[0];
for(i=1;i<10;i++)
{
if(max<a[i])
{
max=a[i];
position=i;
}
}
printf(“Largest Element=%d Located at position=a[%d]”, max, position);
return 0;
}
OUTPUT:
7.Write a C program to find largest element in an array.
SOURCE CODE:
#include<stdio.h>
int main()
{
int a[10],i,n, position,max;
printf(“Enter the size of an array:\n”);
scanf(“%d”,&n);
printf(“Enter the %d elements of an array:\n”,n);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
max=a[0];
for(i=1;i<n;i++)
{
if(max<a[i])
{
max=a[i];
position i;
}
}
printf(“Largest Element=a[%d]=%d”, position,max);
return 0;
}
OUTPUT:
8.Write a C program to find smallest element in an array.
SOURCE CODE:
#include<stdio.h>
int main()
{
int a[10],i,n, min, position;
printf(“Enter the size of an array:\n”);
scanf(“%d”,&n);
printf(“Enter the %d elements of an array:\n”,n);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
min=a[0];
for(i=1;i<n;i++)
{
if(min>a[i])
{
min=a[i], position;
}
}
printf(“Smallest Element=a[%d]=%d”, position,min);
return 0;
}
OUTPUT:
CONCLUSION: