Wednesday, November 12, 2014

Viết chương trình nhập vào một ma trận có kích thước m x n.thực hiện công việc sắp xếp các cột của ma trận theo thứ tự giảm dần.

#include <stdio.h>
#include <conio.h>
void nhapmang(int a[][50],int x,int y)
{
    int i,j;
    for(i=0;i<x;i++)
    for(j=0;j<y;j++)
    {
        printf("nhap phan tu [%d][%d] : ",i,j);
        scanf("%d",&a[i][j]);
    }
}
void inmang(int a[][50],int x,int y)
{
    int i,j;
    for(i=0;i<x;i++)
    {
       for(j=0;j<y;j++)
       printf(" %5d ",a[i][j]);
       printf("\n");
    }
}
void swap(int *a,int *b)
{
      int tg=*a;
      *a=*b;
      *b=tg;
}
void sxcgiam(int a[][50],int x,int y)
{
      for (int j=0;j<y;j++)
      for (int i=0;i<x-1;i++)
      for (int k=i+1;k<x;k++)
      if  (a[i][j]<a[k][j]) swap(&a[i][j],&a[k][j]);
}
int main()
{
    int m,n;
    inta[50][50];
    printf("nhap so hang ,so cot");
    scanf("%d%d",&m,&n);
    printf("nhap mang : \n");
    nhapmang(a,m,n);
    printf("\n xem mang vua nhap  \n");
    inmang(a,m,n);
    sxcgiam(a,m,n);
    printf("\n xem mang sau khi sap xep  \n");
    inmang(a,m,n);
    getch();
}




Viết chương trình nhập vào một ma trận có kích thước m x n.thực hiện công việc sắp xếp các hàng của ma trận theo thứ tự tăng dần.

#include <stdio.h>
#include <conio.h>
void nhapmang(int a[][50],int x,int y)
{
    int i,j;
    for(i=0;i<x;i++)
    for(j=0;j<y;j++)
    {
        printf("nhap phan tu [%d][%d] : ",i,j);
        scanf("%d",&a[i][j]);
    }
}
void inmang(int a[][50],int x,int y)
{
    int i,j;
    for(i=0;i<x;i++)
    {
       for(j=0;j<y;j++)
       printf(" %5d ",a[i][j]);
       printf("\n");
    }
}
void swap(int *a,int *b)
{
      int tg=*a;
      *a=*b;
      *b=tg;
}
void sxhtang(int a[][50],int x,int y)
{
      for (int i=0;i<x;i++)
      for (int j=0;j<y-1;j++)
      for (int k=j+1;k<y;k++)
      if  (a[i][j]>a[i][k]) swap(&a[i][j],&a[i][k]);
}
int main()
{
    int m,n;
    inta[50][50];
    printf("nhap so hang ,so cot");
    scanf("%d%d",&m,&n);
    printf("nhap mang : \n");
    nhapmang(a,m,n);
    printf("\n xem mang vua nhap  \n");
    inmang(a,m,n);
    sxhtang(a,m,n);
    printf("\n xem mang sau khi sap xep  \n");
    inmang(a,m,n);
    getch();
}



Tuesday, November 11, 2014

tìm vị trí có giá trị lớn nhất trong mảng 2 chiều


tìm vị trí có giá trị lớn nhất trong mảng 2 chiều




#include <stdio.h>
#include <conio.h>
void nhapmang(int a[][50],int x,int y)
{
    int i,j;
    for(i=0;i<x;i++)
    for(j=0;j<y;j++)
    {
        printf("nhap phan tu [%d][%d] : ",i,j);
        scanf("%d",&a[i][j]);
    }
}
void inmang(int a[][50],int x,int y)
{
    int i,j;
    for(i=0;i<x;i++)
    {
       for(j=0;j<y;j++)
       printf(" %5d ",a[i][j]);
       printf("\n");
    }
}
void timmax(int a[][50],int x, int y)
{
      inti,j,u=0,v=0,max=a[0][0];
      for(i=0;i<x;i++)
    {
       for(j=0;j<y;j++)
       if(a[i][j]>max)
       {
              max=a[i][j];
              u=i;
              v=j;
       }
   }
   printf("\nVi tri co gia tri lon nhat trong mag 2 chieu co dia chi la     [%d][%d]",u,v);
}
int main()
{
    int m,n;
    inta[50][50];
    printf("nhap so hang ,so cot");
    scanf("%d%d",&m,&n);
    printf("nhap mang : \n");
    nhapmang(a,m,n);
    printf("\n xem mang  \n");
    inmang(a,m,n);
    timmax(a,m,n);
    getch();
}





Thursday, November 6, 2014

Thao tác cơ bản về danh sách LIFO FIFO.

Thao tác cơ bản về danh sách LIFO FIFO.


#include <stdio.h>
#include <conio.h>
typedef struct node
{
      int info;
      node *link;
};
void xem(node *f)
{
      node *p;
      p=f;
      while(p!=NULL)
      {
            printf("%5d",p->info);
            p=p->link;
      }
}
node *nhaplifo(node *f,int n)
{
      node *p;
      for (int i=0;i<n;i++)
      {
            p=new(node);
            printf("\nMoi nhap so nguyen : ");
            scanf("%d",&p->info);
            p->link=f;
            f=p;
      }
      return f;
}
node *nhapfifo(node *f,node *l,int n)
{
      node *p;
      for (int i=0;i<n;i++)
      {
            p=new(node);
            printf("\nMoi nhap so nguyen : ");
            scanf("%d",&p->info);
            if(f==NULL){
                  f=p;
                  l=p;
            }
            else{
                  l->link=p;
                  l=p;
            }
      }
      return f;
}
node *seach(node *f,int x)
{
      node *p;
      p=f;
      while(p!=NULL&&p->info!=x) p=p->link;
      return p;
}
node *themdau(node *f)
{
      node *p;
      p=new(node);
      printf("\nMoi nhap so nguyen : ");
      scanf("%d",&p->info);
      p->link=f;
      return p;
}
node *themcuoi(node *f)
{
      node *p,*x;
      x=new(node);
      printf("\nMoi nhap so nguyen : ");
      scanf("%d",&x->info);
      x->link=NULL;
      if(f==NULL) return x;
      else {
            ////dua con tro p tro vao o cuoi danh sach////
          p=f;
          while (p->link!=NULL) p=p->link;
          /////////////////////////////////////////////
          p->link=x;
          return f;
      }    
}
node *xoadau(node *f)
{
      node *p;
      p=f->link;
      delete(f);
      return p;
}
node *xoacuoi(node *f)
{
      node *p,*x;
      if(f->link==NULL) {
            delete(f);
            returnNULL;
      }
      p=f;
      while(p->link!=NULL){
            x=p;
            p=p->link;
      }
      x->link=NULL;
      delete(p);
      return f;
}
void sapxeptang(node *f)
{
      node *p1,*p2;
      int tg;
      p1=f;
      while(p1->link!=NULL){
            p2=p1->link;
            while(p2!=NULL){
                  if(p1->info>p2->info){
                        tg=p1->info;
                        p1->info=p2->info;
                        p2->info=tg;
                  }
                  p2=p2->link;
            }
            p1=p1->link;
      }
}
void sapxepgiam(node *f)
{
      node *p1,*p2;
      int tg;
      p1=f;
      while(p1->link!=NULL){
            p2=p1->link;
            while(p2!=NULL){
                  if(p1->info<p2->info){
                        tg=p1->info;
                        p1->info=p2->info;
                        p2->info=tg;
                  }
                  p2=p2->link;
            }
            p1=p1->link;
      }
}
void main()
{
      node *f=NULL;    //nếu danh sách fifo thì thay bằng  
                                    //             node *f=NULL,*l=NULL;
      int n;
      printf("\n  N =  ");
      scanf("%d",&n);
      f=nhaplifo(f,n); //nếu danh sách fifo thì thay bằng
             //                                 f=nhapfifo(f,l,n);
      printf("\nDanh sach vua nhap :");
      xem(f);
      printf("\nThem dau           :");
      f=themdau(f);
      printf("                    ");// lệnh in này căn chèn dấu cách cho xem thửng đẹp hơn
      xem(f);
      printf("\nThem Cuoi          :");
      f=themcuoi(f);
      printf("                    ");// lệnh in này căn chèn dấu cách cho xem thửng đẹp hơn
      xem(f);
      printf("\nSap Xep  tang      :");
      sapxeptang(f);
      xem(f);
      printf("\nSap xep Giam       :");
      sapxepgiam(f);
      xem(f);
      printf("\nTim kiem x=5         =>  ");
      if (seach(f,5)==NULL) printf("Khong tim thay x=5");
         elseprintf("Tim thay ");
      printf("\nXoa dau             ");
      f=xoadau(f);
      xem(f);
      printf("\nXoa cuoi            ");
      f=xoacuoi(f);
      xem(f);
      getch();
}