Formatting: How do you display array elements justified to the right in output?

closed account (N1Co216C)
Hi,

I'm trying to make a program that takes unsorted numbers from a text document, displays them, puts them into an array, sorts them, and then re-displays them in descending order. For some reason, when I compile and run the program, I get an error message saying the program has stopped working. Could someone please explain what could be causing my program to not compile and run?

Here is the set of numbers in the text document:


1.23
4.56
7.89
1.4
2.58
3.69
9.5
52.34
15.98
14.7
-1.25
-9.0
41.78
36.42
6.51
87.54
-1.11
25.14
99.99
7.54
-6.5
88.19


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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  /********************************************************************
Purpose:  To write a program that will process a data set of numeric information, 
storing the information within arrays to be displayed, sorted, and displayed again. 
The program will also take data from an external data file and plug that data into the program.
*********************************************************************/

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>


using namespace std;



//Prototypes
int buildArray( double array[] );
void printArray( string reportTitle, double array[], int numberOfValues );
void sortArray( double array[], int numberOfValues );





int main()
{

ifstream inFile;


inFile.open ( "nums.txt" );
if (inFile.fail() )
{
cout<< "The nums.txt input file failed to open";
exit(-1);
}
	
	
	
	double array [50];
	int size;
	
	size = buildArray(array);
	
	printArray("Unsorted Numbers", array, size);
	sortArray(array, size);
	printArray("Sorted Numbers", array, size);

	
return 0;	
}



/***************************************************************
Function: int buildArray( double array[] )

Use: 

Arguments: 

Returns: 
***************************************************************/

int buildArray( double array[] )
{
int i = 0;
double userVal;
ifstream inFile;

inFile>>userVal;

while( userVal != -99.99)	
	{
	array[i] = 	userVal;
	i++;
	inFile>>userVal;
	}
	
	return i;
}



/***************************************************************
Function: void printArray( string reportTitle, double array[], int numberOfValues )

Use: 

Arguments: 

Returns: Nothing
***************************************************************/

void printArray( string reportTitle, double array[], int numberOfValues )
{
	cout << endl <<  reportTitle << endl;
	for( int i=0; i< numberOfValues; i++)
	{
	cout << array[i] << " ";
	}
	cout<<endl<<endl;
}



/***************************************************************
Function: void sortArray( double array[], int numberOfValues )

Use: 

Arguments: 

Returns: Nothing 
***************************************************************/

void sortArray( double array[], int numberOfValues )
{
	int minIndex, minValue;

	//outer loop responsible for moving us along the entire array
	for (int startScan=0; startScan < numberOfValues-1; startScan++)
	{
		//begins each iteration, assuming current element is the smallest remaining
    	minIndex=startScan;
    	minValue=array [startScan];    
    	//inner loop responsible for finding lowest remaining value in the array
		for (int index = startScan + 1; index < numberOfValues; index++)
		{
			//performs comparisons, looking for smallest remaining value in the array
			if(array [index] > minValue)
			{
				minValue=array[index];
				minIndex=index;
			}
	}
//performs the swap	
array[minIndex]= array [startScan];
array[startScan]=minValue;

	}
}




Here's what the resulting output is supposed to look like:


Unsorted Numbers

   1.23   4.56   7.89   1.40   2.58
   3.69   9.50  52.34  15.98  14.70
  -1.25  -9.00  41.78  36.42   6.51
  87.54  -1.11  25.14  99.99   7.54
  -6.50  88.19

Sorted Numbers

  99.99  88.19  87.54  52.34  41.78
  36.42  25.14  15.98  14.70   9.50
   7.89   7.54   6.51   4.56   3.69
   2.58   1.40   1.23  -1.11  -1.25
  -6.50  -9.00
Last edited on
You need to include the header for strings before you can use them.

#include <string>
Last edited on
closed account (N1Co216C)
I included it, but the same thing still happens.
closed account (N1Co216C)
Nvm, I figured it out. Thanks!
On line 74 your while loop is infinite with the values used in the inFile. This will cause your array in the loop to go out of bounds. If you're just trying to fill an array with values from a file then you should check to see if you are at the end of the file instead of checking for a particular value to be entered.

closed account (N1Co216C)
Yeah I just realized that and changed it. The numbers from the text doc work with the program now. However, in the output in "sorted numbers" they seem to change to integers from doubles. Any idea why?

Here's an updated version of my code:

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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*************************************************************************************************
Purpose:  To write a program that will process a data set of numeric information, 
storing the information within arrays to be displayed, sorted, and displayed again. 
The program will also take data from an external data file and plug that data into the program.
*************************************************************************************************/

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <string>


using namespace std;



//Prototypes
int buildArray( double array[] );
void printArray( string reportTitle, double array[], int numberOfValues );
void sortArray( double array[], int numberOfValues );





int main()
{


	
	
	double array [50];
	int size;
	
	size = buildArray(array);
	
	printArray("Unsorted Numbers", array, size);
	sortArray(array, size);
	printArray("Sorted Numbers", array, size);

	
return 0;	
}



/***************************************************************
Function: int buildArray( double array[] )

Use: 

Arguments: 

Returns: 
***************************************************************/

int buildArray( double array[] )
{
	
ifstream inFile;


inFile.open ( "nums.txt" );
if (inFile.fail() )
{
cout<< "The nums.txt input file failed to open";
exit(-1);
}
	
	
	
	
	
int i = 0;
double userVal;


inFile>>userVal;

while( inFile)	
	{
	array[i] = 	userVal;
	i++;
	inFile>>userVal;
	}
	
	inFile.close();
	
	return i;
}



/***************************************************************
Function: void printArray( string reportTitle, double array[], int numberOfValues )

Use: 

Arguments: 

Returns: Nothing
***************************************************************/

void printArray( string reportTitle, double array[], int numberOfValues )
{
	cout << endl <<  reportTitle << endl;
	for( int i=0; i< numberOfValues; i++)
	{
	cout << array[i] << " ";
	}
	cout<<endl<<endl;
}



/***************************************************************
Function: void sortArray( double array[], int numberOfValues )

Use: 

Arguments: 

Returns: Nothing
***************************************************************/

void sortArray( double array[], int numberOfValues )
{
	int minIndex, minValue;

	//outer loop responsible for moving us along the entire array
	for (int startScan=0; startScan < numberOfValues-1; startScan++)
	{
		//begins each iteration, assuming current element is the smallest remaining
    	minIndex=startScan;
    	minValue=array [startScan];    
    	//inner loop responsible for finding lowest remaining value in the array
		for (int index = startScan + 1; index < numberOfValues; index++)
		{
			//performs comparisons, looking for smallest remaining value in the array
			if(array [index] > minValue)
			{
				minValue=array[index];
				minIndex=index;
			}
	}
//performs the swap	
array[minIndex]= array [startScan];
array[startScan]=minValue;

	}
}


Here's what it outputs:



Unsorted Numbers
1.23 4.56 7.89 1.4 2.58 3.69 9.5 52.34 15.98 14.7 -1.25 -9 41.78 36.42 6.51 87.5
4 -1.11 25.14 99.99 7.54 -6.5 88.19


Sorted Numbers
99 88 87 52 41 36 25 15 14 9 7 7 6 4 3 2 1 1 -1 -1 -6 -9


Last edited on
L. 129:
int minIndex, minValue;
However, in the output in "sorted numbers" they seem to change to integers from doubles.

On line 129 your minValue is an int.
closed account (N1Co216C)
Thanks guys. Now I just need to get the output formatting right. How do I get my numbers aligned to the right?



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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*************************************************************************************************
Purpose:  To write a program that will process a data set of numeric information, 
storing the information within arrays to be displayed, sorted, and displayed again. 
The program will also take data from an external data file and plug that data into the program.
*************************************************************************************************/

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <string>


using namespace std;



//Prototypes
int buildArray( double array[] );
void printArray( string reportTitle, double array[], int numberOfValues );
void sortArray( double array[], int numberOfValues );





int main()
{


	
	//declare the array
	double array [50];
	int size;
	
	size = buildArray(array);
	
	//call functions
	printArray("Unsorted Numbers", array, size);
	sortArray(array, size);
	printArray("Sorted Numbers", array, size);

	
return 0;	
}



/**********************************************************************
Function: int buildArray( double array[] )

Use: to fill an array of doubles

Arguments: an array of doubles: the array that will hold the numeric 
information.

Returns: an integer that is equal to the number of values 
that were placed in the array.
**********************************************************************/

int buildArray( double array[] )
{
	
ifstream inFile;


inFile.open ( "nums.txt" );
if (inFile.fail() )
{
cout<< "The nums.txt input file failed to open";
exit(-1);
}
	
	
	
	
	
int i = 0;
double userVal;


inFile>>userVal;

while( inFile)	
	{
	array[i] = 	userVal;
	i++;
	inFile>>userVal;
	}
	
	inFile.close();
	
	return i;
}



/************************************************************************************
Function: void printArray( string reportTitle, double array[], int numberOfValues )

Use: to display the double numbers in the array five values at a time.

Arguments: a string that holds the title for the report that is being displayed, 
an array of doubles that holds the numbers to be displayed, 
and an integer that holds the number of values to be displayed.

Returns: Nothing
************************************************************************************/

void printArray( string reportTitle, double array[], int numberOfValues )
{
	cout << endl <<  reportTitle << endl<<endl;
	for( int i=0; i< numberOfValues; i++)
	{
	cout << setiosflags( ios:: fixed ) << setprecision(2) <<" "<<array[i] << "	 ";
	
	 if ((i + 1) % 5 == 0) 
	 {
     cout << endl;
     }
	
	}
	cout<<endl<<endl;
}



/********************************************************************
Function: void sortArray( double array[], int numberOfValues )

Use: Sorts the numbers in the array into descending order.

Arguments: an array of doubles that holds the numbers to be sorted, 
and an integer that holds the number of values to be sorted.

Returns: Nothing
********************************************************************/

void sortArray( double array[], int numberOfValues )
{
	int minIndex;
	double minValue;

	//outer loop responsible for moving us along the entire array
	for (int startScan=0; startScan < numberOfValues-1; startScan++)
	{
		//begins each iteration, assuming current element is the smallest remaining
    	minIndex=startScan;
    	minValue=array [startScan];    
    	//inner loop responsible for finding lowest remaining value in the array
		for (int index = startScan + 1; index < numberOfValues; index++)
		{
			//performs comparisons, looking for smallest remaining value in the array
			if(array [index] > minValue)
			{
				minValue=array[index];
				minIndex=index;
			}
	}
//performs the swap	
array[minIndex]= array [startScan];
array[startScan]=minValue;

	}
}



It currently outputs:



Unsorted Numbers

 1.23     4.56    7.89    1.40    2.58
 3.69     9.50    52.34   15.98   14.70
 -1.25    -9.00   41.78   36.42   6.51
 87.54    -1.11   25.14   99.99   7.54
 -6.50    88.19


Sorted Numbers

 99.99    88.19   87.54   52.34   41.78
 36.42    25.14   15.98   14.70   9.50
 7.89     7.54    6.51    4.56    3.69
 2.58     1.40    1.23    -1.11   -1.25
 -6.50    -9.00




But I need all the numbers aligned to the right like this:


Unsorted Numbers

   1.23   4.56   7.89   1.40   2.58
   3.69   9.50  52.34  15.98  14.70
  -1.25  -9.00  41.78  36.42   6.51
  87.54  -1.11  25.14  99.99   7.54
  -6.50  88.19

Sorted Numbers

  99.99  88.19  87.54  52.34  41.78
  36.42  25.14  15.98  14.70   9.50
   7.89   7.54   6.51   4.56   3.69
   2.58   1.40   1.23  -1.11  -1.25
  -6.50  -9.00
You can use setw(). If you have time you should look into printf() to use instead of cout though.

To use setw(), replace line 115 with this:
cout << setw(10) << fixed << setprecision(2) << array[i];

This will create a column 10 characters wide and align the text to the right by default.
closed account (N1Co216C)
Got it. Thanks so much for your help!
Topic archived. No new replies allowed.