Load, sort, and average

Hi,
I am currently trying to create a program where I input a file called "exp.txt" that contains two columns: 1) date 2) expenses. There are 100 rows. I'd like for the program to read and load the data into an array, and then sort the expenses in ascending order and produce the average as specified by the user. In the very end, this should all be printed on the a separate output file called "final.txt". I'm having trouble writing the part where the user specifies the beginning and end date and that points to the array. Do I use the void mean() function? Thanks for your help!



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

using namespace std;

void bubbleSort (int[], const int);
void mean (int[], int);

int main()
{
  const int arraySize = 100;
  int a[arraySize];
  int i, hold;
  char date [10];
  double price;

  ifstream inFile ("spx.txt"); // opens the input file
  ofstream outFile ("final.txt");
 
  if (!inFile) // if it is not able to open the file
    {
      cout << "Unable to open file" << endl;
      exit (1);
    }

  while (date >> price)  
  {
    for (i=0; i < arraySize; i++)  // loop
      cout << setw (10) << a[i];
  }

  bubbleSort (a, arraySize); // sorting the array

  for (i=0; i < arraySize; i++)
    cout << setw (10) << a[i];

  cout << endl;

  outFile.open ("final.txt");
  outFile.close();

  return 0;
}

void bubbleSort (int a[], int size)
{
  int hold;
  for (int pass = 0;  pass <size -1; pass++) // passes
    for (int i=0; i < size - 1; i++) // one pass
      if (a[i] > a [i+1]) // then compare
      {
	hold = a[i];
	a[i]= a[i+1];
	a[i+1]= hold;
      }
}
Last edited on
[code] [/code] blocks please!

ifstream inFile ("spx.txt", ios::in); doesn't actually read the file. It just opens it. You need to explicitly read your data in.

From your comments, it sounds like the assignment is to read a file containing DATE-EXPENSE pairs and find the average EXPENSE between two specific DATEs. Is that right?
Hi.
I'm sorry - but what do you mean by " blocks please!"?

Yes - you have got that right. I'm very new at this - thanks for your patience. Also I made some changes. However, when I compile it, I get no errors. But when I run it, there are a bunch of numbers that just goes crazy. Please help!

Thanks!



Last edited on
He means edit your post, select your code, then click on the # symbol under format to put code tags around it so it looks like this:

1
2
3
4
if(true) {
   //code
}
//blah 


And anyway, for your new error, without code I can only really suggest you set a breakpoint (on VC++ you can click to the left of the line, a red dot will appear) and step through your code to find where the problem is.
Last edited on
I don't have VC++. Im using cygwin and emacs, so it's a bit more difficult.
Ah, so if your compiler/debugger are not integrated, then you probably can't set a breakpoint...you would have to give us your changed code so we can try to find the problem.
I modified it above. THanks for your help!!! I'm really stuck in a rut!
Last edited on
i think in line 27 you want
while( date > price)
not while (date >> price)

If someone could post the reason, I'd appreciate it. I'm currently learning C++ from a C background where that is bit shifting but I believe it means something different in C++.

edit: actually I'm confused as to what that while loop is doing. You never change date or price so that's an infinite loop unless I'm missing something.
Last edited on
Nope. C++'s >> and << have the same meaning they had in C.
For integral values, anyway.

(Maybe he wants to loop while date is much greater than price?)
Indeed. What is that loop doing?

outFile is being opened twice. As a matter of fact, what are the files there for?
Last edited on
Topic archived. No new replies allowed.