finding the median from a list of numbers in a file

I have the hardest time figuring out how to even begin properly, im not a computer science person but my math major requires i take a intro to computer science class. Anyways, i have to write a program that takes a list of numbers in a file, and sorts them and finds the median of the list. I have to write the program from scratch and cant for the life of me figure out where to even start truly. I struggle through this class and just need to pass it because i am not a computer science person, any and all help is appreciated, this is what i have so far, but i know its all wrong, im trying to use examples from the book, but to no avail

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

using namespace std;

void createFile();
void sortNumbers(int arr[], int length);
void median();
void printInfo(void);
void getList(void);

int main()
{
   countNumbers() ;
   median() ;
}

void printInfo(void)
{
   cout << endl;
   cout << "This program takes a list of\n" ;
   cout << "numbers from a file and sorts\n" ;
   cout << "them from smallest to largest and\n" ;
   cout << "writes the median of the list.\n" ;
   cout << endl ;

void createFile();
void sortNumbers(int arr[], int length);
void median();
void printInfo(void);
void getList(void);

int main()
{
   countNumbers() ;
   median() ;
}

void printInfo(void)
{
   cout << endl;
   cout << "This program takes a list of\n" ;
   cout << "numbers from a file and sorts\n" ;
   cout << "them from smallest to largest and\n" ;
   cout << "writes the median of the list.\n" ;
   cout << endl ;
}

void getList(void)
{ 
   cout << "Enter the filename for the input:\n" ;
   cin >>  filename;
   cout << endl ;
   cout << "Enter the filename for the output:\n" ;
   cin >> ;
   cout << endl ;

void sortNumbers(int arr[], int length)
{
   for (int i = length-1; i > 0; i--)
      for (int j = 0; j < i; j++)
         if (arr[j] > arr[j+1])
         {
          int temp = arr[j+1];
          arr[j+1] = arr[j];
          arr[j] = temp;
}


I see function declarations and calls for functions that don't exist :)

Forget file handling and all that for now, the question first is do you know how to calculate median? - if not check this link: http://www.mathsisfun.com/median.html

If you already know, do it in a logical fashion...

Open the file, read the data and populate your array.
Sort the array using bubblesort.
Calculate Median.

Do it in stages, get the file opening, then move on to reading the data etc.



apparently i read the requirements wrong, i actually dont need to put the numbers in order, the file already is gonna have the numbers in order, a set of 31 numbers, what im supposed to do is just have the program read the file and spit out the 31 numbers on two separate lines, the first 16 on the first line and the second 15 on the 2nd line, i think i know how to do that, i just am not sure how to make the program actually read into the file?

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

using namespace std;

void printInfo() ;
void getList(void) ;

int main(void)
{

   printInfo() ;
   getList([40]) ;
   return 0;
}


void printInfo(void)
{
   cout << endl ;
   cout << "This program takes a list of 31 ascending numbers\n" ;
   cout << "and writes the median of the list.\n" ;
   cout << endl ;
}

void getList(void)
{
   char inputfile [256] ;
   char outputfile [256] ;

   cout << "Enter the filename for the input:\n" ;
   cin >>  inputfile ;
   cout << endl ;

   ifstream fin ;
   fin.open(inputfile);
   if (fin.fail())
   {
     cout << "FAILED TO OPEN FILE: " << inputfile << endl ;
     exit(1) ;
}

   int nmbr ;

   for (int i=0; i<40; i++)
      {
       fin >> nmbr ;
       cout << nmbr << endl ;
      }
 }

   fin.close() ;

   cout << endl ;
   cout << "Enter the filename for the output:\n" ;
   cin >> outputfile ;
   cout << endl ;

   ifstream fin ;
   fin.open(inputfile);
   if (fin.fail())
   {
     cout << "FAILED TO OPEN FILE: " << inputfile << endl ;
     exit(1) ;
   }

   int umbr ;
   cout << endl ;
   }
   fin.close() ;
   cout << endl ;
}
If you're a math major, then you must be able to think logically. Once you know the syntax of a programming language, then programming is basically applying logic to get it to do what you want.

There are quite a few problems with the code you posted. On line 13, getList([40]) ; this is an error. You said that your function doesn't accept any parameters on lines 7 and 26 (by the way, you don't need void in those parentheses). What is [40] even supposed to mean?

There is also a problem with your for loop.

45
46
47
48
49
50
   for (int i=0; i<40; i++)
      {
       fin >> nmbr ;
       cout << nmbr << endl ;
      }
 }

Why are you looping 40 times? Didn't you say that there are only 31 numbers?

The second half of your function is just a copy-paste of the first half. Why are you trying to open the inputfile again? Shouldn't it be the outputfile?
thats what a computer science major was helping me with, problem is syntax for me just doesnt click, i was told the 40 was to cover the 31, essentially it could be anything as long as it covered my parameters of 31?
Last edited on
basing off my book to the best of my knowledge for now, this is what i currently have,

Then the next thing to do would be to copy the list from the file into the array.
Im going to use getList to do that.

next thing i need to do is to call getList

"However, the function getList needs at least one parameter - maybe more. How can it get the list for the main function if the main function does not give it something in which to copy the elements of the list? You need to set up getInfo so it has an array for a parameter, so you can pass the array to the function when you call it from the main function."

confused on how to do what is asked in the quotes..

is he asking to put my array "int numberList[31] ; " into my "void getList()" such that it would look like
"void getList(int numberList[31])" ?


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

using namespace std;

const int MED_PSTN = 15 ;
void printInfo(void) ;
void getList() ;

int main(void)
{
   int numberList[31] ;
   printInfo() ;
   getList() ;
   return 0;
}


void printInfo(void)
{
   cout << endl ;
   cout << "This program takes a list of 31 ascending numbers\n" ;
   cout << "and writes the median of the list.\n" ;
   cout << endl ;
}

void getList()
{
   char inputfile[256] ;
   char outputfile[256] ;

   cout << "Enter the filename for the input:\n" ;
   cin >>  inputfile ;
   cout << endl ;

   ifstream fin ;
   fin.open(inputfile);
   if (fin.fail())
   {
     cout << "FAILED TO OPEN FILE: " << inputfile << endl ;
     exit(1) ;
   }

   int nmbr ;

   for (int i=0; i<=15; i++)
   {
     fin >> nmbr ;
     cout << nmbr[i] ;
     cout << endl ;
   }
   for (i=26; i<31; i++)
   {
     fin >> nmbr ;
     cout << nmbr[i] ;
     cout << endl ;
   }

   fin.close() ;
   cout << "The data is:\n" ;
   cout << endl ;
}
Last edited on
thats what a computer science major was helping me with, problem is syntax for me just doesnt click, i was told the 40 was to cover the 31, essentially it could be anything as long as it covered my parameters of 31?

In the case of your code, there are only 31 numbers inside the file you're reading, but you're trying to read 40. That means the input operation that was inside the loop fin >> nmbr puts the ifstream into an error state and wouldn't do anything for the last 9 iterations. Now, that doesn't really matter in this case, since you close the file after this loop anyway, but you should remember for future i/o operations.

The other problem which does matter in this case is that you output nmbr each iteration. So assuming the numbers were 1 through 31, your output would look like this:

1
2
...
30
31
31
31
31
31
31
31
31
31


confused on how to do what is asked in the quotes..

Well, you know about variables and scope right? If not, the short explanation is that anything in between brackets {} is a local scope. Any variable declared in the local scope only exists while the program is going through the code in the scope. As soon as the program leaves the scope, the variable no longer exists.

You are expected to fill an array that is declared in the main function by using the getList function. The functions have their own scope so in order for getList to access the array, it needs to be passed as a parameter.

Here's an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

void changeArray(int * a, int length)
{
    for (int i = 0; i < length; ++i)
        a[i] = i;
}

int main()
{
    int array[10] = { 0 };

    std::cout << "Before function call:\n\t";
    for (int i = 0; i < 10; ++i)
        std::cout << array[i] << ' ';
    changeArray(array, 10);
    std::cout << "\n\nAfter function call:\n\t";
    for (int i = 0; i < 10; ++i)
        std::cout << array[i] << ' ';
    return 0;
}
Before function call:
	0 0 0 0 0 0 0 0 0 0 

After function call:
	0 1 2 3 4 5 6 7 8 9  

I hope that example gives you some ideas on how you should change your code too, because as it is, your code doesn't work.
Last edited on
Topic archived. No new replies allowed.