Sunday 23 April 2023

Exercise 9-To demonstrate use of 1-D arrays and functions.

SET A
1. Write a program to accept n numbers in the range of 1 to 25 and count the frequency of
occurrence of each number.

#include<stdio.h>
#include<conio.h>
void main()
{
 int a[10],n,cnt[25],c=0,i,j;
 clrscr();
 printf("\nEnter number:");
 scanf("%d",&n);
 printf("\n%d numbers between 1 t0 25:",n);
 for(i=0;i<n;i++)

     scanf("%d",&a[i]);
 for(i=0;i<25;i++)
     cnt[i]=0;
 for(i=0;i<n;i++)
    {
     for(j=0;j<25;j++)
{
if(a[i]==j)
    cnt[j]=cnt[j]+1;
}

    }
 for(i=0;i<25;i++)
    {
     if(cnt[i]>0)
       printf("\n %d occurenc is %d",i,cnt[i]);
    }
 getch();
}

OUTPUT:

Enter number:7                                                                 
                                                                               
7 numbers between 1 t0 25:                                                     
1                                                                             
2                                                                             
3                                                                             
3
2                                                                             
1                                                                             
5                                                                             
                                                                               
 1 occurenc is 2                                                               
 2 occurenc is 2                                                               
 3 occurenc is 2                                                               
 5 occurenc is 1                                                               

2. Write a function for Linear Search, which accepts an array of n elements and a key as
parameters and returns the position of key in the array and -1 if the key is not found. Accept n
numbers from the user, store them in an array. Accept the key to be searched and search it using
this function. Display appropriate messages.

#include<stdio.h>
#include<conio.h>

int search(int a[],int n,int x)
{
 int ans=-1,i;
 for(i=0;i<n;i++)
    {
     if(a[i]==x)
       ans=i;
    }
 return ans;
}
void main()
{
 int n,a[20],i,x,ans;
 clrscr();
 printf("\nEnter How many Elemennts:");
 scanf("%d",&n);
 printf("\nEnter Elemenet:");
 for(i=0;i<n;i++)
     scanf("%d",&a[i]);
 printf("\nEnter element to be search:");
 scanf("%d",&x);
 ans=search(a,n,x);
 if(ans==-1)
    printf("\n%d Not Found:",x);
 else
    printf("\n%d Found at %d loc",x,ans);
 getch();

}
OUTPUT:

Enter How many Elemennts:3                                                   
                                                                             
Enter Elemenet:3                                                             
2                                                                             
1                                                                             
                                                                             
Enter element to be search:2                                                 
                                                                             
2 Found at 1 loc                                                             
Enter How many Elemennts:3                                                   
                                                                             
Enter Elemenet:3                                                             
2                                                                             
1                                                                             

Enter element to be search:4                                                 
                                                                             
4 Not Found:                                                                 
                                                                             

3. Write a function, which accepts an integer array and an integer as parameters and counts
the occurrences of the number in the array.


#include<stdio.h>
#include<conio.h>
void occurs(int a[],int n,int oc)
{
 int i,cnt=0;
 for(i=0;i<n;i++)
    {
     if(a[i]==oc)
       cnt++;
    }
 printf("\n%d occurs %d times",oc,cnt);

}
void main()
{
 int n,a[20],i,oc;
 clrscr();
 printf("\nEnter how many Numbers:");
 scanf("%d",&n);
 printf("\nEnter Numbers:");
 for(i=0;i<n;i++)
     scanf("%d",&a[i]);
 printf("\nEnter number for count occurrences:");
 scanf("%d",&oc);
 occurs(a,n,oc);
 getch();
}

OUTPUT:

Enter how many Numbers:4                                                     
                                                                             
Enter Numbers:4                                                               
4                                                                             
2                                                                             
1                                                                             
                                                                             
Enter number for count occurrences:4                                         
                                                                             
4 occurs 2 times                                                             

  4. Write a program to accept n numbers and store all prime numbers in an array called prime.
Display this array.

#include<stdio.h>
#include<conio.h>
void main()
{
 int n,i,a[20],prime[20],flag=0,cnt=0,j;
 clrscr();
 printf("\nEnter how many elements:");
 scanf("%d",&n);
 printf("\nEnter numbers:");
 for(i=0;i<n;i++)
    scanf("%d",&a[i]);
 for(i=0;i<n;i++)
    {
     flag=0;
     for(j=2;j<a[i];j++)
{
if(a[i]%j==0)
   {
    flag=1;
    break;
   }
}
     if(flag==0)
       {
prime[cnt]=a[i];
cnt++;
       }
    }
 printf("\nPrime numbers:");
 for(i=0;i<cnt;i++)
     printf("\n%d",prime[i]);
 getch();
}

OUTPUT:

Enter how many elements:5                                                     
                                                                             
Enter numbers:5                                                               
3                                                                             
4                                                                             
6                                                                             
8                                                                             
                                                                             
Prime numbers:                                                               
5                                                                             
3                                                                             

SET B

