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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#include <iostream>
#include <string>
#include <algorithm>
const unsigned short num_pokemon = 6;
const unsigned short num_attributes = 6;
struct Pokemon {
std::string name;
unsigned short attributes[num_attributes];
//store the different attributes in an array
//hp, attack, defense, spAttack, spDefense, speed;
};
int main() {
Pokemon pokemon[num_pokemon];
pokemon[0] = { "Charmander", { 39, 52, 43, 60, 50, 65 } };
pokemon[1] = { "Pidgey", { 40, 45, 40, 35, 35, 56 } };
pokemon[2] = { "Totodile", { 50, 64, 64, 44, 48, 43 } };
pokemon[3] = { "Haunter", { 45, 50, 45, 115, 55, 95 } };
pokemon[4] = { "Abra", { 25, 20, 15, 105, 56, 90 } };
pokemon[5] = { "Magikarp", { 20, 10, 55, 15, 20, 80 } };
std::string attributeStrings[num_attributes] = { "health", "attack", "defense", "spAttack", "spDefense", "speed"};
for (unsigned short i = 0; i < num_attributes; ++i) {
//for each attribute...
unsigned short currentAttribute[num_pokemon];
//create a temporary array
for (unsigned short j = 0; j < num_pokemon; ++j) {
currentAttribute[j] = pokemon[j].attributes[i];
//fill that array with the current attribute data from each pokemon
}
unsigned short index = std::distance(currentAttribute, std::max_element(currentAttribute, currentAttribute + num_pokemon));
//std::max_element returns an iterator to the element in the array with the largest value.
//Since the pokemon array is... an array (and not a vector or something), we can use std::distance to effectively convert the returned iterator to an index location to make it easier to work with.
std::cout << "The largest " << attributeStrings[i] << "\tis " << pokemon[index].name << " with " << pokemon[index].attributes[i] << std::endl;
}
std::cin.get();
return 0;
}
|