Wednesday 22 June 2016

Given a two dimensional array of string like. <”luke”, “shaw”>; <”wayne”, “rooney”>; <”rooney”, “ronaldo”>; <”shaw”, “rooney”>; Where the first string is “child”, second string is “Father”. And given “ronaldo” we have to find his no of grandchildren Here “ronaldo” has 2 grandchildren. So our output should be 2.

#include <stdio.h>
#include <string.h>

int main()
{
char a[10][100],b[10][100],str[100];
int num,k;
scanf("%d",&num);

scanf("%s",&str);

for(int i=0;i<num;i++)
{
scanf("%s%s",&a[i],&b[i]);
if(strcmp(str,b[i]) == 0)
k = i;
}

int count = 0;

for(int i=0;i<num;i++)
{
if(strcmp(a[k],b[i]) == 0)
count++;
}

printf("%d",count);


return 0;

}

OR

#include <iostream>
#include<cstdlib>
using namespace std;
int count=0;
string a[4][2]={
   {"luke", "shaw"},
   {"wayne", "rooney"},
   {"rooney", "ronaldo"},
   {"shaw", "rooney"}
 };
void grandchi(string x){
  for(int i=0;i<4;i++){
 if(x==a[i][1]){
  count++;
 }
  } 
}
int main() {
 string s,s1;
  
 for(int i=0;i<4;i++){
  for(int j=0;j<2;j++){
   cout<<a[i][j]<<"\n";
  }
  
 }
 cout<<"enter string"<<"\n";
 cin>>s;
 cout<<s;
 for(int i=0;i<4;i++){
  if(s==a[i][1]){
   s1=a[i][0];
   grandchi(s1);
  }
 }
 cout<<"grand children:"<<count;
 return 0;
}



No comments:

Post a Comment