Here is my few successful program:

#include<iostream.h>
#include<conio.h>
//==============================================================================
void main() {
   short int n,i,j,k,max;
   int m[50];
   i=0; j=0; k=0; max=-32767;
   cout<<"Value of length: "; cin>>n;
   for(i;i<n;i++) {
      cout<<"Value "<<(i+1)<<": "; cin>>m[i];
      if(m[i]>0&&m[i]%2==0) k++;
      if(m[i]<0&&m[i]>max) {
         max=m[i];
         j++;
      }
   }
   cout<<endl<<"Number of natural even value: "<<k<<endl;
   if(j>0) {
      cout<<"Position of max negative value ("<<max<<"):";
      for(i=0;i<n;i++)
         if(m[i]==max) cout<<" "<<(i+1);
   }
   else cout<<"Don't have negative value!";
   getch();
}

And here is my fail program (run yet but not right), please help me fix this, this program input an student list, then follow their marks and rank them, which t is the name of student, d is mark of each student, n is the number of students, m is the number of objects and tb is the average mark of each student:

#include<iostream>
#include<conio>
#include<fstream>
#include<string.h>
#define ip "C:\\Users\\Admin\\Desktop\\progC\\ip.txt"
#define op "C:\\Users\\Admin\\Desktop\\progC\\op.txt"
short int n,m;
struct hs {
   char t[10];
   short int d[10],tb;
}h[100];
//==============================================================================
void inp();
void prc();
void oup();
//==============================================================================
void inp() {
   ifstream f(ip);
   if(f) {
      short int i,j;
      f>>n>>m;
      for(i=0;i<n;i++)
         f>>h[i].t;
         for(i=0;i<n;i++) {
            h[i].tb=0;
            for(j=0;j<m;j++) {
               f>>h[i].d[j];
               h[i].tb+=h[i].d[j];
            }
         }
   }
   else {
      cout<<"Can't open file!";
      getch();
   }
   f.close();
};
//------------------------------------------------------------------------------
void s(char a[10],char b[10]) {
   char p[10];
   strcpy(p,a);
   strcpy(a,b);
   strcpy(b,p);
};
void prc() {
   short int i,j;
   for(i=0;i<n-1;i++)
      for(j=0;j<n-i-1;j++)
         if(h[j].tb<h[j+1].tb)
            s(h[j].t,h[j+1].t);
         else if(h[j].tb==h[j+1].tb)
            if(h[j].t[0]>h[j+1].t[0])
               s(h[j].t,h[j+1].t);
};
//------------------------------------------------------------------------------
void oup() {
   ofstream f(op);
   if(n!=0) {
      short int i;
      for(i=0;i<n;i++)
         f<<(i+1)<<"-"<<h[i].t<<endl;
   }
   else {
      cout<<"Nothing to print!";
      getch();
   }
   f.close();
};
//==============================================================================
void main() {
   inp();
   prc();
   oup();
}

Recommended Answers

All 8 Replies

t is the name of student, d is mark of each student, n is the number of students, m is the number of objects and tb is the average mark of each student:

Why not give meaningfull names to your variables? studentName instead of t etc. For the moment, you know what t stands for, in a few months, when perhaps you have to revise your program you will not know it any more.

(run yet but not right)

What is not right?

Why not give meaningfull names to your variables?

I'm usually use short name for faster, but as you say, I'm usually forgot what I created, thanks for your hint.

What is not right?

It run perfectly but the result is not like I expect for, it's not rank the student at all, I think it can occur in "prc" function or may be in "s" (swap) function, but I can't check where is the error.
In "prc" function, if 2 students have a same average mark then I will rank them by their name, but I don't find out how to do that, can you give me an algorithm to do that, it my program I just plan to rank by the first letter of their name and even that it still not success.

Thanks you for helped me.

If you expect other humans to help you at least, make the code readable and understandable by humans.

If you expect other humans to help you at least, make the code readable and understandable by humans.

TEST 1:
input:
2 3
A
B
5 6 7
8 9 10
output:
1-B
2-A

TEST 2:
input:
3 3
A
B
C
5 6 7
8 9 10
7 8 9
output:
1-A //???????
2-B
3-C

I have improve my code but still fail :(
here is:

#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();
}

Could you send me the input file so that I can test it?

Could you send me the input file so that I can test it?

You can change the file direction and create a new notepad with these input:

//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

//Thanks for your help

Yes!!! Finally I've fixed my program to nearly perfect, here is my ranking students program

#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();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.