The last variable in an array is incorrect [closed] - c++

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
So, I'm creating a program to create a (sort of) simulation of a melee deathmatch video game (not actually making a video game at the moment, just making simple AIs with goals to kill each other). In order to do this, I am using a tile-based, turn-based system.
Now the introduction is out of the way, here is the specific problem: in one of the arrays I am using, the last value is stored incorrectly in RAM, no matter how many variables in the array. Here is the relevant code:
(I will post all the code I have at the bottom of this, but the problem is in here)
#include "stdafx.h"
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
using namespace std;
int npcTileAttacker[] = { 0, 0, 0, 0, 0};
int s = 0;
while (s < 6)
{
cout << "The value that is being selected from the array is " << s << endl;
cout << npcTileAttacker[s] << endl;
s++;
cout << "The value of s has now been set to " << s << endl;
}
This outputs:
The value that is being selected from the array is 0
0
The value of s has now been set to 1
The value that is being selected from the array is 1
0
The value of s has now been set to 2
The value that is being selected from the array is 2
0
The value of s has now been set to 3
The value that is being selected from the array is 3
0
The value of s has now been set to 4
The value that is being selected from the array is 4
0
The value of s has now been set to 5
The value that is being selected from the array is 5
-858993640
The value of s has now been set to 6
Obviously, this last value from the array is incorrect. What I want to know is why this would be happening.
In addition to this, when the program ends, I get an error message:
"Run-Time Check Failure #2 - Stack around the variable 'npcTileAttacker' was corrupted."
I have tried placing the output values of s and the array piece of code around other arrays in the program, resulting in the same problem occuring.
Here is my full code, if required:
#include "stdafx.h"
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
using namespace std;
int numberOfNPCs = 5;
//Remember whose alive (so we can skip the dead's turns)
int npcAlive[5] = { 1, 1, 1, 1, 1 };
/*This determines what action is going to be carried out on this square. For the moment:
if npcTileActivity[n] = 1;
the goals is death
WARNING! THIS WILL RESULT IN BUGS!!! I need to figure out a way that allows multiple activities on a tile
(maybe apply actions onto NPCs directly, rather than onto their tiles)
*/
int npcTileActivity[] = { 0, 0, 0, 0, 0};
//This tells you who is doing the action on this tile
int npcTileAttacker[5] = { 1, 2, 3, 4, 0 };
int s = 0;
while (s < 6)
{
cout << "The value that is being selected from the array is " << s << endl;
cout << npcTileAttacker[s] << endl;
s++;
cout << "The value of s has now been set to " << s << endl;
}
//This determines whether or not the NPC will fight back. Currently pointless, as this will just kill them.
int npcPacifism[5] = { 0 };
//This is their HP
int npcDefense[5] = {5, 5, 5, 5, 5};
//This is the default damage (presumably this is done with knives)
int npcOffense[5] = {1, 1, 1, 1, 1};
/*This determines what each NPC wants to do.
1 - Kill Target
*/
int npcGoal[5] = {1, 1, 1, 1, 1};
//This is the NPC they are aiming at
int npcTarget[5] = {1, 2, 3, 4, 0};
/*The x coord for their target. In the future:
-I want this to be able to be changed during the sim
-This should be disabled until the NPC can find out where their target is
*/
int npcGoalLocationX[5] = {4, 1, 4, 3, 1};
/* The Y coord for their target
*/
int npcGoalLocationY[5] = {2, 3, 4, 2, 1};
/*Their x coord.
This will change, then the all npcGoalLocations need to be updated
*/
int npcLocationX[5] = {1, 4, 1, 4, 3};
/* Their y coord.
*/
int npcLocationY[5] = {1, 2, 3, 4, 2};
int m = 1;
while (m != 0)
{
int i = 0;
//Loop until every npc has had a turn
while (i < (numberOfNPCs + 1))
{
/*npcGoalLocationY[i] = npcLocationY[npcTarget[i]];
npcGoalLocationY[i] = npcLocationY[npcTarget[i]];*/
if (npcAlive[i] = 1)
{
/* Tile activities:
1 - Kill occupant
*/
if (npcTileActivity[i] = 1)
{
cout << "This shouldn't be the first thing." << endl;
//This gets the attack and defense values for the appropriate acting NPC
int j = 0;
while (j < (numberOfNPCs + 1))
{
if (npcTileAttacker[i] = j)
{
//Defender's HP - Attacker's damage
int rollAttack1 = npcDefense[i] - npcOffense[j];
if (rollAttack1 > 0)
{
npcDefense[i] = rollAttack1;
cout << "NPC " << j << " attacks NPC " << i << endl;
if (npcPacifism[i] = 0)
{
//Defender retaliates
int rollAttack2 = npcDefense[j] - npcOffense[i];
if (rollAttack2 > 0)
{
npcDefense[j] = rollAttack2;
cout << "NPC " << i << " retaliates" << endl;
}else
{
npcAlive[j] = 0;
cout << "NPC " << j << " dies" << endl;
}
}
}else
{
npcAlive[i] = 0;
cout << "NPC " << i << " dies" << endl;
}
}
j++;
}
}else
{
cout << "This should be the first." << endl;
if (npcGoal[i] != 0)
{
if (npcGoalLocationX[i] = npcLocationX[i])
{
if (npcGoalLocationY[i] = npcLocationY[i])
{
//The Tile Activity of the current NPC's target is set to whatever the current NPC's goal is
npcTileActivity[npcTarget[i]] = npcGoal[i];
}
}
if (npcGoalLocationX[i] > npcLocationX[i])
{
npcLocationX[i]++;
}
if (npcGoalLocationX[i] < npcLocationX[i])
{
npcLocationX[i]--;
}
if (npcGoalLocationY[i] > npcLocationY[i])
{
npcLocationY[i]++;
}
if (npcGoalLocationY[i] < npcLocationY[i])
{
npcLocationY[i]--;
}
}
}
}
i++;
}
cin >> m;
}
return 0;
}
Also, I get a problem (around the lines which cout "This should be first" and "This shouldn't be the first thing"): The one which shouldn't be first is first and the one which should be first never even executes. This is probably related to the array error, however.
Thanks for your assistance.

Your condition is off by one:
while (s < 6)
should be
while (s < 5)
The array { 0, 0, 0, 0, 0} has five elements, so valid indexes are 0,1,2,3,4.
Your condition stops when s < 6 is false, so it's still true for s == 5.

Your array has only 5 cells:
int npcTileAttacker[] = { 0, 0, 0, 0, 0};
That is, s should go from 0 to 4, instead of 0 to 5.
The "random" value that you are seeing is in fact whatever value was there on the stack after npcTileAttacker array since you are overflowing that array.

The size of your array is 5. Hence the valid indices are from 0-4. So, npcTileAttacker[5] will always post garbage.

You're out by 1 in your while loop expression.
You'd also be better off using a for loop and not hard coding the length of the array. Try something like this:
int npcTileAttacker[] = { 0, 0, 0, 0, 0};
int npcTileAttackerLength = sizeof(npcTileAttacker)/sizeof(npcTileAttacker[0]);
for(int s=0; s<npcTileAttackerLength; s++)
{
cout << "The value that is being selected from the array is " << s << endl;
cout << npcTileAttacker[s] << endl;
}
This way the length variable will always hold the number of items in the array.

Related

How to use std::multimap to map integer key to two integer values that serve as multidimensional array coordinates (for Tic Tac Toe)?

As an exercise, I am trying to create a TicTacToe game in Visual Studio as a console application. First, I created the 3x3 grid with a multidimensional array. I thought an intuitive way to "write" an 'X' or an 'O' in a particular square of the grid would be by having the player input a number 1-9 and that number would map to a particular square. Below is how the numbers would be correspond to the spots in the grid:
1 2 3
4 5 6
7 8 9
Thus, I used std::multimap to map the player input to a square in the grid to practice using maps and multimaps. Since I am new to std::multimap I guess I messed up somewhere: there is no error, the game compiles, but the input does not seem to be mapping correctly to the right square.
I do not know how to fix the bug because I am unfamiliar with maps and multimaps.
*If someone could tell me how to fix the problem using the mapping method I have chosen that would be great!
*I'm also welcoming other and better ideas as to how to approach mapping player input to specific squares!
Sorry for the long code; I don't think I could cut anything more. Thanks for taking the time!
#include <iostream>
#include <map>
using namespace std;
class TTTClass
{
private:
static const int GRID_LENGTH = 3;
char Grid[GRID_LENGTH][GRID_LENGTH] = {' '};
int POInput;
int PXInput;
bool IsInputValid = false;
public:
TTTClass()
{
POInput = 1;
PXInput = 1;
}
void EmptyGrid()
{
for (int RowCounter = 0; RowCounter < GRID_LENGTH; RowCounter++)
{
for (int ColumnCounter = 0; ColumnCounter < GRID_LENGTH; ColumnCounter++)
{
Grid[RowCounter][ColumnCounter] = ' ';
}
}
}
void DisplayGrid()
{
for (int RowCounter = 0; RowCounter < GRID_LENGTH; RowCounter++)
{
std::cout << " ";
for (int ColumnCounter = 0; ColumnCounter < GRID_LENGTH; ColumnCounter++)
{
std::cout << Grid[RowCounter][ColumnCounter];
if (ColumnCounter != GRID_LENGTH - 1) {std::cout << " | ";}
}
if (RowCounter != GRID_LENGTH - 1)
{
std::cout << "\n __|___|__ \n | |\n";
}
}
std::cout << "\n\n";
}
void POTurn()
{
std::multimap<int, int> Gridmm;
Gridmm.insert(std::make_pair(1, 0)); Gridmm.insert(std::make_pair(1, 0));
Gridmm.insert(std::make_pair(2, 0)); Gridmm.insert(std::make_pair(2, 1));
Gridmm.insert(std::make_pair(3, 0)); Gridmm.insert(std::make_pair(3, 2));
Gridmm.insert(std::make_pair(4, 1)); Gridmm.insert(std::make_pair(4, 0));
Gridmm.insert(std::make_pair(5, 1)); Gridmm.insert(std::make_pair(5, 1));
Gridmm.insert(std::make_pair(6, 1)); Gridmm.insert(std::make_pair(6, 2));
Gridmm.insert(std::make_pair(7, 2)); Gridmm.insert(std::make_pair(7, 0));
Gridmm.insert(std::make_pair(8, 2)); Gridmm.insert(std::make_pair(8, 1));
Gridmm.insert(std::make_pair(9, 2)); Gridmm.insert(std::make_pair(9, 2));
do
{
std::cout << "PlayerO, select a square: ";
std::cin >> POInput;
if (POInput < 1 || POInput > 9)
IsInputValid = false;
else
{
std::pair<std::multimap<int, int>::iterator, std::multimap<int, int>::iterator> RepeaterIterator;
RepeaterIterator = Gridmm.equal_range(POInput);
std::multimap<int, int>::iterator itr1 = RepeaterIterator.first;
std::multimap<int, int>::iterator itr2 = RepeaterIterator.second;
Grid[itr1->second][itr2->second] = 'O';
std::cout << "Value at square " << POInput << "/ Coord. " << itr1->second << ", " << itr2->second;
std::cout << " is: " << Grid[itr1->second][itr2->second] << "\n";
IsInputValid = true;
}
} while (IsInputValid == false);
}
void PXTurn()
{
std::multimap<int, int> Gridmm;
Gridmm.insert(std::make_pair(1, 0)); Gridmm.insert(std::make_pair(1, 0));
Gridmm.insert(std::make_pair(2, 0)); Gridmm.insert(std::make_pair(2, 1));
Gridmm.insert(std::make_pair(3, 0)); Gridmm.insert(std::make_pair(3, 2));
Gridmm.insert(std::make_pair(4, 1)); Gridmm.insert(std::make_pair(4, 0));
Gridmm.insert(std::make_pair(5, 1)); Gridmm.insert(std::make_pair(5, 1));
Gridmm.insert(std::make_pair(6, 1)); Gridmm.insert(std::make_pair(6, 2));
Gridmm.insert(std::make_pair(7, 2)); Gridmm.insert(std::make_pair(7, 0));
Gridmm.insert(std::make_pair(8, 2)); Gridmm.insert(std::make_pair(8, 1));
Gridmm.insert(std::make_pair(9, 2)); Gridmm.insert(std::make_pair(9, 2));
do
{
std::cout << "PlayerX, select a square: ";
std::cin >> PXInput;
if (PXInput < 1 || PXInput > 9)
IsInputValid = false;
else
{
std::pair<std::multimap<int, int>::iterator, std::multimap<int, int>::iterator> RepeaterIterator;
RepeaterIterator = Gridmm.equal_range(PXInput);
std::multimap<int, int>::iterator itr1 = RepeaterIterator.first;
std::multimap<int, int>::iterator itr2 = RepeaterIterator.second;
Grid[itr1->second][itr2->second] = 'X';
std::cout << "Value at square " << POInput << "/ Coord. " << itr1->second << ", " << itr2->second;
std::cout << " is: " << Grid[itr1->second][itr2->second] << "\n";
IsInputValid = true;
}
} while (IsInputValid == false);
}
};
int main()
{
TTTClass MyGame;
MyGame.EmptyGrid();
MyGame.DisplayGrid();
MyGame.PXTurn();
MyGame.DisplayGrid();
MyGame.POTurn();
MyGame.DisplayGrid();
return 0;
}
BTW, I know the game only runs through two turns, but problem shows up regardless.
(Too long for a comment and, may be, actually an answer.)
I believe, OP missed the point that the required mapping is int → int × int.
Either the value type has to be e.g. std::pair<int, int> or there are two mappings needed – one to map input to rows and one to map input to columns.
However, there is a very simple linear relation between input index and grid coordinates:
1 -> 0, 0 | 2 -> 0, 1 | 3 -> 0, 2
----------+-----------+----------
4 -> 1, 0 | 5 -> 1, 1 | 6 -> 1, 2
----------+-----------+----------
7 -> 2, 0 | 8 -> 2, 1 | 9 -> 2, 2
I.e. for input int i: int col = (i - 1) % 3, row = (i - 1) / 3;.
This aside: If OP really wants to use a map, then std::map<int, std::pair<int, int> > makes more sense than multimap.
A std::map is a sorted associative container that contains key-value pairs with unique keys. (which you have). Each input index maps to exactly one grid cell and there are no duplicated keys.
It is irrelevant that the value is a pair of ints. It can be rather any object with a minimal requirement of properties:
CopyInsertable
EqualityComparable
Destructible
A std::multimap is an associative container that contains a sorted list of key-value pairs, while permitting multiple entries with the same key. (which you don't need as your keys are unique).
in OPs code:
std::cin >> i;
if (i >= 1 && i <= 9) {
Grid[/* row: */(i - 1) / 3, /* col: */(i - 1) % 3] = mark;
} else {
// harass user
}
Thereby, char mark could have 'X' or 'O' to consider the hint of PaulMcKenzie about code duplication.

C++ position in index removal of hard coded list of numbers

I have a pretty simple question. My goal for this short program is for it to display the set of numbers(which is hard coded), then have the user specify which index a number of the array should be deleted from. It then outputs the new array. This program works but has one major error. When I run it and choose position 2 for example, which should delete 45, instead deletes 34. The program outputs :
12
45
2
8
10
16
180
182
22
instead of :
12
34
2
8
10
16
180
182
22
notice that the number position I want removed instead removes in the position before the number I actually want removed, if you remember that lists start at 0. Thank you!
//This program demos basic arrays
#include <iostream>
using namespace std;
const int CAP = 10;
int main()
{
//read index from user, delete number in position of index they specify.
//when printing the list, number should be gone.
int size;
int list[CAP] = { 12, 34, 45, 2, 8, 10, 16, 180, 182, 22 };
size = 10;
int i, delIndex;
cout << "Your list is: " << endl;
for (i = 0; i < CAP; i++)
{
cout << list[i] << endl;
}
cout << "\nPlease enter index to delete from: ";
cin >> delIndex;
for (i = delIndex; i <= 10; i++)
{
list[i - 1] = list[i];
}
cout << "The index position you specified has been deleted." << endl;
cout << "The new array is: " << endl;
for (i = 0; i < (size - 1); i++)
{
cout << list[i] << endl;
}
return 0;
}
Replace this:
for (i = delIndex; i <= 10; i++)
{
list[i - 1] = list[i];
}
with that:
for (i = delIndex; i < size-1; i++)
{
list[i] = list[i+1];
}
Since you are with C++, why not just using a std vector?
#include <vector>
// Initialize
int vv[10] = { 12, 34, 45, 2, 8, 10, 16, 180, 182, 22 };
std::vector<int> myvector(&vv[0], &vv[0]+10);
There are other easier ways to initialize the vector depending on the compiler. See how to initialize a vector.
Then you can just use the erase method of the vector (here I assume the user knows the indexing starts with 0, otherwise you can just put delIndex-1):
myvector.erase (myvector.begin()+delIndex);
You can easily iterate over the vector to show its contents (there are easier ways of doing this depending on the compiler, use the auto keyword).
for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it)
std::cout << ' ' << *it;

Strange bug in a while loop (C++)

I'm trying to make a program that works with a simple algorithm.
But for some reason, I get a strange bug (below is the simplified version of the program).
#include "stdafx.h"
#include <iostream>
#include <string>
using std::cout;
using std::string;
void find(int arr[], string name)
{
int t = 8;
int i = 0;
int v = 0;
// t should become equal to the smallest int of the array after this.
while (arr[i])
{
if (arr[i] < t)
{
t = arr[i];
}
++i;
}
/* When this statement below gets executed t gets what looks like a
random value for some reason */
cout << arr[t] << '\n';
for (int b = 0; b < 2; ++b)
{
if (t == arr[b])
{
v = b;
}
}
/* Again, arr[v] gets what looks like a random number */
cout << "The cheapest " << name << " is number " << arr[v] << ".";
}
int main()
{
/* [0] = "Cost for Steve"
[1] = "Cost for Mark"
[2] = "Cost for Andrew" */
int cleaning[] = { 5, 4, 7 };
int cooking[] = { 3, 6, 4 };
int babysitting[] = { 7, 6, 3 };
cout << "Number 1: Steve, Number 2: Mark, Number 3: Andrew.\n";
find(cleaning, "cleaner");
find(cooking, "cook");
find(babysitting, "babysitter");
/* This is to prevent the console application from quitting */
while (true)
{
}
}
I'm sure there is something wrong in the for and the while loop, but what?
If you're reading my code and some text or variable name seems foreign to you, chances are that I forgot to translate it (this is originally written in Italian).
Thanks for taking your time to read this.
EDIT: Thanks to #Tar I fixed the first part of the program, but the part which says The (name) that costs less is n. (arr[v]).still gives me a random number, I compiled and ran the program, the output is:
Number 1: Steve, Number 2: Mark, Number 3: Andrew.
4
The cheapest cleaner is number 4.
3
The cheapest cook is number 3.
3
The cheapest babysitter is number 7.
That is obviously wrong as it should say that the cheapest cleaner is number 2, the cheapest cook is number 1 and the cheapest babysitter is number 3.
PS: As soon as everything is fixed I will take the part which prints the cheapest price out.
The problem is within your first while loop in find:
while (arr[i]) // while the element at arr[i] is NOT 0
{
if (arr[i] < t)
{
t = arr[i];
}
i++;
}
Here you continuously evaluate elements in arr for whether they are not 0. This is not correct. You've declared your arrays as:
int cleaning[3] = { 5, 4, 7 };
int cooking[3] = { 3, 6, 4 };
int babysitting[3] = { 7, 6, 3 };
None of these contain 0, so your while loop will run indefinitely and you'll be reading past the memory for each array which is not good news.
Consider using std::vector instead, and see how much clearer and safer your code becomes:
#include <vector>
#include <iostream>
void find(const std::vector<int>& numbers)
{
auto t = 8;
// Look through each element in the container:
for(auto number : numbers)
{
if (number < t)
{
t = number;
}
}
std::cout << t << std::endl;
}
int main()
{
std::vector<int> cleaning = {5, 4, 7};
find(cleaning);
}
Above all, I want to make a statement: I am not an English-speaker, so if I said wrong words, please excuse me.
I think this question is not very difficult. I fixed your algorithm and output format. Actually, I almost rewrote it.
In my view, your code seems kind of naive. If you only learnt C++ syntax, there is a long way to study algorithm.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int find(const vector<int>& numbers) {
int minVul = numbers[0];
int rank = 0;
for (int i = 1; i < numbers.size(); i++)
{
if (minVul > numbers[i])
{
minVul = numbers[i];
rank = i;
}
}
return rank;
}
int main() {
vector<string> name = { "steve","mark","andrew" };
/* [0] = "Cost for steve"
[1] = "Cost for mark"
[2] = "Cost for andrew" */
vector<int> cleaning = { 5, 4, 7 };
vector<int> cooking = { 3, 6, 4 };
vector<int> babysitting = { 7, 6, 3 };
int cleaner = find(cleaning);
cout << "Cleaning:" << name[cleaner] << " costs least in " << cleaning[cleaner] << endl;
int cooker = find(cooking);
cout << "Cooking:" << name[cooker] << " costs least in " << cooking[cooker] << endl;
int babysitter = find(babysitting);
cout << "Babysitter:" << name[babysitter] << " costs least in " << babysitting[babysitter] << endl;
system("pause"); //This is a common skill to prevent the console application from quitting.
return 0;
}
Outputs:
Cleaning:mark costs least in 4
Cooking:steve costs least in 3
Babysitter:andrew costs least in 3

Comparing one element of an array to multiple elements of another array

I'm writing a piece of code for a slot machine project, I have two arrays working.
I would like to compare one element of array 1 with multiple element of array 2.
This is my current piece of code, I took a swing at it but I don't think this is correct.
char arrayOne[10]{ 1, 1, 2, 2, 3, 4, 4, 5, 5 };
char arrayTwo[10]{ 1, 2, 3, 4, 5 };
if (arrayTwo[0] == arrayOne[0 - 1])
cout << "The Numbers are the same";
else
cout << "the numbers are different";
_getch();
In C++, the syntax you've written is the same as: arrayTwo[0] == arrayOne[-1], which I'm fairly sure we both agree is not what you want.
You're looking for:
((arrayTwo[0] == arrayOne[0]) and (arrayTwo[0] == arrayOne[1]))
If you want to match against an arbitrary number of elements, then you should learn about loops:
bool truthiness = true;
for (int i=0; i<10; ++i) {
truthiness = truthiness and (arrayTwo[0] == arrayOne[i]);
}
if (truthiness) {
cout << "The numbers are the same";
} else {
cout << "The numbers are different";
}
You should use a loop. I've written this small function for you...
int
array_range_compare (char *array, int compare_value, int start, int end)
{
for (int i = start; i <= end; i++) {
if (compare_value != array[i]) {
return 0;
}
}
return 1;
}
This will compare the value of compare_value to the values of keys start through end in array. For you array is arrayOne, start is 0, end is 1, and compare_value is arrayTwo[0], all of the variables but array are integers.
You can use this function like so...
char arrayOne[10] = { 1, 1, 2, 2, 3, 4, 4, 5, 5 };
char arrayTwo[10] = { 1, 2, 3, 4, 5 };
if (array_range_compare (arrayOne, arrayTwo[0], 0, 1)) {
std::cout << "The Numbers are the same";
} else {
std::cout << "the numbers are different";
}

lp_solve consecutive calls to `solve' are incorrect

I need to solve some simple linear integer programming-like problem, I took lp_solve library. The task is to obtain variables' values for some consequtive values of linear function with possible simple (linear) constraints on variables (actually I encountered a problem even without any additional constraints). E.g. I have linear function 4a + 5b. First values I'm interested in are (function value - variables values):
0 - (0, 0); 4 - (1, 0); 5 - (0, 1); 8 - (2, 0); 9 - (1, 1)
The problem is that after getting 8 - (2, 0), lp_solve returns NUMFAILURE code (5) while solving the task and resolves it to 0 - (0, 0)…
If I do not use consequtive calls to 'solve' function and just start from 9 then I got right answer (9 - (1, 1)). Would anyone please explain this? The code is following.
#include <iostream>
#include <cstdio>
#include <lpsolve/lp_lib.h>
# if defined ERROR
# undef ERROR
# endif
# define ERROR() { fprintf(stderr, "Error\n"); exit(1); }
using std::cout;
using std::endl;
void print_res(REAL * vars, int size) {
cout << "(";
for (int i = 0; i < size - 1; ++i) {
cout << round(vars[i]) << ", ";
}
cout << round(vars[size - 1]) << ")";
}
int main()
{
lprec *lp;
int majorversion, minorversion, release, build, min = 0;
lp_solve_version(&majorversion, &minorversion, &release, &build);
const int l = 5; // number of iterations
const int dim = 2; // dimension ot current task
char p_data[] = "4 5"; // objective function: p(a, b) = 4a + 5b
if ((lp=make_lp(0, dim)) == NULL)
ERROR();
set_verbose(lp, CRITICAL);
if (!str_add_constraint(lp, p_data, GE, min)) // p(a, b) >= min
ERROR();
// objective function - p
if (!str_set_obj_fn(lp, p_data))
ERROR();
// work with integer non-negative variables
set_int(lp, 1, TRUE);
set_int(lp, 2, TRUE);
set_lowbo(lp, 1, 0);
set_lowbo(lp, 2, 0);
for (int i = 0; i < l; ++i) {
cout << "Status: " << solve(lp) << endl;
REAL vars[dim];
get_variables(lp, vars);
print_res(vars, dim);
// increase minimum value for p
min = round(get_objective(lp));
cout << ", p = " << min << endl;
if (!set_rh(lp, 1, min + 1))
ERROR();
}
return 0;
}