Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // teststrcmp
- //
- // Created by Rizky Herucakra on 6/13/13.
- // Copyright (c) 2013 Rizky Herucakra. All rights reserved.
- #include <iostream>
- #include <string>
- using namespace std;
- struct student
- {
- char name[50];
- float q1, q2, q3;
- float ave;
- };
- void swap(float* a, float* b){
- float tmp;
- tmp = *a;
- *a = *b;
- *b = tmp;
- }
- void swap(char* a, char* b){
- char buff[50];
- strcpy(buff,a);
- strcpy(a,b);
- strcpy(b,buff);
- }
- void swap(student* a, student* b)
- {
- swap(&(a->q1),&(b->q1) );
- swap(&(a->q2),&(b->q2) );
- swap(&(a->q3),&(b->q3) );
- swap(&(a->ave),&(b->ave) );
- swap(a->name,b->name );
- }
- void my_sort(student* s,int num_item){
- int n = num_item;
- do{
- int newn = 0;
- for (int i = 1; i <= n - 1; ++i ){
- if (strcmp(s[i -1 ].name,s[i].name ) >= 0 ){
- swap(&s[i -1 ],&s[i]);
- newn = i;
- }
- }
- n = newn;
- }while (n != 0);
- }
- int main()
- {
- student x[50];
- int i, j, k;
- char xtemp[50];
- for (i=0; i!=3; i++)
- {
- cout << "\n\n\tPlease enter a Student Name: ";
- cin >> x[i].name;
- cout << "\n\tPlease enter his/her score for QUIZ1: ";
- cin >> x[i].q1;
- cout << "\n\tPlease enter his/her score for QUIZ2: ";
- cin >> x[i].q2;
- cout << "\n\tPlease enter his/her score for QUIZ3: ";
- cin >> x[i].q3;
- x[i].ave = (x[i].q1+x[i].q2+x[i].q3) / 3;
- cout<<"\n\t-----------------------------------";
- }
- my_sort(x,3);
- // display
- cout << "\n\n\n\tNAME\t\tQUIZ1\t\t\QUIZ2\t\tQUIZ3\t\tAVE"<<endl;
- for (i=0; i!=3; i++)
- {
- cout << "\n\t"<<x[i].name;
- cout <<"\t\t"<<x[i].q1;
- cout <<"\t\t"<<x[i].q2;
- cout <<"\t\t"<<x[i].q3;
- cout <<"\t\t"<<x[i].ave<<endl;
- }
- cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement