Invalid operands to binary in my 'NumberSize' function

后端 未结 1 519
逝去的感伤
逝去的感伤 2021-01-29 11:10

I have created a function that is supposed to take the numbers in my linked list and determine which one is the smallest number and which one is the largest number. I created a

1条回答
  •  一向
    一向 (楼主)
    2021-01-29 11:32

    You forgot to step through the list. Using on of the solutions for the average:

    void NumberSize(NewNumber * start)
    {
      double num = 0.0;
    
      double biggest;
      double smallest;
      int flag = 0;
    
      NewNumber *temp = start;
      // set start values
      if(tmp != NULL){
         biggest = temp->newNum;
         smallest = temp->newNum;
         tmp = tmp->next;
         flag = 1;
      }
    
      // while temp is not NULL
      while (temp) {
        // get number from current node
        num = temp->newNum;
        // if that number is bigger than "biggest"
        if (num > biggest) {
          // exchange "biggest" with "num"
          biggest = num;
        }
        // do it the same way for the smallest number
        else if (num < smallest) {
          smallest = num;
        }
        // here is the forgotten line:
        // go to the next node
        temp = temp->next;
      }
      if(flag){
        // no returns, just printing
        printf("The smallest number is: \n", smallest);
        printf("The biggest number is: \n", biggest);
      } else {
        puts("list empty");
      }
    }
    

    0 讨论(0)
提交回复
热议问题