Telephone

QUESTION

You Have to find the name of the person having phone number \”XXXXXXXXXX\” in the telephone directory. Since the telephone directory is in alphabetical order not by numbers, you have to go through each and every name of the telephone directory.

ANSWER

#include <iostream>
#include<cstring>
using namespace std;

void sort(char arr[][20],long m[], int n)
{
   int i, j;long tmp;
  char key[20];
   for (i = 1; i < n; i++)
   {
       strcpy(key,arr[i]);
     tmp=m[i];
       j = i-1;
       while ((j >= 0) && (strcmp(arr[j],key)>0))
       {
           strcpy(arr[j+1] , arr[j]);
           m[j+1]=m[j];
           j = j-1;
       }
       strcpy(arr[j+1] , key);
     m[j+1]=tmp;
   }
}

int main()
{
    int N;long m[20],p;
  char a[20][20];
    cin>>N;
    for(int i=0;i<N;i++)
      cin>>a[i]>>m[i];
  sort(a,m,N);
  cout<<"Ordered List\n";
    for(int i=0;i<N;i++)
      cout<<a[i]<<" "<<m[i]<<endl;
  
  cin>>p;
  cout<<"\nName Telephone Number\n";
  for(int i=0;i<N;i++)
    if (m[i]==p){
      cout<<a[i]<<" "<<m[i]<<endl;
      return 0;
    }
  cout<<"The Entered Number is not in the Directory";
	return 0;
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
CHP Adblock Detector Plugin | Codehelppro