BongMa

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

         BAI TAP CAU TRUC

#include"iostream"

#include<conio.h>

#include<string.h>

#include<stdio.h>

using namespace std;

typedef struct

{

 int MaSo;

 char HoTen[30];

 float Toan,Ly,Hoa;

 float Tong;

}TS;

void NhapTS( TS &t);

void XuatTS(TS t);

void NhapDS (TS a[],int N);

void XuatDSDau (TS a[],int N);

int KT (TS t);

void main()

{

 TS a[100];

 int N;

 cout<<" Nhap vao so luong thi sinh:";

 cin>>N;

 NhapDS(a,N);

 XuatDSDau(a,N);

 getch();

}

void NhapTS(TS &t)

{

 cout<<"

Nhap ma so:";

 cin>>t.MaSo;

 cout<<"

Nhap ho ten:";

 cin>>t.HoTen;

 fflush(stdin);

 cout<<"

Nhap diem Toan:";

 cin>>t.Toan;

 cout<<"

Nhap diem ly:";

 cin>>t.Ly;

 cout<<"

Nhap diem hoa:";

 cin>>t.Hoa;

 t.Tong = t.Toan + t.Ly + t.Hoa;

}

void NhapDS(TS a[],int N)

{

 for(int i=0;i<N;i++)

 {

  cout<<"

Nhap thong tin cua thi sinh thu"<<i+1<<":";

  NhapTS(a[i]);

 }

}

int KT (TS t)

{

 if ( t.Tong>=12 && t.Toan>=2 && t.Ly>=2 && t.Hoa>=2)

  return 1;

 return 0;

}

void XuatTS (TS t)

{

 cout<<endl;

 cout<<"    "<<t.MaSo<<"    "<<t.HoTen<<"       "<<t.Toan<<"       "<<t.Ly<<"       "<<t.Hoa<<"       "<<t.Tong<<"       ";

}

void XuatDSDau( TS a[],int N)

{

 cout<<"

Danh sach cac thi sinh thi dau:";

 cout<<"

MaSo HoTen Toan Ly Hoa  TongDiem";

 for(int i=0;i<N;i++)

  if(KT(a[i])==1)

   XuatTS(a[i]);

}

                DEM

#include "iostream"

#include<conio.h>

#include<string.h>

using namespace std;

 void main()

 {

  char s[100];

  int dem=0;

  cout<<"nhap vao 1 chuoi:";

  dem=0;

  cout<<"chuoi thanh :";

  for(int i=0;i<strlen(s);i++)

  { 

   if(i%2!=0)

   { 

    if(s[i]>=65 && s[i]<=93)

     s[i]=s[i]+32;

    else

     s[i] = s[i];

   }

   else

   {

   if(s[i]>=65 && s[i]<=93)

    s[i]=s[i];

   else

    s[i] = s[i]-32;

   }

  cout<<s[i];

  }                     

         dem co bao nhieu phan tu bang x trong mang

#include "iostream"

#include <conio.h>

using namespace std;

int timx(int a[],int N, int x)

{

 int kq=0;

 for(int i=0;i<N;i++)

 {

  if(a[i]<x)

  {

   kq=kq+1;

  }

 }

 return kq;

}

void main ()

{

 int a[100],N,x;

 cout<<" nhap so phan tu can tim : ";

 cin>> N;

 for(int i=0;i<N;i++)

 {

  cout<< " nhap phan tu thu " <<i<<" :" ;

  cin>> a[i];

 }

 cout<< " nhap so x can tim : ";

 cin >>x;

 int demx=0;

 demx=timx(a,N,x);

 cout <<" co " <<demx<< " phan tu trong mang nho hon x ";

 getch();

}                     

                                   TACH MANG

#include"iostream"

#include<conio.h>

using namespace std;

void tachmang(int a[],int n,int b[],int &m,int c[],int &l)

{

 int k=n/2;

 m=l=0;

 for( int i=0;i<k;i++)

 {

  b[m++]=a[i];

  c[l++]=a[k+i];

 }

 cout<<" mang B la : ";

 for(int i=0;i<m;i++)

 {

  cout <<b[i]<<" ";

 }

 cout << " mang C la : ";

 for(int i=0;i<l;i++)

 {

  cout << c[i]<<" ";

 }

}

void main()

{

 int a[100],n,b[100],m,c[100],l;

 cout <<" nhap so phan tu can su dung :";

 cin>> n;

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

 {

  cout << " nhap phan tu thu "<< i << " : " ;

  cin>>a[i];

 }

 tachmang(a,n,b,m,c,l);

 getch();

}                                                                         

                         DEM PHAN TU CHAN LE TRONG MANG

#include"iostream"

#include<conio.h>

using namespace std;

int demchan(int a[],int N)

{

 int chan=0;

 int le=0;

 for(int i=0;i<N;i++)

 {

  if(a[i]%2==0)

   chan ++;

 }

 return chan;

}

int demle(int a[],int N)

{

 int le=0;

 for(int i=0;i<N;i++)

 {

  if(a[i]%2!=0)

   le ++;

 }

 return le;

}

void main()

{

 int a[100],N;

 cout<< " nhap so phan tu can su dung : ";

 cin>> N;

 for(int i=0;i<N;i++)

 {

  cout<<" nhap phan tu thu " <<i<<" : ";

  cin>>a[i];

 }

 int chan=demchan(a,N);

 int le=demle(a,N);

 cout<<" so phan tu chan trong mang : " <<chan<<endl;

 cout<<" so phan tu le trong mang : " <<le;

 getch();

}                                                                      

                   TIM BOI CUA 3 VA 5 TRONG MANG

#include"iostream"

#include<conio.h>

using namespace std;

void boiso(int a[],int N)

{

 for(int i=0;i<N;i++)

 {

  if(a[i]%3==0 && a[i]%5==0)

   cout<<" boi so cua 3 va 5 la : "<<a[i]<<endl;

 }

}

void main()

{

 int a[100],N;

 cout << " nhap so phan tu can su dung : ";

 cin>>N;

 for(int i=0;i<N;i++)

 {

  cout<<" nhap phan tu thu "<< i << " : ";

  cin>>a[i];

 }

 boiso(a,N);

  getch();

}

                                                              tIM MAX

# include"iostream"

#include<conio.h>

using namespace std;

void main()

{

 int N,a[100];

 cout<<"nhap vao so phan tu can su dung : ";

 cin>>N ;

 for (int i=0; i<N;i++)

 {

  cout<< " nhap vao phan tu thu "<<i<<" : ";

  cin>>a[i];

 }

 cout<< " mang la : " ;

 for(int i=0;i<N;i++)

  cout<< a[i]<< " ";

 int max=a[0];

 for(int i=1;i<N;i++)

  if (a[i] > max)

   max=a[i];

 cout<<" gia tri lon nhat trong mang la : " <<max;

 getch();

                                                                TIM SO NGUYEN TO

# include"iostream"

#include<conio.h>

int N;

using namespace std;

bool ktsonguyento(int so)

{

 bool kq=true;

 bool i;

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

  if(so%i==0)

   break;

 if(i<N)

  kq=false;

 return kq;

}

void main ()

{

 cout <<" nhap N : " ;

  cin>> N;

 bool kq=ktsonguyento(N);

 if(kq==true)

  cout<< N << " la so nguyen to ";

 if(kq==false)

  cout<< N << " khong la so nguyen to ";

 getch();

                             TIM SO NT TRONG DAY

#include "iostream"

#include<conio.h>

using namespace std;

int kiemtrasoNT(int so);

void nhapmang(int a[],int N)

{

 for(int i=0;i<N;i++)

 {

  cout <<" nhap vao phan tu thu ";

  cin>> a[i];

 }

}

int demsoNT(int a[], int N)

{

 int dem=0;

 for(int i=0; i<N;i++)

 {

  if(kiemtrasoNT(a[i])==1)

   dem ++;

 }

 return dem;

}

int kiemtrasoNT(int so)

{

 int kq=1;

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

  if(so%i==0)

   break;

 if(i< so)

 kq=0;

 return kq;

}

void main()

{

 int N,a[100];

 cout<<" nhap vao so phan tu can su dung : ";

 cin>> N;

 nhapmang(a,N);

 int dem=demsoNT(a,N);

 cout<< dem << "   ";

 getch();

}                                                                            

                                      VI TRI LON NHAT TRONG MANG

#include"iostream"

#include<conio.h>

using namespace std;

int vitriphantulonnhat(int a[],int N)

{

 int max=a[0];

 int phantulonnhat;

 for(int i=0;i<N;i++)

 {

  if(a[i]>max)

  {

  max = a[i];

  phantulonnhat= i;

  }

 }

 return phantulonnhat;

}

void main ()

{

 int a[100],N;

 cout <<" nhap so phan tu can su dung : " ;

 cin>>N;

 for(int i=0;i<N;i++)

 {

  cout <<" nhap phan tu thu " <<i<< " : ";

  cin>>a[i];

 }

 int phantulonnhat=vitriphantulonnhat(a,N);

 cout<<" vi tri phan tu lon nhat la : "<< phantulonnhat;

 getch();

                                    TONG PHAN TU CHIA HET CHO 5

#include"iostream"

#include<conio.h>

using namespace std;

int tongchiahetcho5(int a[],int N)

{

 int kq=0;

 for(int i=0;i<N;i++)

 {

  if(a[i]%5==0)

   kq=kq+a[i];

 }

 return kq;

}

void main()

{

 int a[100],N;

 cout<< " nhap so phan tu can su dung : ";

 cin>> N;

 for(int i=0;i<N;i++)

 {

  cout<<" nhap phan tu thu " <<i<<" : ";

  cin>>a[i];

 }

 int kq=tongchiahetcho5(a,N);

 cout<<" tong cac phan tu chia het cho 5 : " << kq;

 getch();

}

                                   XOA PHAN TU #include"iostream"

#include<conio.h>

using namespace std;

void xoavitridau(int a[],int n,int vitri)

{

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

  a[i]=a[i+1];

 n--;

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

 cout << a[i]<<" ";

}

void main()

{

 int a[100],n,vitri;

 cout <<" nhap so phan tu can su dung :";

 cin>> n;

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

 {

  cout << " nhap phan tu thu "<< i << " : " ;

  cin>>a[i];

 }

 cout << " nhap vi tri can xoa : ";

 cin >>vitri;

 xoavitridau(a,n,vitri);

 getch();

}

                                           TINH TONG CAC PHAN TU CHAN TRONG MANG

#include"iostream"

#include<conio.h>

using namespace std;

int demchan(int a[],int N)

{

 int chan=0;

 int le=0;

 for(int i=0;i<N;i++)

 {

  if(a[i]%2==0)

   chan=chan+a[i];

 }

 return chan;

}

int demle(int a[],int N)

{

 int le=0;

 for(int i=0;i<N;i++)

 {

  if(a[i]%2!=0)

   le =le+a[i];

 }

 return le;

}

void main()

{

 int a[100],N;

 cout<< " nhap so phan tu can su dung : ";

 cin>> N;

 for(int i=0;i<N;i++)

 {

  cout<<" nhap phan tu thu " <<i<<" : ";

  cin>>a[i];

 }

 int chan=demchan(a,N);

 int le=demle(a,N);

 cout<<" tong so phan tu chan trong mang : " <<chan<<endl;

 cout<<" tong so phan tu le trong mang : " <<le;

 getch();

                                       SAP XEP KI TU TANG DAN

#include "iostream"

#include <conio.h>

using namespace std;

void hoanvi(int &a, int &b)

{

 int tam=a;

 a=b;

 b=tam;

}

void sapxep(int a[],int N)

{

 for(int i=0;i<N-1;i++)

 for(int j=i+1;j<N;j++)

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

   hoanvi(a[i],a[j]);

 cout <<" mang duoc sap xep la : ";

 for(int i=0;i<N;i++)

 {

 cout <<a[i]<<" ";

 }

}

void main()

{

 int N,a[100];

 cout<<" nhap so phan tu can su dung : ";

 cin>> N;

 for (int i=0;i<N;i++)

 {

  cout <<" nhap vao phan tu thu "<<i<< " : ";

  cin>>a[i];

 }

 sapxep(a,N);

 getch();

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

#chientran