私はプログラミングを改善し、私が持っているクラスからいくつかの資料を学び、再学習するために、Cplusplus.comのWebサイトからいくつかの演習を行っています今年取り上げた問題は、私が働いていること、具体的には最後の太字部分です。整数の配列を比較してC++で最大から最小まで表示する方法
異なる10人(人1、人2、...、人10)で朝食のために食べられるパンケーキの番号を入力するようユーザに要求するプログラムを書く データは、プログラムを入力されるとしなければなりませんデータを分析し、どの人が朝食のために最もパンケーキを食べたかを出力します。それは例えばのように、すべての10人の食べパンケーキの数を出力するように
人4が ..プログラムを変更します。10枚のパンケーキを食べた
人は3:7つのパンケーキを食べました
人8:4つのパンケーキ
を食べましたここで
は私が持っているものである
等...これまで
#include <iostream>
using namespace std;
void getPancakesEaten(int pancakes[]);
void comparePancakesEaten(int pancakes[]);
void displayOrder(int pancakes[]);
/**********************************************************************
* Gets the amount of Pancakes eaten
***********************************************************************/
void getPancakesEaten(int pancakes[])
{
for(int i = 0; i < 10; i++)
{
cout << "Person " << i + 1 << ": ate ";
cin >> pancakes[i];
}
cout << endl;
}
/**********************************************************************
* Compares the amount of pancakes eaten with each person
***********************************************************************/
void comparePancakesEaten(int pancakes[])
{
if(pancakes[0] > pancakes[1] && pancakes[0] > pancakes[2] && pancakes[0] > pancakes[3] && pancakes[0] > pancakes[4] && pancakes[0] > pancakes[5] && pancakes[0] > pancakes[6] && pancakes[0] > pancakes[7] && pancakes[0] > pancakes[8] && pancakes[9])
{
cout << "Person 1 ate " << pancakes[0] << " pancakes" << endl;
}
else if(pancakes[1] > pancakes[0] && pancakes[1] > pancakes[2] && pancakes[1] > pancakes[3] && pancakes[1] > pancakes[4] && pancakes[1] > pancakes[5] && pancakes[1] > pancakes[6] && pancakes[1] > pancakes[7] && pancakes[1] > pancakes[8] && pancakes[1] > pancakes[9])
{
cout << "Person 2 ate " << pancakes[1] << " pancakes" << endl;
}
else if(pancakes[2] > pancakes[0] && pancakes[2] > pancakes[1] && pancakes[2] > pancakes[3] && pancakes[2] > pancakes[4] && pancakes[2] > pancakes[5] && pancakes[2] > pancakes[6] && pancakes[2] > pancakes[7] && pancakes[2] > pancakes[8] && pancakes[2] > pancakes[9])
{
cout << "Person 3 ate " << pancakes[2] << " pancakes" << endl;
}
else if(pancakes[3] > pancakes[0] && pancakes[3] > pancakes[2] && pancakes[3] > pancakes[1] && pancakes[3] > pancakes[4] && pancakes[3] > pancakes[5] && pancakes[3] > pancakes[6] && pancakes[3] > pancakes[7] && pancakes[3] > pancakes[8] && pancakes[3] > pancakes[9])
{
cout << "Person 4 ate " << pancakes[3] << " pancakes" << endl;
}
else if(pancakes[4] > pancakes[0] && pancakes[4] > pancakes[2] && pancakes[4] > pancakes[3] && pancakes[4] > pancakes[1] && pancakes[4] > pancakes[5] && pancakes[4] > pancakes[6] && pancakes[4] > pancakes[7] && pancakes[4] > pancakes[8] && pancakes[4] > pancakes[9])
{
cout << "Person 5 ate " << pancakes[4] << " pancakes" << endl;
}
else if(pancakes[5] > pancakes[0] && pancakes[5] > pancakes[2] && pancakes[5] > pancakes[3] && pancakes[5] > pancakes[4] && pancakes[5] > pancakes[1] && pancakes[5] > pancakes[6] && pancakes[5] > pancakes[7] && pancakes[5] > pancakes[8] && pancakes[5] > pancakes[9])
{
cout << "Person 6 ate " << pancakes[5] << " pancakes" << endl;
}
else if(pancakes[6] > pancakes[0] && pancakes[6] > pancakes[2] && pancakes[6] > pancakes[3] && pancakes[6] > pancakes[4] && pancakes[6] > pancakes[5] && pancakes[6] > pancakes[1] && pancakes[6] > pancakes[7] && pancakes[6] > pancakes[8] && pancakes[6] > pancakes[9])
{
cout << "Person 7 ate " << pancakes[6] << " pancakes" << endl;
}
else if(pancakes[7] > pancakes[0] && pancakes[7] > pancakes[2] && pancakes[7] > pancakes[3] && pancakes[7] > pancakes[4] && pancakes[7] > pancakes[5] && pancakes[7] > pancakes[6] && pancakes[7] > pancakes[1] && pancakes[7] > pancakes[8] && pancakes[7] > pancakes[9])
{
cout << "Person 8 ate " << pancakes[7] << " pancakes" << endl;
}
else if(pancakes[8] > pancakes[0] && pancakes[8] > pancakes[2] && pancakes[8] > pancakes[3] && pancakes[8] > pancakes[4] && pancakes[8] > pancakes[5] && pancakes[8] > pancakes[6] && pancakes[8] > pancakes[7] && pancakes[8] > pancakes[1] && pancakes[8] > pancakes[9])
{
cout << "Person 9 ate " << pancakes[8] << " pancakes" << endl;
}
else if(pancakes[9] > pancakes[0] && pancakes[9] > pancakes[2] && pancakes[9] > pancakes[3] && pancakes[9] > pancakes[4] && pancakes[9] > pancakes[5] && pancakes[9] > pancakes[6] && pancakes[9] > pancakes[7] && pancakes[9] > pancakes[8] && pancakes[9] > pancakes[1])
{
cout << "Person 10 ate " << pancakes[9] << " pancakes" << endl;
}
cout << "\n";
}
/**********************************************************************
* Displays the order in which person ate the most pancakes to the least
***********************************************************************/
void displayOrder(int pancakes[])
{
for(int i = 9; i >= 0; i--)
{
for(int j = 0; j < 9; j++)
{
if(pancakes[j] > pancakes[j + 1])
{
int temp = pancakes[j + 1];
pancakes[j + 1]= pancakes[j];
pancakes[j]=temp;
}
}
cout << "Person " << i + 1 << ": ate " << pancakes[i] << " pancake(s)" << endl;
}
}
/**********************************************************************
* Runs the Entire Program
***********************************************************************/
int main()
{
int pancakes[256];
getPancakesEaten(pancakes);
comparePancakesEaten(pancakes);
displayOrder(pancakes);
}
私はに実行している問題は、私が正しい順序にパンケーキの番号に関連付けられた人を表示しようとすると、たとえば、入力に基づいて間違っていることがあります。
Person 1: ate 12 Person 2: ate 22 Person 3: ate 3 Person 4: ate 4 Person 5: ate 5 Person 6: ate 6 Person 7: ate 7 Person 8: ate 8 Person 9: ate 9 Person 10: ate 10 Person 2 ate 22 pancakes Person 10: ate 22 pancake(s) Person 9: ate 12 pancake(s) Person 8: ate 10 pancake(s) Person 7: ate 9 pancake(s) Person 6: ate 8 pancake(s) Person 5: ate 7 pancake(s) Person 4: ate 6 pancake(s) Person 3: ate 5 pancake(s) Person 2: ate 4 pancake(s) Person 1: ate 3 pancake(s)
代わりに、「人2」の入力値が「人10」に表示されないのはなぜですか? displayOrder
機能の私のforループのためですか?
_「はどこに私のコードでは、私が間違っているんだ?」_ [それはデバッガがために目的とされているものです](http://idownvotedbecau.se/nodebugging/ )! – user0042
ヘルプのデバッグを要求していませんが、これは使用可能ですhttps://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – UKMonkey