Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've been trying to figure out the "too much initializer" problem for hours, but all the solutions I tried leads to a dead end.

Here is the code:

C++
#include 
#include 
using namespace std;

float compute_avewil(const int low_value[30], const int high_value[30])
{
    float average[29];

    average[0] = high_value[0] + low_value[0] / 2.0f;
    float a = average[0];
    average[1] = high_value[1] + low_value[1] / 2.0f;
    float b = average[1];
    average[2] = high_value[2] + low_value[2] / 2.0f;
    float c = average[2];
    average[3] = high_value[3] + low_value[3] / 2.0f;
    float d = average[3];
    average[4] = high_value[4] + low_value[4] / 2.0f;
    float e = average[4];
    average[5] = high_value[5] + low_value[5] / 2.0f;
    float f = average[5];
    average[6] = high_value[6] + low_value[6] / 2.0f;
    float g = average[6];
    average[7] = high_value[7] + low_value[7] / 2.0f;
    float h = average[7];
    average[8] = high_value[8] + low_value[8] / 2.0f;
    float i = average[8];
    average[9] = high_value[9] + low_value[9] / 2.0f;
    float j = average[9];
    average[10] = high_value[10] + low_value[10] / 2.0f;
    float k = average[10];
    average[11] = high_value[11] + low_value[11] / 2.0f;
    float l = average[11];
    average[12] = high_value[12] + low_value[12] / 2.0f;
    float m = average[12];
    average[13] = high_value[13] + low_value[13] / 2.0f;
    float n = average[13];
    average[14] = high_value[14] + low_value[14] / 2.0f;
    float o = average[14];
    average[15] = high_value[15] + low_value[15] / 2.0f;
    float p = average[15];
    average[16] = high_value[16] + low_value[16] / 2.0f;
    float q = average[16];
    average[17] = high_value[17] + low_value[17] / 2.0f;
    float r = average[17];
    average[18] = high_value[18] + low_value[18] / 2.0f;
    float s = average[18];
    average[19] = high_value[19] + low_value[19] / 2.0f;
    float t = average[19];
    average[20] = high_value[20] + low_value[20] / 2.0f;
    float u = average[20];
    average[21] = high_value[21] + low_value[21] / 2.0f;
    float v = average[21];
    average[22] = high_value[22] + low_value[22] / 2.0f;
    float w = average[22];
    average[23] = high_value[23] + low_value[23] / 2.0f;
    float x = average[23];
    average[24] = high_value[24] + low_value[24] / 2.0f;
    float y = average[24];
    average[25] = high_value[25] + low_value[25] / 2.0f;
    float z = average[25];
    average[26] = high_value[26] + low_value[26] / 2.0f;
    float aa = average[26];
    average[27] = high_value[27] + low_value[27] / 2.0f;
    float ab = average[27];
    average[28] = high_value[28] + low_value[28] / 2.0f;
    float ac = average[28];
    average[29] = high_value[29] + low_value[29] / 2.0f;
    float ad = average[29];

    average[30] = { a,b,c,d };

    cout max)
        {
            max = average[i];
        }
    }
}

    

int main(float average[30], int n)
{
    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, n);

    
}


I am also trying (and failing) to find solutions for the

C++
int find_highthat(float average[30],int n,float id_number)
{
    int i;
    int max = average[30];

    for(i=0;i<n;i++)
    {
        if(average[i]>max)
        {
            max = average[i];
        }
    }
}


solution, whats supposed to happen is that the array from the function above this should go to this function and it should get the greatest value in the array, I should also make another function that gets the opposite of the greatest value from the same array.

What I have tried:

I tried researching and coming up for my own solutions for hours on these problems, and the results either don't work or there is usually something wrong with the code that i cannot solve.
Posted

Well, that's simple: if you declare
Quote:
float average[29];
Then you cannot initialize the (not existing) average[29] item.

In order to fix that, properly declare
C++
float average[30];
This way you have 30 items: from 0 to 29 (included).

Moreover, the following piece of code
Quote:
average[30] = { a,b,c,d };

cout max)
{
max = average[i];
}
is wrong and, frankly, quite incomprehensible. What are you trying to do?


