Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m having trouble with a program in my intro to computer science lab. Here are

ID: 3768183 • Letter: I

Question

I'm having trouble with a program in my intro to computer science lab. Here are the details of what I need to get done:

Dynamic Arrays and Intro to Structs - Practice dynamic arrays

You will continue to practice dynamic array allocation and deallocation. I know we have not covered structs yet, but they are fairly simple to use! You can think of them as a container of information. In assignment #6, they hold the information for a specific course, such as the name, day, and time. We are going to work on the name and days of the struct in this lab, you will add and set the other members as part of your assignment.

First, let’s make sure we can create a struct and set the information for one class!

You declare the struct type and the members that are a part of that struct outside of main. You are not making the struct in memory at this point, you are just creating the type. You make this type global because all the functions will need to see this type, just as they all need to see the type int, string, etc.

};

Then you define a struct of that type, just as you do with a variable, but now it has multiple pieces of information, instead of just one! Notice that the struct type is course and the structure we are creating inside main is the type course. The name is cs161, which is the memory location of where this struct we created lives, and the type says that it takes up memory to hold a string and a string pointer!!!

return 0; }

We use the dot operator to access the members inside the struct. For example, let’s get the name from the user.

return 0; }

Now, you have to make a dynamic array of strings for days of the week. You will prompt the user for the number of days, create an array of strings to hold the days of the week, and then prompt them for name of these days. Here is an example of creating a dynamic array of strings inside the struct:

After you have created and filled the array, print the contents of the struct for the user and then delete the array.

After you have finished this, you can work on making an array of courses and setting the information for each of these courses, including the days of the week. You need to print the information for each course back to the user.

. . .

return 0;

}

Now, run your program through valgrind to make sure you do not have any memory leaks!!!

Explanation / Answer

#include<iostream>
#include<string>
using namespace std;

struct course {
   string name;
   string *days; //dynamic array of days
};

int main() {
   course *courses;
   int num_courses;
   cout << "Enter the number of courses: ";
   cin >> num_courses;
  
   courses = new course[num_courses];
  
   int* num_days = new int[num_courses];

   for(int i=0; i<num_courses; i++) {
       cout << "Enter name of the course no. " << i+1 << ": ";
       cin >> courses[i].name;
      
       cout << "Enter no of days for the " << courses[i].name << " course: ";
       cin >> num_days[i];

       courses[i].days = new string[num_days[i]];
      
       for(int j=0; j<num_days[i]; j++) {
           cout << "Enter content for day " << j+1 << " of the " << courses[i].name << " course: ";
           cin >> courses[i].days[j];
       }
       cout << endl;
   }
   cout << endl;
   for(int i=0; i<num_courses; i++) {
       cout << "Course Name: " << courses[i].name << endl;
       cout << "No of days: " << num_days[i] << endl;
       for(int j=0; j<num_days[i]; j++) {
           cout << "Content for day " << j+1 << ": " << courses[i].days[j] << endl;;
       }
       delete [] courses[i].days;
       cout << endl;
   }
   delete [] courses;
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote