C++ advice

bazylik

[H]ard|Gawd
Joined
Aug 18, 2005
Messages
1,405
guys I could use some advice on this. below is part of my pseudo code which explains what should do but it's not :)
here it goes

srand....;

array[] = {'A','B','C'};

do
Enter how many pairs: (for example) 4
for loop
{
function that outputs pair of letters from the array;
}
example output:
AB CA CB BA

Now my issue is this. There is another function that has all the possible outcomes from these letter in pairs of two in lower case.
Bunch of if statements. I have to list the comparables right below the output.
I have no idea how to do that. so far I've only been able to get output like this:
AB ab CA ca CB cb BA ba

or

AB ab
CA ca
CB cb
BA ba

because I have that second function inside that for loop
when I put it outside the for loop I get the output that looks like this

AB CA CB BA
ba ba ba ba

which is what I need but the pairs need to be corresponding with their lower case counterparts
like this:
AB CA CB BA
ab ca cb ba

I need advice on how to accomplish that. I read the sticky so I'm not sure if I should post the code or not. Maybe it would easier for someone to point me in the right direction if I showed the code.
 
Are you allowed to modify those 2 functions that you call?
Are you allowed to use stringstreams?
If you are, then I suggest you read up on stringstreams, and figure out how they can help you.
 
code
#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

char getRandomChar(char array[], int size)
{
int i;
char randomChar;
for(i = 0; i < size; i++)
{
int randChar = rand() % 2;
randomChar = array[randChar];
}
return randomChar;
}

void comparables(char n1, char n2)
{
if ((n1 == 'A' && n2 == 'B') || (n1 == 'B' && n2 == 'A'))
{
cout << 'a' << 'b' << " ";
}
if ((n1 == 'B' && n2 == 'C') || (n1 == 'C' && n2 == 'B'))

{
cout << 'b' << 'c' << " ";
}
if ((n1 == 'A' && n2 == 'C') || (n1 == 'C' && n2 == 'A'))
{
cout << 'a' << 'c' << " ";
}
if (n1 == 'A' && n2 == 'A')
{
cout << 'a' << 'a' << " ";
}
if(n1 == 'B' && n2 == 'B')
{
cout << 'b' << 'b' << " ";
}
if(n1 == 'C' && n2 == 'C')
{
cout << 'c' << 'c' << " ";
}
}

main()
{
srand(time(NULL));

int number;
char n1, n2;
char array[] = {'A','B','C'};


do
{
cout << "Enter number of pairs: ";
cin >> number;

if(number >= 1 && number <= 200)
{
for(int i = 0; i < number; i++)
{
n1 = getRandomChar(array, 2);
n2 = getRandomChar(array, 2);
cout << n1 << n2 << " ";
comparables(n1, n2);
}
cout << endl;
}
else
{
cout << "Number must be between 0 and 200.\n";
cout << "Try again..." << endl << endl;
}
}
while((number <= 0) || (number > 200));

return 0;
}
 
Are you allowed to create/write to arrays?

This question is rhetorical. I think it's a big enough hint that you should know what to do now.
 
I was thinking that I should create an empty array that would be filled with 'pairs' but since I can't use strings, only chars, I'm not sure how to do that. I mean the said array would be than filled with single characters instead of pairs, right? So, how the program would then use that to make the comparison?
 
I see what you're saying, make two arrays that store n1 and n2 respectively. But I'm looking at it and I have no idea how to pass that n1 to this empty array :(
 
got it... ended up using only one n variable and and filled up one array instead of two...
Now I need some kind of formula that will iterate and compare every two elements in the array with the if statements.

uphill battle all the time... how do you guys learn all this? in my 3 months semester they expect us to learn correct syntax and all that without much explaining. *sigh*
 
how do you guys learn all this? in my 3 months semester they expect us to learn correct syntax and all that without much explaining.
Programming is one of the careers where you are "permanently in school". Self-motivation is important, and you have to set aside free time to keep learning so that you stay relevant and current in a constantly changing field.
 
I would also recommend you focus on one language at first until you learn it backwards and forwards. Once you learn advanced methods, it will be easier to port the logic to other languages.
 
I would also recommend you focus on one language at first until you learn it backwards and forwards. Once you learn advanced methods, it will be easier to port the logic to other languages.

THIS. Language is easy, learning to code isn't. Learn to code, and the rest is just syntax.
 
I've noticed.. I have no problem with theory and learning the material, much worse with implementing the code and such. But slowly I'm getting there. Practice is king in this :) thanks guys for nice words of wisdom...
 
on a related note... snippet of this code below is supposed to swap letters according to the user input... the number of swapped letters is AT MOST of what the user gave. I'm including this piece because I believe this is where the rand() not doing what is supposed to. I tried for loop, tried shifting the rand(), tried expanding the range so there were less chances of the rand() repeating itself... I'm out of ideas how to change the rand() around so the number of swaps is exactly what the user puts in..

int swap(char a[], int size)
{
char W = 'w';
char X = 'x';
char Y = 'y';
char randomChar;

int i = rand() % size;
randomChar = a;

if(a == 'A')
{
a = W;
return a;
}
if(a == 'B')
{
a = X;
return a;
}
if(a == 'C')
{
a = Y;
return a;
}
}
 
Back
Top