12 Replies - 671 Views - Last Post: 25 November 2008 - 04:27 PM Rate Topic: -----

#1 rmeto4u  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 10-October 08

vectors

Posted 23 November 2008 - 08:14 PM

- Im sorting numbers from an external file and then sending the output to another external file and I will be using vectors instead of arrays. The file contains nothing but numbers of type int separated by blanks or line breaks. I know you cannot assume that the number of ints in this external file is at most 240,000 because vectors are more flexible than arrays in that the number of entries may be greater.

- The input filename will be input.txt . The output will be output.txt .

- Im using Insertion Sort for this as well but Im not sure if Im putting vectors in this program correctly.

Heres the code I have so far, if I can get help correcting the program, that would be nice. Thanks you guys.



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

const vector<int> MAX = 240000;
void fill_up(int a[], int size, int& number_used);
int sort(int a[], int number_used);
int swap(int one, int two);

int main()
{

	  using namespace std;
	  
	  ifstream in_stream;
	  ofstream out_stream;
	 
	  int number[MAX], number_used;
	  fill_up(number, MAX, number_used);
	  sort(number, number_used);
	 
	  in_stream.close( );
	  out_stream.close( );

	  char dummy;
	  cout << "Enter any key to quit." << endl;
	  cin >> dummy;

	  return 0;
	  
}

void fill_up(int a[], int size, int& number_used)

{
	 using namespace std;
	 int input, index(0);
	 ifstream in_stream;
	 ofstream out_stream; 
	 
	 in_stream.open("input.txt");
	 out_stream.open("output.txt");
	 
	 in_stream >> input;

	 while (!in_stream.eof())
{
	 a[index] = input;
	 index++;
}

	 index = number_used;
}
	 int sort(int a[], int number_used)
{
	 using namespace std;

	 ifstream in_stream;
	 ofstream out_stream;
	 
	 int value_one(0), value_two(1);

	 while (!in_stream.eof())
{
	 int one, two;

	 if(a[value_one] > a[value_two])
{
	 a[value_one] = one;
	 a[value_two] = two;
	 
	 swap(one, two);
	 
	 out_stream << a[value_one];
	 out_stream << a[value_two];

}
	 value_one++;
	 value_two++;
}
	 return(a[number_used]);
}
int swap(int one, int two)
{
	using namespace std;

	int temp;
	one = temp;
	two = one;
	temp = two;
	return (one, two);
}

This post has been edited by rmeto4u: 23 November 2008 - 08:14 PM


Is This A Good Question/Topic? 0
  • +

Replies To: vectors

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: vectors

Posted 23 November 2008 - 09:05 PM

What problem(s) are you experiencing?
Was This Post Helpful? 0
  • +
  • -

#3 rmeto4u  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 10-October 08

Re: vectors

Posted 23 November 2008 - 09:13 PM

Im trying to add vectors instead of using arrays but Im having trouble. just need help correcting program and placing vectors where they should be.
Was This Post Helpful? 0
  • +
  • -

#4 rmeto4u  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 10-October 08

Re: vectors

Posted 24 November 2008 - 08:13 AM

Im sorting numbers from an external file and then sending the output to another external file and I will be using vectors instead of arrays. The file contains nothing but numbers of type int separated by blanks or line breaks. I know you cannot assume that the number of ints in this external file is at most 240,000 because vectors are more flexible than arrays in that the number of entries may be greater.

- Im trying to add vectors instead of using arrays.

- The input filename will be input.txt . The output will be output.txt .

- Im using Insertion Sort for this as well but Im not sure if Im putting vectors in this program correctly.

Heres the code I have so far, if I can get help correcting the program, that would be nice. Thanks you guys.



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

const vector<int> MAX = 240000;
void fill_up(int a[], int size, int& number_used);
int sort(int a[], int number_used);
int swap(int one, int two);

int main()
{

	  using namespace std;
	  
	  ifstream in_stream;
	  ofstream out_stream;
	 
	  int number[MAX], number_used;
	  fill_up(number, MAX, number_used);
	  sort(number, number_used);
	 
	  in_stream.close( );
	  out_stream.close( );

	  char dummy;
	  cout << "Enter any key to quit." << endl;
	  cin >> dummy;

	  return 0;
	  
}

void fill_up(int a[], int size, int& number_used)

{
	 using namespace std;
	 int input, index(0);
	 ifstream in_stream;
	 ofstream out_stream; 
	 
	 in_stream.open("input.txt");
	 out_stream.open("output.txt");
	 
	 in_stream >> input;

	 while (!in_stream.eof())
{
	 a[index] = input;
	 index++;
}

	 index = number_used;
}
	 int sort(int a[], int number_used)
{
	 using namespace std;

	 ifstream in_stream;
	 ofstream out_stream;
	 
	 int value_one(0), value_two(1);

	 while (!in_stream.eof())
{
	 int one, two;

	 if(a[value_one] > a[value_two])
{
	 a[value_one] = one;
	 a[value_two] = two;
	 
	 swap(one, two);
	 
	 out_stream << a[value_one];
	 out_stream << a[value_two];

}
	 value_one++;
	 value_two++;
}
	 return(a[number_used]);
}
int swap(int one, int two)
{
	using namespace std;

	int temp;
	one = temp;
	two = one;
	temp = two;
	return (one, two);
}

Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,663
  • Joined: 23-August 08

Re: vectors

Posted 24 November 2008 - 09:17 AM

Where are your vectors? I only see arrays.
Was This Post Helpful? 0
  • +
  • -

#6 rmeto4u  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 10-October 08

Re: vectors

Posted 24 November 2008 - 10:29 AM

thats what I dont get how to add. can anyone help me start?
Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is online

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3120
  • View blog
  • Posts: 19,166
  • Joined: 14-September 07

Re: vectors

Posted 24 November 2008 - 11:01 AM

First why do you have a constant vector of ints at the top? I'm guessing that should just be one int.

const int MAX = 240000; //large number, using a lot of memory

//code...


//Now, we want to substitute vectors for where you currently have arrays. 
//int number[MAX] will become

vector<int> number; //no need to specify size at this point, vectors resize themselves

//change your function parameters to vectors
void fill(vector<int> num, int& number_used);
//etc...
//Size does not need to be passed, unless you want to
//the best part about STL containers is their flexibility. 
//It is up to the programmer on how to implement

//Now for the most part, the code should still function since you can call vector values like array values.
//Experiment and post specific questions if problems arise


Was This Post Helpful? 0
  • +
  • -

#8 rmeto4u  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 10-October 08

Re: vectors

Posted 24 November 2008 - 03:36 PM

I cant seem to get the use of vectors correct in this program. Need some help. I get a lot of error messages, one being vector isnt declared, but I think I do declare it.

Heres the code:

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

const int MAX = 240000;
int fill_up(vector<int> number, int size, int& number_used);
int sort(vector<int>, int number_used);
int swap(vector<int> one, int two);

int main()
{

	  using namespace std;
	  
	  ifstream in_stream;
	  ofstream out_stream;
	 
	  vector<int> number, number_used;
	  fill_up(number, number_used);
	  sort(number, number_used);
	 
	  in_stream.close( );
	  out_stream.close( );

	  char dummy;
	  cout << "Enter any key to quit." << endl;
	  cin >> dummy;

	  return 0;
	  
}

void fill_up(vector<int> number, int size, int& number_used)

