i have a homework assignment asking me to pick the highest and lowest number out of a group of 4 numbers? How would i set that up?

Recommended Answers

All 2 Replies

you need two variable -- highest and lowest. Initialize them to be the value of the first array element. Next code a loop to count from 1 to the number of items in the array, then test each array element against those two variables, and change the variable value to be the array element when appropriate.

Finding the largest and smallest numbers in a set using "if" statements is normally a horrible idea, but if the size is only 4 then I would recommend some "if"s.

smallest = values[0];
largest = values[0];
for(i = 0; i < 4; i++)
   {
      if(values[i] > largest)
           largest = values[i];
      else if(values[i] < smallest)
           smallest = values[i];
   }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.