1d array

 1. A left rotation operation on an array of size n shifts each of the array's elements 1 unit to the 

left. For example, if 2 left rotations are performed on array [1,2,3,4,5], then the array would 

become [3,4,5,1,2]. Given an array of n integers and a number, d, perform d left rotations on 

the array. Then print the updated array as a single line of spaceseparated integers.

Sol: #include<stdio.h>

int main()

{

 int i,s,j,t;

 long long int n;

 scanf("%lld",&n);

 int a[n];

 scanf("%d",&s);

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

 {

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

 }

 for(j=1;j<=s;j++)

 {

 t=a[0];

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

 {

 a[i]=a[i+1];

 }

 a[n-1]=t;

 }

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

 {

 printf("%d ",a[i]);

 }

 return 0;

}

2. Develop a program to accept the age of n students of class, rearrange the data in ascending 

order and display the age of youngest and eldest student in class. (Use Bubble Sort)

Sol: #include<stdio.h>

int main()

{

int i,n,a[50],temp,j;

printf("enter n value\n");

scanf("%d",&n);

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

{

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

}

for(j=1;j<n;j++)

{

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

 {

 if(a[i]>a[i+1])

{

 temp=a[i];

 a[i]=a[i+1];

a[i+1]=temp;

}

}

}

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

{

printf("%d\n",a[i]);

}

return 0;

}

3. Given an array {9,18,27,36,45,54,63,72,81,90,99}. Trace the steps of binary search algorithm 

to find the values 90 from the array. Write the C module to implement binary search 

algorithm.

Sol: #include<stdio.h>

 int main()

 {

int i,n,l,u,a[100],key,mid,search=0;

scanf("%d",&n);

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

{

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

}

printf("enter element to search");

scanf("%d",&key);

l=0;

u=n-1;

while(l<=u)

{

mid=(l+u)/2;

if(key==a[mid])

{

search=1;

break;

}

else if(key>a[mid])

 {

 l=mid+1;

}

else

{

u=mid-1;

}

}

if(search==1)

{

printf("\n the element is found at the location=%d",mid+1);

}

else

{

printf("\n element is not found");

}

 return 0;

}

4. Given an array arr[] and size of array is n and one another key x, and give you a segment size 

k. The task is to find that the key x present in every segment of size k in arr [].

 Sol: 

 #include<stdio.h>

int main()

{

 int a[100],t,i,n,key,search;

 printf("Size of array is :");

 scanf("%d",&n);

 printf("\nEnter the array elements : ");

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

 { 

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

 }

 printf("\n Enter element to search : ");

 scanf("%d",&key);

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

 {

 if(key==a[i])

 {

 t=i;

 search=1;

 break;

 }

 }

 if(search==1)

 {

 printf("\n Element found at location = a[%d]",t);

 }

 else

 {

 printf("\n Element not found");

 }

 return 0;

}

5. Write a program in C to count a total number of duplicate elements in an array.

Sol: #include <stdio.h> 

 int main() 

 { 

 int arr[] = {1, 2, 3, 4, 2, 7, 8, 8, 3}; 

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

 printf("Duplicate elements in given array: \n"); 

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

 for(int j = i + 1; j < length; j++) { 

 if(arr[i] == arr[j]) 

 printf("%d\n", arr[j]); 

 } 

 } 

 return 0; 

}

6.Write a program in C to find the second largest element in an array.

Sol:

 #include<stdio.h>

int main()

{

int a[100],n,i,p,temp;

scanf("%d",&n);

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

{

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

}

for(p=1;p<n;p++)

{

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

{

if(a[i]>a[i+1])

{

temp=a[i];

a[i]=a[i+1];

a[i+1]=temp;

}

}

}

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

{

if(i==n-2)

{

printf("\n second largest=%d",a[i]);

}

}

return 0;

}

Comments

Popular posts from this blog

strings

2d array