fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. // 6) Vehicle struct specification
  6. struct Vehicle
  7. {
  8. int id;
  9. int year;
  10. string model;
  11. };
  12.  
  13. // 9) Function prototype above main()
  14. void displayCar(Vehicle car);
  15.  
  16. int main()
  17. {
  18. // Given in the lab
  19. int values[5] = {2, 4, 7, 9, 11};
  20. int number = 5;
  21.  
  22. // 1) Declare double pointer named doublePtr, initialize with NULL
  23. double* doublePtr = NULL;
  24.  
  25. // 2) Declare int pointer named ptr, initialize with address of number
  26. int* ptr = &number;
  27.  
  28. // 3) Assign address of values array to doublePtr
  29. // NOTE: values is an int array, but lab asks for double*. This cast matches the worksheet intent.
  30. doublePtr = (double*)values;
  31.  
  32. // 4) cout the value in number WITHOUT using number (dereference ptr)
  33. cout << "Q4 (value of number using *ptr): " << *ptr << endl;
  34.  
  35. // 5) Assign 44 to number WITHOUT using number (dereference ptr)
  36. *ptr = 44;
  37. cout << "Q5 (number after *ptr = 44): " << number << endl;
  38.  
  39. // 7) Declare Vehicle object named myCar (no initialization)
  40. Vehicle myCar;
  41.  
  42. // 8) Declare Vehicle object named yourCar and initialize with given values
  43. Vehicle yourCar = {12345, 2005, "Ford"};
  44.  
  45. // (Optional) call function so you can test that it works
  46. displayCar(yourCar);
  47.  
  48. return 0;
  49. }
  50.  
  51. // Function definition (so the program runs)
  52. void displayCar(Vehicle car)
  53. {
  54. cout << "\nVehicle Info\n";
  55. cout << "ID: " << car.id << endl;
  56. cout << "Year: " << car.year << endl;
  57. cout << "Model: " << car.model << endl;
  58. }
  59.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Q4 (value of number using *ptr): 5
Q5 (number after *ptr = 44): 44

Vehicle Info
ID: 12345
Year: 2005
Model: Ford