Assignment 5

1. 

程序一:


#include <iostream>

#include <cmath>

#include <string>

using namespace std;

int main(int argc,char *argv[])

{

   

    floata[5][4];             

    int m = 0,n =0;               

    int aver = 0;

    string name[5];

    intdwCountOne[5];         

    float dwAverOne[5]; 

    cout << "请输入5同学的姓名和四门科目的成绩:" << endl;

    for (m = 0; m<5; m++)

    {

       cin >> name[m];

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

       {

           cin>>a[m][n];

       }

    }

    for (m= 0; m<5; m++)

    {

       dwCountOne[m] = a[m][1] + a[m][2] +a[m][3] +a[m][4];

       cout << name[m] << "的总成绩是:";

       cout <<dwCountOne[m];

       dwAverOne[m] = 1.0*(a[m][1] + a[m][2]+ a[m][3] +a[m][4])/4;   

       cout<<"   "<<"平均成绩是"<< dwAverOne[m]<<endl;

    }  

   

    int dwNumberMax = 0;

    for(int dwCounter =0; dwCounter<4; dwCounter++)

    {

       if (dwAverOne[dwCounter]>dwAverOne[dwCounter+1])

       {

           dwNumberMax =dwCounter;

           int dwTemp=0;

           dwTemp =dwCountOne[dwCounter];

          dwCountOne[dwCounter+1] = dwCountOne[dwCounter];

          dwCountOne[dwCounter]= dwTemp;

       }

       else

       {

           dwNumberMax =dwCounter +1;

       }

    cout << name[dwNumberMax]; 

    cout<<  "的平均分最高是:"<<dwAverOne[dwNumberMax];

    cout << endl;

    }

    return 0;

程序二:

#include<iostream>

#include<string>

using namespace std;

string name[5];

int a[5][4],average[5];

int main()

{

    void input_data();

    void average_sorce();

    void high_student();

    cout<<"请输入五位学生的姓名和四科成绩:"<<endl;

    input_data();

    cout<<"五位学生的平均成绩:"<<endl;

    average_sorce();

    cout<<"成绩最高的学生名字和分数:"<<endl;

    high_student();

    return 0;

}

void input_data()

{

    int i,j;

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

    {

       cout<<"学生"<<i+1<<":";

        cin>>name[i];

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

        cin>>a[i][j];

    }

}

void average_sorce()

{

    int i;

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

    {

      average[i]=(a[i][0]+a[i][1]+a[i][2]+a[i][3])/4;

       cout<<name[i]<<" "<<"平均成绩:"<<average[i]<<endl;

    }

}

void high_student()

{

    int i,j=0,max;

    max=average[0];

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

       if(average[i]>max)

       {

          max=average[i];

           j=i;

       }

 cout<<name[j]<<" "<<max<<endl;

}

}

2. 找出一个二维数组中的鞍点,即该位置上的元素在该行上最大,在该列上最小(也可能没有鞍点)。

至少准备两组测试数据:

1)二维数组有鞍点,如

1  2  3  4  5

2  4  6  8  10

3  6  9  12  16

4  8  12  16  20

2)二维数组没有鞍点,如

1  12  3  4  5

2  4  16  8  10

3  6  8  12  15

4  8  12  16  20

 

程序代码:

#include <iostream>

using namespace std;

int main(int argc,char *argv[])

{

int a[4][5];

bool flag;

int dwMax = 0,dwMaxJ = 0;

int dwI = 0,dwJ = 0;

cout << "请输入一个长度为5,行数为4的数组:" << endl;

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

{

     for (dwJ = 0;dwJ <5;dwJ ++)

        cin >> a[dwI][dwJ];

}

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

{

      dwMax = a[dwI][0];

      dwMaxJ = 0;

      for(dwJ = 0;dwJ < 5;dwJ ++)

         if (a[dwI][dwJ]>dwMax)

         {

            dwMax = a[dwI][dwJ];

            dwMaxJ = dwJ;

         }

     flag = true;

     int dwK = 0;

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

     {

        if (a[dwK][dwMaxJ]<dwMax)

        {

            flag =false;

           continue;

        }

    

     }

        if (flag)

        {

        cout << "这个数组存在鞍点!鞍点是:" << dwMax << endl;

        }

        break;

}

if (!flag)

{  

     cout << "这个数组不存在鞍点!!!" << endl;

}

 

return 0;

}

3.       编写函数,实现将两个字符串连接起来,结果取代第一个字符串。

(1)       用字符数组,不用strcat函数(即自己写一个具有strcat函数功能的函数)

(2)       用标准库中的strcat函数;

(3)       用string方法定义字符串变量。

程序一:(1)、自定义函数;

#include<iostream>

#include <string>

using namespace std;

int main(int argc,char *argv[])

{

    void sxtcat(char *a,char *b);

    char ch1[50],ch2[10];

    cout << "请给两个数组赋初值:" << endl;

    cin >> ch1 >> ch2;

    sxtcat(ch1,ch2);

    cout << "你输入的两个数组组装到一块是:" << ch1;

    cout << endl;

    return 0;

}

 

 

void sxtcat(char *a,char *b)

{

    if ((a!=NULL)&&(b!=NULL))

    {

       while(*a!='\0')

       {

           *a++;

       }

       while (*b!='\0')

       {

           *a++ = *b++;

       }

       *a = '\0';

    }

}

程序二:(2):用库函数strcat:

#include<iostream>

#include <string>

using namespace std;

int main(int argc,char *argv[])

