hi, i need help with how to find the max number and min number from an array. there are 31 numbers, i know that i am suppose to use a for loop to do this and this is what i got so far
array is inputed by user

#include <iostream.h>
int main ()
{
   int temp[31];
   for(int i=0;i<31;i++)
   {
       cout<<"ENTER THE TEMPERATURE FOR THE MONTH OF DECEMBER"<<endl;
       cin>>temp[i];
    }
   for(i=0;i<31;i++)
   {
      if(temp[i]>temp[i+1])

this is where im stuck, i don't know what to do next. i need to find the max and min so now i just need to find a way to swap the order of the array so later on i can just cout the first and last element of the array to show the max and min.

Dani AI

Generated

The simplest, safest approach is a single linear pass: keep a current minimum and maximum and update them as values are read. That avoids sorting the whole array (unnecessary work for just min/max) and fixes the out-of-bounds bug in the original attempt where temp[i] was compared with temp[i+1] on the last iteration. pointed the right direction with a min/max scan; that idea just needs careful initialization and input checking.

A robust pattern is to set min and max from the first valid input, then update for each subsequent value:

#include <iostream>

int main() {
    int minVal = 0, maxVal = 0;
    for (int i = 0; i < 31; ++i) {
        int v;
        if (!(std::cin >> v)) return 1; // handle invalid input
        if (i == 0) minVal = maxVal = v;
        else {
            if (v < minVal) minVal = v;
            if (v > maxVal) maxVal = v;
        }
    }
    std::cout << "Min: " << minVal << "  Max: " << maxVal << '\n';
}

As an alternative, read all values into a container and use the standard algorithm std::minmax_element from <algorithm> to get both in one call. That is compact and well-tested; see std::minmax_element for details.

Quick troubleshooting tips: do not use old headers like iostream.h—use <iostream> and std::. Always check std::cin for parse failures, avoid accessing temp[i+1] at the end of the array (undefined behavior), and prefer std::vector or std::array<int,31> for clearer code. For 31 values a single O(n) scan is optimal and simple.

Recommended Answers

All 3 Replies

What I would do is this instead of the "read" loop:

int tmax, tmin;

tmax=temp[0];
tmin=temp[0];

for(int i=0; i<31; i++){
    if(temp[i]>tmax){
        tmax=temp[i];
    }
    if(temp[i]<tmin){
        tmin=temp[i];
    }
}
cout<<"The maximum temperature is "<<tmax<<".\n";
cout<<"The minimum temperature is "<<tmin<<".\n";

Do you see where I'm going with this?

Writing an array-sorting algorithm has been the bane of intro-C++ courses since the language was invented, I think. Being a hobbyist, I try to avoid having to sort. If you just need the max and min, what I posted should work fine. If not, ask someone more experienced.

thanks, i understand

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.