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

📄 doublearray.cpp

📁 Visual C++.NET 大学教程 清华出版社 第七章例题代码
💻 CPP
字号:
// Fig. 7.14: DoubleArray.cpp
// Manipulating a double-subscripted array.

#include "stdafx.h"

#using <mscorlib.dll>

using namespace System;

int Minimum( int __gc[,], int, int );
int Maximum( int __gc[,], int, int );
double Average( int __gc *, int );

int _tmain()
{
   int grades[,] = new int __gc[ 3, 4 ]; 
   grades[ 0, 0 ] = 77;
   grades[ 0, 1 ] = 68;
   grades[ 0, 2 ] = 86;
   grades[ 0, 3 ] = 73;
   grades[ 1, 0 ] = 96;
   grades[ 1, 1 ] = 87;
   grades[ 1, 2 ] = 89;
   grades[ 1, 3 ] = 81;
   grades[ 2, 0 ] = 70;
   grades[ 2, 1 ] = 90;
   grades[ 2, 2 ] = 86;
   grades[ 2, 3 ] = 81;

   int students = grades->GetLength( 0 );  // number of students
   int exams = grades->GetLength( 1 );     // number of exams

   // line up column headings
   Console::Write( S"             " );

   // output the column headings
   for ( int i = 0; i < exams; i++ )
      Console::Write( S"[{0}]", i.ToString() );
   
   // output the rows
   for ( int i = 0; i < students; i++ ) {
      Console::Write( S"\ngrades[{0}]     ", i.ToString() );
   
      for ( int j = 0; j < exams; j++ )
         Console::Write( S"{0} ", grades[ i, j ].ToString() );
   } // end for

   Console::WriteLine( S"\n\nLowest grade: {0}\nHighest grade: {1}",
      Minimum( grades, students, exams ).ToString(), 
      Maximum( grades, students, exams ).ToString() );

   for ( int i = 0; i < students; i++ )
      Console::Write( S"\nAverage for student {0} is {1}", i.ToString(),
         Average( &grades[ i, 0 ], exams ).ToString( ".00" ) );
       
   Console::WriteLine();

   return 0;
} // end _tmain

// find minimum grade in grades array
int Minimum( int grades __gc[,], int students, int exams )
{
   int lowGrade = 100;

   for ( int i = 0; i < students; i++ )

      for ( int j = 0; j < exams; j++ )

         if ( grades[ i, j ] < lowGrade )
            lowGrade = grades[ i, j ];

   return lowGrade;
} // end function Minimum

// find maximum grade in grades array
int Maximum( int grades __gc[,], int students, int exams )
{
   int highGrade = 0;

   for ( int i = 0; i < students; i++ )

      for ( int j = 0; j < exams; j++ )

         if ( grades[ i, j ] > highGrade )
            highGrade = grades[ i, j ];

   return highGrade;
} // end function Maximum

// determine average grade for a particular student
double Average( int __gc *setOfGrades, int grades )
{
   int total = 0;

   for ( int i = 0; i < grades; i++ )
      total += setOfGrades[ i ];

   return static_cast< double >( total ) / grades;
} // end function Average

/*************************************************************************
* (C) Copyright 1992-2004 by Deitel & Associates, Inc. and               *
* Pearson Education, Inc. All Rights Reserved.                           *
*                                                                        *
* DISCLAIMER: The authors and publisher of this book have used their     *
* best efforts in preparing the book. These efforts include the          *
* development, research, and testing of the theories and programs        *
* to determine their effectiveness. The authors and publisher make       *
* no warranty of any kind, expressed or implied, with regard to these    *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or       *
* consequential damages in connection with, or arising out of, the       *
* furnishing, performance, or use of these programs.                     *
*************************************************************************/

⌨️ 快捷键说明

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