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

It\'s shown in the picture This assignment applies the use of an array of struct

ID: 3555962 • Letter: I

Question

It's shown in the picture

This assignment applies the use of an array of structs and sorting such an array based on one of the members in the struct. The program pr s a sorted table of calculated odds of profiting from playing several scratch-off lottery games. You will read in data from a file to populate structs and put them o an array. Some of the fields will have to be calculated, and one of the fields you will have to use to sort the structures in the arrays. First of all, your program should ask the user to enter the lowest amount that they would like to profit when playing a scratch-off lottery. Read this value in as an integer. Once this value is known, you can compute the odds of profiting (by at least that amount) from each game. Your program should read in data from an input file for the scratcher games, compute the odds, and pr the games and odds sorted by the best odds first in a nicely formatted table (see the example). Your program must read information about the scratcher games from a text file whose name the program prompts for (like your previous assignments). The file will consist of several multi-line "blocks", where each block represents a scratcher game. Each block will be formatted as follows: The first line will contain only the name of the game. The name will contain any kind of character including spaces, letters, digits, punctuation, etc. (Please use getline()!) The second line contains two integers followed by a double. The first integer is the cost of the ticket in dollars; the second integer is the number of possible prizes that can be won for that scratcher. (The number of prizes has been given so that you can easily use a for-loop to read the subsequent lines.) Finally, the 3rd and last number (a double) is the odds of winning any prize in the game when it was first printed. To keep the math simple, we'll assume that these odds remain the same even as tickets are bought and claimed. (I strongly suggest you use >> to read this line and the subsequent lines for each game.) After the second line, there is one line for each prize on the scratcher (in other words, if you call the second integer on the line above N, then there are N lines). Each prize line contains three integers. The first is the value of the prize in dollars; the second is the total number of tickets that were printed with that prize; the third is the number of tickets not yet claimed for that prize. After the last prize line, there will be a blank line, and then a new block will start (or you've reached the end of the file). Because the output must be sorted, you must read in all of the data and store it o an array of structures. Here is an example input file with only three games, although an input file may contain many more. You can use this example during your development by adding this as a new item under the Source folder. Grading on Web-CAT will use different files, and you should make additional entries in your data file to test your code sufficiently before you start submitting to Web-CAT. In the example below, the first game is called "Small Beans", a $1 game with 5 possible prizes: $10K, $5K, $100, $10, and $1. For the $10K prize, 10 tickets were printed, and 10 have not yet been claimed. For the $5,000 prize, 50 tickets were printed and 8 are left. The other two games are designed similarly. Your program should start by prompting the user to enter a dollar amount that is the smallest amount that they want to profit (meaning that the prize must be larger than the desired profit plus the cost of the ticket; more on this later). Once you know the desired profit, your program needs to store all of the information in an array of structures, because you need to sort it before printing it out. I will come back to that functionality later. You must define your own structure (struct or class) to hold data about each game that you read from the input file. I leave the exact choice of fields in that structure up to you, but you must have each structure contain enough information to calculate the odds, sort them in the proper order, and then pr the table at the end of the program. You may store the scratcher game structs in a simple array (or an ArrayList, or Vector if you understand those). You must sort the scratcher games before pr ing them out. For each game, you should pr a neatly formatted line of output that includes the following information: The name of the game, exactly as it was read from the input file. The cost of a ticket for that game, preceded with a dollar sign (you may have spaces between the dollar sign and the amount, for alignment purposes). The odds of winning your desired profit in that game, written as "1 in N", where N will be computed as described below. The denominator should be displayed to two decimal places. If it is not possible to profit from that game, pr "Not possible" instead of the odds. To compute the odds, you need to look at the information for each of the prizes on that scratcher. First, you need to compute a running sum of total number of remaining tickets for that game, regardless of prize value. Then, you also need to compute a running sum of the total number of remaining tickets that would result in a profit; in other words, the number of remaining tickets where the prize value is greater than the desired profit plus the cost of the ticket. (We're notinterested in breaking even, so make sure you check strictly greater-than.) Then, we want the odds to be "1 in something", so we compute the "something" by dividing the total number of remaining tickets by the number of remaining profitable tickets. (Notice that this is the opposite of what we would do if we were computing the probability as a percentage. Also note that we must take special care that we handle the "Not possible" case correctly, since that would involve dividing by zero.) This "something" is also what you sort on. Sorting based on a member/field in a struct will be talked about in class and there is an example in the class notes. For the example input file above, imagine that the user said that they wanted to see what their odds wrere of profiting $1,000 on any of those games. Your output, sorted by odds of winning, should look like this:

Explanation / Answer

Dropbox link:          https://dl.dropboxusercontent.com/u/41585969/chegg/game.cpp

Code:

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iomanip>

using namespace std;

struct game{
    string name;
    int cost;
    double odds;
};

bool mycomp(game* i, game* j){
      if(i->odds == 0){
          return (i->odds > j->odds);
      }
      else if(j->odds == 0){
          return (i->odds > j->odds);
      }
      return (i->odds < j->odds);
}

void print_string(string s){
    for(int i=0; i<s.length()-1; i++){
        cout << s[i];
    }
}

int main()
{
   int lowest_amt;
   cout << "Enter the lowest amount of profit:" << endl;
   cin >> lowest_amt;

   string filename;
   cout << "Enter the file name:" << endl;
   cin >> filename;

   string name,blank;
   int cost, N, value, total, claimed;
   double odds;
   ifstream file;
   file.open(filename.c_str());

   vector<game*> v;
   while(!file.eof())
   {
        getline(file, name);
        name.erase(name.length()-1);
      
        game* g = new game();
        g->name = name;
        double num = 0, den = 0;
        file >> cost >> N >> odds;
        g->cost = cost;
        for(int i=0; i<N; i++){
            file >> value >> total >> claimed;
            num += claimed;
            if(value > lowest_amt + cost){
                den += claimed;
            }
            if(den == 0){
                g->odds = 0;
            }
            else{
                g->odds = (num/den)*odds;
            }
        }
        v.push_back(g);
        getline(file, blank);
        getline(file, blank);
   }

   sort(v.begin(), v.end(), mycomp);
   cout << "Game " << "Cost " << "Odds" << endl;
   cout << "--------------------------------------------------------" << endl;
   for(vector<game*>::iterator i=v.begin(); i!= v.end(); i++){
        if((*i)->odds == 0){
            cout << setprecision(2) << (*i)->name << " $" << (*i)->cost << " Not Possible " << endl;
        }
        else cout << fixed << setprecision(2) << (*i)->name << " $" << (*i)->cost << " 1 in " << (*i)->odds << endl;
   }

   return 0;
}

----------------------------------------------------------------------------------------------------------------------------------------------

Input file:

Small Beans
1 5 4.18
10000 10 10
5000 50 8
100 100 6
10 1000 4
1 10000 2

Pirate's Booty, Arrrgh
10 6 3.99
50000 20 0
10000 100 0
1000 500 0
100 2000 738
10 7500 2945
1 10000 4476

Big Money Hu$tla$
20 7 2.16
1000000 10 7
500000 50 29
10000 100 78
1000 500 396
100 2000 1439
20 5000 3218
10 10000 6210

--------------------------------------------------------------------------------------------------------------------------------------------------
Output:

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