I get stuck when my sort function does not work, or anything else?

I get stuck when my sort function (void process) does not work, or anything else?

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
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#define ip "C:\\Users\\Admin\\Desktop\\progC\\ip.txt"
#define op "C:\\Users\\Admin\\Desktop\\progC\\op.txt"
//==========================================================================
struct Student {
   char name[20];
   short mark[20],sum;
}s[100]/*each student*/;
short n/*number of students*/,m/*number of marks per student*/;
void input();
void process();
void output();
//==========================================================================
void input() {
   ifstream f(ip);
   if(f) {
   short i,j;
   f>>n>>m;
   cout<<"================================\n";
   cout<<"...Read successful...\n";
   cout<<"Number of students: "<<n;
   cout<<"\nNumber of marks per student: "<<m;
   cout<<"\n================================\n";
   for(i=0;i<n;i++) {
      f>>s[i].name;
      cout<<"Name: "<<s[i].name<<'\n';
      cout<<"Marks: ";
      s[i].sum=0;
      for(j=0;j<m;j++) {
         f>>s[i].mark[j];
         cout<<s[i].mark[j]<<' ';
         s[i].sum+=s[i].mark[j];
      }
      cout<<'\n'<<"Sum of marks: "<<s[i].sum<<'\n'<<'\n';
   }
   else {
      cout<<"...Read fail...\nCan't access input file!";
   }
   getch();
   f.close();
};
void process() {
   short i,j;
   struct Student t;
   for(i=0;i<n/2;i++)
      for(j=0;j<n-i-1;j++) {
         if(s[j].sum>s[j+1].sum)
            {t=s[j]; s[j]=s[j+1]; s[j+1]=t;}
         if(s[n-j-1].sum<s[n-j-2].sum)
            {t=s[j]; s[j]=s[j+1]; s[j+1]=t;}
      }
};
void output() {
   ofstream f(op);
   if(f) {
      short i;
      cout<<"================================\n";
      cout<<"...Write successful...\n";
      cout<<"================================\n";
      cout<<"Ranked list:\n";
      for(i=0;i<n;i++) {
         f<<(i+1)<<'-'<<s[i].name<<'\n';
         cout<<(i+1)<<'-'<<s[i].name<<'\n';
      }
   }
   else {
      cout<<"...Write fail...\nNothing to print!";
   }
   getch();
   f.close();
};
//==========================================================================
void main() {
   input();
   process();
   output();
}


Here is few test:
//Test1:
//input 1
3 3
C
8 9 10
B
7 8 9
A
6 7 8

//real output 1 (failure result), why my sort function doesn't work? Or anythings else goes wrong?
1-C
2-B
3-A

//expect output 1 (right result)
1-A
2-B
3-C

//Test 2
//input
7 6
Hoang
7 8 9 5 7 8
Viet
6 7 8 5 5 5
Tien
10 10 10 10 10 10
Tai
9 9 8 8 7 6
Thien
9 8 7 6 5 2
Khoa
5 6 7 5 3 2
Trung
6 7 8 9 10 10

//real output (failure result)
1-Viet
2-Tien
3-Tai
4-Hoang
5-Thien
6-Khoa
7-Trung

//expect output (right result)
1-Tien
2-Trung
3-Tai
4-Hoang
5-Thien
6-Viet
7-Khoa
One error that I can see is that your temp variable is of type Student, when it should be short (like sum).

Also, it seems you are applying a Bubble sort where there are two bubbles, 1 going from left to right, other from right to left. I haven't seen any such sort function before so I can't comment on its correctness.

However you can try the simple 'one-way' Bubble sort and see if it works.

1
2
3
4
5
6
7
8
9
10
11
void process() {
   short i,j;
   short t;
   for(i=0;i<n;i++)
      for(j=0;j<n-i-1;j++) {
         if(s[j].sum>s[j+1].sum)
            {t=s[j]; s[j]=s[j+1]; s[j+1]=t;}
//         if(s[n-j-1].sum<s[n-j-2].sum)
   //         {t=s[j]; s[j]=s[j+1]; s[j+1]=t;}
      }
};
Last edited on
Thanks you, I've fixed my error by using directly struct type, but it still not work, I still not recognize what is wrong in my code

1
2
3
4
5
6
7
8
9
10
11
void process() {
   short i,j;
   struct Student t;
   for(i=0;i<n/2;i++)
   	for(j=0;j<n-i-1;j++) {
      	   if(s[j].sum>s[j+1].sum)
              {t=s[j]; s[j]=s[j+1]; s[j+1]=t;}
           if(s[n-j-1].sum<s[n-j-2].sum)
              {t=s[j]; s[j]=s[j+1]; s[j+1]=t;}
        }
};


But I can confirm that my sort way is perfect, I tested it in here:
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
#include<iostream.h>
#include<conio.h>

//Dual Sort
void s(short *a,short *b) {
   *a=*a-*b;
   *b=*a+*b;
   *a=*b-*a;
}

void main() {
   short const k=100;
   short n,i,j,m[k];
   cout<<"Sort Program \nNumber? "; cin>>n;
   for(i=0;i<n;i++)	{
   	cout<<"Value "<<(i+1)<<": "; cin>>m[i];
   }
   for(i=0;i<n/2;i++)
   	for(j=0;j<n-i-1;j++) {
      	   if(m[j]>m[j+1]) s(&m[j],&m[j+1]);
           if(m[n-j-1]<m[n-j-2]) s(&m[n-j-1],&m[n-j-2]);
   }
   cout<<"\nSorted array: ";
   for(j=0;j<n;j++)
   	cout<<m[j]<<' ';
   getch();
}


Last edited on
Why do you have semicolons after your function definitions? Remove the semicolons.

Also, since you have multiple variables including arrays in the struct Student, I would prefer to override the assignment operator for that struct.

1
2
3
4
5
6
7
8
9
10
11
void process() {
   short i,j;
   Student t; // remove the struct keyword.
   for(i=0;i<n/2;i++)
   	for(j=0;j<n-i-1;j++) {
      	   if(s[j].sum>s[j+1].sum)
              {t=s[j]; s[j]=s[j+1]; s[j+1]=t;}
           if(s[n-j-1].sum<s[n-j-2].sum)
              {t=s[j]; s[j]=s[j+1]; s[j+1]=t;}
        }
} // remove the semicolon. 
Can you tell me the different of having or not having semicolons after function definitions? Cause my prototype functions at the head so I think it need to be have semicolons at each function, I not sure, I don't know when need to put or not to put semicolons?
Last edited on
Oh I recognized!!! I forgot to edit the second swap statements, those must be like this:
 
{t=s[n-j-1]; s[n-j-1]=s[n-j-2]; s[n-j-2]=t;}
So here is the complete program of ranking students

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
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#define ip "C:\\Users\\Admin\\Desktop\\progC\\ip.txt"
#define op "C:\\Users\\Admin\\Desktop\\progC\\op.txt"
//==========================================================================
struct Student {
   char name[20];
   float mark[20],sum;
}s[100]/*each student*/;
short n/*number of students*/,m/*number of marks per student*/;

void input();
void process();
void output();
//==========================================================================
void input() {
   ifstream f(ip);
   if(f) {
   short i,j;
   f>>n>>m;
   cout<<"================================\n";
   cout<<"...Read successful...\n";
   cout<<"Number of students: "<<n;
   cout<<"\nNumber of marks per student: "<<m;
   cout<<"\n================================\n";
   for(i=0;i<n;i++) {
      f>>s[i].name;
      cout<<"Name: "<<s[i].name<<'\n';
      cout<<"Marks: ";
      s[i].sum=0;
      for(j=0;j<m;j++) {
         f>>s[i].mark[j];
         cout<<s[i].mark[j]<<' ';
         s[i].sum+=s[i].mark[j];
      }
      cout<<'\n'<<"Sum of marks: "<<s[i].sum<<'\n'<<'\n';
   }
   else {
      cout<<"...Read fail...\nCan't access input file!";
   }
   getch();
   f.close();
}

void process() {
   short i,j;
   Student t;
   for(i=0;i<n/2;i++)
      for(j=0;j<n-i-1;j++) {
         if(s[j].sum>s[j+1].sum)
            {t=s[j]; s[j]=s[j+1]; s[j+1]=t;}
         if(s[n-j-1].sum<s[n-j-2].sum)
            {t=s[n-j-1]; s[n-j-1]=s[n-j-2]; s[n-j-2]=t;}
      }
}

void output() {
   ofstream f(op);
   if(f) {
      short i;
      cout<<"================================\n";
      cout<<"...Write successful...\n";
      cout<<"================================\n";
      cout<<"Ranked list:\n";
      for(i=0;i<n;i++) {
         f<<(i+1)<<'-'<<s[i].name<<'\n';
         cout<<(i+1)<<'-'<<s[i].name<<'\n';
      }
   }
   else {
      cout<<"...Write fail...\nNothing to print!";
   }
   getch();
   f.close();
}
//==========================================================================
void main() {
   input();
   process();
   output();
}
Last edited on
Topic archived. No new replies allowed.