Thread: Homework help...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    7

    Exclamation 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

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    7
    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");
    }
    Last edited by pSyXaS; 05-08-2011 at 02:24 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I'll do your homework for you....
    By Salem in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 04-13-2006, 03:14 PM
  2. Need help on my homework
    By mejv3 in forum C++ Programming
    Replies: 15
    Last Post: 10-11-2005, 03:43 PM
  3. homework help
    By computerjunkie5 in forum C++ Programming
    Replies: 13
    Last Post: 10-27-2003, 11:54 AM