Parsing Arrays in C++An array can be defined as a data structure consisting of a group of elements. Basically, all these elements have the same data type, for example, a string or integer. Arrays are used in C++ and other programming languages to organize data or information, so that, values and var...
Dynamic variables and composition
#include
#include
#include
#include
#include
#include "Move.h"
#include "Pokemon.h"
#include "PokemonTrainer.h"
using namespace std;
// Load the pokemons from file
void loadPokemons(const string &filename, Pokemonpokemons[5])
{
// Load the pokemons from file
ifstreaminFile("pokemonList.txt");
if (!inFile.is_open())
{
cout<< "Failed to open file." <>move.damage;
pokemons[i].addMove(move);
}
}
inFile.close();
}
// Let the user select a pokemon to add to their team
void initializeTrainerPokemons(PokemonTrainer&trainer, Pokemonpokemons[5])
{
cout<< "List of Pokemons to choose" <> number))
{
cout<< "Invalid option." <= 0 && number < 5 && !chosenPokemons[number])
{
trainer.addPokemon(pokemons[number]);
break;
}
cout<< "Invalid option." <= 3)
{
// No more pokemons left
cout<< "You have no pokemons left. You lose!" <= 0)
break;
cout<< "Error: Invalid move." <
using namespace std;
// Default constructor
Pokemon::Pokemon()
: name(""), health(0), numMoves(0)
{
}
// Create a pokemon
Pokemon::Pokemon(const string &name, int health)
: name(name), health(health), numMoves(0)
{
}
// Add a move for the pokemon
void Pokemon::addMove(const Move &move)
{
if (numMoves>= 3)
return;
moves[numMoves++] = move;
}
// Get the pokemon's health
int Pokemon::getHealth() const
{
return health;
}
// Return the pokemon's name
string Pokemon::getName() const
{
return name;
}
// Return the first move
Move Pokemon::getFirstMove() const
{
return moves[0];
}
// Return the second move
Move Pokemon::getSecondMove() const
{
return moves[1];
}
// Return the third move
Move Pokemon::getThirdMove() const
{
return moves[2];
}
// Attack the target pokemon
void Pokemon::move(int index, Pokemon&target)
{
if (index < 0 || index >= 3)
return;
target.health -= moves[index].damage;
if (target.health< 0)
target.health = 0;
}
// Print the moves
void Pokemon::displayMoves() const
{
for (int i = 0; i
using namespace std;
// Default constructor
PokemonTrainer::PokemonTrainer()
{
maxNumberPokemons = 3;
currentNumberPokemons = 0;
indexPokemonFighting = 0;
teamHealth = 0;
pokemons = new Pokemon[maxNumberPokemons];
}
// Copy constructor
PokemonTrainer::PokemonTrainer(const PokemonTrainer&other)
{
maxNumberPokemons = other.maxNumberPokemons;
currentNumberPokemons = other.currentNumberPokemons;
indexPokemonFighting = other.indexPokemonFighting;
teamHealth = other.teamHealth;
pokemons = new Pokemon[maxNumberPokemons];
for (int i = 0; iteamHealth = teamHealth;
}
// Get the team health
int PokemonTrainer::getTeamHealth() const
{
return teamHealth;
}
// Get the maximum pokemons a trainer can hold
int PokemonTrainer::getMaxNumberPokemons() const
{
return maxNumberPokemons;
}
// Return the number of pokemons left
int PokemonTrainer::getCurrentNumberPokemons() const
{
return currentNumberPokemons;
}
// Return the index of pokemon fighting
int PokemonTrainer::getIndexOfCurrentPokemonFighting() const
{
return indexPokemonFighting;
}
// Add a new pokemon to the team
void PokemonTrainer::addPokemon(const Pokemon&pokemon)
{
if (currentNumberPokemons>= maxNumberPokemons)
return;
pokemons[currentNumberPokemons++] = pokemon;
teamHealth += pokemon.getHealth();
}
// Display the team
void PokemonTrainer::displayTeam() const
{
cout<< "Pokemon Team" <= currentNumberPokemons)
indexPokemonFighting = 0;
}
Data storage and computation in 1D arrays
#include
#include
#include
using namespace std;
int valid_rows = 0;
void read_from_file(ifstream&input_file, double gallons[], double miles[])
{
string line, temp;
double num;
int totalValid = 0, lineCount = 0, i, currentCol = 0, k = 0;
double gallon_val = 0, mile_val = 0;
while(getline(input_file,line))
{
stringstream s;
s << line;
lineCount++;
currentCol = 0;
gallon_val = 0;
mile_val = 0;
while (!s.eof())
{
s >> temp;
stringstream(temp) >> num;
if(lineCount == 1)
totalValid = num;
else
{
if(currentCol % 2 == 0)
gallon_val = num;
else
mile_val = num;
}
temp = "";
currentCol++;
}
if(currentCol>= 1)
{
if(gallon_val>= 1 &&mile_val>= 1)
{
gallons[k] = gallon_val;
miles[k] = mile_val;
valid_rows++;
k++;
}
}
}
cout<< "\nTotal valid rows mentioned in file: "< array[i])
min = array[i];
}
return min;
}
double max(double array[], int N)
{
double max = array[0];
for(int i = 1; i< N; ++i)
if(max < array[i])
max = array[i];
return max;
}
double slope(int count, double x, double y, double xy, double xx)
{
double slope = double((count*xy- x*y)) / double((count*xx - x*x));
return slope;
}
double intercept(int count, long x, double y, double xy, double xx)
{
double Yintercept = double((y*xx - x*xy)) / double((count*xx - x*x));
return Yintercept;
}
int main(int argc, char** argv)
{
double gallons[100], miles[100];
if(argc == 1)
cout<< "Please enter filename in the arguements. Invalid Entry!"< 2)
cout<< "Too many command line arguement. The program needs just the filename as the argument."<maxX: " < " <maxY: " < " <