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

Data elements (integers) should be read from a file (the data elements could be

ID: 3760397 • Letter: D

Question

Data elements (integers) should be read from a file (the data elements could be in multiple lines; data elements in a line separated by one or more spaces).

Overload the Print function (PrintList) to display the output the data to file (reference to the file should be used as an argument).

Each data element should be printed on a different line in the output file.

Use the following formatting for the data elements.

1. Add the digits of the integer

2. If the sum of the digits is > 20, divide the sum by 20 and take the remainder

3. Print number of “*’” corresponding to the sum or the remainder

4. Print a space and the original number after the “*”

For example, if the data element is 156

In the output file, it should be printed as (sum of the digits is 12)

************ 156

The data element 5678 will be printed as (5+6+7+8 will be 26, remainder will be 6 after dividing by 20)

****** 5678

__________________________________________________________________________________________________

// Assign2.cpp : Defines the entry point for the console application.
//

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
/*
* Node Declaration
*/
struct node
{
int info;
struct node *next;
}*start;

/*
* Class Declaration
*/
class single_llist
{
public:

node* create_node(int);
void AddAppend();
void PrintList();
int CountList();
void SortList();

single_llist()
{
  start = NULL;
}
};

/*
* Main :contains menu
*/

Explanation / Answer

Program is given below:

/**
* This program creates a singly linked list from the integer data elements stored in a file and
* also prints the contents of the list to an outout file as per specified format. Output format is
* processed as following:
* 1. Add the digits of the integer
* 2. If the sum of the digits is > 20, divide the sum by 20 and take the remainder
* 3. Print number of “*’” corresponding to the sum or the remainder
* 4. Print a space and the original number after the “*” *
*/
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>

using namespace std;

/**
* Node Declaration
*/
struct node
{
int info;
struct node *next;
}*start;

/**
* Class Declaration
*/
class single_llist
{
public:
   //default constructor
   single_llist()
   {
      start = NULL;
   }
   node* create_node(int); //implemented
   void AddAppend(node*); //implemented
   void PrintList(string); //implemented
   int CountList(); //not implemented
   void SortList(); //not implemented
};

/**
* Creates a new node
*/
node* single_llist::create_node(int item)
{
   struct node* n=new node;
   n->info=item;
   n->next=NULL;
   return n;
}

/**
* Adds a node to the start of the list
*/
void single_llist::AddAppend(node* n)
{
   if(start==NULL)
   {
       start=n;
   }
   else
   {
       n->next=start;
       start=n;
   }
}

/**
* Prints a contents of the list to an output file as per specified format.
*/
void single_llist::PrintList(string filename)
{
   //opens the output file.
   ofstream outfile(filename.c_str());

   if(outfile.is_open())
   {
       node* curr=start;
       string astr=""; //asterisks string. length depends on sum of digits or remainder
       //loop to traverse the list
       while(curr!=NULL)
       {
           int num=curr->info;
           int digit_sum=0; //stores sum of digits
           //loop to calculate sum of digits
           while(num!=0)
           {
               digit_sum+=num%10;
               num=num/10;
           }
           //cout<<"item:"<<curr->info<<", sum:"<<digit_sum<<endl;

           //checks if sum of digits > 20
           if(digit_sum>20)
           {
               //calculates remainder
               int remainder=digit_sum%20;

               //loop to prepare asterisk string as per remainder value
               for(int i=1;i<=remainder;i++)
               {
                   astr+="*";
               }
               //cout<<"astr:"<<astr<<endl;
           }
           else
           {
               //loop to prepare asterisk string as per sum of digits
               for(int i=1;i<=digit_sum;i++)
               {
                   astr+="*";
               }
               //cout<<"astr:"<<astr<<endl;
           }
           stringstream ss;
           ss<<curr->info;
           string num_str=ss.str();

           //output string
           string outstring=astr+" "+num_str+" ";

           //writes output string to output file
           outfile<<outstring;

           //next node in the list
           curr=curr->next;
       }
       outfile.close();
   }
   else
       cerr<<"Unable to open outout file."<<endl;
}
int main() {
   single_llist list; //reference to singly linked list
   string infilename="./src/data.txt"; //path to input data file. change it as per the path of your input file.
   ifstream infile(infilename.c_str()); //opens input data file
   string line;

   //reads input data file and prepare singly linked list
   if(infile.is_open())
   {
       //loop to read lines in data file
       while(getline(infile,line))
       {
           //loop to parse a single line
           istringstream iss(line);
           while(!iss.eof())
           {
               string word;
               iss >> word;
               int num=atoi(word.c_str());
               list.AddAppend(list.create_node(num)); //add number to the start of list
               cout << "number: "<<num<<endl;
           }
       }
       infile.close();
   }
   else
       cerr<<"Unable to open input file."<<endl;

   string outfilename="./src/output.txt"; //path to output data file. change it as per the path of your output file.

   list.PrintList(outfilename); //write output to file
   return 0;
}

Sample input file (data.txt)

156 5678
123

Sample output file (output.txt)

****** 123
************ 5678
************************ 156