0% found this document useful (0 votes)
36 views11 pages

Task 7

The document discusses various operations on 1D arrays in C including: 1) Finding the minimum and maximum elements in an integer array. 2) Performing linear search to check if an element is present in an array. 3) Reversing the elements of a 1D integer array. 4) Calculating the 2's complement of a binary number. 5) Eliminating duplicate elements from an array. 6) Finding the smallest and largest elements in a float array. 7) Counting the frequency of each element in an array.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
36 views11 pages

Task 7

The document discusses various operations on 1D arrays in C including: 1) Finding the minimum and maximum elements in an integer array. 2) Performing linear search to check if an element is present in an array. 3) Reversing the elements of a 1D integer array. 4) Calculating the 2's complement of a binary number. 5) Eliminating duplicate elements from an array. 6) Finding the smallest and largest elements in a float array. 7) Counting the frequency of each element in an array.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 11

Tutorial 7: 1 D Arrays: searching.

Lab 7:1D Array manipulation, linear search


i) Find the min and max of a 1-D integer array. ii) Perform linear search on1D array.
iii) The reverse of a 1D integer array iv) Find 2’s complement of the given binary
number.
v) Eliminate duplicate elements in an array. vi) Find out smallest and biggest element in
an 1D Float Array.
vii) Count frequency of each element.

Aim: Find the min and max of a 1-D integer array.


Description: We are given with an array of integers. The task is to find the minimum
and maximum element of the array in the minimum number of comparisons.
Input
Arr[] = { 1,2,4,5,-3,91 }
Output
Maximum element : 91 Minimum Element : -3
Explanation − Here to minimize the number of comparisons, we will initialize the
maximum and minimum element with Arr[0]. And starting from the 2nd element
compare each value with min and max and update accordingly.
Program:
#include <stdio.h>
#include <conio.h>

int main()
{
int a[1000],i,n,min,max;
printf("Enter size of the array : ");
scanf("%d",&n);
printf("Enter elements in array : ");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
min=max=a[0];
for(i=1; i<n; i++)
{
if(min>a[i])
min=a[i];
if(max<a[i])
max=a[i];
}
printf("minimum of array is : %d",min);
printf("\nmaximum of array is : %d",max);
return 0;
}

Aim: Perform linear search on1D array.


Description: Linear Search is a sequential searching algorithm in C that is used to find
an element in a list.
 We can implement linear search in C to check if the given element is present in
both random access and sequential access data structures such as arrays, linked
lists, trees, etc.
 Linear Search compares each element of the list with the key till the element is
