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

Create a C++ Program 1. Reads real numbers stored as ASCII text, one number per

ID: 663250 • Letter: C

Question

Create a C++ Program

1. Reads real numbers stored as ASCII text, one number per line from a file o numbers may be positive or negative o number may or may not have an embedded decimal point o numbers are not ordered in any way within the file.

2 .Dynamically allocates memory for a structure (linked list node) containing two variables o one of type float for the real number o one a structure pointer type that will point to the next node in the list.

3. Inserts each node into a singly-linked list in ascending order o start at the list head and move towards the tail o link the new node into its proper place in the list.

4. Traverse the linked list o start at the head and advance to the next node until the tail is reached o write the number in each node an output file.

Algorithm:

1. Dynamically allocates memory for filename strings. a. Input filename. b. Output filename created from in filename by adding _out before extension.

2. Prompt the user for the name of an input file. a. Open this file if it exists. b. if file does not exist prompt the user for another file name. c. Continue until a file that exists is entered.

3. Read a float data item from each line in this file.

4. Dynamically allocates memory for and creates a node to hold this data. a. Data type for content of this node is float. b. Data type for next node is for struct node type.

5. Insert a node into linked list in ascending order (a linked list is a pointer to a node type).

6. Close the input file.

7. After the linked list is built, it contains the numbers from the input file in ascending order.

8. Open the output file by its name.

9. Traverse the linked list from its head to its tail. a. Write the float from the current node to the output file. b. Advance to the next node.

10. Close the output file.

11. Free all dynamically allocated memory. a. Filename strings. b. Nodes in the linked list.

12. Exit program

Explanation / Answer

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include "Sort.h"

using namespace std;
struct LinkedListNode //linked list node declaring
{
int data;
LinkedListNode* next;
};

node *head_ptr = NULL;
void ShowData(); //to show data avaiable
void copy(const LinkedListNode* source_ptr, LinkedListNode*&head_ptr, LinkedListNode*&tail_ptr);
Sort();


int main()
{
ifstream input;
ofstream output;

Sort sort;
while(true){
string filename;
cout<<" Enter file name" //reading file name until it is correct one
cin>>filename;
input.open (filename);
if (!input)
{
cout << "sorry..cannot open" << endl;
system ("pause");
return 1;
}
else{
break;
}
}
  

input >> sort; //reading data from sort

while(input)
{
cout << sort<< endl; //printing data which was read from file
input >> sort;
}

system("pause");
return 0;

Sort();
}


void Sort() //ascending ordering the elements read from file
{
node* curr = head_ptr;
int temp = 0;
while(curr!=NULL)
{
temp++;
curr = curr->NEXT;
}
for(int i = temp ; i > 1 ; i-- )
{
node *temp, *temp1;
temp1 = HEAD;
for(int j = 0 ; j < temp-1 ; j++ )
{
if(temp1->DATA > temp1->NEXT->DATA) //swapping the values to assure sort values.
{
node *temp2 = temp1->NEXT;
temp1->NEXT = temp2->NEXT;
temp2->NEXT = temp1;
if(temp1 == HEAD)
{
HEAD = temp2;
temp1 = temp2;
}
else
{
temp1 = temp2;
temp->NEXT = temp2;
}
}
temp = temp1;
temp1 = temp1->NEXT;
}
}
}

void copy(const LinkedListNode* source_ptr, LinkedListNode*&head_ptr, LinkedListNode*&tail_ptr)
{
LinkedListNode* temp;// to allocate new nodes
head_ptr=NULL;
tail_ptr=NULL;

if(source_ptr==NULL)
return;

head_ptr=new LinkedListNode;
head_ptr->next=NULL;
head_ptr->data=source_ptr->data;
tail_ptr=head_ptr;
source_ptr=source_ptr->next;

while(source_ptr!=NULL)
{
temp = new LinkedListNode;
temp->next=NULL;
temp->data =source_ptr-> data;
tail_ptr->next=temp;
tail_ptr = tail_ptr->next;
source_ptr = source_ptr->next;
}
}


---------------------------------------------------------------
header failure

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

class Sort
{
private:
int nodes;
public:
Sort() { }

Sort();

friend ostream &operator<<( ostream &output, const Sort &D )
{
output << D.nodes;
return output;
}

friend istream &operator>>( istream &input, Sort &D )
{
input >> D.nodes;
return input;
}
};