cslt bai tap chuong 3

Màu nền
Font chữ
Font size
Chiều cao dòng

BÀI TẬP CHƯƠNG 3

Câu 1.Viết chương trình nhập vào một số nguyên dương hệ 10 rồi hiển thị ra màn hình giá trị của số ở hệ 2.

Bài làm:

#include<stdio.h>

#include<conio.h>

int chuyen(int n);

void main()

{

clrscr();

printf("nhap so nguyen duong n= "); scanf("%d",&n);

printf(" la dang co so 2 cua so da cho", chuyen(n));

getch();

}

int chuyen(n)

{

 int i,j,a[15];

 i=0;

  while(n!=0)

  {

   a[i]=n%2;

   n/=2;

   i++;

   }

   for(j=i-1;j>=0;j--)

   printf("%d",a[j]);

}

Câu 2. Viết chương trình nhập số nguyên âm hệ 10 rồi hiển thị ra màn hình giá trị của số ở hệ 2

Bài làm :

#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

{

  int x,a[26],dem=15,i;

  clrscr();

  printf("Nhap mot so nguyen am:");

  scanf("%d",&x);

  x=abs(x);

  memset(a,0,15*2);

  while(x!=0){a[dem--]=x%2;x/=2;}

  for(i=0;i<=15;i++)a[i]=1-a[i];

  i=15;

  while((a[i]==1)&&(i>=0))a[i--]=0;

  a[i]=1;

  printf("so bieu dien o he 2 la:");

  for(i=0;i<=15;i++)printf      ("%2d",a[i]);

  getch();

}

Câu 3.Cho một dãy số a­1,a2,……….a­n.Viết chương trình tách dãy số thành hai dãy sô âm và không âm được xếp tăng dần và hiện thị kết quả ra màn hình dạng :

Dãy số âm có …… phần tử là :

Dãy số không âm có …. phần tử là :

Bài làm:

#include<stdio.h>

 #include<math.h>

 #include<conio.h>

 void main()

 {

   int a[100],n, i,j,k,trung_gian;

   clrscr();

   printf("nhap so phan tu n=");scanf("%d",&n);

   for (i=0;i<n;i++)

   {

    printf("nhap a[%d] =",i+1);

    scanf("%d",&a[i]);

    }

    for (i=0; i<n;i++)

      for(j=i+1;j<n;j++)

       {

            if(a[i]>a[j])

             {

              trung_gian=a[i];

              a[i]=a[j];

              a[j]= trung_gian;

              }

            }

        for(j=0;j<n;j++)

    if (a[j]<0 && a[j+1]>=0)

     { printf(" day so am co %d phan tu la",j+1);

       for(k= 0; k<=j;k++)

            { printf("%d",a[k]);

            }

            printf("day so duong co %d phan tu la",n-j-1);

       for(k=j+1;k<n;k++)

            printf("%d ",a[k]);

     }

     getch();

 }

4.Viết chương trình tính tích 2 ma trận vuông cấp n và hiển thị ma trận tích lên màn hình

Bài làm :

#include<stdio.h>

#include<conio.h>

main()

{

 float a[3][2],b[2][4],c[3][4],x;

 int i,j,k;

 printf("BAI TINH TICH MA TRAN");

 /* Vao ma tran a*/

 for(i=0;i<3;++i)

   for(j=0;j<2;++j)

 {    printf("

a[%d][%d]=",i+1,j+1);

      scanf("%f",&x);

      a[i][j]=x;

 }

  /*vao ma tran b*/

     for(i=0;i<2;++i)

   for(j=0;j<4;++j)

 {    printf("

b[%d][%d]=",i+1,j+1);

      scanf("%f",&x);

      b[i][j]=x;

 }

 /*tinh ma tran tich c*/

 for(i=0;i<3;++i)

    for(j=0;j<4;++j)

 {    c[i][j]=0;

      for(k=0;k<2;++k)

      c[i][j]+=a[i][k]*b[k][j];

 }

 /*in ma tran a*/

 printf("

IN MA TRAN A

");

 for(i=0;i<3;++i)

  { for(j=0;j<1;++j)

    printf("%8.2f",a[i][j]);

    for(j=1;j<2;++j)

    printf("%8.2f

",a[i][j]);

  }

/*in ma tran b*/

 printf("

IN MA TRAN B

");

 for(i=0;i<2;++i)

  { for(j=0;j<3;++j)

    printf("%8.2f",b[i][j]);

   if(j==3)

    printf("%8.2f

",b[i][j]);f

  }

 /*in ma tran a*/

 printf("

IN MA TRAN C

");

 for(i=0;i<3;++i)

   {for(j=0;j<3;++j)

    printf("%8.2f",c[i][j]);

    if(j==3)

    printf("%8.2f

",b[i][j]);

    }

   getch();

}

Câu 5.Nhập vào mảng danh sách 100 sinh viên gồm 2 cột: họ tên, điểm trung bình.Hãy in ra danh sách 10 sinh viên có điểm trung bình cao nhất.

Bài làm:

#include <stdio.h>

#include <conio.h>

#include <string.h>

char name[101][30];

float dtb[101];

void nhap()

{

 char str[30][100];

 printf("Nhap so SV:");

 scanf("%d",&n);

 for(i=1;i<=n;i++)

 {

  printf("

Hoc Sinh Thu %d:

",i);

  printf("Ten:");

  fflush(stdin);

  gets(name[i]);

  printf("DTB:");

  scanf("%f",&dtb[i]);

 }

}

void doi_cho(float *x,float *y)

{

  float temp=*x;*x=*y;*y=temp;

}

void sapxep()

{ int i,j;

  char temp[30];

  for(i=1;i<=n-1;i++)

   for(j=i+1;j<=n;j++)

            if(dtb[i]<dtb[j])

            {

             strcpy(temp,name[i]);

             strcpy(name[i],name[j]);

             strcpy(name[j],temp);

             doi_cho(&dtb[i],&dtb[j]);

            }

}

void xuat()

{

  printf("

3 Sv co diem cao nhat:

");

  for(i=1;i<=3;i++)

   {

             printf("

%d:",i);puts(name[i]);

             printf("DTB:%3.2f

",dtb[i]);

   }

}

void main()

{

  clrscr();

  nhap();

  sapxep();

  xuat();

  getch();

}

Câu 6.Không dùng hàm mẫu viết chương trình để so sánh 2 xâu bất kì

Bài làm:

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

  char s1[30],s2[30];

  int i=0,het=0,n1,n2;

  clrscr();

  printf("Nhap Xau S1:");

  gets(s1);n1=strlen(s1);

  printf("Nhap Xau S2:");

  gets(s2);n2=strlen(s2);

  while(!het)

  {

            if((s1[i]=='\0')||(s2[i]=='\0')){het=1;break;}

            if(s1[i]!=s2[i])break;

            i++;

  }

  if(het)

   {

            if(n1==n2)printf("Hai Xau Co Do Lon Bang Nhau");

            if(n1>n2)printf("Xau 1 lon hon");

            if(n1<n2)printf("Xau 2 lon hon");

   }

   else if (s1[i]<s2[i])printf("Xau 2 lon hon");

   else printf("Xau 1 lon hon");

 getch();

}

Câu 7.Nhập vào mảng một danh sách 10 sinh viên gồm 3 cột : họ tên đệm,tên, điểm trung bình.Hãy in ra danh sách trên đã xếp theo thứ tự alphabet của tên sinh viên.Yêu cầu truy nhập phần tử mảng bằng con trỏ.

Bài làm:

#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<stdlib.h>

char *hodem[20],*ten[20];

float dtb[20];

void nhap()

{

 char **ptr1,**ptr2;

 clrscr();

 printf("Nhap so SV:");

 scanf("%d",&n);

 for(i=0;i<=10;i++)

 {

  hodem[i]=(char*)malloc(30);

  ten[i]=(char*)malloc(30);

 }

 ptr1=hodem;

 ptr2=ten;

 for(i=1;i<=n;i++)

 {

  printf("Sv thu %d

",i);

  printf("Ho Dem:");

  fflush(stdin);

  gets(*(++ptr1));

  printf("Ten:");

  gets(*(++ptr2));

  printf("DTB:");

  scanf("%f",&dtb[i]);

 }

}

void doi_cho(char *s1,char *s2)

{

  char temp[30];

  strcpy(temp,s1);

  strcpy(s1,s2);

  strcpy(s2,temp);

}

void sapxep()

{

  int i,j;

  float temp;

  for(i=1;i<=n-1;i++)

   for(j=i+1;j<=n;j++)

            if (strcmp(ten[i],ten[j])>0)

             {

               doi_cho(hodem[i],hodem[j]);

               doi_cho(ten[i],ten[j]);

               temp=dtb[i];dtb[i]=dtb[j];dtb[j]=temp;

             }

}

void xuat()

{

 printf("Danh sach sau khi sap xep:

");

 for(i=1;i<=n;i++)

  {

            printf("%d:",i);

            printf("%s %s

",hodem[i],ten[i]);

            printf("DTB:%3.2f

",dtb[i]);

  }

}

void main()

{

 nhap();

 sapxep();

 xuat();

 getch();

}

Câu 8.Nhập vào mảng một xâu bất kì rồI loạI bỏ tất cả các khoảng trống trong xâu và hiện thị kết quả đó ra màn hình.

Bài làm:

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

void main()

{

 char s1[50],s2[50];

 int i,dem=0;

 clrscr();

 printf("Nhap mot xau:");

 fflush(stdout);

 gets(s1);

 for(i=0;s1[i]!='\0';i++)

  if(s1[i]!=' ')s2[dem++]=s1[i];

 s2[dem]='\0';

 printf("

Xau truoc khi loai khoang trong:%s",s1);

 printf("

Xau khi loai bo khoang trong:%s",s2);

 getch();

}

Câu 9.Nhập vào một số thực và đọc giá trị đó bằng chữ.Ví dụ: số nhập: 1234.56 được hiện thị là một nghìn hai trăm ba mươi tư phẩy năm mươi sáu.

Bài làm

#include<stdio.h>

#include<conio.h>

#include<math.h>

const double  EPSILON=1e-5;

const char doc[15][20]={"","muoi","tram","nghin"};

const char so[10][6]={"khong","mot","hai","ba","bon","nam","sau","bay","tam","chin"};

const char docto[15][20]={"","nghin","trieu","ti"};

void spelling(int a[],int n)

{

 int i,tgn,tga[10],dem,tram,chuc,donvi;

 int nhom[5][3];

 tgn=n%3;

 dem=tgn-1;

 if(tgn>0)

 {

 for(i=n-1;i>=n-tgn;i--)tga[dem--]=a[i];

 for(i=tgn-1;i>=0;i--)printf("%s %s ",so[tga[i]],doc[i]);

 }

 dem=(n-tgn)/3;

 if(tgn>0)printf("%s ",docto[dem--]);

 else dem--;

 for(i=n-tgn-1;i>=0;i=i-3)

 {

   tram=a[i];

   chuc=a[i-1];

   donvi=a[i-2];

   printf("%s tram %s muoi %s ",so[tram],so[chuc],so[donvi]);

   printf("%s ",docto[dem--]);

 }

}

void main()

{

 double x,du,tg;

 long nguyen,thuc;

 int a1[30],i,dem1=0,dem2=0,a2[30];

 clrscr();

 printf("Nhap mot so:");

 scanf("%lf",&x);

 nguyen=(long)floor(x);

 du=x-nguyen+1e-15;

 while(du-floor(du)>EPSILON)du*=10;

 thuc=(long)du;

 printf("Phan nguyen=%ld",nguyen);

 printf("

Phan thuc=%ld

",thuc);

 if(nguyen==0){a1[0]=0;dem1=1;}

 if(thuc==0){a2[0]=0;dem2=1;}

 while(nguyen){a1[dem1++]=nguyen%10;nguyen/=10;}

 while(thuc){a2[dem2++]=thuc%10;thuc/=10;}

 spelling(a1,dem1);

 printf("phay ");

 spelling(a2,dem2);

 getch();

}

Bạn đang đọc truyện trên: Truyen2U.Pro