Text wont show up in array loop

closed account (N1Co216C)
Hello, I have a program that I am writing that is supposed to use an array and functions to take numbers entered by the user, display them, arrange them in descending order, and then display them again. However, for some reason I can't get the text labeling the unsorted and sorted numbers to show up, and if I take them out of the function input and enter them somewhere else in my main function as cout statements, then the program fails to compile. My program is supposed to look something like this once it compiles and runs. Could someone please help explain why the text headers "unsorted" and "sorted numbers" won't show up?


Enter a number number (-99.99 to quit): 1.23
Enter another number: 4.56
Enter another number: 7.89
Enter another number: 2.5
Enter another number: 4.8
Enter another number: 9.32
Enter another number: -99.99

Unsorted Numbers
 1.23  4.56  7.89  2.50  4.80
 9.32

Sorted Numbers
 9.32  7.89  4.80  4.56  2.50
 1.23







Here is the code I have written:


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
  /*******************************************************************************

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>


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[] )
{
int i = 0;
double userVal;

cout<< "Enter a number  (-99.99 to quit): ";

cin>> userVal;

while( userVal != -99.99)	
	{
	array[i] = 	userVal;
	i++;
	cout<< "Enter another number  (-99.99 to quit): ";
	cin>> userVal;
	}
	
	return i;
}



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

Use: 

Arguments: 

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

void printArray( string reportTitle, double array[], int numberOfValues )
{
for( int i=0; i< numberOfValues; i++)
	{
	cout<< array[i] << 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;

	}
}



Last edited on
You do send the string in the printArray function, But you never actually print it out. Change the function so it looks like this -

1
2
3
4
5
6
7
8
9
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;
}


Edit: Entering -99.99 to exit anything ever should be a crime. Make it easier :p
Last edited on
closed account (N1Co216C)
That worked! Thanks!
Topic archived. No new replies allowed.