Monday, December 8, 2014

C প্রোগ্রামিং - 12 অধ্যায়–Structure (কিছু : example) part 2

Namesort / ID sort / CGPA sort

 

 

normal :input output

#include<stdio.h>
#include<string.h>
struct cse12
{
    char name[20];
    int sid;
    double cgpa ;
 
}student;
 
 
int main()
{
   // struct cse12 student ;
    strcpy(student.name,"sharif");
    student.sid=1;
    student.cgpa=3.00;
 
    printf("student name :%s\n",student.name);
    printf("student ID :%d\n",student.sid);
    printf("student CGPA :%lf\n",student.cgpa);
 
 
 
 
 
    return 0;
}


using arrary



#include<stdio.h>
#include<string.h>
struct cse12
{
    char name[20];
    int sid;
    double cgpa ;
 
} student[10];
void print(struct cse12 student[])
{
    int i;
    for(i=0; i<10; i++)
    {
 
        printf("student name :%s\n",student[i].name);
        printf("student ID :%d\n",student[i].sid);
        printf("student CGPA :%lf\n",student[i].cgpa);
 
    }
}
 
int main()
{
    int i;
    //struct cse12 ;
    for(i=0; i<10; i++)
    {
        scanf("%s\n",student[i].name);
        scanf("%d\n",&student[i].sid);
        scanf("%lf\n",&student[i].cgpa);
    }
    print(student);
    return 0;
}

Namesort / ID sort // Cgpa sort



#include<stdio.h>
#include<string.h>
struct cse12
{
    char name[20];
    int sid;
    double cgpa ;
 
} student[10];
//...........................................
input(struct cse12 student[])
{
    int i;
    printf("Enter 5 student info:\n");
    for(i=0; i<5; i++)
    {
        scanf("%s\n",student[i].name);
        scanf("%d\n",&student[i].sid);
        scanf("%lf\n",&student[i].cgpa);
    }
}
//..........................................
void output(struct cse12 student[])
{
    int i;
    for(i=0; i<5; i++)
    {
        printf("student ID :%d\n",student[i].sid);
        printf("student name :%s\n",student[i].name);
        printf("student CGPA :%.2lf\n",student[i].cgpa);
 
    }
}
//.........................................
void namesort(struct cse12 student[])
{
    int i,j;
    for(i=0; i<4; i++)
    {
        for(j=i+1; j<5; j++)
        {
            if(strcmp(student[i].name,student[j].name)>0)
            {
                struct cse12 temp;
                strcpy(temp.name,student[i].name);
                temp.sid=student[i].sid;
                temp.cgpa=student[i].cgpa;
 
                strcpy(student[i].name,student[j].name);
                student[i].sid=student[j].sid;
                student[i].cgpa=student[j].cgpa;
 
                strcpy(student[j].name,temp.name);
                student[j].sid=temp.sid;
                student[j].cgpa=temp.cgpa;
            }
        }
    }
}
//...........................................
void idsort(struct cse12 student[])
{
    int i,j;
    for(i=0; i<4; i++)
    {
        for(j=i+1; j<5; j++)
        {
            if(student[i].sid>student[j].sid)
            {
                struct cse12 temp;
                strcpy(temp.name,student[i].name);
                temp.sid=student[i].sid;
                temp.cgpa=student[i].cgpa;
 
                strcpy(student[i].name,student[j].name);
                student[i].sid=student[j].sid;
                student[i].cgpa=student[j].cgpa;
 
                strcpy(student[j].name,temp.name);
                student[j].sid=temp.sid;
                student[j].cgpa=temp.cgpa;
            }
        }
    }
}
//.........................................
void cgpasort(struct cse12 student[])
{
    int i,j;
    for(i=0; i<4; i++)
    {
        for(j=i+1; j<5; j++)
        {
            if(student[i].cgpa>student[j].cgpa)
            {
                struct cse12 temp;
                strcpy(temp.name,student[i].name);
                temp.sid=student[i].sid;
                temp.cgpa=student[i].cgpa;
 
                strcpy(student[i].name,student[j].name);
                student[i].sid=student[j].sid;
                student[i].cgpa=student[j].cgpa;
 
                strcpy(student[j].name,temp.name);
                student[j].sid=temp.sid;
                student[j].cgpa=temp.cgpa;
            }
        }
    }
}
//..........................................
int main()
{
    input(student);
    output(student);
    //namesort(student);
      //idsort(student);
    cgpasort(student);
    printf("\n\nAfter sorting :");
    output(student);
    return 0;
}

No comments:

Post a Comment