Strange bug in a while loop (C++) - 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

Related

Getting wierd output in recursion code! Even though logic seems to be correct

I've been trying to attempt a question on recursion pattern and it is pretty easy one but I am not able to getting why I am getting such an unexpected output. I traced the variables and parameters in callstack and my logic seems to be right but still it is printing random numbers.
#include <bits/stdc++.h>
using namespace std;
vector<int> pattern(int);
int main()
{
int n;
cin >> n;
vector <int> ans = pattern(n);
for (int u: ans)
{
cout << u << " ";
}
cout << "\n";
}
vector<int> sol;
vector<int> pattern(int N)
{
if (N <= 0)
{
// cout << "stopped" << endl;
sol.push_back(N);
return sol;
}
sol.push_back(N);
pattern(N-5);
sol.push_back(N);
}
i/p: 10
expected o/p: 10 5 0 5 10
but it is giving this result:
6422500 1997786416 -1172770555 0 6422508 1997703885 -1 1997899634 0 0 4199136 2576384 0 32 0 0 0 10672 726272 791648 826264 57158026 0 35651584 0 590440 36175880 524296 590376 36175880 524298 524832 35651584 9 655944 34603080 4718603 721424 34604104 4718597 328208 34604104 71303179 262672 34603080 4718599 262672 34604096 71827463 197252 58785856 -2143289343 66305 34635840 71958539 66434 50495552 -2143289343 524840 38273024 524295 721424 34603016 524293 721424 51512330 -2080374783 560 34866184 3 66322 34635784 655371 721424 50397184 -2147287039 66305 ..... [Truncated]```
You are missing a return statement from pattern() function.
vector<int> pattern(int N)
{
if (N <= 0)
{
// cout << "stopped" << endl;
sol.push_back(N);
return sol;
}
sol.push_back(N);
pattern(N-5);
sol.push_back(N);
return sol; // <--------- this was missing
}
Let N = 10, so pattern(10) will be called, then:
sol = {10}
pattern(5)
sol = {10, 5}
pattern(0) -> sol = {10, 5, 0} -> return sol -> but you don't store return value of pattern anywhere, so the return value is lost
sol = {10, 5, 0, 5}
function returns garbage, but that's also not captured anywhere
sol = {10, 5, 0, 5, 10}
function returns garbage which is captured by ans.
Actually, your function isn't required to return a vector at all, since your vector is declared as global. You could have void return type and it would work without any return statement. The below code works fine:
#include <iostream>
#include <vector>
using namespace std;
vector<int> sol;
void pattern(int);
int main()
{
int n;
cin >> n;
pattern(n);
for (auto& u: sol)
{
cout << u << " ";
}
cout << "\n";
}
void pattern(int N)
{
if (N <= 0)
{
// cout << "stopped" << endl;
sol.push_back(N);
return;
}
sol.push_back(N);
pattern(N-5);
sol.push_back(N);
}
Also a side note, it's recommended by many people to not to use:
#include <bits/stdc++.h>
Instead use individual includes:
#include <iostream>
#include <vector>

How to pick a sequence of numbers (from a fixed list) that will sum to a target number?

Let say I've a target number and a list of possibile values that I can pick to create a sequence that, once summed every picked number, will sum to the target:
target = 31
list = 2, 3, 4
possible sequence: 3 2 4 2 2 2 4 2 3 2 3 2
I'd like to:
first decide if there is any sequence that will reach the target
return one of the many (possible) sequence
This is my attempt:
#include <iostream>
#include <random>
#include <chrono>
#include <vector>
inline int GetRandomInt(int min = 0, int max = 1) {
uint64_t timeSeed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
std::seed_seq ss{ uint32_t(timeSeed & 0xffffffff), uint32_t(timeSeed >> 32) };
std::mt19937_64 rng;
rng.seed(ss);
std::uniform_int_distribution<int> unif(min, max);
return unif(rng);
}
void CreateSequence(int target, std::vector<int> &availableNumbers) {
int numAttempts = 1;
int count = 0;
std::vector<int> elements;
while (count != target) {
while (count < target) {
int elem = availableNumbers[GetRandomInt(0, availableNumbers.size() - 1)];
count += elem;
elements.push_back(elem);
}
if (count != target) {
numAttempts++;
count = 0;
elements.clear();
}
}
int size = elements.size();
std::cout << "count: " << count << " | " << "num elements: " << size << " | " << "num attempts: " << numAttempts << std::endl;
for (auto it = elements.begin(); it != elements.end(); it++) {
std::cout << *it << " ";
}
}
int main() {
std::vector<int> availableNumbers = { 2, 3, 4 };
CreateSequence(31, availableNumbers);
}
But it can loop infinitely if the list of number can't be appropriate to reach such sum; example:
std::vector<int> availableNumbers = { 3 };
CreateSequence(8, availableNumbers);
No sequence of 3 will sum to 8. Also, if the list is huge and the target number high, it can lead to a huge amount of processing (cause lots of while check fails).
How would you implement this kind of algorithm?
Your suggested code is possibly very fast, since it is heuristic. But as you said, it gets potentially trapped in a nearly endless loop.
If you want to avoid this situation, you have to search the complete set of possible combinations.
Abstraction
Let's define our algorithm as a function f with a scalar target t and a vector <b> as parameters returning a vector of coefficients <c>, where <b> and <c> have the same dimension:
<c> = f(t, <b>)
First the given set of numbers Sg should be reduced to their reduced set Sr so we reduce the dimension of our solution vector <c>. E.g. {2,3,4,11} can be reduced to {2,3}. We get this by calling our algorithm recursively by splitting Sg into a new target ti with the remaining numbers as the new given set Sgi and ask the algorithm, if it finds any solution (a non-zero vector). If so, remove that target ti from the original given set Sg. Repeat this recursively until no solutions found any more.
Now we can understand this set of numbers as a polynomial, where we are looking for possible coefficients ci to get our target t. Let's call each element in Sb bi with i={1..n}.
Our test sum ts is the sum over all i for ci * bi, where each ci can run from 0 to ni = floor(t/bi).
The number of possible tests N is now the product over all ni+1: N = (n1+1) * (n2+1) * ... * (ni+1).
Iterate now over all possibilities by representing the coefficient vector <c> as an vector of integers and incrementing c1 and carrying over an overrun to the next element in the vector, resetting c1 and so forth.
Example
#include <random>
#include <chrono>
#include <vector>
#include <iostream>
using namespace std;
static int evaluatePolynomial(const vector<int> &base, const vector<int> &coefficients)
{
int v=0;
for(unsigned long i=0; i<base.size(); i++){
v += base[i]*coefficients[i];
}
return v;
}
static bool isZeroVector(vector<int> &v)
{
for (auto it = v.begin(); it != v.end(); it++) {
if(*it != 0){
return false;
}
}
return true;
}
static vector<int> searchCoeffs(int target, vector<int> &set) {
// TODO: reduce given set
vector<int> n = set;
vector<int> c = vector<int>(set.size(), 0);
for(unsigned long int i=0; i<set.size(); i++){
n[i] = target/set[i];
}
c[0] = 1;
bool overflow = false;
while(!overflow){
if(evaluatePolynomial(set, c) == target){
return c;
}
// increment coefficient vector
overflow = true;
for(unsigned long int i=0; i<c.size(); i++){
c[i]++;
if(c[i] > n[i]){
c[i] = 0;
}else{
overflow = false;
break;
}
}
}
return vector<int>(set.size(), 0);
}
static void print(int target, vector<int> &set, vector<int> &c)
{
for(unsigned long i=0; i<set.size(); i++){
for(int j=0; j<c[i]; j++){
cout << set[i] << " ";
}
}
cout << endl;
cout << target << " = ";
for(unsigned long i=0; i<set.size(); i++){
cout << " +" << set[i] << "*" << c[i];
}
cout << endl;
}
int main() {
vector<int> set = {4,3,2};
int target = 31;
auto c = searchCoeffs(target, set);
print(target, set,c);
}
That code prints
4 4 4 4 4 4 4 3
31 = +4*7 +3*1 +2*0
Further Thoughts
productive code should test for zeros in any given values
the search could be improved by incrementing the next coefficient if the evaluated polynomial already exceeded the target value.
further speedup is possible, when calculating the difference of the target value and the evaluated polynomial when c1 is set to zero, and checking if that difference is a multiple of b1. If not, c2 could be incremented straight forward.
Perhaps there exist some shortcuts exploiting the least common multiple
As ihavenoidea proposed, I would also try backtracking. In addition, I will sort the numbers in decreasing order, il order to speed up the process.
Note: a comment would be more appropriate than an answer, but I am not allowed to. Hope it helps. I will suppress this answer if requested.

A vector as a patchwork of two other vectors

Subset a vector
Below is the benchmark of two different solutions to subset a vector
#include <vector>
#include <iostream>
#include <iomanip>
#include <sys/time.h>
using namespace std;
int main()
{
struct timeval timeStart,
timeEnd;
// Build the vector 'whole' to subset
vector<int> whole;
for (int i = 0 ; i < 10000000 ; i++)
    {
whole.push_back(i);
}
// Solution 1 - Use a for loops
gettimeofday(&timeStart, NULL);
vector<int> subset1;
subset1.reserve(9123000 - 1200);
for (int i = 1200 ; i < 9123000 ; i++)
{
subset1.push_back(i);
}
gettimeofday(&timeEnd, NULL);
cout << "Solution 1 took " << ((timeEnd.tv_sec - timeStart.tv_sec) * 1000000 + timeEnd.tv_usec - timeStart.tv_usec) << " us" << endl;
// Solution 2 - Use iterators and constructor
gettimeofday(&timeStart, NULL);
vector<int>::iterator first = whole.begin() + 1200;
vector<int>::iterator last = whole.begin() + 9123000;
vector<int> subset2(first, last);
gettimeofday(&timeEnd, NULL);
cout << "Solution 2 took " << ((timeEnd.tv_sec - timeStart.tv_sec) * 1000000 + timeEnd.tv_usec - timeStart.tv_usec) << " us" << endl;
}
On my old laptop, it outputs
Solution 1 took 243564 us
Solution 2 took 164220 us
Clearly solution 2 is faster.
Make a patchwork of two vectors
I would like to create a vector as a patchwork of two different vectors of the same size. The vector starts as one and then takes the value of the other and back and forth. I guess I don't fully understand how to copy values to a vector by using iterator pointing to elements in another vector. The only implementation I can think of requires using an analogous to solution 1 above. Something like...
#include <vector>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <sys/time.h>
#include <limits.h>
using namespace std;
int main()
{
// input
vector<int> breakpoints = {2, 5, 7, INT_MAX};
vector<int> v1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
vector<int> v2 = { 10, 20, 30, 40, 50, 60, 70, 80, 90 };
// Create output
vector<int> ExpectedOutput;
ExpectedOutput.reserve(v1.size());
int origin = 0;
int breakpoints_index = 0;
for (int i = 0 ; i < v1.size() ; i++)
{
if (origin)
{
ExpectedOutput.push_back(v1[i]);
} else
{
ExpectedOutput.push_back(v2[i]);
}
if (breakpoints[breakpoints_index] == i)
{
origin = !origin;
breakpoints_index++;
}
}
// print output
cout << "output: ";
for (int i = 0 ; i < ExpectedOutput.size() ; i++)
{
cout << ExpectedOutput[i] << " ";
}
cout << endl;
return 0;
}
which outputs
output: 10 20 30 4 5 6 70 80 9
It feels like there must be a better solution such as something analogous to Solution 2 from above. Is there a faster solution?
Repeating push_back() means that every time around the loop, a check is being performed to ensure capacity() is large enough (if not, then more space must be reserved). When you copy a whole range, only one capacity() check needs to be done.
You can still be a bit smarter with your interleaving by copying chunks. Here's the very basic idea:
int from = 0;
for( int b : breakpoints )
{
std::swap( v1, v2 );
int to = 1 + std::min( b, static_cast<int>( v1.size() ) - 1 );
ExpectedOutput.insert( ExpectedOutput.end(), v1.begin() + from, v1.begin() + to );
from = to;
}
For the sake of brevity, this code actually swaps v1 and v2 and so always operates on v1. I did the swap before the insert, to emulate the logic in your code (which is acting on v2 first). You can do this in a non-modifying way instead if you want.
Of course, you can see a bit more is going on in this code. It would only make sense if you have considerably fewer breakpoints than values. Note that it also assumes v1 and v2 are the same length.

summing numbers recursive C++

I'm trying to make a recursive program that sums an array or a list of numbers.
Using visual studio 2013, C++ console application.
My 1st question is:
Now I know how many numbers I have and I know the size of my array. How can I program it the way that don't know the numbers in advance, like while it's calculating the numbers there are still new numbers adding up, with the least space usage?
My 2nd question is that:
How can i improve the program that still works recursively and its time and space usage be optimal?
Here is my code:
// summing a list of number.cpp
#include "stdafx.h"
#include "iostream"
int array[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int sum = 0, i = 0;
int sumarray(int i){
if (i < 9){
sum += array[i];
i++;
sumarray(i);
}
else
return sum;
}
int main(){
std::cout << "sum is ::: " << sumarray(i);
getchar();
}
I hope you'll stop writing functions that depend on global variables to work when they can be easily made to work only with the input they have been provided.
Here's a version that works for me.
#include <iostream>
int sumarray(int array[], int i)
{
if ( i <= 0 )
{
return 0;
}
return sumarray(array, i-1) + array[i-1];
}
int main()
{
int array[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
std::cout << "sum is : " << sumarray(array, 0) << std::endl;
std::cout << "sum is : " << sumarray(array, 5) << std::endl;
std::cout << "sum is : " << sumarray(array, 10) << std::endl;
}
Output:
sum is : 0
sum is : 15
sum is : 55
If i >= 9, your function does a return sum;.
(that is fine and good)
Where does your function return if i < 9???
if (i < 9){
sum += array[i];
i++;
sumarray(i); // I see no return statement here!!
}
Basically, if you call sumarray(3), there is no return statement that gets hit.
In your program, there is a global variable called i.
There is also a local parameter to the function also called i.
The local variable shadows the global variable, so there is no clear purpose to the global i.
I'd do it like this:
int array[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Pass in the current index, and the size of the array
int sumarray(int i, int sz)
{
if (sz == 0)
{
return 0;
}
return array[i] + sumarray(i+1, sz-1);
}
int main(){
std::cout << "sum is ::: " << sumarray(0, 10);
// Start at the beginning (index 0)
// Run for 10 elements.
getchar();
}
The first recursive call will be to sumarray(1,9); then to sumarray(2,8);... when finally sumarray(10,0) is called, it will return 0.
A function to sum the elements of an array would normally accept the array as an argument. In that case, as a practical matter it must also accept the size of the array. Something like this:
int sumarray(int a[], size_t size) {
A signature like that furthermore gives you access to better recursive approaches. In particular, you could recursively compute the sum of the first and second halves of the array, and return their sum:
size_t midpoint = size / 2;
return sumarray(a, midpoint) + summaray(a + midpoint, size - midpoint);
That's not a complete solution: you need a termination condition (when size is less than 2). Putting that in and finishing off the function are left as an exercise for you, since you'll learn better if you have to put some work into it yourself.
That approach limits the recursion depth and thus stack size (memory overhead) to be proportional to the logarithm of the array size, though it still involves total numbers of function calls and integer additions proportional to the array size. I don't think you can achieve better asymptotic space or time complexity with a recursive algorithm. (A non-recursive algorithm for this task requires only a fixed number of function calls and and a fixed amount of memory overhead, however.)
here is a working C++ code in Qt, which i wrote - Good Luck
I added some debug points outputs to make its understanding clearer
#include <QCoreApplication>
#include <QDebug>
int sum=0;
int sumrec(int *array,int n)
{
if (n>=0)
{
int element=*(array+n); // note *(array+n) -> moving the pointer
// *array+n -> this is adding n to the pointer data (wrong)
// what is array ?
qDebug() << " element value " << *(array+n) << " at n=" << n << " array address = " << array;
n--;
sum=sum+element;
qDebug() << "sum = " << sum;
sumrec(array,n);
return sum;
}
else
{
return 0;
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
int A[10]={12,13,14,15,16,17,18,19,20,11};
int b=sumrec(&A[0],9);
qDebug() << "answer = " << b;
//return a.exec();
}
here is the output of the terminal
element value 11 at n= 9 array address = 0x7fff5fbffb78
sum = 11
element value 20 at n= 8 array address = 0x7fff5fbffb78
sum = 31
element value 19 at n= 7 array address = 0x7fff5fbffb78
sum = 50
element value 18 at n= 6 array address = 0x7fff5fbffb78
sum = 68
element value 17 at n= 5 array address = 0x7fff5fbffb78
sum = 85
element value 16 at n= 4 array address = 0x7fff5fbffb78
sum = 101
element value 15 at n= 3 array address = 0x7fff5fbffb78
sum = 116
element value 14 at n= 2 array address = 0x7fff5fbffb78
sum = 130
element value 13 at n= 1 array address = 0x7fff5fbffb78
sum = 143
element value 12 at n= 0 array address = 0x7fff5fbffb78
sum = 155
answer = 155
In C++ you have all the tools to do that in a very simple, readable and safe way. Check out the valarray container:
#include <iostream>
#include <valarray>
int main () {
std::valarray<int> array{1,2,3,4,5,6,7,8,9,10};
std::cout << array.sum() << '\n';
return 0;
}

The last variable in an array is incorrect [closed]

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.