-1

How do I print out this multi-dimensional vector? I just can't figure out what to put as a condition in the inner loop in my print section in the code below. I can't seem to figure out how to loop through the array to print grades. Any help would be appreciated.

#include <iostream>

using namespace std;

int main()
{
    int course, grades;

    int** crsgrd;
    // get inputs and asign grades
    cout << "Enter number of courses: ";
    cin >> course;
    crsgrd = new int * [course];
    for (int c = 0; c < course; c++) {
        cout << "Enter number of grades: ";
        cin >> grades;
        crsgrd[c] = new int[grades];
        for(int g = 0; g < grades; g++)  {
            cout << "Enter your grade: ";
            cin >> crsgrd[c][g];
        }
    }
    // print grade report
    for(int c = 0; c < course; c++) {
        for(int g = 0; g <= ?????????; g++)
            cout << crsgrd[c][g] << " ";
        cout << endl;
    }

    // free the array
    for(int i = 0; i < course; i++)
        delete [] crsgrd[i];
    delete [] crsgrd;

    return 0;
}
  • `g < grades`??? – AndyG Mar 15 '19 at 02:29
  • As an aside, this being C++, one should use a `std::vector` so they don't have to worry about all this memory management. – AndyG Mar 15 '19 at 02:30
  • It seems that you have created the problem: `cin >> grades; crsgrd[c] = new int[grades];` . You invite the user to give a different number of grades for each course, yet you do not store the number of grades. There are several ways to handle this: 1. You could store the number of grades as the first element in each vector. 2. You could use std::vector> courseGrades. You can come up with other methods as well. – Gardener Mar 15 '19 at 02:53
  • 1
    Improvement on @JohnMurray 's suggestion of a `std::vector` of `std::vector`s: A single `std::vector` [wrapped in a class](https://stackoverflow.com/a/2076668/4581301) that makes it look 2D. This keeps all of the data close together and a modern CPU **loves** it when you do that. It makes preloading the cache dead easy. – user4581301 Mar 15 '19 at 03:21
  • 1
    @user4581301 suppose the first course had 5 grades and the second course had 10. Would you create a Matrix::reserve(size_t rows, size_t cols) function to allocate the additional space for the longer row where cols = max(Lengths of all rows thus far)? I agree that keeping the data together is better than individually allocating the rows, creating a caching nightmare. – Gardener Mar 15 '19 at 12:35

2 Answers2

0

g < crsgrd[c].size() Alternatively, you could create your own size function if this is some kind of assignment that doesn’t allow built in functions.

sDetta
  • 11
  • 1
  • 3
  • `crsgrd[c].size()` is not a valid function as the OP has used a standard c-style array for each row. This would be the perfect solution if combined with using a std::array<> for each individual row. – Gardener Mar 15 '19 at 12:29
0

You have created a 2D array, not a standard vector. Arrays do not store their length, so you have to either come up with a way to store the length or switch to another type of storage container.

Here is an example that stores the number of grades as the first element of the array for a given course:

#include <iostream>

using namespace std;

int main()
{
  int course, grades;

  int** crsgrd;
  // get inputs and asign grades
  cout << "Enter number of courses: ";
  cin >> course;
  crsgrd = new int * [course];
  for (int c = 0; c < course; c++) {
    cout << "Enter number of grades: ";
    cin >> grades;
    crsgrd[c] = new int[grades + 1];  // extra cell for # of grades
    crsgrd[c][0] = grades;
    // start storing grades at cell [c][1] as the first cell has the length
    for(int g = 1; g < grades+1; g++)  {
      cout << "Enter your grade: ";
      cin >> crsgrd[c][g];
    }
  }
  // print grade report
  for(int c = 0; c < course; c++) {
    cout<< "Course #" << c+1 << " grades:" << endl;

    // use first element as limit for for loop.
    // Be sure to print one more element as we have an extra cell 
    // at the end.
    for(int g = 1; g < crsgrd[c][0] + 1; g++) 
       cout << crsgrd[c][g] << " ";
       cout << endl;
  }

  // free the array
  for(int i = 0; i < course; i++)
    delete [] crsgrd[i];
  delete [] crsgrd;

  return 0;
}
Gardener
  • 2,591
  • 1
  • 13
  • 22