Question

In: Computer Science

C++ You are to input the following numbers from a file: 75 80 90 95 100...

C++

You are to input the following numbers from a file:

75

80

90

95

100

100

70

30

20

70

Place them into a linked list (Like the one I uploaded on this homework). The file I uploaded shows how to sort the numbers using bubble sort. You are to sort them based on selection sort!

Only sort the values, do not modify the pointers. Use the file I uploaded as a good guide!

#include <iostream>
#include <fstream>
using namespace std;

struct ListNode
{
        double value;
        ListNode *next;

        ListNode(double value1, ListNode *next1 = NULL)
        {
                value = value1;
                next = next1;
        }
};

int main()
{
        ifstream inFile;
        inFile.open("Grades.txt");
        ListNode *head= NULL;
        double value;
        for (int x=0; x<10; x++)
        {
                inFile>>value;
                head = new ListNode(value,head);
        }
        ListNode *ptr1;
        double temp;
        for (int x=0; x<10; x++)
        {
                ptr1=head;
                for (int y=0; y<10-1-x; y++)
                {
                        if (ptr1->value > ptr1->next->value)
                        {
                                temp = ptr1->value;
                                ptr1->value = ptr1->next->value;
                                ptr1->next->value = temp;
                        }
                        ptr1=ptr1->next;
                }
        }


        ListNode *ptr = head;
        for (int x=0; x<10; x++)
        {
                cout<<ptr->value<<endl;
                ptr=ptr->next;
        }

        return 0;
}

Solutions

Expert Solution

#include <iostream>

#include <iostream>

#include <fstream>

using namespace std;

struct ListNode

{

double value;

ListNode *next;

ListNode(double value1, ListNode *next1 = NULL)

{

value = value1;

next = next1;

}

};

int main()

{

ifstream inFile;

inFile.open("Grades.txt");

ListNode *head= NULL;

double value;

for (int x=0; x<10; x++)

{

inFile>>value;

head = new ListNode(value,head);

}

ListNode *ptr1;

double temp;

ListNode * curr = head;

while (curr!= NULL) {

ListNode* min = curr;

ListNode* r = curr->next;

while (r != NULL) {

if (min->value > r->value)

min = r;

r = r->next;

}

int x = curr->value;

curr->value = min->value;

min->value = x;

curr = curr->next;

}


ListNode *ptr = head;

for (int x=0; x<10; x++)

{

cout<<ptr->value<<endl;

ptr=ptr->next;

}

return 0;

}



===========================================================
SEE OUTPUT

Thanks, PLEASE COMMENT if there is any concern. PLEASE UPVOTE


Related Solutions

Temperature (x) 65 70 75 80 85 90 95 100 105 Number of ice cream cones...
Temperature (x) 65 70 75 80 85 90 95 100 105 Number of ice cream cones sold per hour (y) 8 10 11 13 12 16 19 22 23 Calculate the linear correlation coefficient r, for temperature (x) and number of ice cream cones sold per hour (y). (round to 3 decimal place) Is there a linear relation between the two variables x, and y? If yes, indicate if the relationship is positive or negative. (Hint : use the Critical...
C++ Text file contains numbers 92 87 65 49 92 100 100 100 82 75 64...
C++ Text file contains numbers 92 87 65 49 92 100 100 100 82 75 64 55 100 98 -99 Modify your program from Exercise 1 so that it reads the information from the gradfile.txt file, reading until the end of file is encountered. You will need to first retrieve this file from the Lab 7 folder and place it in the same folder as your C++ source code. Run the program #include <iostream> using namespace std; typedef int GradeType[100];...
Benzene at 80 C is burned with 30% excess air at 100 C. Conversion is 90%...
Benzene at 80 C is burned with 30% excess air at 100 C. Conversion is 90% with 85% going to CO2. The flame temperature is 670 C, calculate the Q.
Permissive Authoritative Authoritarian 70 70 55 90 80 80 90 90 75 90 70 67 M=85...
Permissive Authoritative Authoritarian 70 70 55 90 80 80 90 90 75 90 70 67 M=85 75 50 M=81 63 65 M=65 grand mean=75 Calculate your F ratio. [ If you could not answer any of the questions above about the SS and df values, go back now and make up numbers to fill in for those values then use those wrong numbers to find this F ratio. Those will be wrong, but this could be right based on those...
Exam 1 Scores 91, 83, 75, 85, 90, 80, 67, 82, 88, 87, 95, 91, 73,...
Exam 1 Scores 91, 83, 75, 85, 90, 80, 67, 82, 88, 87, 95, 91, 73, 80, 83, 92, 94, 68, 75, 91, 79, 95, 87, 76, 91, 85, 59, 70, 69, 78 Dummy: 0, 0 What are the Hypothesis Statement? What is Ho? What is Ha? What is the P-Value? What is the Claim? What is the Decision Rule? (Hint: Do you reject or fail to reject the null hypothesis?) What is the Level of Significance? (Hint: α) What...
Final_exam assignment_grade Tutorial_attend 100 90 5 100 75 5 90 75 5 85 85 5 85...
Final_exam assignment_grade Tutorial_attend 100 90 5 100 75 5 90 75 5 85 85 5 85 100 5 80 95 5 70 80 5 60 95 5 60 80 5 55 95 5 55 25 4 50 80 5 45 90 5 40 65 5 40 65 4 35 0 3 30 70 4 30 55 4 25 85 5 25 90 4 15 5 3 15 80 5 15 50 5 15 45 3 5 75 3 5 70...
For the following data 86 84 91 75 78 80 74 75 87 76 82 90...
For the following data 86 84 91 75 78 80 74 75 87 76 82 90 98   68 What is the mean?    ______ What is the median?_________ What is the mode (if any)? _____ What is the standard deviation? ____ What is the variance? ______ What is the five-number summary for this data? Draw a boxplot with reasonable attention to scale. Create a stemplot of this data. Using the same data set and ignoring the fact that the 5-number summary...
z = [ 100 93 90 89 80 79 78 75 67 60 45 42 41...
z = [ 100 93 90 89 80 79 78 75 67 60 45 42 41 40 30 20 15 12 10 9 2 1] Using MATLAB, Write fully vectorized code (no loops) to find index number of which the numbers have decreased by 40. Print results to command window.
In this lab, you open a file and read input from that file in a prewritten...
In this lab, you open a file and read input from that file in a prewritten C++ program. The program should read and print the names of flowers and whether they are grown in shade or sun. The data is stored in the input file named flowers.dat. Instructions Ensure the source code file named Flowers.cpp is open in the code editor. Declare the variables you will need. Write the C++ statements that will open the input file flowers.dat for reading....
uConstruct confidence intervals for the population mean of 80%, 90%, 95%, 99% using the following data...
uConstruct confidence intervals for the population mean of 80%, 90%, 95%, 99% using the following data and a population standard deviation of 900: un = 100 u?x ̅ = 425
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT