Question

C++ Question 2: Create a dynamic jagged 2-d array of vehicle structs that holds an array...

C++ Question 2:

Create a dynamic jagged 2-d array of vehicle structs that holds an array of vehicles for different people. You will read the number of people and number of vehicles for each person from the user. Hint: Think about why you need a pointer for the num_vehicles.

#include <iostream>

using namespace std;

int main() {

int num_people, *num_vehicles;

struct vehicle **v;

cout << “How many people? ”;

cin >> num_people;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

using namespace std;

struct vehicle{

};

int main() {

int num_people, *num_vehicles;

num_vehicles = new int;

struct vehicle **v;

cout << "How many people? ";

cin >> num_people;

v = new vehicle*[num_people];

for(int i=0; i<num_people; i++){

cout<<"How many vehicles for people "<<(i+1)<<" ? ";

cin>>*num_vehicles;

v[i] = new vehicle[*num_vehicles];

}

//free memory

for(int i=0; i<num_people; i++){

delete[] v[i];

}

delete v;

}

Here I created 2d array with dynamically.

Let me know if you have any clarifications. Thank you...

Add a comment
Know the answer?
Add Answer to:
C++ Question 2: Create a dynamic jagged 2-d array of vehicle structs that holds an array...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std;...

    Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std; int main(){    int rows = 5; int cols = 5; int x;    int** arr = new int[rows][cols]    cin >> x; arr[x][x] = x; cout << "arr[x][x] = " << arr[x][x];    return 0; } Question 2: Fix the code to initialize the 2D array elements to x #include <iostream> using namespace std; int main(){    int rows; int cols; int x;...

  • Make the pointer point at the 5th element of an array 1 #include <iostream> 3 using namespace std; 4 //make the p...

    Make the pointer point at the 5th element of an array 1 #include <iostream> 3 using namespace std; 4 //make the pointer point at the 5th element of an array 6 int main) 7 int x[10]; 9 cin x[i]; 10 11 int*ptr; 12 13 14 cout <<*ptr << endl; 15 1 #include 3 using namespace std; 4 //make the pointer point at the 5th element of an array 6 int main) 7 int x[10]; 9 cin x[i]; 10 11 int*ptr;...

  • #include <iostream> using namespace std; int main(void) {    int SIZE;    cout<<"Enter the size of the...

    #include <iostream> using namespace std; int main(void) {    int SIZE;    cout<<"Enter the size of the array"<<endl;    cin>>SIZE;     int *numlist = new int[SIZE];     // Read SIZE integers from the keyboard     for (int i = 0; i<SIZE; i++ )    {        cout << "Enter value #" << i+1 << ": ";        cin >> numlist[i];     }     // Display the numbers in a reverse order     for (int i = SIZE; i > 0; i--...

  • /* Array expander: Write a function that accepts an int array as argument. The function should...

    /* Array expander: Write a function that accepts an int array as argument. The function should create a new array that is n times the size of the argument array, where n is a user input. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0. The function should return a pointer to the new array */ #include <iostream> using namespace std; const int NUM_ELEM...

  • How to initialize a dynamic two-dimensional array with values? (C++) Here's my problem... # include using...

    How to initialize a dynamic two-dimensional array with values? (C++) Here's my problem... # include using namespace std; int main() {    // Q#5 - dynamic 2d arrays, indexing via subscript operator, pointer arithmetic    // tic tac toe board is an array of int pointers    // each int pointer in the board points to a row    // declare a pointer to an array of int pointers (i.e. a pointer to a pointer of type int)    const...

  • I have to write a loop that moves the content of the dynamic array to the...

    I have to write a loop that moves the content of the dynamic array to the static array but I can't seem to figure it out. Exit Full Screen code.cpp New 1 #include <iostream> 3 using namespace std; 4 5 int main) 6 int *ptr 7 ptr -new int[5]; 8 int arr[5]; 9int x; 10 cin >> x; 11 for( int í = 0; 1 < 5; 1++) // initialization 12 13 ptr[i] x; = 14 for O//your code goes...

  • How would I add a line to the main function to sort the people vector by...

    How would I add a line to the main function to sort the people vector by Age using the STL sort function? #include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std; struct Person { string name; int age; string favoriteColor; }; bool sortByName(Person &lhs, Person &rhs) { return lhs.name < rhs.name; } bool sortByAge(Person &lhs, Person &rhs) { return lhs.age < rhs.age; } bool sortByColor(Person &lhs, Person &rhs) { return lhs.favoriteColor < rhs.favoriteColor; } int main() { vector<Person>...

  • For an ungraded C++ lab, we have practice with structs. I'm having a hard time calling...

    For an ungraded C++ lab, we have practice with structs. I'm having a hard time calling the functions relating to our book struct correctly. The issues lie in main(). Here is the .cpp: #include "book.h" void add_author(Book& book){         if (book.num_auth==3){             std::cout<<"\nA book can't have more than 3 authors!";         }         else{             std::cout<<"\nAdd Author: ";             getline(std::cin, book.authors[book.num_auth]);             book.num_auth++;         } }; void remove_last(Book& book){         if(book.num_auth<=1){             std::cout<<"\nA book must have at least 1 author!";...

  • Write a C++ program that demonstrates use of programmer-defined data structures (structs), an array of structs, passing...

    Write a C++ program that demonstrates use of programmer-defined data structures (structs), an array of structs, passing an array of structs to a function, and returning a struct as the value of a function. A function in the program should read several rows of data from a text file. (The data file should accompany this assignment.) Each row of the file contains a month name, a high temperature, and a low temperature. The main program should call another function which...

  • I'm just a beginner in programming,how to make this program more simple without using #include<iostream> and #include<redex> here is the question Terms to know: If-else statement,for.....

    I'm just a beginner in programming,how to make this program more simple without using #include<iostream> and #include<redex> here is the question Terms to know: If-else statement,for..while..do while,loops,pointer,address,continue,return,break. Create a C++ program which contain a function to ask user to enter user ID and password. The passwordmust contain at least 6 alphanumeric characters, 1 of the symbols !.@,#,$,%,^,&,* and 1 capital letter.The maximum allowable password is 20. Save the information. Test the program by entering the User ID and password. The...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT