So I am writing a program for class and the program takes a number of random ordered pairs then calculates the length of the array by adding the abscissa of each ordered pair together. I have that part done. From this point it is supposed to fill the array with the the ordinate of the ordered pair and have the same amount of letters as the number of the abscissa.
Example (4, A)(1,B) would net:
0 A
1 A
2 A
3 A
4 B
I am having trouble getting this part of the program done. If anyone could take a look at my code and offer some pointers I would appreciate it. Thanks
On line 59 you're assigning to an element one beyond the array bounds.
If I understand the task, you want a 2nd loop to copy each pairVector.at(i).second
pairVector.at(i).first number of times.
I'll use an extra variable k to keep the index for charArray[k] easy to figure. Perhaps you want:
1 2 3 4 5 6 7 8 9
char * charArray = newchar [arraySize + 1] ;// for the '\0' you need at the end for cout << charArray to work
unsigned k = 0;
for (unsignedint i = 0; i < pairVector.size(); i++)
for (unsignedint j = 0; j < pairVector.at(i).first; j++)
charArray[ k++ ] = pairVector.at(i).second;
charArray[arraySize] = '\0';// the null terminator
cout << charArray;
You're saving my life right now fun2code. That's what I'm looking for. Is there a way to make it so that each letter has a value 0-n in front of it kind of like a list format? If so could you give me an idea so that I could try to figure it out? And is there a way that I could have it print vertically instead of horizontally?
Do you even need to save the characters in charArray? Or do you just want the output you specified? I suppose you could do both. The 2 for loops are already suited to produce the output you desire.
1 2 3 4
int k = 0;
for (unsignedint i = 0; i < pairVector.size(); i++)
for (unsignedint j = 0; j < pairVector.at(i).first; j++)
cout << k++ << pairVector.at(i).second << '\n';// the number, the letter and a newline
EDIT: Made correction to output line.
Or. Keep the 1st code for filling charArray[] and replace line 9 (the output) with:
1 2
for (unsignedint i = 0; i < arraySize; i++)
cout << i << charArray[i] << '\n';
You problem is that you're trying to change the input vector into the output vector. You need a separate output vector:
1 2 3 4 5 6 7 8 9 10 11
//This is where my problem is//
vector<pair<unsignedint, char> > outputVector;
for (unsignedint i = 0; i < pairVector.size(); i++) {
for (unsignedint j = 0; j < pairVector[i].first; ++j) {
outputVector.push_back(pair<unsignedint, char>(1, pairVector[i].se\
cond));
}
}
printRandomPairs(outputVector);
The last line should make you uncomfortable. After all this isn't the random pairs that you're printing. Perhaps it would be better to rename printRandomPair() to be printVector() instead.
Why are you using pair<>? By using a template, you're throwing away the ability to provide meaningful names and forcing the reader and the coder to remember if first is the count or the character. Here is the code with a class instead of pair<>. I haven't changed all the names, but you get the idea.
fun2code - I didn't need to save them to charArray just print them, if I didn't save them would the code look much different? Thanks so much for everything man you are the best. You helped me a lot and now I at least understand the concept better and can implement it again or at least have a reference! :D
dhayden - My instructor coded the first bit generating the pairs and choosing what to do I don't know what his though process was behind it. I like the idea of the class it makes things simpler and cleaner. I will use it for a guideline and try to redo this program using a class instead thank you for your input and advice I would never have thought to do it this way, I'm still a baby when it comes to c++ and some concepts I don't fully grasp still.
Thank you both for the help I appreciate it very much.