Oct 15, 2013 at 6:23am UTC
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 Oct 15, 2013 at 6:27am UTC
Oct 15, 2013 at 6:53am UTC
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 Oct 15, 2013 at 6:57am UTC
Oct 15, 2013 at 7:18am UTC
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 Oct 15, 2013 at 7:20am UTC
Oct 15, 2013 at 7:34am UTC
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 Oct 15, 2013 at 8:00am UTC