{
	 using namespace std;
	 int input, index(0);
	 ifstream in_stream;
	 ofstream out_stream; 
	 
	 in_stream.open("input.txt");
	 out_stream.open("output.txt");
	 
	 in_stream >> input;

	 while (!in_stream.eof())
{
	 a[index] = input;
	 index++;
}

	 index = number_used;
}
	 int sort(vector<int> number, int number_used)
{
	 using namespace std;

	 ifstream in_stream;
	 ofstream out_stream;
	 
	 int value_one(0), value_two(1);

	 while (!in_stream.eof())
{
	 int one, two;

	 if(a[value_one] > a[value_two])
{
	 a[value_one] = one;
	 a[value_two] = two;
	 
	 swap(one, two);
	 
	 out_stream << vector<int>[value_one];
	 out_stream << vector<int>[value_two];

}
	 value_one++;
	 value_two++;
}
	 return(vector<int>[number_used]);
}
int swap(int one, int two)
{
	using namespace std;

	int temp;
	one = temp;
	two = one;
	temp = two;
	return (one, two);
}

Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,663
  • Joined: 23-August 08

Re: vectors

Posted 24 November 2008 - 03:41 PM

Try adding using namespace std; after the #includes.
Was This Post Helpful? 0
  • +
  • -

#10 rmeto4u  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 10-October 08

Re: vectors

Posted 24 November 2008 - 03:58 PM

yea, Im still having trouble. after inserting that i get even more errors.
Was This Post Helpful? 0
  • +
  • -

#11 KYA  Icon User is online

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3120
  • View blog
  • Posts: 19,166
  • Joined: 14-September 07

Re: vectors

Posted 24 November 2008 - 04:56 PM

And by not being specific with what is going on at what line in what manner, you give the people who could help no information. It's like looking for a black cat in a dark room that doesn't exist.
Was This Post Helpful? 0
  • +
  • -

#12 rmeto4u  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 10-October 08

Re: vectors

Posted 25 November 2008 - 03:50 PM

I get a ton of errors. if you have a compiler, try compiling this program and see what im talkin about. its really too many errors to say. if any one can help me run this using vectors which is what i have in the program, i need help correcting it so it can function properly. if anyone can help that would be great.

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

const int MAX = 240000;
int fill_up(vector<int> number, int size, int& number_used);
int sort(vector<int>, int number_used);
int swap(vector<int> one, int two);

int main()
{

	  using namespace std;
	  
	  ifstream in_stream;
	  ofstream out_stream;
	 
	  vector<int> number, number_used;
	  fill_up(number, number_used);
	  sort(number, number_used);
	 
	  in_stream.close( );
	  out_stream.close( );

	  char dummy;
	  cout << "Enter any key to quit." << endl;
	  cin >> dummy;

	  return 0;
	  
}

void fill_up(vector<int> number, int size, int& number_used)

{
	 using namespace std;
	 int input, index(0);
	 ifstream in_stream;
	 ofstream out_stream; 
	 
	 in_stream.open("input.txt");
	 out_stream.open("output.txt");
	 
	 in_stream >> input;

	 while (!in_stream.eof())
{
	 a[index] = input;
	 index++;
}

	 index = number_used;
}
	 int sort(vector<int> number, int number_used)
{
	 using namespace std;

	 ifstream in_stream;
	 ofstream out_stream;
	 
	 int value_one(0), value_two(1);

	 while (!in_stream.eof())
{
	 int one, two;

	 if(a[value_one] > a[value_two])
{
	 a[value_one] = one;
	 a[value_two] = two;
	 
	 swap(one, two);
	 
	 out_stream << vector<int>[value_one];
	 out_stream << vector<int>[value_two];

}
	 value_one++;
	 value_two++;
}
	 return(vector<int>[number_used]);
}
int swap(int one, int two)
{
	using namespace std;

	int temp;
	one = temp;
	two = one;
	temp = two;
	return (one, two);
}

This post has been edited by rmeto4u: 25 November 2008 - 05:15 PM

Was This Post Helpful? 0
  • +
  • -

#13 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,663
  • Joined: 23-August 08

Re: vectors

Posted 25 November 2008 - 04:27 PM

I started to look at this, then I realized it's just a bunch of random copy and paste without any sort of understanding. I don't think it's to your benefit to help you. I think you need to crack the books, so to speak, and learn about vectors.

Look, DIC has a nice tutorial on vectors!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1