Program to find largest and smallest among 5 numbers without using array

前端 未结 15 2279
遇见更好的自我
遇见更好的自我 2021-01-06 07:25

Yesterday I went for an interview where I have been asked to create a program to find largest and smallest among 5 numbers without using array.

I know how to create

15条回答
  •  醉酒成梦
    2021-01-06 08:06

    Heres what I did, without using an array. This was a method to return the highest number of 5 scores.

    double findHighest(double score1, double score2, double score3, double score4, double score5)
     {
       double highest = score1;
       if (score2 > score1 && score2 > score3 && score2 > score4 && score2 > score5)
          highest = score2;
       if(score3 > score1 && score3 > score2 && score3 > score4 && score3 > score5)
          highest = score3;
       if(score4 > score1 && score4 > score2 && score4 > score3 && score4 > score5)
          highest = score4;
       if (score5 > score1 && score5 > score2 && score5 > score3 && score5 > score4)
          highest = score5;
       return highest;
     }
    

    An array is going to be far more efficient, but I had to do it for homework without using an array.

提交回复
热议问题