found or we reach the end of the list.
Program:
#include <stdio.h>
int main()
{
int array[100], search, c, number;
printf("Enter the number of elements in array\n");
scanf("%d",&number);
printf("Enter %d numbers\n", number);
for ( c = 0 ; c < number ; c++ )
{
scanf("%d",&array[c]);
}
printf("Enter the number to search\n");
scanf("%d",&search);
for ( c = 0 ; c < number ; c++ )
{
if ( array[c] == search ) /* if required element found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if ( c == number )
{
printf("%d is not present in array.\n", search);
}
return 0;
}
Aim: The reverse of a 1D integer array
Description:

In C, there are no inbuilt functions to reverse an array, we have to write the code for it
on our own. An array can be reversed using different methods and algorithms - printing
it from the last element, using an auxiliary array, recursion, swapping, etc.

For example, if the given array is [1,4,6,7,8,4,2,6,2] then the reversed array will look
like [2,6,2,4,8,7,4,6,1]

Program:

#include <stdio.h>
#define N 1000
int main()
{
int arr[N];
int n;
// Inputting the size of the array
printf("Enter the size of the array: ");
scanf("%d", &n);
// Inputting the array
printf("Enter an array: ");
for (int i = 0; i< n; i++)
{
scanf("%d", &arr[i]);
}
// Printing the reverse of the array
printf("Reversed array: ");
for (int i = n-1; i>=0; i--)
{
printf("%d ", arr[i]);
}
return 0;
}
Enter the size of the array: 5
Enter an array: 3 8 4 9 6
Reversed array: 6 9 4 8 3
Aim: Find 2’s complement of the given binary number.
Description:
The two’s complement for a given binary number can be calculated in two methods,
which are as follows −
 Method 1 − Convert the given binary number into one’s complement and then,
add 1.
 Method 2 − The trailing zero’s after the first bit set from the Least Significant Bit
(LSB) including one that remains unchanged and remaining all should be
complementing.
In simple words twos complement is defined as sum of ones complement of a binary
number and 1.

Program:
#include <stdio.h>
#define SIZE 8
int main()
{
char binary[SIZE + 1], onesComp[SIZE + 1], twosComp[SIZE + 1];
int i, carry=1;
printf("Enter %d bit binary value: ", SIZE);
/* Input 8-bit binary string */
gets(binary);

/* Find ones complement of the binary number */


for(i=0; i<SIZE; i++)
{
if(binary[i] == '1')
{
onesComp[i] = '0';
}
else if(binary[i] == '0')
{
onesComp[i] = '1';
}
}
onesComp[SIZE] = '\0';

/*
* Add 1 to the ones complement
*/
for(i=SIZE-1; i>=0; i--)
{
if(onesComp[i] == '1' && carry == 1)
{
twosComp[i] = '0';
}
else if(onesComp[i] == '0' && carry == 1)
{
twosComp[i] = '1';
carry = 0;
}
else
{
twosComp[i] = onesComp[i];
}
}
twosComp[SIZE] = '\0';
printf("Original binary = %s\n", binary);
printf("Ones complement = %s\n", onesComp);
printf("Twos complement = %s\n", twosComp);
return 0;
}

Enter 8 bit binary value: 01101100


Original binary = 01101100
Ones complement = 10010011
Twos complement = 10010100
Aim: To Eliminate duplicate elements in an array.
Description: In a given array we have elements 1 1 2 4 3 5 6 5 7 1 then we have to
eliminate repeated elements by using program.

Program:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[50],i,j,k, count = 0, dup[50], number;
printf("Enter size of the array");
scanf("%d",&number);
printf("Enter Elements of the array:");
for(i=0;i<number;i++)
{
scanf("%d",&a[i]);
dup[i] = -1;
}
printf("Entered element are: ");
for(i=0;i<number;i++)
{
printf("%d ",a[i]);
}
for(i=0;i<number;i++)
{
for(j = i+1; j < number; j++)
{
if(a[i] == a[j])
{
for(k = j; k <number; k++)
{
a[k] = a[k+1];
}
j--;
number--;
}
}
}
printf("After deleting the duplicate element the Array is: ");
for(i=0;i<number;i++)
{
printf("%d ",a[i]);
}
return 0;
}

Output:
Enter size of the array
10
Enter Elements of the array:
1124356571
Entered element are:
1124356571
After deleting the duplicate element, the Array is:
1243567

Aim: To find out smallest and biggest element in an 1D Float Array.


Description:
In a given array we have to find smallest and biggest number . Suppose if
the array has this data 3.0, 4.5, 6.7, 3.25, 6.75 then smallest number is 3.0
and biggest number is 6.75. To find out biggest and smallest number
through logic we have to compare each element with another .

Program:
#include <stdio.h>
int main() {
int n;
double arr[100];
printf("Enter the number of elements (1 to 100): ");
scanf("%d", &n);

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


printf("Enter number%d: ", i + 1);
scanf("%lf", &arr[i]);
}

// storing the largest number to arr[0]


for (int i = 1; i < n; ++i) {
if (arr[0] < arr[i]) {
arr[0] = arr[i];
}
}

printf("Largest element = %.2lf", arr[0]);

return 0;
}
Output:

Enter the number of elements (1 to 100): 5


Enter number1: 34.5
Enter number2: 2.4
Enter number3: -35.5
Enter number4: 38.7
Enter number5: 24.5
Largest element = 38.70

Aim: To Count frequency of each element.


Description:

we have an array of elements to count the occurrence of its each element. One of the
approaches to resolve this problem is to maintain one array to store the counts of each
element of the array. Loop through the array and count the occurrence of each element
as frequency and store it in another array fr.

In the given array, 1 has appeared two times so its frequency be 2 and 2 has appeared
four times so have frequency 4 and so on.

Program:
#include <stdio.h>

int main()
{
//Initialize array
int arr[] = {1, 2, 8, 3, 2, 2, 2, 5, 1};

//Calculate length of array arr


int length = sizeof(arr)/sizeof(arr[0]);

//Array fr will store frequencies of element


int fr[length];
int visited = -1;
for(int i = 0; i < length; i++){
int count = 1;
for(int j = i+1; j < length; j++){
if(arr[i] == arr[j]){
count++;
//To avoid counting same element again
fr[j] = visited;
}
}
if(fr[i] != visited)
fr[i] = count;
}

//Displays the frequency of each element present in array


printf("---------------------\n");
printf(" Element | Frequency\n");
printf("---------------------\n");
for(int i = 0; i < length; i++){
if(fr[i] != visited){
printf(" %d", arr[i]);
printf(" | ");
printf(" %d\n", fr[i]);
}
}
printf("---------------------\n");
return 0;
}
Output :

You might also like