{

    char ch1[50],ch2[10];

    cout << "请给两个数组赋初值:" << endl;

    cin >> ch1 >> ch2;

    strcat(ch1,ch2);

    cout << "你输入的两个数组组装到一块是:" << ch1;

    cout << endl;

    return 0;

}

程序三:(3)用string方法定义字符串变量。

#include<iostream>

#include <string>

using namespace std;

int main(int argc,char *argv[])

{

    string str1,str2;

    cout << "请给两个字符串赋初值:" << endl;

    cin >> str1 >> str2;

    str1= str1+str2;

    cout << "两个字符串组装到一块是:" << str1 << endl;

    return 0;

}

 

 

4.      有15个数按由大到小的顺序存放在一个数组中,输入一个数,要求用折半查找法找出该数是数组中的第几个元素的值。如果该数不在数组中,则打印出“无此数”。

程序代码:

#include <iostream>

using namespace std;

int main(int argc,char *argv[])

{

    int array[15];

    int dwI = 0,dwJ = 0,dwK = 0;

    int dwFind = 0;

    int leap = -1;

    cout << " 请输入15个由大到小的整数:" << endl;

    for (;dwI < 15;dwI ++)

    {

       cin >> array[dwI];

    }

    cout << "请输入你想要查找的数字:" ;

    cin >> dwFind;

    if (dwFind >array[7]&&dwFind < array[0])

    {

       for (dwI = 0;dwI <7;dwI ++)

       {

           if(array[dwI] == dwFind)

           cout <<"要找的数字的位置是:" << dwI+1<< endl;

           leap = 0;

           break;

       }

    }

    else if (dwFind ==array[7])

    {

       cout << "要找的数字的位置是:" <<8 << endl;

       leap = 0;

    }

    else if (dwFind < array[7])

    {

       for(dwI = 8;dwI <15;dwI ++)

           if(array[dwI]== dwFind)

           {

             cout << "要找的数字的位置是:"<< dwI+1 << endl;

             leap = 0;

             break;

           }

    }

    if (leap == -1)

    {

       cout << "要找的数字不存在!" << endl;

    }

    return 0;

}


5.         编写一个程序模拟掷两次筛子,计算两次投掷结果之和 。(两个值之和应该在2到12之间,其中7是出现频率最多的值)。程序实现掷两个筛子36000。用一个一维数组记录每种和值的出现次数。用表格的形式打印结果。同时,还要确定结果是否合理。例如,有6种组合方式使和为7,因此大约有1/6的投掷结果为7。

 

 

 

 

 

1

2

3

4

5

6

1

2

3

4

5

6

7

2

3

4

5

6

7

8

3

4

5

6

7

8

9

4

5

6

7

8

9

10

5

6

7

8

9

10

11

6

7

8

9

10

11

12

图1掷两个筛子的26中可能结果

 

示例输出:

 Sum  Total   Expected   Actual

2     1000   2.778%    2.778%

3     1958   5.556%    5.439%

……

……

12    950    2.778%   2.639%

 

#include <iostream>

#include <cstdlib>

#include <iomanip>

#include <ctime>

using namespace std;

int main()

{

       int a[11]={0};

       int b[11]={2,3,4,5,6,7,8,9,10,11,12};

       int dwCounter = 0;

       int dwA = 0,dwB = 0;

       int dwSum = 0;

       int dwI = 0;

       srand(time(0));

       while (dwCounter <= 36000)

       {

             

             dwA = rand()%6 +1;

             dwB = rand()%6 +1;    //设置两个随机数字,模拟两次投掷筛子时的点数

             dwSum = dwA + dwB;

             if (dwSum == 2)       //存储所得的和的次数

             {

                    a[0]=a[0]+1;

             }

             else if (dwSum == 3)

             {

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

             }

             else if (dwSum == 4)

             {

                    a[2]+=1;

             }

             else if (dwSum == 5)

             {

                    a[3]+=1;

             }

             else if (dwSum == 6)

             {

                    a[4]+=1;

             }

             else if (dwSum == 7)

             {

                    a[5]+=1;

             }

             else if (dwSum == 8)

             {

                    a[6]+=1;

             }

             else if (dwSum == 9)

             {

                    a[7]+=1;

             }

             else if (dwSum == 10)

             {

                    a[8]+=1;

             }

             else if(dwSum == 11)

             {

                    a[9]+=1;

             }

             else if(dwSum == 12)

             {

                    a[10]+=1;                    //a[dwI]是实际循环36000次之后结果中每次和的次数

             }                                       //b[i]表示可能出现的和

             dwCounter++;                   //c[i]表示理论出现的百分比

      }                                              //f[i]表示实际上出现的百分比

       int d[11];

       float c[11];

       int j = 0;

       int k =1;

       for (j=0;j<=5;j++)   //解决理论上应该得到的和的次数

             {

                    d[j]=j+1;

             }

       for(j =10;j>=6;j--)

             {

                    d[j] = k;

                    k++;

             }

       float f[11];

       for (j = 0;j<11;j++)  //计算理论和的百分比

       {

             c[j] = (1.0*d[j]/26)*100;

             f[j] = (1.0*a[j]/36000)*100;

       }

       cout << "sum"<<"        "<<"Total" <<"           "<< "Expected"<<"             " << "Actual" << endl;

       for (dwI = 0;dwI <11;dwI ++)

       {

             cout << b[dwI];

             cout <<"        "<< a[dwI]<<"            "<<setw(8)<<f[dwI]<<"%"<<"          "<<c[dwI]<<"%"<<endl;

       }

      

       return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值