Reputation: 1
im very new to C++ programming and i have an issue with 2D array and for loop.. i was looking for solution all over program that i made recently, but everything else works good. Except this for loop.
This is just a small part of program, but seems problem is over this code. This program is basically a test. There is like 5 questions with 3 answer options and user should enter 1, 2, 3 (int values, function that is responsible for getting these values to atsakymai[] array is working properly tho). JDM, USDM, EUDM arrays should keep points for right answer (they are set to 0 at start). atsakymoVerte array keeps answers order from which points depends on. For ex. 1 - +1 point to JDM; 2 - 1+ to USDM; 3 - 1+ to EUDM.
This program seems to work properly until for loop reaches y = 4. But after that it gives bad values... first 3 times loop works good and increments right variables(variables from JDM, USDM or EUDM arrays) by one. Everything works good until this 4th time in the loop... i have no idea what i should change to get it working right. I was sitting beside this problem for a few hours, but yeah... no solution. I hope you guys will understand what is my problem. Thanks.
int *JDM = new int[6]();
int *USDM = new int[6]();
int *EUDM = new int[6]();
int atsakymoVerte[6][4] = {
{0,0,0,0},
{0,2,1,3},
{0,2,1,3},
{0,1,2,3},
{0,3,1,2},
{0,3,1,2}
};
for (int y = 1 ; y < 6; y++) {
for (int x = 1; x < 4; x++) {
if (atsakymai[y] == atsakymoVerte[y][x]) {
switch (x) {
case 1: JDM[y]++;
break;
case 2: USDM[y]++;
break;
case 3: EUDM[y]++;
break;
}
}
}
}
Upvotes: 0
Views: 152
Reputation: 1235
I think you are actually calculating loop by n-2 , change you for loops to
for (int y = 0 ; y < 6; y++)
for (int x = 0; x < 4; x++)
and where is this atsakymai[y]
declared you must define it to compare your array otherwise you continuously getting error
You are faking a 1-based array, you should not do that. This causes many problems and wastes memory. Why make useless elements, what if you have a "2d" array of 1000 by 1000, you would be wasting 2000 elements.
I would have the following
// remove useless elements
int *JDM = new int[5]();
int *USDM = new int[5]();
int *EUDM = new int[5]();
int atsakymoVerte[5][3] = {
{2,1,3},
{2,1,3},
{1,2,3},
{3,1,2},
{3,1,2}
};
for (int y = 0 ; y < 5; y++) {
for (int x = 0; x < 3; x++) {
// WARNING: Make sure to remove useless elements
if (atsakymai[y] == atsakymoVerte[y][x]) { // in atsakymai and re-write previous indexes
switch (x) {
case 1: JDM[y]++;
break;
case 2: USDM[y]++;
break;
case 3: EUDM[y]++;
break;
}
}
}
}
Upvotes: 1
Reputation: 30747
Don't know about c++ - though many other languages have 0-index arrays. This means that 0 is the first element.
so instead of
for (int y = 1 ; y < 6; y++)
for (int x = 1; x < 4; x++)
It should really be
for (int y = 0 ; y < 6; y++)
for (int x = 0; x < 4; x++)
By using the loop you have at the moment, the first pass has y
equal to 1
- this would be atsakymai[1]
- which is actually the 2nd element in the atsakymai
array, not the first.
If you're not careful, you can also end up trying to reference an index that doesn't exist.
Upvotes: 1