-4

I'm having trouble with one of my assignments (or maybe I'm overthinking it?) I need to create

  1. a function to take integer parameters for number of students and tests.
  2. Allocate the memory needed for the array of students and the array of test scores for each student.
  3. Return a pointer to the array of Student structures. No display output is done in this function.

    int main()
    {
        int studentNum;
        int testNum;
    
        cout << "How many students are there?\n";
        cin >> studentNum;
        cout << "How many tests are there?";
        cin >> testNum;
    
        system("pause");
        return 0;
    }
    

my function

GradeBook *initArrays(int s, int t)
{
    GradeBook *ptr;

// Allocate the array of Student structures.
ptr = new GradeBook[s];

// Allocate an array of ints (to hold test scores)
// for each element of the array of Student structures.
for (int count = 0; count < s; count++)
{
    ptr[count].tests = new int[t];
}

// Return a pointer to the array of structures.
return ptr;

}

edit: I have edited my function, could i get some opinions on that?

4
  • 2
    Where are your curly braces ({})? This isn't even approximately valid c++ code. Commented Jul 8, 2018 at 18:55
  • 1
    Step 1: Write a function that allocates one dimension. Get that working. This may shake loose all that you need to figure out how to produce the 2 dimension version. But also look into std::vector and writing a simple matrix class. Commented Jul 8, 2018 at 19:00
  • How could int* be "a pointer to the array of Student structures"? Your function needs to allocate memory for those structures (you should not use a global variable). Commented Jul 8, 2018 at 19:05
  • Just a good practice point: always use meaningful names for variables: count is good, ptr is bad. Use something like pGradeBooks. It will not block you now but you should take good habits. Commented Jul 8, 2018 at 23:02

2 Answers 2

1

if you are writing this in c++, use classes. if i understand correctly, you should create a structure to save a students id,name,or something and a corresponding grade?

something like:

class Test{
public:
    int id;
    int grade;
    Test(int id, int grade){
        this->id = id;
        this->grade = grade;
    }
};

class Student{
public:
    int id;
    std::string name;
    std::vector<Test> tests;
    Student(int id, std::string name)
    {
        this->id = id;
        this->name = name;
    }
};

int main(){
    vector<Student> students;
    int studentnum;
    for (int i = 0; i < studentnum; i++){
        students.push_back(Student(i, "name"));
        //insert all tests of the student by calling students[i].tests.push_back(Test(id, grade))
    }
}

this way you don't have to allocate memory, which you can easily overlook freeing.

edit: this is very basic and not a sophisticated solution, as the properties of the classes are all public.

edit 2:

    typedef struct Test{
    int id;
    int grade;
}Test;

typedef struct Student{
    int id;
    std::string name;
    Test * tests;
}Student;

int main(){
    Student * students;
    int studentnum;
    students = (Student*)malloc(sizeof(Student)*studentnum);
    for (int i = 0; i < studentnum; i++){
        students[i]->id = id;
        students[i]->name = "name";
        student[i]->tests = (Test*)malloc(sizeof(Test)*numberofgradesofthatstudent);
        for (int j = 0; j < numberofgradesofthatstudent; j++)
        {
            students[i]->tests[j]->id = testid;
            students[i]->tests[j]->grade = grade;
        }
    }
}

this is schematic! new and malloc reserve memory on the heap, do not forget to free everything when you are done with it.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer. however this is the way my professor wants the assignments to be done, no vectors or classes.
The second version requires the objects to observe the Rule of Three or Five. More work is needed before this version is working properly. There aslo doesn't appear to be any need to use malloc. new should suffice.
@user4581301 yes, you are right. this is the c-way of doing this and its only schematic and is not going to work properly without some editing. it is meant as a little help not as a working solution.
-1

As said a little above, be careful using brackets {} to delimit your blocks.

Secondly,the syntax:

array[studIndex].Tests

supposes that the value array[studIndex] (here an integer) has a member value named Tests. But in this case it doesn't.

Think about your problem: you need to store two values "connected" to one another in a static array. The way I see it, you should try on with two dimensional arrays:

int 2dArray[nbStudents][nbTests];

If you don't want to bother with 2dimensional arrays, you can also try

int 2dArray[nbStudents * nbTests];

But for conveniance, it is often better to use 2d arrays.

Also, think about declaring your array before the for loops in your function. Then concatenate two for loops as you did and I'll let you think about the rest...

4 Comments

Three big problems with this answer. The compiler in use may not support C-Style Variable Length Arrays (and the C compiler might not either. They're optional these days). 2dArray will be an Automatic (a local) variable. It will go out of scope when returned. Third, the syntax for returning a 2D variable Length Array makes returning one impossible. A moot point in the face of problem 2.
I see the problem... So he would need to supply the array as an argument to the function... Well, my bad, didn't make much of a point here.
It's also impossible to pass a variable length array into a function. The function prototype needs to know the size of the inner dimensions.
I have edited my function in main post, could you guys look at it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.