Question

In: Computer Science

Having trouble with this assignment: Rewrite your most recent high scores program (shown below) so that...

Having trouble with this assignment:

Rewrite your most recent high scores program (shown below) so that each name/score pair is stored in a struct named highscore. Except as noted below, this new program will continue to meet all of the requirements of your most recent high scores program. Your new program should meet the following requirements:

The highscore struct should have two fields:

an int named score

and a char array named name. The char array should have 24 elements, making the maximum length of the name 23. (If you prefer to use a char pointer and a dynamically allocated array, that is fine as well. However, this may result in a number of complications, so be prepared for the challenge.)

The data should be stored in a single array, a dynamically allocated array of highscore structs.

Your program should use three functions that accept the array of highscore structs:

void initializeData(highscore scores[], int size)
void sortData(highscore scores[], int size)
void displayData(const highscore scores[], int size)

You may use any sort algorithm, but I would recommend using the selection sort from lesson 9.6. Don't use C++'s sort() function, but you can use the swap() function.

Note that when you swap your array elements, you can swap the entire struct. You don't need to swap the name and the score separately.

You may assume that the user enters names that are 23 characters or less. Getting this to work correctly if the user enters names that are too long -- that is, making it so that you put the first 23 characters in the name variable and ignore the remaining characters on the line -- is complicated. You can do this as an extra challenge if you want, but it's not required.

My high score program:

#include<iostream>

using namespace std;

int main()

{

int num;

int i;

int j;

int tmp;

  

string t;

cout << "How many scores will you enter?: ";

cin >> num;

  

int *a = new int [num];

string *b = new string [num];

for(i=0;i<num;i++){

cout << "Enter the name for score #" << i+1 << ": ";

cin >> b[i];

cout << "Enter the score for score #" << i+1 << ": ";

cin >> a[i];

}

cout << "\nTop Scorers:" << endl;

for(i=0;i<num;i++){

for(j=i+1;j<num;j++){

if(a[i]<a[j]){

tmp=a[i];

t=b[i];

a[i]=a[j];

a[j]=tmp;

b[i]=b[j];

b[j]=t;

}

}

}

for(i=0;i<num;i++){

cout<<b[i]<<": "<<a[i]<<endl;

}

}

Solutions

Expert Solution

#include<iostream>
using namespace std;
struct highscore//struct declaration with name highscore
{
   int score;//to store score
   char name[23];//to store name
   struct highscore *next;//to link next struct
};
struct highscore* initializeData(struct highscore *h, int size)
{
   int i,n=size;
   for(i=0;i<n;i++)
   {
       struct highscore *t = new highscore();
       cout << "Enter the name for score #" << i+1 << ": ";
       cin >> t->name;
       cout << "Enter the score for score #" << i+1 << ": ";
       cin >> t->score;
      
       t->next = h;
       h=t;
   }
   return h;
}
struct highscore* sortData(struct highscore *h,int size)//sorting using selection sort
{
   struct highscore *temp = h,*r=h,*min=h,*mprev,*prev=h,*l=h;
   int i=0;
   while(i<size)
   {  
       temp = r;
       min =r;
       mprev =r;
       prev =r;
   //   cout<<"Hello 1\n";
       while(temp!=NULL)
       {
           if(min->score<temp->score)
           {
               mprev = prev;
               min = temp;  
           }
           prev = temp;
       //   cout<<temp->score<<"   Hello 3\n";
           temp = temp->next;
              
       }
   //   cout<<"Hello 2\n";
       if(mprev == min)
       {
          
           r=r->next;  
       }
       else
       {
           mprev->next = min->next;
           min->next=NULL;
          
           min->next = r;
           h=min->next;
          
       }
       i++;      
   //   cout<<"Hello 2\n";
   }
  

   return h;
  
}
void displayData(struct highscore *h,int size)
{
   struct highscore *temp = h;
   while(temp!=NULL)
   {
       cout<<"name: "<<temp->name<<"\n"<<"score: "<<temp->score<<"\n\n";
       temp=temp->next;
   }
}
int main()
{
   int n,i;
   struct highscore *h=NULL;//initially empty
   cout << "How many scores will you enter?: ";
   cin >> n;
   h=initializeData(h,n);
   h=sortData(h,n);
   displayData(h,n);
  
   return 0;
}

ouput:-


Related Solutions

Rewrite your most recent high scores program in C++ so that each name/score pair is stored...
Rewrite your most recent high scores program in C++ so that each name/score pair is stored in a struct named Highscore. Except as noted below, this new program will continue to meet all of the requirements of your most recent high scores program. Your new program should meet the following additional requirements: The Highscore struct should have two fields: an int named score and a char array named name. The char array should have 24 elements, making the maximum length...
The League with DMA Rewrite your League program from Assignment 8 so that it uses Dynamic...
The League with DMA Rewrite your League program from Assignment 8 so that it uses Dynamic Memory Allocation (DMA) to create the team names and scores arrays. This is a good test of the modularity of your program. You will only need to make slight modifications to your main() function if you wrote your original program using functions similar to the following: void initializeData(string names[], int wins[], int size) void sort(string names[], int wins[], int size) void display(string names[], int...
​​​​​​This is an assignment that I'm having trouble figuring out in python: Modify Node class so...
​​​​​​This is an assignment that I'm having trouble figuring out in python: Modify Node class so it has a field called frequency and initialize it as one. Add a search function. If a number is in the list, return its frequency. Modify Insert function. Remove push, append, and insertAfter as one function called insert(self, data). If you insert a data that is already in the list, simply increase this node’s frequency. If the number is not in the list, add...
Hey! I'm having trouble answering this for my assignment. Thank you so much in advance. 1)...
Hey! I'm having trouble answering this for my assignment. Thank you so much in advance. 1) Which type of vessels, arteries or veins, has more muscle fibers? What is the functional significance of this? 2a) In general, we have no conscious control over smooth muscle or cardiac muscle function, whereas we can consciously control to some extent all skeletal muscles. Can you consciously control your breathing? What does this tell you about the muscle type of the diaphragm? 2b) What...
I am having a trouble with a python program. I am to create a program that...
I am having a trouble with a python program. I am to create a program that calculates the estimated hours and mintutes. Here is my code. #!/usr/bin/env python3 #Arrival Date/Time Estimator # # from datetime import datetime import locale mph = 0 miles = 0 def get_departure_time():     while True:         date_str = input("Estimated time of departure (HH:MM AM/PM): ")         try:             depart_time = datetime.strptime(date_str, "%H:%M %p")         except ValueError:             print("Invalid date format. Try again.")             continue        ...
I am having the most trouble with 1d: 1. a. Prove that if f : A...
I am having the most trouble with 1d: 1. a. Prove that if f : A → B, g : B → C, and g ◦f : A → C is a 1-to-1 surjection, then f is 1-to-1 and g is a surjection. Proof. b. Prove that if f : A → B, g : B → C, g ◦ f : A → C is a 1-to-1 surjection, and g is 1-to-1, then f is a surjection. Proof. c....
Rewrite Program to store the pairs of states and capitals so that the questions are displayed...
Rewrite Program to store the pairs of states and capitals so that the questions are displayed randomly. import java.util.*; public class quiz { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String arr[][]= {{"Alabama","Montgomery"},{"Alaska","Juneau"},{"Arizona","Phoenix"}}; int n =arr.length; int count=0; for(int i=0;i<n;i++) { System.out.printf("What is the capital of %s? ",arr[i][0]); String capital=sc.next(); if (arr[i][1].equalsIgnoreCase(capital)) { count++; System.out.println("Your answer is correct"); } else { System.out.printf("The correct answer should be %s\n",arr[i][1]); } } System.out.printf("The correct count is %d",count); } }
The clause below was written by the seller. You are the buyer. Rewrite it so it...
The clause below was written by the seller. You are the buyer. Rewrite it so it is to your advantage. Delivery; Title; and Risk of Loss. Unless otherwise stated in Exhibit A, the Seller shall deliver the Goods FOB the Seller’s facility and title to and risk of loss of the Goods will pass to the Buyer upon such delivery by the Seller. Any stated delivery dates are approximate. The Seller will not be liable for any losses, damages, penalties,...
The most recent financial statements for Shinoda ManufacturingCo. are shown below:  Income StatementBalance...
The most recent financial statements for Shinoda Manufacturing Co. are shown below:  Income StatementBalance SheetSales$64,300Current assets$28,500Debt$44,700Costs44,630Fixed assets81,400Equity65,200Taxable income$19,670Total$109,900Total$109,900Tax (40%)7,868Net Income$11,802  Assets and costs are proportional to sales. Debt and equity are not. The company maintains a constant 44 percent dividend payout ratio. No external equity financing is possible.What is the sustainable growth rate? (Do not round intermediate calculations and enter your answer as a percent rounded to 2 decimal places, e.g., 32.16.)Sustainable growth rate              %
Data on Trenton Travel Inc. for the most recent year are shown below, along with the...
Data on Trenton Travel Inc. for the most recent year are shown below, along with the days sales outstanding of the firms against which it benchmarks. The firm's new CFO believes that the company could reduce its receivables enough to reduce its DSO to the benchmarks' average. If this were done, by how much would receivables decline? Use a 365-day year. Sales $110,000 Days sales outstanding (DSO) 49.78 Benchmark days sales outstanding (DSO) 20
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT