niusouti.com

c++,运行下面代码出错#include <iostream>using namespace std;struct student_info{ string name; int chinese; int math; int english;};student_info student[5];void inputinfo(){ int i = 0; char a[20]; for(i = 0;i < 5;i++) { cout<<"enter the "<<i+1<<" student's informa

题目
c++,运行下面代码出错

#include <iostream>using namespace std;struct student_info{ string name; int chinese; int math; int english;};student_info student[5];void inputinfo(){ int i = 0; char a[20]; for(i = 0;i < 5;i++) { cout<<"enter the "<<i+1<<" student's information "<<endl; cout<<"enter the name"<<endl; cin>>a; student[i].name = a; cout<<"enter chinese performance :"<<endl; cin>>student[i].chinese; cout<<"enter math performance :"<<endl; cin>>student[i].math; cout<<"enter english performance :"<<endl; cin>>student[i].english; }}void outputinfo(){ int i = 0; while(i < 5) { cout<<i+1<<" student's information"<<endl; cout<<student[i].name.c_str()<<endl; cout<<"chinese "<<endl; cout<<student[i].chinese<<endl; cout<<"math "<<endl; cout<<student[i].math<<endl; cout<<"english "<<endl; cout<<student[i].english<<endl; i++; }}int avgfunction(int arr[],int n){ int i = 0; int total = 0; for(i = 0;i < n;i++) { total += arr[i]; } return total / n;}void computavg() //这里是否正确?{ int theavg = 0; theavg = avgfunction(student[i].chinese,5); }int main(){ inputinfo(); outputinfo(); computavg(); cin.get(); return 0;} 帮我找出错误 然后改过来 择优为满意答案 不分先后


相似考题
参考答案和解析
float avgfunction(float total,int n) { return total / n; } void computavg() //这里是否正确? { int theavg = 0; float total = 0; for (int i = 0; i < 5; i++) { total += student[i].chinese; } theavg = avgfunction(total, 5); } 这个地方重写了一下,平均数最好是用浮点数,最后主函数里面再加一个输出就可以了。
更多“c++,运行下面代码出错 ”相关问题
  • 第1题:

    下面代码段的输出是( )。

    A.编译出错

    B.5752

    C.true

    D.无任何输出


    正确答案:A
    本题考查对位运算符和逻辑运算符的理解。位运算符”&”和”|"用于按位将两个数进行与和或的操作,两个操作数可以是整型、字节型、长整型和短整型,但不能是浮点型数据。逻辑运算符只能对两个布尔型的数据进行运算,返回的结果也是布尔型的。

  • 第2题:

    12、下面代码的运行结果是‪‪‪‪‪‫‪‪‪‪‪‪‫‪‪‪‪‪‪‪‪‪‪‪‫‪‪‪‪‪‪‪‪‪‪‪‪‪‪‪‪‪‪‪‪‪‪‪‪‪‪‪‪‪‪‪‪ def func(num): num+=1 a=10 func(a) print(a)

    A.10

    B.int

    C.11

    D.出错


    0 [1,2,3]

  • 第3题:

    找出错误并修改下面的代码: public class Test{ public static void main(String[] args){ double[100] r; for(int i = 0; i < r.length(); i++); r(i) = Math.random * 100; } }指出错误代码的位置,并写出修改后的正确代码。提交的作业内容中的程序代码以代码格式提交(如题目中的代码)。


    错误语句: void Subtract_C(Complex Sub,Complex c1,Complex c2) 真确的: void Subtract_C(Complex &Sub,Complex c1,Complex c2) 少写了一个& 详细见第一章中给出的课件 第0章 C++复习.ppt

  • 第4题:

    阅读下列说明、c++代码和运行结果,填补代码中的空缺(1)~(6),将解答填入

    答题纸的对应栏内。

    【说明】

    很多依托扑克牌进行的游戏都要先洗牌。下面的c++程序运行时先生成一副扑克牌,

    洗牌后再按顺序打印每张牌的点数和花色。

    【c++代码】

    inciude <iostream>

    4Finclude <stdlib. h>

    include <ctime>

    inciude <aigorithm>

    include <string>

    Using namespace std

    Const string Rank[13]={”A”,”2”,”3”,”4“,“5”,”6,”’“7”8“,9”,”10,”J”,

    ”Q”,”K”}j//扑克牌点数


    正确答案:
    分析本题考查c-++语言程序设计能力,涉及类、对象、函数的定义和相关操作。要求考生根据给出的案例和代码说明,认真阅读,理清程序思路,然后完成题目。本题目中涉及到扑克牌、牌桌等类以及洗牌和按点数排序等操作。根据说明进行设计。定义了两个数组,Rank表示扑克牌点数,Suits表示扑克牌花色,定义时进行初始化,而且值不再变化,故用const修饰。Card类有两个属性,rank和suit,在使用构造函数Card(intrankintsuit)新建一个Card的对象时,所传入的参数指定rank和suit这两个属性值。因为参数名称和属性名称相同,所以用this->前缀区分出当前对象。在类Card中包含方法getRank()和getSuit(),分别返回当前对象的rank和suit属性值。printCard()函数打印扑克牌点数和花色。DeckOfCards类包含Card类型元素的数组deck[52],表示牌桌上一副牌(52张)。构造函数中对牌桌进行初始化并进行洗牌。先用Card对象填充牌桌,即创建52个Card对象并加入deck数组。然后洗牌,即将数组中的Card对象根据花色和点数随机排列。printCards0函数将所有Card对象打印出来。主控逻辑代码在main函数中实现。在main()函数中,先初始化DeckOfcards类的对象指针d,即生成一个牌桌:Deckofcards*d=newdeckofcards()并发牌,即调用d的printCards()函数,实现打印一副扑克牌中每张牌的点数和花色。在printCards()函数体内部,为每个数组元素调用当前对象的pnntCard()一张牌。Main()函数中使用完数组对象之后,需要用delete操作进行释放对象,对d对象进行删除,即deleted。因此,空(1)和(2)需要表示当前对象的this->;空(3)需要牌桌上纸牌对象,即数组元素deck[i]:空(4)也需要纸牌对象调用printCard(),即数组元素deck[i].:空(5)处为创建DeckOfCards类的对象指针d的newDeckofcards0;空(6)需要用对象指针d调用打印所有纸牌的pnntCards()函数,即d->printCards0.试题五参考答案(1)this->(2)this->(3)deck[i]或*(deck+i)或等价表示(4)deck[i]或*(deck+i)或等价表示(5)newDeckOfCards()(6)d->printCards0或等价表示

  • 第5题:

    5、下面代码的输出结果是()。 def printpara(x): '''just print x''' print(x) printpara("Hi")

    A.just print x

    B.x

    C.Hi

    D.出错


    D

  • 第6题:

    11、下面代码的运行结果是 def func(num) : num += 1 a = 10 func(a) print(a)

    A.10

    B.11

    C.出错

    D.int


    0 [1,2,3]