Hi, can anyone translate the following code into an algorithm/java code? I don\'
ID: 3607459 • Letter: H
Question
Hi, can anyone translate the following code into an algorithm/java code? I don't understand pointers but I do understand algorithms/java programming language, so it'll be easier to understand
The problem is :
You want to throw a big party and have put together a list of your n friends. You also know who knows whom, so you have a list containing which pairs of people know each other. Being a major socialite and a networking guru, you decide that it would best to invite the maximum number of friends possible, subject to the following constraint: at the party, each person should have at least four people they know and at least four people they do not know.
Design an efficient algorithm that takes as input the list of n people and the list of pairs who know each other and outputs the best choice of party invitees. Analyze the worst-case running time in terms of n. The more efficient your algorithm is in terms of its asymptotic worst-case running time, the more credit you may get.
Code :
#include<stdio.h>
#include<string.h>
Void main()
{
char **ppl,**prs,*p;
int num_elements,i,num_pairs,j,*know_count;
printf("How Many People");
scanf("%d",num_elements);
*know_count = (int*) calloc(num_elements, sizeof(int));
**ppl = (char**) calloc(num_elements, sizeof(char*));
for ( i = 0; i < num_elements; i++ )
{
ppl[i] = (char*) calloc(10, sizeof(char));
}
printf("Enter the name of %d People",num_elements);
for(i = 0; i < num_elements; i++)
scanf("%s",ppl[i]);
printf("How Many Pairs");
scanf("%d",num_pairs);
**prs = (char**) calloc(num_pairs, sizeof(char*));
for ( i = 0; i < num_pairs; i++ )
{
prs[i] = (char*) calloc(21, sizeof(char));
}
printf("Enter Pairs(Names seperated by -)");
for(i = 0; i < num_pairs; i++)
scanf("%s",prs[i]);
for(i=0;i<num_elements;i++)
{
for(j=0;j<num_pairs;j++)
{
p=strstr(prs[j],ppl[i]);
if(p)
{
know_count[i]++;
p=null;
}
}
}
printf("Following is the list of Invitees");
for(i=0;i<num_elements;i++)
{
if(know_count[i]>5 && ((num_elements-know_count)>5))
printf("%s /n",ppl[i]);
}
}
Explanation / Answer
The algorithm for the code is as follows:
1.Take the number of people as the first input (say n)
2.Dynamically create a array of strings to hold the names of n people)(Say P)
3.Input the number of pairs
4.Dynamically create a array of strings to hold the strings for m pairs) (say M)
every pair is name1-name2
5.Have a parallel array for storing the count of the persons known to evrey person in P. (Say K)
6.Then populate the data in K by going in a loop in P for every person, search its presence in M.Whenever an entry in M contains P[i]) increase the count in K[i].
7. Once all the names get processed. Print out all the i's in P wherevere K[i]
is more than 5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.