read file into array & sorting array issue

The code below is read file into array and i had attached the txt file link below. i wonder if this code is valid or not although it can run successfully. also, i have no idea how to sort value from read file? any solution?

[preview]data.txt = https://www.scribd.com/doc/265606003/data

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
#include <fstream>
#include <iostream>

using namespace std;

int main()
{ 
	int arraySize = 9999, arrayPosition = 0;
	char * array = new char[arraySize];

	ifstream fin("data.txt"); 

	if (fin.is_open())
	{
		cout << "Successfully Opened.";
		while (!fin.eof() && arrayPosition < arraySize)
		{
			fin.get(array[arrayPosition]);
			arrayPosition++;
		}
		array[arrayPosition - 1] = '\0';

		cout << endl << endl << "Unsorted Numbers: " << endl;
		for (int i = 0; array[i] != '\0'; i++)
		{
			cout << array[i];
		}
	}
	else
	{
		cout << "ERROR." << endl;
	}

	system("PAUSE");
	return 0;
}
Why use a char array over an std::string and don't use naked news.
what do u mean exactly? im quite confusing though.
You're using a massive char array, which seems pointless when you have access to std::string which is dynamically sized and has far more versatility. Using pointers to new variables is bad because you end up with memory leaks so std::unique_ptr and std::shared_ptr are very useful.
i wish i could know unique_ptr or shared_ptr. there are no such things in my current syllabus :(
In all programming, google is always your friend. Also the documentation on this website is quite extensive.
You're not being clear at all as to what you want your code to do.
Are you trying to read numbers into an array and sort them in ascending order?
Als
yes. im trying to read numbers into an array and sort them in ascending order.
Alright cool.
I don't think you need to do anything with a character array.
Once you've initialized an array and read in the integers; you've given them a position in the array. So then you can use a logical condition to check the integers in each position and see if they're smaller than the previous integer. As in if (small > array[i]) then small = array[i]. I'll implement it fully in a code so you can see what I mean.

Alright so check this out. The data file you posted consisted of numbers with decimal spaces. An integer array can only read in whole numbers; I think that's why you included the get function to get the whole line, but you can just change the array to a double and it will store all the values.

I'm gonna post something but it's going to look different then what you have because my syntax style is different.

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
#include <fstream>
#include <iostream>
using namespace std;

const int MAX=9999; // create a constant value for 9999

int main()
{ 
double numbers[MAX]; // create a array of doubles at max size
int arraysize=0; // this will be our array counter
ifstream fin;

fin.open("data.txt"); // I deleted the if statement and just open the file

fin >> numbers[arraysize]; // reads in the first line
while (fin) // you can also do a eof loop with just the input variable
{
arraysize++; // counts how many numbers are in the file;
fin >> numbers[arraysize]  // reads in the next lines
}

Here is the sorting strategy that you need to implement:
Bubblesort sorting strategy
 decide on desired sorting order (ascending or descending)
 perform the following
1) move through the list comparing adjacent values
2) if adjacent values are out of desired order, interchange them
3) when a complete pass through the list has been made, the smallest
(largest) value will have "bubbled" to its proper location
4) repeat steps 1 and 3 ignoring the already sorted values (requires length-1
passes through the list) 

// Sort an array of integers into descending order.
// Parameters:
// numbers – array of integers to be sorted
// arraysize - (integer) number of values in the array
// Value passed back: sorted list
{
 double temp; //place holder when values are interchanged
 for (int i=0; i < arraysize-1; i++)
 for (int j=0; j < arraysize-(i+1); j++)
 if (numbers[j] < double[j+1])
 {
 temp = numbers[j];
 numbers[j] = numbers[j+1];
 numbers[j+1] = temp;
 }
 
	system("PAUSE");
	return 0;
}
i think the code is work now? without sorting

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
#include <fstream>
#include <iostream>
using namespace std;

const int MAX = 9999; // create a constant value for 9999

int main()
{
	double numbers[MAX]; // create a array of doubles at max size
	int arraysize = 0; // this will be our array counter
	ifstream fin;

	fin.open("data.txt"); // I deleted the if statement and just open the file

	fin >> numbers[arraysize]; // reads in the first line
	while (fin) // you can also do a eof loop with just the input variable
	{
		arraysize++; // counts how many numbers are in the file;
		fin >> numbers[arraysize];  // reads in the next lines
	}
	numbers[arraysize] = '\0';

	cout << "Unsorted Numbers: " << endl;
	for (int i = 0; numbers[i] != '\0'; i++)
	{
		cout << endl << numbers[i];
	}
	cout << endl;

	double temp; //place holder when values are interchanged
	for (int i = 0; i < arraysize - 1; i++)
		for (int j = 0; j < arraysize - (i + 1); j++)
			if (numbers[j] < numbers[j + 1])
			{
				temp = numbers[j];
				numbers[j] = numbers[j + 1];
				numbers[j + 1] = temp;
			}

	system("PAUSE");
	return 0;
}
Last edited on
Ye that will work but it orders them from greatest to highest i.e. descending order.
There's only 1 thing you need to change to order from highest to greatest.
And then include a cout of the sorted list.

1
2
3
4
5
cout << "Sorted Numbers:" << endl;
	for (int i = 0; i < arraysize; i++)
	{
	    cout << numbers[i] << endl;
	}


Here's what I got when I ran the code:
Sample input:
20.2
10.5
30.11
40.99
2
1
99
8232
9281
123


Output:
Unsorted Numbers:                                                                                                                                                               
20.2                                                                                                                                                                            
10.5                                                                                                                                                                            
30.11                                                                                                                                                                           
40.99           
2                                                                                                                                                                               
1                                                                                                                                                                               
99                                                                                                                                                                              
8232                                                                                                                                                                            
9281

Sorted Numbers:                                                                                                                                                                 
9281                                                                                                                                                                            
8232                                                                                                                                                                            
123                                                                                                                                                                             
99                                                                                                                                                                              
40.99                                                                                                                                                                           
30.11                                                                                                                                                                           
20.2       
10.5                                                                                                                                                                            
2                                                                                                                                                                               
1  


Do you understand everything that's happening though?
Also on line 26, move the "<< endl" after "<< numbers[i] "
Last edited on
Yes i understand now.

(*just curious) how if write the sorted numbers into a binary file (get the name of binary file from the user)? i knew write, but i dont know how to get the name of binary from user.
Last edited on
Ye it's fairly simple to write to an output file.
So you already have the <fstream> header.
<fstream> – header file that must be included in a program that requires file I/O
In there you have two data types.
ifstream – data type for variables that represent input files
ofstream– data type for variables that represent output files

So just like how you assigned "fin" to ifstream.
You can create a variable called "fout" to ofstream.
Like so:
ofstream fout;

If you already know the data file you want to write in, you would use the same snytax such as:
1
2
3
fout.open("output file.txt")
fout << "I can write here" << endl;
fout.close();


If you want to prompt the user for the input or output file, then you would create a string variable that represents the name of the file.
Here's an example for prompting a user for an input file.

1
2
3
4
5
6
7
8
9
10
11
ifstream infile; //infile represents the input file
string fname; //actual name of file
//prompt for name of input file to be opened
cout << "Please enter name of input file" << endl;
cin >> fname;
//read name of file to open
//open input file for reading
infile.open(fname.c_str());
/* c_str() is a function that converts a string variable into a null-
terminated array of characters, which is
the type of parameter the open function expects */


For your particular program do this:
create the output file by using ofstream
create a string variable for the name of the file
prompt the user for the output file
read in the name of the file into your string variable
open the file
write to the file
close the file.
Last edited on
seems not working for me.

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
#include <fstream>
#include <string>
#include <iostream>
using namespace std;

const int MAX = 9999; // create a constant value for 9999

int main()
{
	double numbers[MAX]; // create a array of doubles at max size
	int arraysize = 0; // this will be our array counter
	ifstream fin;
	ofstream fout;
	string fname;

	fin.open("data.txt"); // I deleted the if statement and just open the file

	fin >> numbers[arraysize]; // reads in the first line
	while (fin) // you can also do a eof loop with just the input variable
	{
		arraysize++; // counts how many numbers are in the file;
		fin >> numbers[arraysize];  // reads in the next lines
	}
	numbers[arraysize] = '\0';

	cout << "Unsorted Numbers: " << endl << endl;
	for (int i = 0; numbers[i] != '\0'; i++)
	{
		cout << numbers[i] << endl;
	}

	double temp; //place holder when values are interchanged
	for (int i = 0; i < arraysize - 1; i++)
		for (int j = 0; j < arraysize - (i + 1); j++)
			if (numbers[j] < numbers[j + 1])
			{
				temp = numbers[j];
				numbers[j] = numbers[j + 1];
				numbers[j + 1] = temp;
			}

	cout << endl << "Sorted Numbers:" << endl << endl;
	for (int i = 0; i < arraysize; i++)
	{
		cout << numbers[i] << endl;

	}

	for (int i = 0; i < arraysize; i++)
	{
		cout << "Please enter name for binary file: ";
		cin >> fname;
		fout.open(fname.c_str());
		fout << numbers[i] << endl;
		fout.close();
	}

	system("PAUSE");
	return 0;
}
You need to include the prompt and read in for the output file outside of your for loop.
It should look like this (starting at line 49):
1
2
3
4
5
6
7
8
cout << "Please enter name for binary file: ";
cin >> fname;
fout.open(fname.c_str());
	for (int i = 0; i < arraysize; i++)
	{
		fout << numbers[i] << endl;
	}
		fout.close();
noted. thank you so much sir. i had learnt so much from u :D
Topic archived. No new replies allowed.