klknowles
klknowles

Reputation: 5

trying to output the element of an array in c++

I am trying to search which elements in this c++ array contain the highest value and then output the key of that element but it keeps outputting a list of numbers like 9044007. What I want to know is how do I output the first number only because I'm guessing it is outputting all the keys of the elements with the same value.

double max = 0;
for (int i = 1; i <= 9; i++) {
    if (responselayer[i] > max) {
        max = responselayer[i];
    }
}
for (int i = 1; i < 10; i++) {
    if (responselayer[i] == max) {
        return i;
        break;
    }
}

Here is the part of the code that fills the entire thing. It's a lot of code so it's hard to make it minimal. This is my first time trying stack so I don't really know how everything works on here.

//final layer input calculation and output//
    for (int j = 1; j <= 10; j++) {
        double subval = 0;
        if (j == 1) {

            for (int i = 0; i < 5; i++) {
                subval = (midlayerweight4[i] * midlayer3[i]) + subval;
                responselayer[j] = subval;
            }
            responselayer[j] = 1 / (1 + exp(-responselayer[j]));

        }
        else if (j == 2) {
            int k = 0;
            for (int i = 5; i < 10; i++) {

                subval = (midlayerweight4[i] * midlayer3[k]) + subval;
                responselayer[j] = subval;
                k++;
            }
            responselayer[j] = 1 / (1 + exp(-responselayer[j]));

        }
        else if (j == 3) {
            int k = 0;
            for (int i = 10; i <15; i++) {
                subval = (midlayerweight4[i] * midlayer3[k]) + subval;
                responselayer[j] = subval;
                k++;
            }
            responselayer[j] = 1 / (1 + exp(-responselayer[j]));

        }
        else if (j == 4) {
            int k = 0;
            for (int i = 15; i < 20; i++) {
                subval = (midlayerweight4[i] * midlayer3[k]) + subval;
                responselayer[j] = subval;
                k++;
            }
            responselayer[j] = 1 / (1 + exp(-responselayer[j]));
        }
        else if (j == 5) {
            int k = 0;
            for (int i = 20; i <25; i++) {
                subval = (midlayerweight4[i] * midlayer3[k]) + subval;
                responselayer[j] = subval;
                k++;
            }
            responselayer[j] = 1 / (1 + exp(-responselayer[j]));
        }
        else if (j == 6) {
            int k = 0;
            for (int i = 25; i <30; i++) {
                subval = (midlayerweight4[i] * midlayer3[k]) + subval;
                responselayer[j] = subval;
                k++;
            }
            responselayer[j] = 1 / (1 + exp(-responselayer[j]));
        }
        else if (j == 7) {
            int k = 0;
            for (int i = 30; i <35; i++) {
                subval = (midlayerweight4[i] * midlayer3[k]) + subval;
                responselayer[j] = subval;
                k++;
            }
            responselayer[j] = 1 / (1 + exp(-responselayer[j]));
        }
        else if (j == 8) {
            int k = 0;
            for (int i = 35; i < 40; i++) {
                subval = (midlayerweight4[i] * midlayer3[k]) + subval;
                responselayer[j] = subval;
                k++;
            }
            responselayer[j] = 1 / (1 + exp(-responselayer[j]));
        }
        else if (j == 9) {
            int k = 0;
            for (int i = 40; i <45; i++) {
                subval = (midlayerweight4[i] * midlayer3[k]) + subval;
                responselayer[j] = subval;
                k++;
            }
            responselayer[j] = 1 / (1 + exp(-responselayer[j]));
        }
    }
    double max = 0;
    for (int i = 1; i <10; i++) {
        if (responselayer[i] > max) {
            max = responselayer[i];
        }
    }
    for (int i = 1; i < 10; i++) {
        if (responselayer[i] == max) {
            return i;
        }
    }

}

} midlayerweight4[] was filled by an array before it in this same fashion and so was others behind that. This is the declaration for responselayer and all the other arras used were of type double and they were all declared outside of the array.

double responselayer[10];

Upvotes: 0

Views: 62

Answers (1)

jschmerge
jschmerge

Reputation: 320

A few things:

  • Array indexing in C/C++ starts at zero.
  • You do not show us the type of your array, so I'm going to assume it is double responselayer[SZ]; If this is not the case, you have some issues involving implicit type conversion from integral to floating point types.
  • It is sloppy to use 2 different styles of conditions in your for-loop.
  • It is sloppy to use magic numbers in your code.

I would write the code as follows:

size_t find_max_index(const double data[], size_t size) {
  size_t max_index = 0;
  // No need to compare data[0] to itself on the first iteration, so
  // loop starts at one
  for (size_t i = 1; i < size; ++i)
    if (data[i] > data[max_index])
      max_index = i;
  return max_index;
}

Or getting a little fancier and taking care of the possibility of floating point strangeness, we can make it a templated function:

template <typename T>
size_t find_max_index(const T data[], size_t size) {
  size_t max_index = 0;
  for (size_t i = 1; i < size; ++i)
    if (data[i] > data[max_index])
      max_index = i;
  return max_index;
}

Upvotes: 1

Related Questions