Need help with bubble sort program--how to change it to take from a text file?

I am new to programming and for a class project, we are to create a bubble sort program. This program works right now by asking the user to enter in the number of integers and then the integers themselves. I am wondering how I can change this so that the program just accepts text files instead? For example, the user would enter at the terminal
"./bubblesort 10 < text.txt" meaning you'd want to read the first ten numbers in whatever text file??
Thanks!

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

int main(int argc, char *argv[])
{
	int count = 1000000;
	if(argc==2)
	count = atoi(argv[1]);

	int array[100000], n, i, j, swap;
	


	
	for (i=0; i < (n-1); i++)
	{
	for (j=0; j < n - i - 1; j++)
	{
	if(array[j] > array[j+1])
	{
	swap = array[j];
	array[j] = array[j+1];
	array[j+1] = swap;
	}
	}
	}
	
	printf("sorted list in ascending order:\n");
	
	for ( i=0; i < n; i++)
	printf("%d\n", array[i]);
	

Last edited on
./bubblesort 10 < text.txt would redirect std::cin to read from text.txt, so you would just read from std::cin like normal.

Remember that if your program is given that second argument, then you shouldn't display any prompts or ask for the number of numbers (you already have it).

EDIT: Is this supposed to be in C? If so, you must specify that - this is a C++ forum. Either way, not much changes here.
Last edited on
Topic archived. No new replies allowed.