1. Write a program to accept n numbers from the user and store them in an array such that the
elements are in the sorted order. Display the array. Write separate functions to accept and display
the array. (Hint: Insert every number in its correct position in the array)

#include<stdio.h>
#include<conio.h>
void accept(int a[],int n)
{
 int i;
 for(i=0;i<n;i++)
     scanf("%d",&a[i]);
}
void display(int a[],int n)
{
 int i;
 for(i=0;i<n;i++)
     printf("\n%d",a[i]);
}
void bsort(int a[],int n)
{
 int i ,j,t;
 for(i=0;i<n;i++)
    {
     for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
   {
    t=a[j];
    a[j]=a[j+1];
    a[j+1]=t;
   }
}
    }
}
void main()
{
 int n,a[20];
 clrscr();
 printf("\nEnter how many Numbers:");
 scanf("%d",&n);
 accept(a,n);
 bsort(a,n);
 display(a,n);
 getch();
}

OUTPUT:

Enter how many Numbers:6                                                     
6                                                                             
5                                                                             
4                                                                             
3                                                                             
2                                                                             
1                                                                             
                                                                             
1                                                                             
2                                                                             
3                                                                             
4                                                                             
5                                                                             
6                                                                             
2. Write a function to sort an array of n integers using Bubble sort method. Accept n numbers
from the user, store them in an array and sort them using this function. Display the sorted array.



3. Write a program to accept a decimal number and convert it to binary, octal and hexadecimal.
Write separate functions.

#include<stdio.h>
void Binary(int n)
{
 int a[10],cnt=0,i;
 while(n>0)
      {
       a[cnt]=n%2;
       n=n/2;
       cnt++;
      }
 printf("\nBinary no is:\n");
 for(i=0;i<cnt;i++)
     printf("%d",a[i]);
}
void Octal(int n)
{
 int a[10],cnt=-1,i;
 while(n>0)
      {
       a[cnt++]=n%8;
       n=n/8;

      }
 printf("\nOctal no is:\n");
 for(i=0;i<cnt;i++)
     printf("%d",a[i]);
}
void Hexadeci(int n)
{
 int a[10],cnt=-1,i,r;
 while(n>0)
      {
       r=n%16;
       if(r<10)
a[cnt++]=48+r;
       else
a[cnt++]=55+r;

      n=n/16;
      }
 printf("\nHexadecimal no is:\n");
 for(i=0;i<cnt;i++)
     printf("%c",a[i]);
}
void main()
{
 int n,i=0,ch;
 clrscr();
 printf("\nEnter Number:");
       scanf("%d",&n);
   printf("\n1.Binary\n2.Octal\n3.Hexadecimal\n4.exit");
 while(ch!=4)
      {


       printf("\nEnter your choise:");
       scanf("%d",&ch);
       switch(ch)
     {
      case 1:Binary(n);
     break;
      case 2:Octal(n);
     break;
      case 3:Hexadeci(n);
     break;
     }
      }

 getch();
}

OUTPUT:

Enter Number:401                                                             
                                                                             
1.Binary                                                                     
2.Octal                                                                       
3.Hexadecimal                                                                 
4.exit                                                                       
Enter your choise:1                                                           
                                                                             
Binary no is:                                                                 
100010011                                                                     
Enter your choise:2                                                           
                                                                             
Octal no is:                                                                 
26
Enter your choise:3

Hexadecimal no is:
91
Enter your choise:4

4. Write a program to find the union and intersection of the two sets of integers (store it in two
arrays).

#include<stdio.h>
#include<conio.h>

void inters(int a[],int n1,int b[],int n2)
{
 int k[20],cnt=0,y,i,j;
 for(i=0;i<n1;i++)
     {
      for(j=0;j<n2;j++)
{
  if(a[i]==b[j])
    {
     k[cnt]=a[i];
     cnt=cnt+1;
     break;
    }
}
     }
 printf("\nIntersect array is:");
 for(i=0;i<cnt;i++)
     printf("\n%d",k[i]) ;
}
void main()
{
 int a[10],b[10],n1,n2,i;
 clrscr();
 printf("\nEnter the how many elemenets in array:");
 scanf("%d",&n1);
 printf("\nEnter elemenets:");
 for(i=0;i<n1;i++)
     scanf("%d",&a[i]);
 printf("\nEnter hoe many elements in second array:");
 scanf("%d",&n2);
 printf("\nEnter elemenets:");
 for(i=0;i<n2;i++)
     scanf("%d",&b[i]);

 inters(a,n1,b,n2);
 getch();
}

OUTPUT:
Enter the how many elemenets in array:4                                       
                                                                             
Enter elemenets:4                                                             
3                                                                             
2                                                                             
1                                                                             
                                                                             
Enter hoe many elements in second array:5                                     
                                                                             
Enter elemenets:                                                             
5                                                                             
4
3                                                                             
2                                                                             
1                                                                             
                                                                             
Intersect array is:                                                           
4                                                                             
3                                                                             
2


No comments:

Post a Comment

Questions

  Exercise 1 Set A . Apply all the three program development steps for the following examples.  1. Accept dimensions of a cylinder and p...