program to implement Insertion sort algorithm for the given inputfile.

program to implement Insertion sort algorithm for the given inputfile.

Page 1 of 1

6 Replies - 16755 Views - Last Post: 22 March 2007 - 10:24 PM Rate Topic: -----

#1 maxsaint  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-March 07

program to implement Insertion sort algorithm for the given inputfile.

Posted 21 March 2007 - 05:25 PM

program to implement Insertion sort algorithm for a list of auto part
records. Each part record has the part's 5-digit unique part ID, part's name with at most 12
characters, number of parts available in the store inventory and the part price. All the part
records are in an input file as a sequence of lines where each line has the data for a single
part as below:

68057 wiper-blade 27 15.25

The number of lines in the input file is unspecified.
Read the record data from the input file, create the records as you read them, put them in an
array sequentially as they appear in the input file, print that unsorted array, then sort that
array using Insertion sort algorithm on the unique part ID and print the sorted list of records.

CAN SOMEONE PLEASE HELP ME!!! i dont know how to do this.

Is This A Good Question/Topic? 0
  • +

Replies To: program to implement Insertion sort algorithm for the given inputfile.

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: program to implement Insertion sort algorithm for the given inputfile.

Posted 21 March 2007 - 08:05 PM

Well we can help but you really need to give us some code to show that you have made at least an effort. We are not allowd to do you homework for you: See forum rules

To get started make a structure to hold the data. Next create an array to hold the data, then open the file for reading, read in the stucture until EOF (or the end of the array). Then look up the insertion sort either in your text book or online and get a real good feel for how it works. I suggest you do it at least once on paper (or will a deck of cards... that is how I learned most of the searching and sorting algorithems). Then code away.

If you need more help, give us code and/or ask specific questions.
Was This Post Helpful? 0
  • +
  • -

#3 maxsaint  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-March 07

Re: program to implement Insertion sort algorithm for the given inputfile.

Posted 22 March 2007 - 08:30 AM

well i really dont know much coding but i came up with this code. still dont know if that works since its not compling and its giving me errors

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
  
