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

Need help and some tips on how to get this program to work. The program takes in

ID: 3784356 • Letter: N

Question

Need help and some tips on how to get this program to work. The program takes in integer numbers from a .dat file and puts them into an array. I have been able to read the number from the .dat file but i also need to be able to output 3 more values to the .dat file from user input. Once all the values are in the array, the program uses a reverse bubble sort method of sorting the values of the array. There is something going wrong with my b_sort function but im not quite sure what it is.

What I need:

Implementation of the load_from_file function

Implementation of the save_to_file function

Correction of the b_sort function

-----------------------------------------------------------------------------

Here is what I have in my main so far:

#include <iostream>
#include <fstream>
#include <string>
#include "numlist.h"

using namespace std;

int main(){
ifstream inputfile;
string inputfilename;
int array_size = 1000;
char * array = new char[array_size];
cout << "Input file name: ";
getline (cin, inputfilename);
inputfile.open(inputfilename.c_str(),ios::in | ios::binary);
string line;
if (inputfile.is_open()){
int position = 0;
int numToSave;
cout << "file opened successfully ";
while (!inputfile.eof() && position < array_size){
inputfile.get(array[position]);
position++;
}

for(int i = 0; array[i] != ''; i++)
       {
           cout << array[i];
       }
  
inputfile.close();
}
else cout << "Unable to open file ";
return 0;
}

Here is the numlist.cc

---------------------------------------------------------------

#include "numlist.h"
#include <iostream>
#include <fstream>
#include <cstdio>
using namespace std;

// Constructor
NumList::NumList(){
   used = 0;
}

void NumList::insert(int num){
   if(used <CAPACITY){
       data[used] = num;
       used++;
   }
   else{
       cout<<"Error. List capacity has been reached. ";
   }
}

void NumList::load_from_file(istream& ins){

}
void NumList::save_to_file(ostream& outs){

}
void NumList::see_all()const{
   if(used == 0)
   cout<<"Empty list. ";
   else
   for(size_t i = 0; i<used; ++i)
       cout<<data[i]<<endl;
}
int NumList::get_item(size_t index)const{
   if(index < used)
       return data[index];
   else
       return -1;
}
void NumList::b_sort(){
bool done = false;
int j;
int tmp;
while(!done){
       done = !true;
       for(j=used-1; j>= 0; --j){
       if(data[j] < data[j-1]){
           done = false;
           tmp = data[j];
           data[j] = data[j-1];
           data[j=1] = tmp;
       }
       }
}
}

Here is numlist.h

-----------------------------------------------------------

#ifndef NUMLIST_H
#define NUMLIST_H
#include <iostream>
#include <cstdlib>
#include <cmath>

class NumList{
public:
   static const size_t CAPACITY = 100;
  
   // Default constructor
   NumList();

   void insert(int num);
   size_t size()const {return used;}

   void load_from_file(std::istream& ins);

   void save_to_file(std::ostream& outs);
   void b_sort();
   int get_item(size_t index)const;
   void see_all()const;
private:
   int data[CAPACITY];
   size_t used;
};

#endif

And finally here is the smaller.dat file

-----------------------------------------------------------

14140
22089
26522
5203
9172
4435
28318
24583
6816
4587

Thank you very much for your time. Any help you can provide is very much appreciated.

Explanation / Answer

I could not understand what you are planning to do with save_to_file() function...

I have modified the code as -- read the input from file and sort the same and store it in another file. Have modified the code according to the above assumption...Have only given comments to the part of the code I have added or modified.

CODE:

numlist.cpp-----------------------------------

#include "numlist.h"
#include <iostream>
#include <fstream>
#include <cstdio>
#include <string>

using namespace std;
// Constructor
NumList::NumList(){
used = 0;
}
void NumList::insert(int num){
if(used <CAPACITY){
data[used] = num;
used++;
}
else{
cout<<"Error. List capacity has been reached. ";
}
}

// Please take a note of change from istream to ifstream here.
void NumList::load_from_file(ifstream& ins){
string line;
used = 0;

// if the file is open...
if (ins.is_open()) {
// till end of the file read..
while(getline(ins,line)){
// since the input read is string, convert it into integer using stoi()
// and save the same in data[i].
data[used] = std::stoi(line);
used++;
}
// close the pointer to file...
ins.close();
}
}

// Please take a note of change from ostream to ofstream here.
void NumList::save_to_file(ofstream& outs){
// check if the outs is open...
if(outs.is_open()){
// print out the contents of data[i] to the file..
for(int i=0;i<used;i++)
outs << data[i] << endl;

// at the end close the file.
outs.close();
}
}

void NumList::see_all()const{
if(used == 0)
cout<<"Empty list. ";
else
for(size_t i = 0; i<used; ++i)
cout<<data[i]<<endl;

cout << endl;

}
int NumList::get_item(size_t index)const{
if(index < used)
return data[index];
else
return -1;
}

// your bubble sort logic did not have two loops,
// because of which I believe you were able to see the results sorted
// only once.
void NumList::b_sort(){
// for every index in the data array check for all the other indices..
// once we are done with the index, move on and repeat the same for all the other inices.
for (int i = 0; i < used; ++i){
for (int j = 0; j < used - i - 1; ++j){
if (data[j] > data[j + 1]){
int temp = data[j];
data[j] = data[j + 1];
data[j + 1] = temp;
}
}
}
}

numlist.h-----------------------------------------------------------------------------------

#ifndef NUMLIST_H
#define NUMLIST_H
#include <iostream>
#include <cstdlib>
#include <cmath>
class NumList{
public:
static const size_t CAPACITY = 100;

// Default constructor
NumList();
void insert(int num);
size_t size()const {return used;}

// Have changed refernce to ifstream.
void load_from_file(std::ifstream& ins);

// Have changed refernce to ofstream.
void save_to_file(std::ofstream& outs);
void b_sort();
int get_item(size_t index)const;
void see_all()const;
private:
int data[CAPACITY];
size_t used;
};
#endif

m.cpp (MAIN FILE) -------------------------------------------------------------------

#include <iostream>
#include <fstream>
#include <string>
#include "numlist.h"

using namespace std;

int main(){
ifstream inputfile;
string inputfilename;
int array_size = 1000;
char * array = new char[array_size];
cout << "Input file name: ";
cin >> inputfilename;

// I have commented your code... have written some code below.
// Creating an instance of NumList.
NumList nl;

// Create an object of ifstream to read from the file.
ifstream fp (inputfilename);

// pass a refernce to the fp instance created above to read from file.
nl.load_from_file(fp);

// check to see if all the data have been loaded.
nl.see_all();

// carry out the bubble sort operation.
nl.b_sort();

// check to see if the data is sorted.
nl.see_all();

// create a new file outputtfilename.dat to store the sorted data.
// create an object of ofstream and pass it as refernce to save the data.
ofstream fp1 ("outputtfilename.dat");
nl.save_to_file(fp1);

//inputfile.open(inputfilename.c_str(),ios::in | ios::binary);
/*string line;
if (inputfile.is_open()){
int position = 0;
int numToSave;
cout << "file opened successfully ";
while (!inputfile.eof() && position < array_size){
inputfile.get(array[position]);
position++;
}
for(int i = 0; array[i] != ''; i++)
{
cout << array[i];
}

inputfile.close();
}
else cout << "Unable to open file "; */
return 0;
}

// I HAVE USED SAME INPUT FILE.

------------------------------------------------------------------

OUTPUT:

$ g++ -c numlist.cpp -o num.o

$ g++ num.o m.cpp

$ ./a.out

Input file name: in.txt (in.txt is the input file I have used)

14140
22089
26522
5203
9172
4435
28318
24583
6816


4587
4435
4587
5203
6816
9172
14140
22089
24583
26522
28318

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote