[C++]
Outline:
The movie data is read from a file into an array of structs and is sorted using the Selection Sort method and the search is done using the Binary Search algorithm with the year of release as key. This assignment upgrades to an array of pointers to the database elements and converts the Selection Sort and Binary search procedure on the database to selection sort and binary search on the array of pointers to the database.
Requirements:
Your data structure will now be an array of pointers. Each element of the array will now point to a MovieData record. The function headers will have an array of pointers as parameter. Accessing the movie data record fields will be done using pointer access mechanisms (using indirection ->) as in the example above. Make all the array related access including function parameters to a pointer array.
You are given a file called MovieData.txt that contains the following information:
The following fields are separated by a comma. Each movie is on a separate line in the file.
Title: String containing the title of the movie
ReleaseYear: int in the format YYYY
Director: String
Genre : String : Comedy, Thriller etc.
Starring: String: Holds the name of two leading characters.
MovieData.txt:
The Sound of Silence,2000, John Calaveras, Romance, Julie
Sheppard, Robert De Niro
Black Beauty,1975, Neil Diamond, Animals, Tatum O Neill, Ryan O
Neill
Amazing Grace,1950, William Keyford, Historical, Ben Shapiro, Meryl
Screen
Star Wars,1972, Luke SkyWalker, Science Fiction, William Shatner,
Leonard Nimoy
The GodFather,1980, Frances Ford Coppola, Thriller, Al Pacino, Myra
Adams
The Shining,1990, Stephen King, Horror, Jack Nichols, Amanda
Green
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
=================================
// MovieDataFile.txt (input file)
The Sound of Silence,2000, John Calaveras, Romance, Julie
Sheppard, Robert De Niro
Black Beauty,1975, Neil Diamond, Animals, Tatum O Neill, Ryan O
Neill
Amazing Grace,1950, William Keyford, Historical, Ben Shapiro, Meryl
Screen
Star Wars,1972, Luke SkyWalker, Science Fiction, William Shatner,
Leonard Nimoy
The GodFather,1980, Frances Ford Coppola, Thriller, Al Pacino, Myra
Adams
The Shining,1990, Stephen King, Horror, Jack Nichols, Amanda
Green
======================================
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
struct Movie {
string title;
int releaseYear;
string director;
string genre;
string starring;
};
void menu();
void selectionSort(Movie* movies, int count);
void display(Movie* movies, int count);
int search(Movie* movies, int count, int year);
int main()
{
//Declaring variables
char choice;
string line;
int count = 0;
int index = -1;
int year;
string title, director, genre, starring;
ifstream dataIn;
dataIn.open("MovieDataFile.txt");
//checking whether the file name is valid or not
if (dataIn.fail()) {
cout << "** File Not Found **";
return 1;
}
else {
//Reading the data from the file
while (getline(dataIn, line)) {
count++;
}
dataIn.close();
Movie* movies = new Movie[count];
dataIn.open("MovieDataFile.txt");
for (int i = 0; i < count; i++) {
getline(dataIn, line);
index = line.find(",");
title = line.substr(0, index);
line = line.substr(index + 1, line.length() - 1);
index = line.find(", ");
year = atoi(line.substr(0, index).c_str());
line = line.substr(index + 1, line.length() - 1);
index = line.find(", ");
director = line.substr(0, index);
line = line.substr(index + 1, line.length() - 1);
index = line.find(", ");
genre = line.substr(0, index);
starring = line.substr(index + 1, line.length() - 1);
movies[i].title = title;
movies[i].releaseYear = year;
movies[i].director = director;
movies[i].genre = genre;
movies[i].starring = starring;
}
dataIn.close();
selectionSort(movies, count);
while (true) {
menu();
cin >> choice;
switch (choice) {
case 'D':
case 'd': {
display(movies, count);
continue;
}
case 'S':
case 's': {
cout << "Please enter the year of the movie in yyyy format
:";
cin >> year;
int index = search(movies, count, year);
if (index == -1) {
cout << "Movie Not found" << endl;
}
else {
cout << setw(25) << left << movies[index].title
<< setw(10) << left << movies[index].releaseYear
<< setw(25) << left << movies[index].director
<< setw(20) << left << movies[index].genre
<< setw(25) << left << movies[index].starring
<< endl;
}
continue;
}
case 'Q':
case 'q': {
cout << "GoodBye!" << endl;
break;
}
default: {
cout << "** Invalid Choice **" << endl;
continue;
}
}
break;
}
}
return 0;
}
void selectionSort(Movie* arr, int SIZE)
{
int minIndex;
Movie temp;
for (int i = 0; i < SIZE - 1; i++) {
minIndex = i;
for (int j = i + 1; j < SIZE; j++) {
if (arr[j].releaseYear < arr[minIndex].releaseYear)
minIndex = j;
}
// swap array
if (minIndex != i) {
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
}
void menu()
{
cout << "Movie Selection Menu" << endl
<< endl;
cout << "Display available Movies : D" <<
endl;
cout << "Search a Movie by Year : S" << endl;
cout << "Quit : Q" << endl;
cout << "Enter Your choice :";
}
void display(Movie* movies, int count)
{
for (int i = 0; i < count; i++) {
cout << setw(25) << left << movies[i].title
<< setw(10) << left << movies[i].releaseYear
<< setw(25) << left << movies[i].director
<< setw(20) << left << movies[i].genre <<
setw(25) << left << movies[i].starring <<
endl;
}
}
int search(Movie* array, int count, int year)
{
int first = 0, last = count - 1, middle, position = -1;
bool found = false;
while (!found && first <= last) {
middle = (first + last) / 2;
if (array[middle].releaseYear == year) {
found = true;
position = middle;
}
else if (array[middle].releaseYear > year)
last = middle - 1;
else
first = middle + 1;
}
return position;
}
==========================================
Output:
=====================Could you plz rate me
well.Thank You
[C++] Outline: The movie data is read from a file into an array of structs and...
Using an object-oriented approach, write a program to read a file containing movie data (Tile, Director, Genre, Year Released, Running Time) into a vector of structures and perform the following operations: Search for a specific title Search for movies by a specific director Search for movies by a specific genre Search for movies released in a certain time period Search for movies within a range for running time Display the movies on the screen in a nice, easy to read...
This lab will combine reading data from a file and searching the array to find a specific value. Assignment Write a program that reads in a file full of strings into an array, and prompts the user for a string to find in the array. The program should loop until a sentinel value (such as -1) is entered. After looping in main() for the input, write a search function, with the following prototype: int findWord(string [], int, string); with arguments...
For your second program, please read the data from the input file directly into an array. (You may safely dimension your array to size 300.) Then close the input file. All subsequent processing will be done on the array. Your program should have two functions besides main(). The first function will print out the contents of the array in forward order, 10 numbers per line, each number right justified in a 5 byte field. The second function will print out...
You are going to implement Treesort algorithm in C++ to sort string data. Here are the steps to complete the homework 1) Use the following class definition for binary search tree nodes. Its constructor is incomplete you should first complete the constructor. class TreeNode t public: string data; / this is the string stored in the node TreeNode left: TreeNode right; TreeNode (string element, TreeNode 1t, TreeNode rt //your code here 2) Write a function that will insert a string...
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...
Files given in this assignment (right-click on the file to download) Assignment7.cpp (need to complete) Student.h (Given. Just use it, don't change it!) Student.cpp (Partially filled, need to complete) 1. Assignment description In this assignment, you will write a simple class roster management system for ASU CSE100. Step #1: First, you will need to finish the design of class Student. See the following UML diagram for Student class, the relevant header file (class declaration) is given to you as Student.h,...
c++ program8. Array/File Functions Write a function named arrayToFile. The function should accept three arguments: the name of a file, a pointer to an int array, and the size of the array. The function should open the specified file in binary mode, write the contents of the array to the file, and then close the file. Write another function named fileToArray. This function should accept three argu- ments: the name of a file, a pointer to an int array, and the size...
The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...
C programming The program will require the following structure: struct _data { char *name; long number; }; The program will require command line arguments: int main(int argv, char **argc) { Where argv is the number of arguments and argc is an array holding the arguments (each is a string). Your program must catch any case where no command line arguement was provided and print a warning message (see below). You MUST include/use the following functions, defined as follows: int SCAN(FILE...
C++ Data Structure Write a program to read a list of students from a file and create a list. The program should use a linked list for implementation. Each node in the linked list should have the student’s name, a pointer to the next student, and a pointer to a linked list of scores. There may be up to four scores for each student.