Need help printing out correct answer

What I have to do is, if there's at least more than 3 pairs that are close to each other for example G1 G2 G3 (once they're sorted) they get printed in order. The printing stops once the other number is not close by 1. Example
G1 G2 G3 G5 G4
It would still print only up to G1 G2 G3

I have it all sorted and done. However once I print I never get last digit

1
2
3
4
5
6
7
8
9
10
    if(spausdintiG)
    {
        for(int i = 1; i <= gKiek; i++)
        {
            if(gMas[i] + 1 == gMas[i + 1])
            {
                cout << gMas[i] << " ";
            }
        }
    }


However I can force the last digit to be printed by doing this :

1
2
3
4
5
6
7
8
9
10
11
    if(spausdintiG)
    {
        for(int i = 1; i <= gKiek; i++)
        {
            if(gMas[i] + 1 == gMas[i + 1])
            {
                cout << gMas[i] << " ";
            }
        }
        cout << gMas[gKiek]; // the way I can force last digit to be printed
    }


But it's not all. I have one more array that has to be printed.
The output should be :
M1 M2 M3 M4
M9 M10 M11

And again, the answer I get is M1 M2 M3
M9 M10

I could force the M11 to show up by doing the trick but it only works for last digit so the M4 won't show up.

EDIT

I found out that it never checks with last digit
Example

G1 G2 G3

1 + 1 = 2
Prints 1
2 + 1 = 3
Prints 2
3 + 1 = ?

So what do I do now ?
Last edited on
Hello DdavidDLT,

In C++ as with some other languages arrays and other containers along with some others parts of the language are (0) zero based.

This means that when accessing the array the first element is (0) zero not one, I.e., a five element array
1
2
3
constexpr size_t MAXSIZE{ 5 };

gMas[MAXSIZE];

the elements are gMas[0] - gMas[4] for a five element array.

Starting the for loop at (1) you are missing the first element of the array and with out seeing the rest of the code I am not sure why the "<=" is not working. That is one of those problems I would like to test.

You are also likely to have a problem with gMas[i + 1] if "i = 1" goes beyond the end of the array or into an element of the array that does not have a good value.

I would be helpful to see the rest of the code to put everything into perspective.

Hope that helps,

Andy
The reason I'm starting here in loop i = 1 is because of previous code that forced me to do this way. You can see that I started my rKiek, gKiek etc as a 0 and then rKiek++ becomes 1. So you can see where this goes. The rest of the code is just sorting the new arrays and the one I gave (for printing out the answers).

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
    int rMas[100], rKiek = 0;
    int gMas[100], gKiek = 0;
    int mMas[100], mKiek = 0;
    int jMas[100], jKiek = 0;

    for(int i = 0; i < n; i++)
    {
        if(RGMJ[i] == 'R')
        {
            rKiek++;
            rMas[rKiek] = skaic[i];
        }
        else if(RGMJ[i] == 'G')
        {
            gKiek++;
            gMas[gKiek] = skaic[i];
        }
        else if(RGMJ[i] == 'M')
        {
            mKiek++;
            mMas[mKiek] = skaic[i];
        }
        else
        {
            jKiek++;
            jMas[jKiek] = skaic[i];
        }
    }


EDIT :

"You are also likely to have a problem with gMas[i + 1] if "i = 1" goes beyond the end of the array or into an element of the array that does not have a good value."

Yes, that's what I mentioned in my previous edited post that i goes beyond the end of the array. What I though I could make is adjust the if statment if || however that doesn't seem to be working :

 
if(gMas[i] + 1 == gMas[i + 1] || gMas[i + 1] - 1 == gMas[i])
Last edited on
Hello DdavidDLT,

The reason I'm starting here in loop i = 1 is because of previous code that forced me to do this way.


There is your first problem. Do not count on previous code to work here. The code you use in this program may have to be adjusted from the previous code.

Starting your variables "rKiek, gKiek and the others" at zero is fine and as it should be.

Your for loop for(int i = 0; i < n; i++) looks like it should work for now, but I have no idea where "n" came from, what it is fore, being a single letter instead of a better name, and its value.

"RGMJ[i]" tends to make me think that this is defined a constant array with values that can not be changed. is that true?

Sorry when I said "see the rest of the code" I should have said the whole program. Something that I can compile and see how it runs.

Right now there is to much guess work to make a program that runs and to duplicate the problems you are having.

Your if statement if(gMas[i] + 1 == gMas[i + 1] || gMas[i + 1] - 1 == gMas[i]). On the lhs of || you are taking the value of "gMas[i]" and adding one to this value then checking it against the next element of the array. On the rhs this is the opisit of the lhs. Having no idea what values are in "gMas" I do not know if this will ever work and my first thought is that it will never work.

I will see if I can work something up to test your code, but do not know if it will be the right way.

Hope that helps,

Andy

P.S. what we have here is the XY problem http://xyproblem.info/ Please take a look.
Hello DdavidDLT,

I did manage to put something together to run the code that you have posted so far.

When I looked at the for loop with all the if statements I noticed:
1
2
3
4
5
6
if (RGMJ[i] == 'R')
{
	rKiek++;

	rMas[rKiek] = SKAIC[i];
}

This add one to "rKiek" before you use it and since you defined the variable and initialized it to zero you essentially starting wit element one of the array.

You need to reverse these lines in the if statements and add one after you have used it or write the line as rMas[rKiek++] = SKAIC[i];. This will use the value of "rKiek" and store the rhs of "=" into the array before adding one for the next use. It all works out the same.

Not that I have the right information to use the arrays properly, but for the other for loop it should look like this:for (int i = 0; i < gKiek - 1; i++). Then when you use gMas[i + 1] it will not go past the usable part of the array or the end.

Hope that helps,

Andy
Topic archived. No new replies allowed.