Click here to Skip to main content
15,887,280 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is my full code:

C++
<pre>
#include <iostream>
#include <algorithm>
using namespace std;

float compute_avewil(const int low_value[30], const int high_value[30])
{
    float average_value[30];
    for(int i=0;i<30;i++) //loop to add and divide indices
    {
        average_value[i] = low_value[i] + high_value[i];
        average_value[i] = average_value[i] / 2;
    }
    for(int i=0;i<30;i++)// to show contents of array
    {
        cout << average_value[i] << " ";
    }

    cout << endl;
    return 0;
}

int find_highthat(float average_value[30])
{
    int maxex = 0;
    float max = average_value[maxex];

    for(int i=1;i<30;++i)
    {
        if(max<average_value[i])
        {
            maxex = i;
            max = average_value[i];
        }
    
    }
    cout << endl;
    return maxex;
}
int main(float average_value[30])
{
    int id_number[30] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30 };
    int high_value[30] = { 10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210,220,230,240,250,260,270,280,290,300 };
    int low_value[30] = { 1,2,3,4,5,6,7,8,9,10,9,8,7,6,5,4,3,2,1,0,1,2,3,4,5,4,3,2,1,0 };

    compute_avewil(low_value, high_value);
    find_highthat(average_value);
    
}



The Exception thrown issue comes in at:
C++
int find_highthat(float average_value[30])
{
    int maxex = 0;
    float max = average_value[maxex];

    for(int i=1;i<30;++i)
    {
        if(max<average_value[i])
        {
            maxex = i;
            max = average_value[i];
        }
    
    }
    cout << endl;
    return maxex;
}


Specifically the line:
C++
float max = average_value[maxex];


I thought this was weird since this function seemed ok to me. I searched up similar problems, I knew what the meaning of the problem was but my code seemed fine, so I do not see the problem nor do I know any solution for it.

What I have tried:

I tried searching up what's wrong, I get what the problem it self means but I still have not fix it since I don't know any solutions and the function itself seems ok to me.
Posted
Updated 22-Nov-23 14:19pm
v2

Your code fixed and heavily commented:
C++
#include <iostream>
using namespace std;

enum
{
  Items = 30, // use a symbolic name, instead of a number
};


//float compute_avewil(const int low_value[30], const int high_value[30])

// pass the output parameter 'average_value' explicitely. Make the return value 'void', this function always succeds
void compute_avewil(const int low_value[], const int high_value[], float average_value[])
{
    for(int i=0;i<Items;i++) //loop to add and divide indices
    {
        average_value[i] = low_value[i] + high_value[i];
        average_value[i] = average_value[i] / 2;
    }
    // do not output anything here, keep computations and output seperated
}

int find_highthat(float average_value[])
{
    int maxex = 0;
    float max = average_value[maxex];

    for(int i=1;i<Items;++i)
    {
        if(max<average_value[i])
        {
            maxex = i;
            max = average_value[i];
        }
    }
    // do not output anything here, ...
    return maxex;
}

//int main(float average_value[30])
int main() // main has a fixed signature, either 'int main()' or 'int main(int argc, char * argv[])'
{
    //What's the purpose of this?  int id_number[30] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30 };
    int high_value[Items] = { 10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210,220,230,240,250,260,270,280,290,300 };
    int low_value[Items] = { 1,2,3,4,5,6,7,8,9,10,9,8,7,6,5,4,3,2,1,0,1,2,3,4,5,4,3,2,1,0 };
    float average_value[Items]; // create the array used for results


    compute_avewil(low_value, high_value, average_value); // compute the averages

    // show the average
    cout << "averages: ";
    for(int i=0;i<Items;i++)
    {
        cout << average_value[i] << " ";
    }
    cout << endl;


    int maxex = find_highthat(average_value); // find the maximum of the averages

    // show the maximum of the averages
    cout << "maximum of averages: average_value[" << maxex << "] = " << average_value[maxex] << endl;
}


Note, C++ allows a more terse approach:
C++
#include <algorithm>
#include <array>
#include <vector>

using namespace std;

using Value = array<int, 2>; // first item of the array is 'high', second item is 'Low'
using Values = vector<Value>; // the collection of 'high' and 'low' 

double avg(const Value & v) // computes the average
{
  return (v[0] + v[1]) / 2.0;
}


int main()
{ //                 high,low   high,low   high,low   high,low
  Values values = { { 10, 1 }, { 20, 2 }, { 30, 3 }, { 40, 4 } /*, {50,5} and so on.. */ };

  vector <double> averages;

  for (auto const & v : values)
    averages.push_back(avg(v));


  cout << "averages ";
  for (auto a : averages)
    cout << a << " ";
  cout << endl;

  auto itamax = max_element(averages.begin(), averages.end());
  auto amax_index = distance(averages.begin(), itamax);
  cout << "max of averages: averages[" << amax_index << "] = " << (*itamax) << endl;
}
 
Share this answer
 
v2
Comments
k5054 23-Nov-23 11:57am    
We could even delete the avg() function and use a lambda:
 averages.push_back( [v](){ return (v[0] + v[1])/2.0; }() )
Dave Kreskowiak 23-Nov-23 16:17pm    
Not a good idea in a homework assignment. Use that and the teacher will know the OP got outside help at the least and will even suspect the code is not the OP's work.
CPallini 23-Nov-23 16:20pm    
While you're right, let us (OP included) have a glimpse of elegant C++
CPallini 23-Nov-23 16:21pm    
Wow. Indeed.
merano99 25-Nov-23 6:11am    
For performance optimization, you could perhaps also use std::transform.
transform(values.begin(), values.end(), back_inserter(averages),
[](const Value &v) { return (v[0] + v[1]) / 2.0; });
From your exception message you can understand that it fault reading the data. As you specify the faulting line is the float max = average_value[maxex] then it not able to access the data of your array average_value at the index of maxex. You have the array parameter function argument in range of 0 to 30 and your variable maxex has value of 0 which is in valid array range. The the issue is in the average_value parameter itself and outside the find_highthat function. So you should look higher. From your main code you can see that the you made calculation of average_value in the compute_avewil function. And the average_value variable defined locally. Surprisal the array with the same name appears as an argument of your main function and that argument is passed to the find_highthat function which is fault. So the issue cause the parameter which you pass to the main function and the code for that is not displayed in the snippet you provided.

I think you should declare the average_value in main function as a variable then pass it to the compute_avewil function so it will be filled in there and then use it in find_highthat function. The local declaration of that variable in compute_avewil function should be removed. The argument with name average_value of the main function also should be removed.

Regads,
Maxim.
 
Share this answer
 
A non-standad-main function has been noticed in the program, which may not be initialized.
C++
int main(float average_value[30])

The access to average_value could then be the cause.
If it is test data, random numbers could possibly be used.
C++
int main(void)
{
    float average_value[30];
    randomize();
    for (int i = 0; i < sizeof(average_value) / sizeof(float); i++) {
        average_value[i] = (float)(rand() * 100.0) / RAND_MAX;
    }
    
    ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900