Dynamic Arrays help please

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

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <cstdlib>
#include <vector>
#include <utility>
using namespace std ;

// --------------------------------------------------------------------------

void generateRandomPairs ( vector< pair<unsigned int,char> > & pairVector )
  {
  unsigned int randSeedValue ;
  cout << "\nEnter random number generator seed value: " ;
  cin >> randSeedValue ;
  srand ( randSeedValue ) ;
  unsigned int numberOfPairs = (rand() % 4 + 3) ;
  pairVector.clear() ;
  for ( unsigned int i = 1 ; i <= numberOfPairs ; i ++ )
    {
    unsigned int randomCount = (rand() % 4 + 1) ;
    char randomUppercase = (rand() % 26 + 'A') ;
    pair<unsigned int,char> randomPair ( randomCount, randomUppercase ) ;
    pairVector.push_back ( randomPair ) ;
    } // end for loop
  } // end generateRandomPairs function

// --------------------------------------------------------------------------

void printRandomPairs ( const vector< pair<unsigned int,char> > & pairVector )
  {
  cout << "\nThe vector of ordered pairs is ...\n\n\t< " ;
  for ( unsigned int i = 0 ; i < pairVector.size() ; i ++ )
    {
    pair<unsigned int,char> numberLetterPair = pairVector.at(i) ;
    cout << '(' << numberLetterPair.first << ',' << numberLetterPair.second << ") " ;
    } // end for loop
  cout << ">\n\n" ;
  } // end printRandomPairs function

// --------------------------------------------------------------------------

int main ()
  {
  vector< pair<unsigned int,char> > pairVector ; // initially empty
  generateRandomPairs ( pairVector ) ;
  printRandomPairs ( pairVector ) ;

  unsigned int arraySize (0), count (0);
  
  for (unsigned int i = 0; i < pairVector.size(); i++)
	  arraySize += pairVector.at(i).first;

	  cout << arraySize;

//This is where my problem is//

	 char * charArray = new char [arraySize] ;
	 
	 for (unsigned int i = 0; i < pairVector.size(); i++)
		 charArray[arraySize] = pairVector.at(i).second;

		 cout << charArray;

  
  } // end main function

// --------------------------------------------------------------------------
Last edited on
I have this same problem in my class and am currently working on it, I will post here as soon as I can! let me know if you finish it before then!
Where do you go to school? Yeh I'm still working on it it sucks :p.
Why oh why are you messing with pointers, arrays, new[], and delete[] if you are already allowed to use std::vector? Is your professor insane?
I have no idea I'm just trying to get my program done.
closed account (D80DSL3A)
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 = new char [arraySize + 1] ;// for the '\0' you need at the end for cout << charArray to work
	 
        unsigned k = 0;
	 for (unsigned int i = 0; i < pairVector.size(); i++)
              for (unsigned int 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?
closed account (D80DSL3A)
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 (unsigned int i = 0; i < pairVector.size(); i++)
      for (unsigned int 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 (unsigned int i = 0; i < arraySize; i++)
    cout << i << charArray[i] << '\n';
Last edited on
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<unsigned int, char> > outputVector;

    for (unsigned int i = 0; i < pairVector.size(); i++) {
        for (unsigned int j = 0; j < pairVector[i].first; ++j) {
            outputVector.push_back(pair<unsigned int, 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.
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <cstdlib>
#include <vector>
#include <utility>
using namespace std;

class CountedChar {
public:
    CountedChar(unsigned c, char _ch):
        count(c), ch(_ch)
    {}
    unsigned int count;         // number of characters
    char ch;                    // the character
};


// --------------------------------------------------------------------------

void
generateRandomPairs(vector <CountedChar> &ccVector)
{
    unsigned int randSeedValue;
    cout << "\nEnter random number generator seed value: ";
    cin >> randSeedValue;
    srand(randSeedValue);
    unsigned int numberOfCCs = (rand() % 4 + 3);
    ccVector.clear();
    for (unsigned int i = 1; i <= numberOfCCs; i++) {
        unsigned int randomCount = (rand() % 4 + 1);
        char randomUppercase = (rand() % 26 + 'A');
        CountedChar randomCC(randomCount, randomUppercase);
        ccVector.push_back(randomCC);
    }                           // end for loop
}                               // end generateRandomPairs function

// --------------------------------------------------------------------------

void
printCCVec(const vector < CountedChar> &pairVector)
{
    cout << "\nThe vector of ordered pairs is ...\n\n\t< ";
    for (unsigned int i = 0; i < pairVector.size(); i++) {
        CountedChar numberLetterPair = pairVector.at(i);
        cout << '(' << numberLetterPair.count << ',' << numberLetterPair.ch << \
") ";
    }                           // end for loop
    cout << ">\n\n";
}                               // end printRandomPairs function

// --------------------------------------------------------------------------

int
main()
{
    vector < CountedChar> pairVector;           // initially empty
    generateRandomPairs(pairVector);
    printCCVec(pairVector);

    unsigned int arraySize(0), count(0);

    for (unsigned int i = 0; i < pairVector.size(); i++) {
        arraySize += pairVector.at(i).count;
    }

    cout << arraySize;

    vector<CountedChar> outputVector;

    for (unsigned int i = 0; i < pairVector.size(); i++) {
        for (unsigned int j = 0; j < pairVector[i].count; ++j) {
            outputVector.push_back(CountedChar(1, pairVector[i].ch));
        }
    }
    printCCVec(outputVector);
}                               // end main function 

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.
Last edited on
Topic archived. No new replies allowed.