fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. //#include "Movie.h"
  5. #include <iomanip>
  6. using namespace std;
  7. #ifndef _Movie_
  8. #define _Movie_
  9. #include <iostream>
  10.  
  11.  
  12. struct Movie
  13. {
  14. std::string title;
  15. int criticRating; // from 0 to 100
  16. int audienceRating; // from 0 to 100
  17. };
  18.  
  19. #endif /*defined _Movie_*/
  20.  
  21. void PrintMovies(Movie*, int);
  22. void SortMoviesByAudienceRating(Movie*, int);
  23. void InsertMovie(Movie*, int, const Movie&);
  24.  
  25. int main()
  26. {
  27. //auto automatically assumes what type
  28. //constexpr = static const int
  29. constexpr auto NumMovies = 10;
  30. Movie* movie = new Movie[NumMovies];
  31. movie[0] = { "Kung Fu Panda 3", 81, 86};
  32. movie[1] = { "Hail, Caesar", 81, 46};
  33. movie[2] = { "Star Wars VII - The Force Awakens", 92, 90};
  34. movie[3] = { "The Revenant", 82, 85};
  35. movie[4] = { "The Choice", 7, 67};
  36. movie[5] = { "Pride and Prejudice and Zombies", 45, 59};
  37. movie[6] = { "The Finest Hours", 59, 72};
  38. movie[7] = { "Ride Along 2", 13, 55};
  39. movie[8] = { "The Boy", 31, 45};
  40. movie[9] = { "Dirty Grandpa", 9, 51};
  41.  
  42. PrintMovies(movie, NumMovies);
  43. SortMoviesByAudienceRating(movie, NumMovies);
  44. cout<<"---"<<endl;
  45. PrintMovies(movie, NumMovies);
  46. cout<<"---"<<endl;
  47.  
  48. Movie newMovie = { "The Jungle Book", 84, 80 };
  49.  
  50. InsertMovie(movie, NumMovies, newMovie);
  51. PrintMovies(movie, NumMovies);
  52.  
  53. return 0;
  54. }
  55.  
  56. void PrintMovies(Movie*m, int n) {
  57. for (int i=0; i<n; i++)
  58. cout<<m[i].criticRating << "\t" <<m[i].audienceRating<<"\t"<< m[i].title<<endl;
  59. }
  60. void SortMoviesByAudienceRating(Movie*m, int n) {
  61. sort(m, m+n, [](Movie&a,Movie&b){return a.audienceRating>b.audienceRating;});
  62. }
  63.  
  64.  
  65. void InsertMovie(Movie* movies, int numMovies, const Movie& movie)
  66. {
  67. Movie temp1, temp2;
  68.  
  69. for (int i = 0; i < numMovies; ++i)
  70. {
  71. if (movie.audienceRating >= movies[i].audienceRating)
  72. {
  73. for (int j = numMovies - 1; j > i; --j)
  74. {
  75. movies[j] = movies[j - 1];
  76. }
  77.  
  78. temp1 = movies[i];
  79. movies[i] = movie;
  80. break;
  81. }
  82. }
  83. }
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
81	86	Kung Fu Panda 3
81	46	Hail, Caesar
92	90	Star Wars VII - The Force Awakens
82	85	The Revenant
7	67	The Choice
45	59	Pride and Prejudice and Zombies
59	72	The Finest Hours
13	55	Ride Along 2
31	45	The Boy
9	51	Dirty Grandpa
---
92	90	Star Wars VII - The Force Awakens
81	86	Kung Fu Panda 3
82	85	The Revenant
59	72	The Finest Hours
7	67	The Choice
45	59	Pride and Prejudice and Zombies
13	55	Ride Along 2
9	51	Dirty Grandpa
81	46	Hail, Caesar
31	45	The Boy
---
92	90	Star Wars VII - The Force Awakens
81	86	Kung Fu Panda 3
82	85	The Revenant
84	80	The Jungle Book
59	72	The Finest Hours
7	67	The Choice
45	59	Pride and Prejudice and Zombies
13	55	Ride Along 2
9	51	Dirty Grandpa
81	46	Hail, Caesar