Eventually, we have
Quote:
find_highthat
Assuming you're trying to find out the maximum value in the average array, then you could write it this way
C++
int find_max(float average[30])
{
  int index_of_max = 0;
  float max = average[index_of_max]; // initialize both 'index_of_max' and 'max' using item 0

  // then search for the maximum in the remaining items
  for (int i=1; i<30; ++i)
  {
    if ( max < average[i] )
    {
      index_of_max = i;
      max = average[i];
    }
  }
  return index_of_max;
}


Two final notes:
  • Use loops instead of long list of statements.
  • Use double instead of float, they are more precise and possibly faster, in your system.
 
Share this answer
 
v2
Comments
SAMBA ANGELES 22-Nov-23 16:34pm    
average[30] = { a,b,c,d };

cout max)
{
max = average[i];
}


For this piece of code:
average[30] = { a,b,c,d };

The array was for putting stuff in the array itself, you see the array itself was only supposed to have the averaged solution of the high_value and low_value arrays. I did that by individually assigning the average indices to a solution between two indices between the high_value and low_value arrays.For example average[29]=high_value[29]+low_value[29]/2. I did this for the 28 other indices for those 3 arrays. The indices of the average[] array might be filled, but there was no content in the average[] array itself, so i decided to assign the individual solutions to float variables.For example:

average[29]=high_value[29]+low_value[29]/2;
float y= average[29];

I did this for the other indices as well. My plan was to put all the float variables in the average[] array, but then the initializer error came and halted my progress.

For this piece of code:
cout max)
{
max = average[i];
}

It was one of the solutions I had to find the greatest value in the average[] array. It didn't work out and the result is this post asking for help on those problems

Also to answer the question why I individually assigned the indices even though I could have used a for loop, its because I thought it would be more accurate for some reason. The prompt was to write a function that calculates each individual index in the high_value[] array with the low_value[] array and divide the sum by 2. I thought it would be more accurate to do that with each individual index of each array. It looks like I was wrong. I could tell about the prompt that our professor gave us if it still does not make sense.
Mr Pallini gave the correct answer. I would add one small thing : avoid using explicit numbers everywhere. Instead, use a constant value or pass in the size. The constant would be declared as
C++
const int ValueCount = 30;
or something similar. Your function could then be :
C++
void compute_averages( const int low_value[], const int high_value[], float averages[] )
{
    for( int n = 0; n < ValueCount; ++n )
        averages[n] = ( high_value[n] + low_value[n] ) / 2.0f;
}
Here is how you could assign random values to them :
int main( void )
{
    float averages[ ValueCount ];
    int high_values[ ValueCount ];
    int low_values[ ValueCount ]

    int seed = (int) time( nullptr ) % RAND_MAX;
    srand( seed )    // seed the pseudo random number generator

    for( int n = 0; n < ValueCount; ++n )
    {
        high_values[ n ] = rand();
        low_values[ n ] = rand();
    }

    compute_averages( low_values, high_values, averages );
    return 0;
}
 
Share this answer
 
Rick York's solution explicitly uses a float constant e.g. 2.0f. This reminded me that, in general, one should use double in preference to float. 1) it's usually faster, 2) it has better precision, which can reduce unexpected results.

For example given:
C
#include <stdio.h>
int main()
{
    float f = 0.0f;
    for(size_t i = 0; i < 100000000; ++i)
        f += 0.01;
    printf("f = %f\n", f);
}
This has a precision issue. It should print something near 1,000,000, but it actually prints 262,144.000000. At that point, an additional 0.01 is outside the scale of a float's precision, so the answer returned is incorrect.
If we replace float with double, we get the answer 1,000,000.000779, which is much better (we don't get 1,000,000 exactly due to inexact base-10 to base-2 conversions).
If we time both programs, on my X86-64 linux box the float version takes about 3 times longer to run. This is true if I compile in either 32 or 64 bit mode.

The moral of the all this is, that unless you absolutely need a float (and sometimes you do), stick with a double for your floating point calculations.
 
Share this answer
 
Comments
Rick York 25-Nov-23 1:11am    
I work on a fairly big application that uses a mix of doubles and floats. The reason for this is a large part of the code is written to work on both CPUs and GPUs (using CUDA) and floats are considerably faster than doubles on the GPU. I use doubles most of the time elsewhere and always in my home projects, as seen in my profile. They are all graphics-oriented and I would see lots of artifacts when I use floats.

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