Bubble Sorting

closed account (3wk9LyTq)
I having a bit of an issue with this bit of code below


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <vector>
using namespace std;

void bubbsort(int arr[]);

int main()
{

	string file;

	const int a = 100, b = 10, c = 10;

	int count = 0, count1 = 0, d = 0, swap = 0;

	int clam[a] = { 0 }, ray[b][c] = { 0 };

	cout << "Type name of the file: " << endl;
	cin >> file;

	ifstream data;
	data.open(file);

	vector<int> array;

	int number;

	while (data >> number);
	{
		array.push_back(number);
		count++;
		d = count;
		clam[count];
	}

	data.close();
	data.open(file);

	while (data.good())
	{
		int i;
		for (i = 0; i<d; i++)
		{
			data >> clam[i];
			cout << clam[i] << " ";
			count1++;
		}
	}
	cout << endl << "There are " << d << " integers within " << ' " ' << file << '"' << " file!" << endl;

	data.close();


	for (int k = 0; k <= count - 1; k++)
	{
		for (int l = k + 1; l <= count - 1; l++)
		{
			int temp = 0;
			if (clam[k]>clam[l]){
				temp = clam[k];
				clam[k] = clam[l];
				clam[l] = temp;
				swap++;
			}
		}
		
	}

	cout << endl << "Sorting this " << swap << " # of swaps" << endl;

	data.close();

	cout << endl;

	for (int y = 0; y<count; y++)
	{
		for (int z = 0; z <= 9; z++)
		{
			if (y != count)
			{
				cout << right << setw(4) << clam[y];
				
				y++;
			}
			else 
			{
				cout << endl;
				
				system("pause");
				return 0;
			}
		}
		y = y - 1;
		cout << endl;

	}

	system("pause");

	return 0;
}



The problem as it stands at the moment is, this program only works by typing in the name of the file.

I need to switch it to where the code just opens up the text file right off the bat and performs the bubble sort along with it.

For example of numbers within the text file: -58, 5, -11, 4, 9, -1, 13, 98 and then -58, -11, -1, 4, 5, 9, 13, 98.
You want just run -> program.exe filename , and let it does its work right ?

change main
 
int main(int argc,char *argv[])

you'll get argc as count of param(in here 2).
And argv is array of string you inputted,(i.e.,{"program.exe","filename"}).

So, you can use something like file = string(argv[1]);.

Hope this's what you want.
Last edited on
Also, just as a side note, avoid using 'system' commands. They are neither portable nor secure.
closed account (3wk9LyTq)
Not quite lsk, doing that seems to lead to more issues.

I changed this

1
2
	cout << "Type name of the file: " << endl;
	cin >> file;



to this

string file = "numberblock.txt";

the name I'm going for.

Only thing is it prints the 1st half without a hitch, but on the 2nd half only

98 shows up and nothing else. Any reason why that is?
Topic archived. No new replies allowed.