-
Homework help...
Hi guys, i wanna make a function in which you give as parameters two sets of temperatures and it prints the same elements.I 've made this but it doesn't work...
Code:
void tomh(struct list_node *head1, struct list_node *head2 )
{
int j=0,k=0;
struct list_node *p;
if (head1->temperature>=1000 || head2->temperature>=1000)
k=1;
p=head2;
while(head1->temperature<1000)
{
p=head2;
while(head1->temperature<=p->temperature)
{
if(head1->temperature==p->temperature)
{
printf("the %d same element is: %d\n", j+1, p->temperature);
}
p=p->next;
j++;
}
head1=head1->next;
}
if(j==0) k=1;
if(k==1) printf("No same elements\n");
}
Thnx in advance for your help ;)
-
Code:
if (head1->temperature>=1000 || head2->temperature>=1000)
k=1;
p=head2;
Are those two lines supposed to belong to the if, or just the one? (Your indentation needs work.)
Code:
for each element in head1
for each element in head2
if head1->something == head2->something
print it
Now keep in mind that you should be using different pointers for at least h2, so that you can get back to the start each time the inner loop starts again.
Quzah.
-
Hi quzah! About your first advice i forgot to tell that the "price" 1000 or more defines the end of the list so with this IF i check if on of the sets is empty. About your second advice you are right and i "corrected" my code but still it doesn't work... If i m not mistaken for some reason the program doesn't "get" inside the IF that is inside the WHILE... :S
Code:
void tomh(struct list_node *head1, struct list_node *head2 )
{
int j=0,k=0;
struct list_node *p;
if (head1->temperature>=1000 || head2->temperature>=1000)
k=1;
p=head2;
while(head1->temperature<1000)
{
p=head2;
while(head1->temperature>=p->temperature)
{
if(p->temperature==head1->temperature)
{
printf("the %d same element is: %d\n", j+1, p->temperature);
j++;
}
p=p->next;
}
head1=head1->next;
}
if(j==0) k=1;
if(k==1) printf("No same elements\n");
}