⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex7_03.cpp

📁 一本语言类编程书籍
💻 CPP
字号:
// Exercise 7.3 Here's the amended program. 
// For the price of having to say at the start how many students' details
// you want to enter, you get rid of all the limitations of constants and
// fixed-length arrays. 
// Make sure you don't forget to delete the memory you allocated using new.

#include <iostream>
#include <string>
using  std::cout;
using  std::cin;
using  std::endl;
using  std::string;

int main() {
  int student_count = 0;
  double total_marks = 0;
  double class_average = 0;

  cout << "How many students' details do you wish to enter?: ";
  cin >> student_count;

  string* student_names = new string[student_count];
  float* student_marks = new float[student_count];

  // Data entry loop. This loop allows the teacher to enter the students' marks.
  for(int i = 0 ; i < student_count ; i++) {
    cout << "Enter student's name: ";
    cin >> student_names[i];

    cout << "Enter student's mark: ";
    cin  >> student_marks[i];

    total_marks += student_marks[i];
    cout << endl;
  }

  // Calculating the class average.
  class_average = total_marks / student_count;

  // Displaying the class average.
  cout << endl << "The class average for "
               << student_count
               << " students is "
               << class_average;
  cout << endl;

  // Displaying the students' names and marks.
  for(int j = 0; j < student_count ; j++)
    cout << endl << student_names[j] << "\t\t" << student_marks[j];
  cout << endl;

  delete [] student_names;
  delete [] student_marks;

  return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -