File and Sorting

Hey...I am trying to do this but I need help. I tried my program but the errors don't seem to go away. Below is the question I have to do and following that is my code for the first part of the question.

P.S I just don't want it done for me I want to learn so, a explanation would be nice.

Thanks,
srk


Write a program that reads salaries from a file, sorts them, determines and outputs the median salary. The median should be calculated and returned by the function getMedian that calls the selectionSort function. The median value is the middle value when an odd number of values are sorted, and the mean of the two middle values when an even number of values are sorted.

Consider the following salaries: 75000, 45000, 65000, 100000, 50000.
When sorted, the numbers are 45000, 50000, 65000, 75000, 100000.
The median is 65000 because two values are lower and two values are higher.

Now consider the following salaries: 75000, 45000, 65000, 100000, 50000, 80000.
When sorted, the numbers are 45000, 50000, 65000, 75000, 80000, 100000.
Since we have an even number of salaries (six), no salary has the same number of higher values and lower values. We therefore take the mean of the two middle values as the median. The two middle values are 65000 and 75000. The median is (65000+75000)/2 = 70000. Note that even if all the values are integers the median may not be.

The prototype for the median function should be similar to:
double getMedian(int array[], int);

The median function should call the selection sort function
void selectionSort (int array[], int size)






#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;

void selectionSort (int Array[index], int S);

int main()
{

{
ofstream myfile ("salary.text");

const int S = 5;
int Array[index] = {75000, 45000, 65000, 100000, 50000};
myfile.close();

selectionSort (Array[index], S);

}



void selectionSort (int Array[index], int S)
{
ifstream myfile ("salary.text");
{
sort (Array, Array + S );

cout << "Sorted Array" << endl;

for (size_t i=0; i != S; ++i)
cout<<Array<< "\n";
}

myfile.close();
}

return 0;

}



I still need help. My updated code is below for the full question.


#include <iostream>
#include <fstream>
using namespace std;

void selectionSort (int A[], int S);
double getMedian(int A[], int S);

int main()
{
ofstream myfile ("salary.text");

const int Size = 5;
int Array[] = {75000, 45000, 65000, 100000, 50000};
myfile.close();

selectionSort (Array[], Size);

for (int i=0;i<Size; i++)
{
cout << "Sorted Emements: " << sortedelements << " ";
}

getMedian(Array[], Size);

}



void selectionSort (int A[], int S)
{
ifstream myfile ("salary.text");
{

for (int i=0; i<(S-1); i++)
{
int minelement= A[i];
int index= i;

for (int j=(i=1); j<S, j++)
{
if (minelement>A[j])

minelement= A[j];
int index=j;
}

int sortedelements = A[i];


}
myfile.close();
}




double getMedian(int A[], int S)
{

for (int i=0; i<(S-1);i++)
{
if (S%2==0)
{
i=S/2;
int mediian= (A[i] + A[i+1])/2;

cout<<"Medain: "<<median<< endl;
}

else (S%2!=0)
{
i=S/2;
int median= A[i];

cout<<"Medain: "<<median<< endl;
}

}

return 0;
}
What is the problem ? Wrong input, output, crash, compile error ?
compile error.
You came so close to saying something useful. If only you had told us what the error was.
16: expected expression|

20: use of undeclared identifier 'sortedelements'|

23: expected expression|

39: expected ';' in 'for' statement specifier|

58: function definition is not allowed here|

81: expected '}'|
Code put within tags and indentation added.

function definition is not allowed here: There are mismatched brackets and it looks like the functions are being declared within the main function instead of outside it.

use of undeclared identifier 'sortedelements' : The main function doesn't know what this variable is.

expected ';' in 'for' statement specifier|: There's a , in a for statement that should be a ;


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

void selectionSort (int A[], int S);
double getMedian(int A[], int S);

int main()
{
    ofstream myfile ("salary.text");
    const int Size = 5;
    int Array[] = {75000, 45000, 65000, 100000, 50000};
    myfile.close();

    selectionSort (Array[], Size);

    for (int i=0;i<Size; i++)
    {
        cout << "Sorted Emements: " << sortedelements << " ";
    }

    getMedian(Array[], Size);
}

void selectionSort (int A[], int S)
{
    ifstream myfile ("salary.text");
    {
        for (int i=0; i<(S-1); i++)
        {
            int minelement= A[i];
            int index= i;

            for (int j=(i=1); j<S, j++)
            {
                if (minelement>A[j])
                    minelement= A[j];
                int index=j;
            }

            int sortedelements = A[i];
        }
        myfile.close();
    }

double getMedian(int A[], int S)
{
    for (int i=0; i<(S-1);i++)
    {
        if (S%2==0)
        {
            i=S/2;
            int mediian= (A[i] + A[i+1])/2;
            cout<<"Medain: "<<median<< endl;
         }
        else (S%2!=0)
        {
            i=S/2;
            int median= A[i];
            cout<<"Medain: "<<median<< endl;
        }
    }

return 0;
Thank You for the reply however, I am still getting....

15: expected expression|
17: expected expression|


Updated code:

#include <iostream>
#include <fstream>
using namespace std;

void selectionSort (int A[], int S);
double getMedian(int A[], int S);

int main()
{
ofstream myfile ("salary.text");
const int Size = 5;
int Array[] = {75000, 45000, 65000, 100000, 50000};
myfile.close();

selectionSort (Array[], Size);

getMedian(Array[], Size);
}

void selectionSort (int A[], int S)
{
ifstream myfile ("salary.text");
{
for (int i=0; i<(S-1); i++)
{
int minelement= A[i];
int index= i;

for (int j=(i=1); j<S; j++)
{
if (minelement>A[j])
minelement= A[j];
int index=j;
}

int sortedelements = A[i];

for (int i=0;i<S; i++)
{
cout << "Sorted Emements: " << sortedelements << " ";
}
}
myfile.close();
}
}

double getMedian(int A[], int S)
{
for (int i=0; i<(S-1);i++)
{
if (S%2==0)
{
i=S/2;
int median= (A[i] + A[i+1])/2;
cout<<"Medain: "<<median<< endl;
}
if (S%2!=0)
{
i=S/2;
int median= A[i];
cout<<"Medain: "<<median<< endl;
}
}

return 0;
}






When you send an entire array in a function call, just use the name of the array without any brackets.
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
#include <iostream>
#include <fstream>
using namespace std;

void selectionSort (int A[], int S);
double getMedian(int A[], int S);

int main()
{
ofstream myfile ("salary.text");
const int Size = 5;
int Array[] = {75000, 45000, 65000, 100000, 50000};


selectionSort (Array, Size);

getMedian(Array, Size);
}

void selectionSort (int A[], int S)
{
ifstream myfile ("salary.text");
{
for (int i=0; i<(S-1); i++)
{
int minelement= A[i];
int index= i;

for (int j=(i=1); j<S; j++)
{
if (minelement>A[j])
minelement= A[j];
int index=j;
}

int sortedelements = A[i];

for (int i=0;i<S; i++)
{
cout << "Sorted Emements: " << sortedelements << " ";
}
}
myfile.close();
}
}

double getMedian(int A[], int S)
{
for (int i=0; i<(S-1);i++)
{
if (S%2==0)
{
i=S/2;
int median= (A[i] + A[i+1])/2;
cout<<"Medain: "<<median<< endl;
}
if (S%2!=0)
{
i=S/2;
int median= A[i];
cout<<"Medain: "<<median<< endl;
}
}

return Medain;
}
Last edited on
question #2

1
2
3
4
5
6
7
8
9
10
11
12
13
# include <iostream>
#include <fstream>

int main()

{

std::fstream myfile ("code");

  for (int ccode; myfile >> ccode; )
    myfile << static_cast<char>(ccode);
}
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
#include <iostream>
#include <fstream>
using namespace std;

void selectionSort (int A[], int S);
double getMedian(int A[], int S);

int main()
{
ofstream myfile;
const int Size = 5;
int Array[] = {75000, 45000, 65000, 100000, 50000};


selectionSort (Array, Size);

getMedian(Array, Size);
}

void selectionSort (int A[], int S)
{
 ifstream myfile;
 {
   for (int i=0; i<(S-1); i++)
   {
     int minelement= A[i];
     int index= i;

    for (int j=(i=1); j<S; j++)
    {
      if (minelement>A[j])
      minelement= A[j];
      int index=j;
    }

    int sortedelements = A[i];

    for (int i=0;i<S; i++)
    {
     cout << "Sorted Emements: " << sortedelements << " ";
    }
  } 
  myfile.close();
 }
}

double getMedian(int A[], int S)
{
 for (int i=0; i<(S-1);i++)

  {
   if (S%2==0)
   {
    i=S/2;
    int median= (A[i] + A[i+1])/2;
    cout<<"Medain: "<<median<< endl;
   }
   if (S%2!=0)
   {
    i=S/2;
    int median= A[i];
    cout<<"Medain: "<<median<< endl;
   }
  }

return Medain;
}
Last edited on
/Users/iamvs405/Desktop
Ok here is a cleaner version, partially fixed, got rid of the unnecessary file commands, and gave you somewhere to input 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
74
75
76
77
78
#include <iostream>
#include <fstream>
using namespace std;

void selectionSort (int A[], int S);
double getMedian(int A[], int S);

int main()
{
int Size = 100;
int Array[];
int numRead, salary;
myfile.open("fileDirectoryInputHere");

if(!myfile.open())
  {
      while(myfile >> salary)
      {
          if(numRead<Size)
          {
              Array[numRead] = salary;
              numRead++;
          }
          else
          {
              cout<< "error"<<endl;
              break;
          }
      }
      myfile.close();
  }
  else
  cout<< "cannot open file"<< endl;

getMedian(Array, Size);
}

void selectionSort (int A[], int S)
{
   for (int i=0; i<(S-1); i++)
   {
     int minelement= A[i];
     int index= i;

      for (int j=(i=1); j<S; j++)
        {
           if (minelement>A[j])
           minelement= A[j];
           int index=j;
        }

    int sortedelements = A[i];

    for (int i=0;i<S; i++)
       {
          cout << "Sorted Emements: " << sortedelements << " ";
       }
  }
}

double getMedian(int A[], int S)
{
    selectionSort (Array, Size);
 for (int i=0; i<(S-1);i++)

  {
   if (S%2==0)
   {
    int median= (A[S/2] + A[S/2+1])/2;
   }
   if (S%2!=0)
   {
    int median= A[S/2];
   }
  }

return Medain;
}


Last edited on
Topic archived. No new replies allowed.