2019年8月3日 星期六

1080803老師提供的程式作業解答 輸入三筆資料 找最大值 以及及格者 和 三筆平均

程式設計老師提供的程式作業解答
輸入三筆資料 找最大值 以及及格者 和 三筆平均



// ConsoleApplication3.cpp : 定義主控台應用程式的進入點。
//

#include "stdafx.h"


// Ex3_01.cpp
// A nested if demonstration
#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int main()
{
 int g1;
 int g2;
 int g3;
 int passed = 0;
 double average = 0;
 // input
 cin >> g1 >> g2 >> g3;                        // then read a character

 //operation
 int max = 0;
 // find max
 if (g1 > max)
 {
  max = g1;
 }
 if (g2 > max)
 {
  max = g2;
 }
 if (g3 > max)
 {
  max = g3;
 }
 // find passed
 if (g1 >= 60)
 {
  passed++;
 }
 if (g2 > 59)
 {
  passed++;
 }
 if (g3 >=60)
 {
  passed++;
 }

 average = (g1 + g2 + g3) / 3.0;
 // output
 cout << "max = " << max << endl;
 cout << "passed = " << passed << endl;
 cout << "average = " << average << endl;

 return 0;
}

---------------------------------------------------
--------------------------------------------------
老師提供的程式作業解答2
用迴圈 輸入任意筆資料  算及格人數 和平均
程式可以輸入 N (n)停止



#include "stdafx.h"

// Ex3_10.cpp
// Using an infinite for loop to compute an average
#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int main()
{
 double value(0.0);               // Value entered stored here
 double sum(0.0);                 // Total of values accumulated here
 int i(0);                        // Count of number of values
 char indicator('n');             // Continue or not?
 int passed = 0;
 int nopassed = 0;
 int max = -1;
 int min = 101;
 for (;;)                          // Infinite loop
 {
  cout << endl
   << "Enter a grade: ";
  cin >> value;                 // Read a value
  ++i;                          // Increment count
  sum += value;                 // Add current input to total

  if (value > max)
   max = value;
  if (value < min)
   min = value;
  if (value >= 60)
   passed++;
  else
   nopassed++;


  cout << endl
   << "Do you want to enter another value (enter n to end)?


---------------------------------------------------
--------------------------------------------------

輸入四位同學各三個成績
// ConsoleApplication2.cpp : 定義主控台應用程式的進入點。
//

#include "stdafx.h"

// Ex4_04.cpp
// Storing strings in an array.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main()
{


 int grades[4][3] = {
  {80,20,30},
  {40, 90, 70},
  {17,40,80},
  {60, 77,99},
 };
 double average[3] = { 0 };
 double total[3] = { 0 };
 int max[3] = { 0 };
 int min[3] = { 100,100,100 };
 // input
 for (int i = 0; i < 4; i++)
 {
  for (int j = 0; j < 3; j++)
  {
 
  }
 }

 for (int i = 0; i < 4; i++)
 {
  for (int j = 0; j < 3; j++)
  {
   total[j] += grades[i][j];
   if (grades[i][j] > max[j])
    max[j] = grades[i][j];

   if (grades[i][j] < min[j])
    min[j] = grades[i][j];
  }
 }
 // output
 cout << endl;

 for (int i = 0; i < 4; i++)
 {
  for (int j = 0; j < 3; j++)
  {
   cout << grades[i][j] << " ";

  }
  cout << endl;
 }
 cout << "average = ";
 for (int j = 0; j < 3; j++)
 {
  cout << total[j]/4 << " ";
 }
 cout << endl;
 cout << "max = ";
 for (int j = 0; j

---------------------------------------------------
--------------------------------------------------

2016-03-05_實作題_試題下載
https://apcs.csie.ntnu.edu.tw/wp-content/uploads/2018/12/1050305APCSImplementation.pdf

// ConsoleApplication2.cpp : 定義主控台應用程式的進入點。
//

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;
//using std::cin;
//using std::cout;
//using std::endl;
//using std::setw;
//using std::sort;

int main()
{
 // input
 int num;
 int grades[20] = { 0 };
 int max = -1;
 int min = 101;
 int pass = 0;
 int nopass = 0;
 cin >> num;
 for (int i = 0;i < num; i++)
 {
  cin >> grades[i];
 }


 for (int i = 0; i < num; i++)
 {
  if (grades[i] >= 60)
  {
   pass++;
   if (grades[i] <= min)
    min = grades[i];
  }
  else
  {
   nopass++;
   if (grades[i] >= max)
    max = grades[i];
  }
 }

 std::sort(grades, grades + num);

 for (int i = num-1; i >0; i--)
 {
  cout << grades[i] << " ";
 }
 cout << grades[0] << endl;
 // output
 for (int i = 0; i < num-1; i++)
 {
  cout << grades[i] <<" ";
 }
 cout << grades[num-1] << endl;

 if (pass
第一題解析






沒有留言:

張貼留言