void insertion_sort(int x[],int length)
{
  int key,i;
  for(int j=1;j<length;j++)
  {
	 key=x[j];
	 i=j-1;
	 while(x[i]>key && i>=0)
	 {
			   x[i+1]=x[i];
		 i--;
	 }
	 x[i+1]=key;
  }
}
int main () {
  string line;
  ifstream myfile ("inputfile.txt");
  if (myfile.is_open())
  {
	while (! myfile.eof() )
	{
	  getline (myfile,line);
	  cout << line << endl;
	}
	myfile.close();
  }

  else cout << "Unable to open file"; 
{
  //int A[ELEMENTS]={5,2,4,6,1,3};
  int x;
 
  cout<<"NON SORTED LIST:"<<endl;
  for(x=0;x<ELEMENTS;x++)
  {
	   cout<<A[x]<<endl;
  }
  insertion_sort(A,ELEMENTS);
  cout<<endl<<"SORTED LIST"<<endl;
  for(x=0;x<ELEMENTS;x++)
  {
	   cout<<A[x]<<endl;
  }

  return 0;
}



Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: program to implement Insertion sort algorithm for the given inputfile.

Posted 22 March 2007 - 09:21 AM

	else cout << "Unable to open file";
	{

Here is the first problem I found... I think you mean:
 else { cout << "Unable to open file"; }

either way, the open bracket is never closed, so either put the cout inside of it, or delete it.

When someone posts code and says: IT just dosen't compile. The first ting I do is reformat it with indentions for each open bracket. Most IDE's have a neat feature where you hilight a blobk of code and press the tab button and they indent for you. This reveals two of the most common beginner mistakes, non-matching brackets, and improper if-else structures.

if you get into the habit of ALWAYS using if (condition) { statement; } rather than trying to omit the (unrequired) brackets it will save a world of touble.

Here is code that will compile (no idea if it works)

#include <iostream>
#include <fstream>
#include <string>

//Had to add this definition
#define ELEMENTS 6

using namespace std;

void insertion_sort(int x[],int length)
{
	int key,i;
	for(int j=1;j<length;j++)
	{
		key=x[j];
		i=j-1;
		while(x[i]>key && i>=0)
		{
			x[i+1]=x[i];
			i--;
		}
		x[i+1]=key;
	}
}

int main () 
{
	string line;
	ifstream myfile ("inputfile.txt");
	if (myfile.is_open())
	{
		while (! myfile.eof() )
		{
			getline (myfile,line);
			cout << line << endl;
		}
		myfile.close();
	}
	
	//Had to put this in brackets...
	else {cout << "Unable to open file"; }
	//had to uncomment this line (as A was undefined).
	int A[ELEMENTS]={5,2,4,6,1,3};
	int x;
	
	cout<<"NON SORTED LIST:"<<endl;
	for(x=0;x<ELEMENTS;x++)
	{
		cout<<A[x]<<endl;
	}
	insertion_sort(A,ELEMENTS);
	cout<<endl<<"SORTED LIST"<<endl;
	for(x=0;x<ELEMENTS;x++)
	{
		cout<<A[x]<<endl;
	}
	
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#5 maxsaint  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-March 07

Re: program to implement Insertion sort algorithm for the given inputfile.

Posted 22 March 2007 - 09:58 AM

THANKS NOW it works but it doesn't sort the input file which consist of char and intergers...
Was This Post Helpful? 0
  • +
  • -

#6 maxsaint  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-March 07

Re: program to implement Insertion sort algorithm for the given inputfile.

Posted 22 March 2007 - 05:26 PM

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

//Had to add this definition
//#define ELEMENTS 6

using namespace std;
struct parts {
int id;
char name[13];
int quatity;
float price;
}

int main()
{
ifstream infile("Inputfile.txt");

int counter;
parts lists[100];
parts line;


cout << "Creating an insertion sort algorithm."<<endl;
cout<<"Unsorted records."<<endl;


// Read the input data into the array

int x = 0;
while (! infile.eof() )
{
infile >> lists[x].id >> lists[x].name >> lists[x].quatity >> lists[x].price;
x=x+1;
}

infile.close();


//display unsorted
counter = x;
x = 0;
for (x = 0; x <= counter; x = x+1)
{
cout<<setfill('0')<<setw(5)<< lists[x].id <<" "<<setfill(' ')<<setw(12)<<lists[x].name<<" "<< lists[x].quatity <<" "<<lists[x].price<<endl;

}

cout<<"There are "<<counter<<" records in the file."<<endl;


// Insertion Sort

{

int y=1; // Counter for the run
while(y < counter)
{
int i;
int min;
min = lists[y].id;
line = lists[y];
for (i=y-1; i >= 0 && min < lists[i].id; i=i-1) // sorting using the ID
{
lists[i+1] = lists[i]; // Swap entire line
lists[i]=line; // Swap entire line

}

y = y+1;
}


// Display sorted array

cout<<endl<<endl;
cout<<"Sorted Records"<<endl;

for(int c=0; c < counter; c=c+1)
{
cout<<setfill('0')<<setw(5)<< lists[c].id <<" "<<setfill(' ')<<setw(12)<<lists[c].name<<" "<< lists[c].quatity <<" "<<lists[c].price<<endl;
}
}

return 0;
}



i m stil having some problem its giving me 3 errors

1) parts follow by int is illegal
2) return type of main should be int instead of parts
3) main: fucntion must reurn the value.
Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: program to implement Insertion sort algorithm for the given inputfile.

Posted 22 March 2007 - 10:24 PM

one little semicolon:

struct parts {
int id;
char name[13];
int quatity;
float price;
};


Now in your mind catalog one semicolon caused 3 errors, this is currently your max. Now over the time that you program (hopfully years and years) the lack of semicolons will cause LOTS of erros. Now the game is, "What are the MOST errors that you can cause by leaving out only 1 semicolon?" I have no idea what the world record is, I myself have only gotten to 8 for a SINGLE semicolon. Bummer is that some compilers only allow about 10 before they give up... oh, warnings count too. :)

This post has been edited by NickDMax: 22 March 2007 - 10:32 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1