Home > Back-end >  Kneel to beg
Kneel to beg

Time:03-23

2, the program to read the topic (a total of 6 item, each item 10, a total of 60 points) int main ()
{
? Int n=10;
? Int sum=0, CNT=0, avg=0;
? for (int i=1; i <=n; I++)
? If (I % 3==0)
? {
? Sum=sum + I;
? Cnt++;
? }? Avg=sum/CNT;
? Cout & lt; } 1. Please write the code above run results, 2. Change the code in the n to 12, the running result is what? 3. Change the code in the n to 20, the running result is what? 4. The above code if there is a reliability problem? 5. Above this code if there is any performance issues? 6. Change the code in the n to 2, what happens? Why is that?

CodePudding user response:

6. Change the code in the n to 2, CNT will be equal to zero; Zero divisor

CodePudding user response:

Reference:
 # include 
Int main ()
{
Int n=10;
Int sum=0, CNT=0, avg=0;
for (int i=1; i <=n; I++)
If (I % 3==0) {
Sum=sum + I;
Cnt++;
}
Avg=sum/CNT;
Cout & lt; <", the average is "& lt;
}

//within 10 integers have 3, can be divided exactly by 3 sum to 18, the average is 6


//within 12 can be divided exactly by 3 integers have four, sum to 30, average of 7


//within 20 integers can be divided exactly by 3 have 6, total was $63, average of 10


//4 5 lack of CNT value judgment, potential avg=sum/CNT; Will go wrong

//the code n 2, instead of CNT will be equal to zero, avg=sum/CNT; Error
  • Related