trouble with finding repeated alphabets

So I want to build a game using C++. Everything was fine until I met a problem.

The game was suppose to count the number of repeated and adjacent alphabets from some random alphabets but for some reason I would not get the function to word the output was suppose to be the total number of repeated numbers and adjacent numbers.

a = rand() % 7;
printf("%c", alphabet[a]);
beta[count] = beta[a];
Here is the code:

for (i = 0; i <=12; i++)
{
for (g = i + 1; g <= 12; g++)
{
if (beta[i] == beta[g])
{
repeated++;
}
i = g + 1;

if (beta[i] == beta[g])
{
adjacent++;
}
}

printf("\nnumber of repeated alphabets=%d\n", repeated);
printf("number of adjacent alphabets=%d\n", adjacent);
}
Let's say that the random generator generates C,B,A,A,B,D,G, the for loop is suppose to count the number of times a alphabet has repeated so A,A,B,B appear, so it should show that the number of repeated characters is 2 and the adjacent character is 1 but my current program out shows the number of repeated alphabets to be 6 and adjacent to be 1 so i have no idea will i is wrong with the problem
beta[count] = beta[a];


Should this be beta[count] = a;? Using beta[a] doesn't make sense here if this is initialization, since you haven't filled anything in the beta array yet.

EDIT: or maybe beta[count] = alphabet[a];? Depends what you want to actually be in the beta array.
Last edited on
ya thanks but it still does not solve the problem for the output of repeated and adjacent
i = g + 1;

This is wrong.

i is your outer loop counter, so modifying it like this will disrupt the flow of your outer loop.
so what should i do to replace this i=g+1;, because if i delete it the output would even shown, sorry i am a newbie at this
Well, what were you trying to accomplish with that line?
because initially when i play the code the output will not show so i play around with the for loop and my friend told me if i add this i=g+1, it will work but so far its still not working
Randomly poking at code hardly ever works. You need to understand what your code is doing.

So what do you want the code to do?
for my code i need the for loop to count the 7 alphabets that is store inside the beta array, and from there count how many repeated and adjacent alphabets are there
So you have an array, 'beta' which contains letters of the alphabet. And you want to check that array to see if 2 adjacent letters are the same.

So... ignore your code for now. Instead, how can you check to see if 2 letters (let's say, beta[0] and beta[1]) are the same? Can you show me the code for that?
actually i can use the
if (beta(i)=='A')
{
repeated++
}
and so on
but it also does not work
but it also does not work


IMO, you should stop trying to just find something that works. The best thing for you is to understand the problem. For that, I'm trying to break it down into smaller parts.

So... let's start small. You have 2 letters, beta[0] and beta[1]. How can you see if they are the same?
i can just put beta[0]==beta[1] and it should work
Correct!

So we know that beta[0] and beta[1] are adjacent, obviously. But how do we know that?
we can print out the output to make sure??
Nah.

We know they're adjacent because their indexes are next to each other.

IE:

0 and 1 are adjacent.
1 and 2 are adjacent
2 and 3 are adjacent

0 and 2 are NOT adjacent.


If you'll notice, this means you can add (or subtract) 1 from an index to get an adjacent index.


So... given an index 'n'... let's say we have beta[n]. How can we check to see if an adjacent letter is the same as that one?
we check if beta[n] is beside [m] or [o]
Last edited on
Topic archived. No new replies allowed.