Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a C++ Expert!

Join 244,308 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 819 people online right now. Registration is fast and FREE... Join Now!




Insertion sort using vectors?

 
Reply to this topicStart new topic

Insertion sort using vectors?

rmeto4u
4 Dec, 2008 - 09:06 AM
Post #1

D.I.C Head
**

Joined: 10 Oct, 2008
Posts: 72

How can you use insertion sort using vectors inside of this program? Isnt that only done if you were to use arrays?

Heres code that I want to use insertion sort with vectors for but I dont think you can use insertion sort on vectors unless you're using arrays.
CODE
#include <iostream>  
#include <vector>  
#include <fstream>  

using namespace std;  
  
void fill(vector<int>&);  
void print(vector<int>&);  
void write(vector<int>&);  
  
int main()  
{  
    vector<int> numbers;  
    cout << "We will now fill a vector from a list of ints in a text file: \n";  
    fill(numbers);  
    cout << "Printing the numbers in the array retreived from the text file \n";  
    print(numbers);  
    write(numbers);  
  
    cout << "\nSee ya Later!\n";  
    return 0;  
}  
  
void fill(vector<int>& nums)  
{  
    fstream file;  
    int temp = 0;  
    file.open("input.txt");  
      
    while(!file.eof())  
    {  
        file >> temp;  
        nums.push_back(temp);  
    }  
    file.close();  
}  
  
void print(vector<int>& nums)  
{  
    for(int i = 0; i < nums.size(); i++)  
    {  
        cout << nums[i] << " ";  
    }  
}  
  
void write(vector<int>& nums)  
{  
    ofstream file;  
    file.open("output.txt");  
    for(int i = 0; i < nums.size(); i++)  
    {  
        file << nums[i] << " ";  
    }  
    file.close();  
    cout << "\nFile has been written to. Check it.\n";  
}


User is offlineProfile CardPM
+Quote Post


KYA
RE: Insertion Sort Using Vectors?
4 Dec, 2008 - 09:46 AM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 9,508



Thanked: 363 times
Dream Kudos: 2550
Expert In: C, C++, Java

My Contributions
Normal insertion sort:

cpp

void insertionSort(int size, int data[]){
int j, val;

//iterate through entire list
for(int i = 1; i < size; i++){
val = data[i];
j = i - 1;

while(j >= 0 && data[j] > val){
data[j + 1] = data[j];
j = j - 1;
}//end while
data[j + 1] = val;
}//end for

//Prints the sorted list
for ( int i = 0; i < size; i++ )
cout << data[i] << " ";


}//end insertionSort Function


With vectors:

cpp

void insertionSort(int size, vector<int>& data){//pass a reference so you can modify data
//you don't even need the size parameter here
int j, val;

//iterate through entire list
for(int i = 1; i < data.size(); i++){
val = data[i];
j = i - 1;

while(j >= 0 && data[j] > val){
data[j + 1] = data[j];
j = j - 1;
}//end while
data[j + 1] = val;
}//end for

//Prints the sorted list
for ( int i = 0; i < data.size(); i++ )
cout << data[i] << " ";


}//end insertionSort Function



So i would use:

cpp

void insertionSort(vector<int>& data){//pass a reference so you can modify data
//you don't even need the size parameter here
int j, val;

//iterate through entire list
for(int i = 1; i < data.size(); i++){
val = data[i];
j = i - 1;

while(j >= 0 && data[j] > val){
data[j + 1] = data[j];
j = j - 1;
}//end while
data[j + 1] = val;
}//end for

//Prints the sorted list
for ( int i = 0; i < data.size(); i++ )
cout << data[i] << " ";


}//end insertionSort Function


This post has been edited by KYA: 4 Dec, 2008 - 09:47 AM
User is offlineProfile CardPM
+Quote Post

rmeto4u
RE: Insertion Sort Using Vectors?
4 Dec, 2008 - 10:55 AM
Post #3

D.I.C Head
**

Joined: 10 Oct, 2008
Posts: 72

Ok, would the code with insertion sort using vectors look like this?

CODE
#include <iostream>  
#include <vector>  
#include <fstream>  

using namespace std;  
  
void fill(vector<int>&);  
void print(vector<int>&);  
void write(vector<int>&);  
  
int main()  
{  
    vector<int> numbers;  
    cout << "We will now fill a vector from a list of ints in a text file: \n";  
    fill(numbers);  
    cout << "Printing the numbers in the array retreived from the text file \n";  
    print(numbers);  
    write(numbers);  
  
    cout << "\nSee ya Later!\n";  
    return 0;  
}  
  
void fill(vector<int>& nums)  
{  
    fstream file;  
    int temp = 0;  
    file.open("input.txt");  
      
    while(!file.eof())  
    {  
        file >> temp;  
        nums.push_back(temp);  
    }  
    file.close();  
}  
  
void print(vector<int>& nums)  
{  
    for(int i = 0; i < nums.size(); i++)  
    {  
        cout << nums[i] << " ";  
    }  
}  
  
void write(vector<int>& nums)  
{  
    ofstream file;  
    file.open("output.txt");  
    for(int i = 0; i < nums.size(); i++)  
    {  
        file << nums[i] << " ";  
    }  
    file.close();  
    cout << "\nFile has been written to. Check it.\n";  
}  

void insertionSort (vector<int>& data)
{
     int j, val;
    
     for(int i= 1; i < data.size(); i++){
             val = data[i];
             j = i - 1;
            
     while(j >= 0) && data[j] > val){
             data[j + 1] = data[j];
             j = j - 1;
     }
     data [j + 1] = val;  
}
     for (int i = 0; i < data.size(); i++ )
         cout << data[i] << " ";
        
}

User is offlineProfile CardPM
+Quote Post

KYA
RE: Insertion Sort Using Vectors?
4 Dec, 2008 - 11:25 AM
Post #4

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 9,508



Thanked: 363 times
Dream Kudos: 2550
Expert In: C, C++, Java

My Contributions
Have you run it? I recall showing you how to fill and print vectors of integers from a text file. So if that worked and what I posted just now works then hypothetically, putting them together should work.
User is offlineProfile CardPM
+Quote Post

rmeto4u
RE: Insertion Sort Using Vectors?
4 Dec, 2008 - 02:59 PM
Post #5

D.I.C Head
**

Joined: 10 Oct, 2008
Posts: 72

Yes I ran it but I got two errors and they said "expected ; before '[' token" and then "label 'data'used but not defined". When I try to define data then I get even more error messages and then it starts saying "size" isnt defined, etc. All I want to do with this program is use insertion sort to sort numbers within it.
User is offlineProfile CardPM
+Quote Post

KYA
RE: Insertion Sort Using Vectors?
4 Dec, 2008 - 03:34 PM
Post #6

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 9,508



Thanked: 363 times
Dream Kudos: 2550
Expert In: C, C++, Java

My Contributions
This are simple syntax errors. Can you not really find/fix them? The compiler points them out to do. First example, you're missing a '(. Are you even looking?

You didn't even try to call the insertionSort method in main. Not to mention missing a prototype. Why??

cpp

#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

void fill(vector<int>&);
void print(vector<int>&);
void write(vector<int>&);
void insertionSort(vector<int>&);

int main()
{
vector<int> numbers;
cout << "We will now fill a vector from a list of ints in a text file: \n";
fill(numbers);
cout << "Printing the numbers in the array retreived from the text file \n";
print(numbers);
insertionSort(numbers);
write(numbers);

cout << "\nSee ya Later!\n";

system ("pause");
return 0;
}

void fill(vector<int>& nums)
{
fstream file;
int temp = 0;
file.open("input.txt");

while(!file.eof())
{
file >> temp;
nums.push_back(temp);
}
file.close();
}

void print(vector<int>& nums)
{
for(int i = 0; i < nums.size(); i++)
{
cout << nums[i] << " ";
}
}

void write(vector<int>& nums)
{
ofstream file;
file.open("output.txt");
for(int i = 0; i < nums.size(); i++)
{
file << nums[i] << " ";
}
file.close();
cout << "\nFile has been written to. Check it.\n";
}

void insertionSort (vector<int>& data)
{
int j, val;

for(int i= 1; i < data.size(); i++){
val = data[i];
j = i - 1;

while((j >= 0) && data[j] > val){
data[j + 1] = data[j];
j = j - 1;
}
data [j + 1] = val;
}
for (int i = 0; i < data.size(); i++ )
cout << data[i] << " ";

}


Works now.

This post has been edited by KYA: 4 Dec, 2008 - 03:34 PM
User is offlineProfile CardPM
+Quote Post

rmeto4u
RE: Insertion Sort Using Vectors?
4 Dec, 2008 - 07:16 PM
Post #7

D.I.C Head
**

Joined: 10 Oct, 2008
Posts: 72

Im trying to make this program into a class rather than a structure as well as making the member variables private. I need to include member functions for each of the following:
1. One to return initial balance
2. One to return balance at maturity
3. One to return interest rate
4. One to return term

I also need to include:
1. A constructor that sets the member variables to any specified values.
2. A default constructor.
3. An input member function with one formal parameter of type istream
4. An output member function with one formal parameter of type ostream.

So far, this is what Ive came up with the code and if I can get help, Id appreciate it. Thanks

CODE
#include <iostream>

double(InitialBalance);
double(BalanceAtMaturity);
double(InterestRate);
int(Term);

//default constructors
InterestRate();
InitialBalance();
BalanceAtMaturity;
Term();

using namespace std;

int main()
{
  char dummy;

  double balance;
  double intRate;
  int term;
  int CDAccount;
  CDAccount account = CDAccount( 100.0, 10.0, 6 );
  
  cout << "CD Account interest rate: "
       << account.InterestRate() << endl;
  cout << "CD Account initial balance: "
       << account.InitialBalance() << endl;
  cout << "CD Account balance at maturity is: "
       << account.BalanceAtMaturity() << endl;
  cout << "CD Account term is: " << account.Term()
       << " months"<< endl;
  account.output( cout );
  cout << "Enter CD initial balance, interest rate, "
       << " and term:" << endl;
  account.input(cin);
  cout << "CD Account interest rate: "
       << account.InterestRate() << endl;
  cout << "CD Account initial balance: "
       << account.InitialBalance() << endl;
  cout << "CD Account balance at maturity is: "
       << account.BalanceAtMaturity() << endl;
  cout << "CD Account term is: " << account.Term()
       << " months"<< endl;
  account.output( cout );
  cout << endl;

  cout << "Enter any character and press return to terminate." << endl;
  cin >> dummy;
  return 0;
}

//with all the member functions, my goal was to make each private but not sure
//on how to do that correctly
//member function
void(double initial_balance);
{
public:
       initial_balance()
       {
           return_initial_balance;
       }
private: initial_balance;
};

//member function    
void(double maturity_balance);
{
public:
        maturity_balance()
        {
           return_maturity_balance;
        }
private: initial_balance;
};

//member function
void(double interest_rate);
{
public:
        interest_rate()
        {
            return_interest_rate;
        }
private: interest_rate;
};

//member function
void(int term);
{
public:
       term()
       {
           return_term;
       }
private: term;
};

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 7/4/09 07:03PM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month