Question

In: Computer Science

Program Specification: (Visual Studio C++) 1. Read data for names and weights for 15 people from...

Program Specification: (Visual Studio C++)
1. Read data for names and weights for 15 people from the console where there is a name on a line followed by a weight on the next line.
2. Your program will build a list for the data maintained in ascending order based on both name and weight via a doubly linked list.
3. This dll will use one pointer to keep weights in sorted order, and use the other link to keep names on sorted order.
4. You need to build the list as you go maintaining this ordering, so at any time a print method was called it would print the related field in order. (This means nodes are added to the list in sorted order, elements are not added to the list followed by a sort called on the list.)

For example after 3 elements are added for (Name – Weight):
Michael – 275, Tom – 150, Abe – 200.

Output:
Names & weights sorted(ascending) by name. : Abe – 200, Michael – 275, Tom - 150
Names & weights sorted(ascending) by weight. : Tom – 150, Abe – 200, Michael - 275

Jim
150
Tom
212
Michael
174
Abe
199
Richard
200
April
117
Claire
124
Bobby
109
Bob
156
Kevin
145
Jason
182
Brian
150
Chris
175
Steven
164
Annabelle
99

Solutions

Expert Solution

#include<iostream>
using namespace std;

struct data{
   string name;       // name to be stored
   double weight;       // weight to be stored
  
   struct data *nw;   // pointer to next node with greater weight
   struct data *nn;   // pointer to next node with next name in ascending order
};


void print(struct data *t1,struct data *t2)
{
   //struct data *t1 = head_nm;
   //struct data *t2 = head_wt;
          
   cout<<"Names & weights sorted(ascending) by name. :";
   while(t1!=NULL)
   {
       cout<<t1->name<<" - "<<t1->weight<<",";
       t1=t1->nn;
   }
   cout<<endl;
          
   cout<<"Names & weights sorted(ascending) by weight. :";
   while(t2!=NULL)
   {
       cout<<t2->name<<" - "<<t2->weight<<",";
       t2=t2->nw;
   }
   cout<<endl;
}


int main()
{
   int flag = 1;
   struct data *head_wt = NULL;   // head node for weights
   struct data *head_nm = NULL;    // head node for names
  
   string inp_n;
   double inp_wt;
  
   while(flag!=3)
   {
       cout<<"Please enter the choices from below : \n";
       cout<<"Type 1 : for entering data\n";
       cout<<"Type 2 : for print\n";
       cout<<"Type 3 : for exit\n";
      
       cin>>flag;
      
       if(flag == 2 || flag == 3)
       {
           print(head_nm,head_wt);
           continue;
       }
      
       cin>>inp_n;
       cin>>inp_wt;
      
       struct data *new_node = new data;
       new_node->name = inp_n;
       new_node->weight = inp_wt;
       new_node->nn = NULL;
       new_node->nw = NULL;
      
       struct data *temp = head_nm;
      
       //for entering names
       if(head_nm == NULL)
       {
           head_nm = new_node;
       }
       else if( head_nm->name > new_node->name)
       {
           new_node->nn = head_nm;
          
           head_nm = new_node;
       }
       else
       {
           int flag = 0;
           while(temp->nn!=NULL)
           {
               if(temp->nn != NULL && temp->nn->name > new_node->name)
               {
                   struct data *temp2 = temp->nn;
                   new_node->nn = temp2;
                   temp->nn = new_node;
                   flag = 1;
                   break;
               }
               temp = temp->nn;
           }
           if(flag == 0)
           {
               temp->nn = new_node;
           }
       }
      
       // for weight addition
       temp = head_wt;
      
       if(head_wt == NULL)
       {
           head_wt = new_node;
       }
       else if( head_wt->weight > new_node->weight)
       {
           new_node->nw = head_wt;
          
           head_wt = new_node;
       }
       else
       {
           int flag = 0;
           while(temp->nw!=NULL)
           {
               if(temp->nw != NULL && temp->nw->weight > new_node->weight)
               {
                   struct data *temp2 = temp->nw;
                   new_node->nw = temp2;
                   temp->nw = new_node;
                   flag = 1;
                   break;
               }
               temp = temp->nw;
           }
           if(flag == 0)
           {
               temp->nw = new_node;
           }
       }
   }
   return 0;
}

Output :

Names & weights sorted(ascending) by name. : Abe – 200, Michael – 275, Tom - 150,
Names & weights sorted(ascending) by weight. : Tom – 150, Abe – 200, Michael - 275,


Related Solutions

C++ PROGRAM Using the attached C++ code (Visual Studio project), 1) implement a CoffeeMakerFactory class that...
C++ PROGRAM Using the attached C++ code (Visual Studio project), 1) implement a CoffeeMakerFactory class that prompts the user to select a type of coffee she likes and 2) returns the object of what she selected to the console. #include "stdafx.h" #include <iostream> using namespace std; // Product from which the concrete products will inherit from class Coffee { protected:    char _type[15]; public:    Coffee()    {    }    char *getType()    {        return _type;   ...
In visual Studio C++ Create a program that uses a for loop to input the high...
In visual Studio C++ Create a program that uses a for loop to input the high temperature, and low temperature for each day of the week. The high and low will be placed into two elements of the array. For each loop the high and low will be placed into the next set of elements of the array. After the temps for all seven days have been entered into the array, a for loop will be used to pull out...
String Manipulator Write a program to manipulate strings. (Visual Studio C++) In this program take a...
String Manipulator Write a program to manipulate strings. (Visual Studio C++) In this program take a whole paragraph with punctuations (up to 500 letters) either input from user, initialize or read from file and provide following functionalities within a class: a)   Declare class Paragraph_Analysis b)   Member Function: SearchWord (to search for a particular word) c)   Member Function: SearchLetter (to search for a particular letter) d)   Member Function: WordCount (to count total words) e)   Member Function: LetterCount (ONLY to count all...
C++ Visual Studio 2019 Part A : Program Description: Write a program to generate a report...
C++ Visual Studio 2019 Part A : Program Description: Write a program to generate a report based on input received from a text file. Suppose the input text file student_grades.txt contains the student’s Last name , First name, SSN, Test1, Test2, Test3 and Test4. (25%) i.e. Alfalfa   Aloysius   123-45-6789 40.0    90.0   100.0    83.0 Generate the output Report File student_final.txt in the following format : LastName FirstName   SSN Test1   Test2   Test3   Test4 Average FinalGrade i.e. Alfalfa   Aloysius   123-45-6789 40.0    90.0   100.0   ...
answer the following using C# Design and program a Visual Studio Console project in C# that...
answer the following using C# Design and program a Visual Studio Console project in C# that allows your user to enter a number. The program will examine the number to see if it is prime. If it is prime, it will print the next higher prime and the next lower primes to the console. If the number entered by the user is not prime, display a message to that effect. All code should be written by you. Do not copy/paste...
ONLY USE VISUAL STUDIO (NO JAVA CODING) VISUAL STUDIO -> C# -> CONSOLE APPLICATION In this...
ONLY USE VISUAL STUDIO (NO JAVA CODING) VISUAL STUDIO -> C# -> CONSOLE APPLICATION In this part of the assignment, you are required to create a C# Console Application project. The project name should be A3<FirstName><LastName>P2. For example, a student with first name John and Last name Smith would name the project A1JohnSmithP2. Write a C# (console) program to calculate the number of shipping labels that can be printed on a sheet of paper. This program will use a menu...
USING VISUAL STUDIO 2017, LANGUAGE VISUAL C# I have struggled on this program for quite some...
USING VISUAL STUDIO 2017, LANGUAGE VISUAL C# I have struggled on this program for quite some time and still can't quite figure it out. I'm creating an app that has 2 textboxes, 1 for inputting customer name, and the second for entering the number of tickets the customer wants to purchase. There are 3 listboxes, the first with the days of the week, the second with 4 different theaters, and the third listbox is to display the customer name, number...
 VISUAL STUDIO (File Creation and Submissions)  FLOWCHART  Writing program in C++ ( cout,...
 VISUAL STUDIO (File Creation and Submissions)  FLOWCHART  Writing program in C++ ( cout, \n, \t, solving expressions )  Correcting errors Q1: Draw flow Chart of the following problems: a) Display “Hello World” on screen. b) Display Your Name, date of birth and mobile number on screen. c) Compute and display the perimeter and area of a rectangle with a height of 7 inches and width of 5 inches. d) Compute and display the perimeter and area...
create a C++ program using Visual Studio that could be used by a college to track...
create a C++ program using Visual Studio that could be used by a college to track its courses. In this program, create a CollegeCourse class includes fields representing department, course number, credit hours, and tuition. Create a child (sub class) class named LabCourse, that inherits all fields from the the CollegeCourse class, includes one more field that holds a lab fee charged in addition to the tuition. Create appropriate functions for these classes, and write a main() function that instantiates...
write a c++ program using micro soft visual studio 2010 to write a program and store...
write a c++ program using micro soft visual studio 2010 to write a program and store 36 in variable x and 8 in variable y. add them and store the result in the variable sum. then display the sum on screen with descriptive text. calculate the square root of integer 36 in x. store the result in a variable. calculate the cube root of integer 8 in y. store result in a variable. display the results of square root and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT