Task 7
Task 7
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;
}
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);
/*
* 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;
}
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
Program:
#include <stdio.h>
int main() {
int n;
double arr[100];
printf("Enter the number of elements (1 to 100): ");
scanf("%d", &n);
return 0;
}
Output:
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};