Hi everyone,
This program returns 1 if person N1 is hierarchical superior of person N2 or 0 otherwise.
The assignment to People[] in the code below is meant that Vanessa is a direct superior of Peter, Harry is a direct superior of George.
A person N1 is said to be a "hierarchical superior" of another person N2 if either:
N1 is a direct superior of N2
N1 is a direct superior of a person N3 who is a superior of N2.
I can get result 1 of the "right" case, but if the N1 is not HS of the N2 then the program terminates. Why?
Try with difference names in N1 and N2.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 6
#define SIZE 100
typedef struct{
char *personA;
char *personB;
}people;
int IsSuperior(people array[N],char Name1[SIZE], char Name2[SIZE]);
int main(){
static people People[N]={"Vanessa", "Peter",
"Harry", "George",
"George", "Tony",
"Ron", "Andrea",
"Vanessa", "Diana",
"Tony", "Ron"};
int result, i;
char N1[SIZE], N2[SIZE];
for(i=0;i<6;i++){
printf("%s, %s\n", People[i].personA, People[i].personB);
}
strcpy(N1,"Harry");
strcpy(N2,"Ron");
result = IsSuperior(People,N1,N2);
printf("%d\n", result);
return 0;
}
/*************************************************************/
/*Task: returns 1 if person N1 is hierarchical superior of person N2
* or 0 otherwise.
*/
int IsSuperior(people array[N],char Name1[SIZE], char Name2[SIZE]){
int i = 0;
int found = 0;
//int test;
while(strcmp(Name1,array[i].personA)!=0){
i++;
}
printf("test2\n");
if(strcmp(array[i].personB, Name2) == 0){
found = 1;
}
else
found = IsSuperior(array,array[i].personB,Name2);
return found;
}