I need help finding the max of the array.

0 votes
asked Apr 1 by TylerBlachowiak (230 points)

This is my code so far. The code works properly until the highlighted section. It has to be in C++ not C.

#include <stdio.h>
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
    const int array_size = 32;
    int daily_temp[array_size];
    int num_values;
    int index;
    float average_high;
    int total = 0;
    int min;
    int max;
    
    
    do
    {
     cout<< "Enter the number of days for which you have data: ";
     cin>>num_values;
     if((num_values<1)||(num_values > array_size-1))
     {
     cout<< "The number of days must be in the range 1 to "
         << array_size-1<<endl;
     }
    }while((num_values<1)||(num_values > array_size-1));
    
    for(index = 1; index<= num_values; index++)
    {
        cout<<"Enter the high temperature for day "<<index<< ": ";
        cin>>daily_temp[index];
    }
    
    cout<< "The array contains high temperatures for " << num_values
        <<" days.\n";
    cout<< "The values are as follows.\n";
    
    for(index = 1; index<= num_values; index++)
    {
        cout<< "Day "<< index<< ": "<< daily_temp[index]<<endl;
        total=total+daily_temp[index];
    }
    
    average_high= float(total)/ float(num_values);
    
    cout<<"The average high temperature during the "<<num_values
        <<"-day period was "<< setprecision(2)<< average_high
        <<" degrees.\n";
    
    
    max = daily_temp[index];


    for (index = 0; index < num_values; index++)
    {
        if (max < daily_temp[index])
            max = daily_temp[index];
    }
    min = daily_temp[index];


    for (index = 0; index < num_values; index++)
    {
        if (min > daily_temp[index])
            min = daily_temp[index];
    }

    cout << "Largest element: " << max <<endl;
    cout << "Smallest element: " << min <<endl;


    return 0;
}

1 Answer

+1 vote
answered Apr 2 by Vladimir Sánchez Garrido (160 points)
I marked with //////////////// the lines that I changed to make it works, but, this is not the correct way to do what you want to do.
I hope I have been helpful. Greetings.

#include <stdio.h>
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
    const int array_size = 32;
    int daily_temp[array_size];
    int num_values;
    int index;
    float average_high;
    int total = 0;
    int min;
    int max;
    
    do
    {
     cout << "Enter the number of days for which you have data: ";
     cin >> num_values;
     if ((num_values < 1)||(num_values > array_size - 1))
     {
     cout << "The number of days must be in the range 1 to "
          << array_size - 1 << endl;
     }
    }while ((num_values < 1)||(num_values > array_size - 1));
    
    for (index = 0; index < num_values; index++)////////////////
    {
        cout << "Enter the high temperature for day " << index + 1 << ": ";////////////////
        cin >> daily_temp[index];
    }
    
    cout << "The array contains high temperatures for " << num_values
         <<" days.\n";
    cout << "The values are as follows.\n";
    
    for(index = 0; index < num_values; index++)
    {
        cout << "Day " << index + 1 << ": " << daily_temp[index] << endl;////////////////
        total = total + daily_temp[index];
    }
    
    average_high = float(total) / float(num_values);
    
    cout << "The average high temperature during the " << num_values
         << "-day period was " << setprecision(2) << average_high
         << " degrees.\n";
    index = 0;////////////////
    max = daily_temp[index];
    for (index = 0; index < num_values; index++)
    {
        if (max < daily_temp[index])
            max = daily_temp[index];
    }
    index = 0;
    min = daily_temp[index];
    for (index = 0; index < num_values; index++)
    {
        if (min > daily_temp[index])
            min = daily_temp[index];
    }

    cout << "Largest element: " << max << endl;
    cout << "Smallest element: " << min << endl;
    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...