bugkExercise 5.5 Calculating average student grades.

从输出结果来看,这个程序是错的。完全不知道什么意思。

Exercise 5-5. Write a program that will calculate the average grade for the students in
each of an arbitrary number of classes. the program should read in all the grades for
students in all classes before calculating the averages. Output the student grades
for each class followed by the average for that class.

//Exercise 5.5 Calculating average student grades.
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>

int main(void)
{
  size_t nclasses = 0;                  // Number classes
  size_t nstudents_max = 0;             // Maximum number of students in a class
  char answer = ‘N‘;

  printf("How many students are in the largest class? :");
  scanf("%zd", &nstudents_max);
  printf("How many classes are there? :");
  scanf("%zd", &nclasses);
  size_t class = 0;
  size_t student = 0 ;

  int grades[nclasses][nstudents_max];                     // Stores the grades
  size_t students[nclasses];                               // Stores the number of students in each class

  for( class = 0 ; class < nclasses ; ++class)
    {
    printf("Enter the grades for students in class %zd.\n", class + 1);
    students[class] = 0;                                   // Student count within a class
    while(true)
    {
      printf("Enter the integer grade for student %zd: ", students[class] + 1);
      scanf("%d", &grades[class][students[class]]);
      if(++students[class] == nstudents_max)                // Increment and check student count
      {
        printf("Class %zd complete.", class + 1);
        break;
      }
      printf("Any more students in class %zd (Y or N): ", class + 1);
      scanf(" %c", &answer);
      if(toupper(answer) == ‘N‘)
      break;
    }
  }
  printf("\n");
  for( class = 0 ; class < nclasses ; ++class)
    {
    int class_total = 0;
    printf("Student grades for class %zd are:\n", class + 1);
    for( student = 0 ; student < students[class] ; ++student)
    {
      class_total += grades[class][student];
      if((student + 1) % 6 == 0)
        printf("\n");
      printf("%5d", grades[class][student]);
    }
    printf("\nAverage grade for class %zd is %.2lf\n", class + 1, (double)class_total/students[class]);
  }
    return 0;
}
时间: 2024-11-05 20:24:17

bugkExercise 5.5 Calculating average student grades.的相关文章

Exercise 2.4 Calculating average hourly pay rate

#include <stdio.h> int main (void) { double pay = 0.0; double hours = 0.0; int dollars = 0; int cents = 0; //ger the weekly pay printf ("Enter your weekly pay in the dollars:"); scanf ("%lf", pay); //get the weekly hours printf (

Python Codecademy Student Becomes the Teacher

#三个学生的信息 lloyd = { "name": "Lloyd", "homework": [90.0, 97.0, 75.0, 92.0], "quizzes": [88.0, 40.0, 94.0], "tests": [75.0, 90.0] } alice = { "name": "Alice", "homework": [100.0,

对于List的All,Any,Where,FirstOrDefault,Average,Sum,Distinct,Union,AddRange,RemoveRange,InsertRange,GetRange操作

对于集合的操作发现现在越来越丰富,今日圣诞节,以常用的List<T>为例静静地看了集合的一些操作,用了发现还真方便. 一.集合的合并,插入,删除,显示AddRange(一个集合):将指定集合的元素添加到末尾.RemoveRange(要移除的元素的范围从零开始的起始索引,要移除的元素数):移除一定范围的元素InsertRange(在此处插入新元素的从零开始的索引,一个集合):将集合插入到指定索引处.也可以对集合选择范围后再插入GetRange(范围开始处从零开始的起始索引,范围中的元素数):返回

MongoDB University 第五周作业——aggregate聚合高级查询

HOMEWORK: HOMEWORK 5.2 (HANDS ON) Crunching the Zipcode datasetPlease calculate the average population of cities in California (abbreviation CA) and New York (NY) (taken together) with populations over 25,000. For this problem, assume that a city nam

[translation]The rise of college ‘Grade Forgiveness’

Education 教育 The rise of college 'Grade Forgiveness' /*the rise of -的增加*/大学等级宽恕现象的增加. Universities are letting students take classes over again---a consequence of the pressure schools feel to ensure their "customers" are satisfied. /*over again

javascript数据结构和算法[转]

字符串表示的数组 join() 和 toString() 函数返回数组的字符串表示.这两个函数通过将数组中的元素用逗号分隔符分割,返回字符串数组表示. 这里有个例子: var names = ["David", "Cynthia", "Raymond", "Clayton", "Mike", "Jennifer"]; var namestr = names.join(); print(n

参差不齐的数组

var grades = [ [ 89, 77 ], [ 76, 82, 81 ], [ 91, 94, 89, 99 ] ]; var total = 0; var average = 0.0; for ( var row = 0; row < grades.length; ++row) { for ( var col = 0; col < grades[row].length; ++col) { total += grades[row][col]; } average = total /

Drools文档(八) 规则语言参考

规则语言参考 概述 Drools有一个"本地"的规则语言.这种格式在标点符号上非常轻,并且通过"扩展器"支持自然语言和领域特定的语言,使语言能够变形到您的问题领域.本章主要与本机规则格式一致.用于表示语法的图表被称为"铁路"图表,它们基本上是语言术语的流程图.技术上非常热衷的也可以参考DRL.g这是规则语言的Antlr3语法.如果您使用Rule Workbench,则可以通过内容帮助为您完成许多规则结构,例如,输入"ru"并按

Radio Basics for RFID

Radio Basics for RFID (2015/09/24 22:30:37) Radio Basics for RFID (2015/09/24 22:30:37) Radio Basics for RFID (2015/09/24 22:30:37) Radio Basics for RFID (2015/09/24 22:30:37) Radio Basics for RFID (2015/09/24 22:30:37) Radio Basics for RFID (2015/09