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

A5 Solutions

The document provides 15 questions and solutions related to arrays in C programming. Some key operations covered include: 1. Reversing an array 2. Inserting and deleting elements from an array 3. Linear and binary search of arrays 4. Merging, union, and intersection of two arrays 5. Removing duplicate elements from an array 6. Rotating array elements 7. Calculating mean, median, mode and standard deviation of array elements 8. Converting a decimal number to binary representation in an array

Uploaded by

krishiv7734
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views11 pages

A5 Solutions

The document provides 15 questions and solutions related to arrays in C programming. Some key operations covered include: 1. Reversing an array 2. Inserting and deleting elements from an array 3. Linear and binary search of arrays 4. Merging, union, and intersection of two arrays 5. Removing duplicate elements from an array 6. Rotating array elements 7. Calculating mean, median, mode and standard deviation of array elements 8. Converting a decimal number to binary representation in an array

Uploaded by

krishiv7734
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Solution of Assignment No.

5
Q1) Reverse of an array
#include<stdio.h>
int main()
{ int n, arr[n], i;
printf("Enter the size of the array: ");
scanf("%d", &n);
printf("Enter the elements: ");
for(i = 0; i < n; i++)
{ scanf("%d", &arr[i]);
}
int rev[n], j = 0;
for(i = n-1; i >= 0; i--)
{
rev[j] = arr[i];
j++;
}
printf("The Reversed array: ");
for(i = 0; i < n; i++)
{
printf("%d ", rev[i]);
}
}

Q2) Insertion in an array


#include<stdio.h>
int main()
{ int arr[100], n,i, num, pos;
printf("How many number you want to take input ");
scanf("%d", &n) ;
printf("now enter the %d numbers", n);
for(i=0; i<=n-1;i++) // input
{ scanf("%d", &arr[i]);
}
printf("enter the number to inserted::");
scanf("%d", &num) ;
printf("\n enter the position at which the number has to be added");
scanf("%d", &pos) ;

for(i=n;i>=pos;i--) // change position


{
arr[i+1]=arr[i];
}
arr[pos]=num; // write values in desired position
for(i=0;i<=n;i++) // print the values
{
printf("%d", arr[i]);
}

return 0;
}

Q3) Deletion in an array


#include <stdio.h>
int main()
{ int array[100], position, c, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]);
printf("Enter the location where you wish to delete element\n");
scanf("%d", &position);
if ( position >= n+1 )
printf("Deletion not possible.\n");
else
{ for ( c = position - 1 ; c < n - 1 ; c++ )
array[c] = array[c+1];

printf("Resultant array is\n");

for( c = 0 ; c < n - 1 ; c++ )


printf("%d\n", array[c]);
}
return 0;
}

Q4) Linear Search,


Q5) Binary Search
Q6) Sum of elements in an array and find its average
Q8) Largest and smallest in an array
( Given in lecture notes- Array pdf)
Q7) Merging two arrays
#include <stdio.h>
int main()
{
int n1,n2,n3; //Array Size Declaration
int a[10000], b[10000], c[20000];
printf("Enter the size of first array: ");
scanf("%d",&n1);
printf("Enter the array elements: ");
for(int i = 0; i < n1; i++)
scanf("%d", &a[i]);
printf("Enter the size of second array: ");
scanf("%d",&n2);
printf("Enter the array elements: ");
for(int i = 0; i < n2; i++)
scanf("%d", &b[i]);
n3 = n1 + n2;
for(int i = 0; i < n1; i++)
c[i] = a[i];
for(int i = 0; i < n2; i++)
c[i + n1] = b[i];

printf("The merged array: ");


for(int i = 0; i < n3; i++)
printf("%d ", c[i]); //Print the merged array

return 0;
}

Q9) Union of arrays


#include <stdio.h>
int main()
{
int a[10], b[10], flag = 0, n1, n2, i, j;
printf("Enter array1 size : ");
scanf("%d",&n1);
printf("\nEnter array2 size : ");
scanf("%d",&n2);
printf("\nEnter array1 element : ");
for(i = 0;i < n1;i++)
scanf("%d",&a[i]);
printf("\nEnter array2 element : ");
for(i = 0;i < n2;i++)
scanf("%d",&b[i]);
printf("\nUnion:");
for(i = 0;i < n1;i++)
printf("%d, ",a[i]);
for(i = 0;i < n2;i++)
{
for(j = 0;j < n1;j++)
{
if(b[i] == a[j])
{
flag = 1;
}
}
if(flag == 0)
{
printf("%d, ",b[i]);
}
flag = 0;
}
return 0;
}

Intersection of two arrays


#include <stdio.h>
int main()
{
int a[10], b[10], flag = 0, n1, n2, i, j;
printf("Enter array1 size : ");
scanf("%d",&n1);
printf("\nEnter array2 size : ");
scanf("%d",&n2);
printf("\nEnter array1 element : ");
for(i = 0;i < n1;i++)
scanf("%d",&a[i]);
printf("\nEnter array2 element : ");
for(i = 0;i < n2;i++)
scanf("%d",&b[i]);
printf("Intersection: ");
for(i = 0;i < n1;i++)
{
for(j = 0;j < n2;j++)
{
if(b[i] == a[j])
{
flag = 1;
}
}
if(flag == 1)
{
printf("%d ", b[i]);
}
flag = 0;
}
return 0;
}
Q10) Remove duplicate elements in an array
#include <stdio.h>
int main ()
{
// declare local variables
int arr[20], i, j, k, size;

printf (" Define the number of elements in an array: ");


scanf (" %d", &size);

printf (" \n Enter %d elements of an array: \n ", size);


// use for loop to enter the elements one by one in an array
for ( i = 0; i < size; i++)
{
scanf (" %d", &arr[i]);
}
// use nested for loop to find the duplicate elements in array
for ( i = 0; i < size; i ++)
{ for ( j = i + 1; j < size; j++)
{ // use if statement to check duplicate element
if ( arr[i] == arr[j])
{ // delete the current position of the duplicate element
for ( k = j; k < size - 1; k++)
{ arr[k] = arr [k + 1];
}
// decrease the size of array after removing duplicate element
size--;
// if the position of the elements is changes, don't increase the index j
j--;
} } }
/* display an array after deletion or removing of the duplicate elements */
printf (" \n Array elements after deletion of the duplicate elements: ");

// for loop to print the array


for ( i = 0; i < size; i++)
{ printf (" %d \t", arr[i]);
} return 0;
}

Q11) 2-D array, not in MST syllabus


Q12) Sorting done in class
Q13) Array element rotation
#include <stdio.h>

int main()
{
//Initialize array
int arr[] = {1, 2, 3, 4, 5};
//Calculate length of array arr
int length = sizeof(arr)/sizeof(arr[0]);
//n determine the number of times an array should be rotated
int n = 3;

//Displays original array


printf("Original array: \n");
for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}
//Rotate the given array by n times toward left
for(int i = 0; i < n; i++){
int j, first;
//Stores the first element of the array
first = arr[0];
for(j = 0; j < length-1; j++){
//Shift element of array by one
arr[j] = arr[j+1];
}
//First element of array will be added to the end
arr[j] = first;
}

printf("\n");

//Displays resulting array after rotation


printf("Array after left rotation: \n");
for(int i = 0; i < length; i++){
printf("%d ", arr[i]);
}
return 0;
}

Q14) Mean, Median, Mode, Standard Deviation in an Array


int main()
{
float x[MAXSIZE];
int i, n;
float average, variance, std_deviation, sum = 0, sum1 = 0;

printf("Enter the value of N \n");


scanf("%d", &n);
printf("Enter %d real numbers \n", n);
for (i = 0; i < n; i++)
{
scanf("%f", &x[i]);
}
/* Compute the sum of all elements */
for (i = 0; i < n; i++)
{
sum = sum + x[i];
}
average = sum / (float)n;
/* Compute variance and standard deviation */
for (i = 0; i < n; i++)
{
sum1 = sum1 + pow((x[i] - average), 2);
}
variance = sum1 / (float)n;
std_deviation = sqrt(variance);
printf("Average of all elements = %.2f\n", average);
printf("variance of all elements = %.2f\n", variance);
printf("Standard deviation = %.2f\n", std_deviation);
}

Q15) Decimal to Binary in Array


#include<stdio.h>
int main()
{
int i,n,a[10];
printf("\nEnter the Decimal value:");
scanf("%d",&n);
for(i=0;n!=0;i++)
{
a[i]=n%2;
n=n/2;
}
n=i;
printf("\nBinary Value:");
for(i=0;i<n;i++)
{
printf("%d",a[n-i-1]);
}
return 0;
}

You might also like