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;
}

Wednesday, December 3, 2014

Numeric :Gauss Seidel

 

Gauss Seidel

 

#include<stdio.h>
int main()
{
 
    double a[50],b[50],c[50],X1,X2,X3,eax1,eax2,eax3,p,q,r;
    int i,n=4;
   // freopen("input.txt","r",stdin);
//printf("Enter length :");
//scanf("%d",&n);
//............................
printf("Enter 1st equation :");
for(i=0;i<n;i++)
{
scanf("%lf",&a[i]);
}
//..........................
printf("Enter 2nd equation :");
for(i=0;i<n;i++)
{
scanf("%lf",&b[i]);
}
//...........................
printf("Enter 3rd equation :");
for(i=0;i<n;i++)
{
scanf("%lf",&c[i]);
}
printf("ENter the value of X1 :");
scanf("%lf",&X1);
printf("ENter the value of X2 :");
scanf("%lf",&X2);
printf("ENter the value of X3 :");
scanf("%lf",&X3);
 
//...........................
X1=p;
X2=q;
X3=r;
 
 
X1=((a[3]-(a[2]*X2)-(a[1]*X3))/a[0]);
 
printf("X1 is %lf\n",X1);
 
X2=((b[3]-(b[2]*X3)-(b[0]*X1))/b[1]);
printf("X2 is %lf\n",X2);
 
X3=((c[3]-(c[1]*X2)-(c[0]*X1))/c[2]);
printf("X2 is %lf\n",X3);
//...........................
eax1=(((X1-p)/X1)*100);
eax2=(((X2-q)/X2)*100);
eax3=(((X3-r)/X3)*100);
printf("eax1=%.1lf\n",eax1);
printf("eax2=%.1lf\n",eax2);
printf("eax3=%.1lf\n",eax3);
return 0;
}