在基本的C++中需要关于如何正确地遍历零件并找到最小值的帮助
嗨,我需要一些帮助。我在介绍编程类,我们正在使用C++。我希望有人能够帮助我完成昨天到期的任务(我明白,不要期待奇迹的回应,但女孩总是可以尝试)。在基本的C++中需要关于如何正确地遍历零件并找到最小值的帮助
我有两个问题,我知道。首先是关于最小的价值。 最大的问题是试图让它循环三次的要求,但不会损失我的总数。我无法使用数组或任何我没有学过的东西,这就是为什么我发布了这个。我看到过类似的问题和疑问,但最终他们的回答太复杂了,因为目前的课程进度太复杂了。因此,这里的问题说明:
说明 1)写一个程序,找到平均值,最大值,以及一组从键盘输入提供数字的最小值。数据集中的值的数量必须在0到20之间(包括0和20)。用户将首先输入数据集中的数值(使用变量int Number)。给用户3次尝试输入给定范围内的数字。如果输入的数值超出此范围,请写出错误消息但继续。如果用户在3次尝试中没有输入有效的Number值,则输出错误消息并终止程序。
2)格式仅打印时输出为平均值到3位小数。
3)中输入作为输入数据组中的值可以是任何值正,负或零。
4)使程序输出可读的(参见下面的示例)。 (注意:您不会像通常所要求的那样打印输入到此程序中的输入值,这是因为我们尚未在研究中涵盖此工具所需的“工具”)。
下面将会从你的程序的执行输出: (为了使用这些值的数据集 - >19.0 53.4 704.0 -15.2 0 100.0
)
The largest number: 704
The smallest number: -15.2
The average of the 6 numbers entered: 143.533
yourName L4p2XX.cpp
Lab#4 prob 2 XX-XX-12
这是我在解决穷人的借口:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double Number = 0, minValue, maxValue, average, total = 0;
int ct = 0, numCount;
cout << "How many numbers would you like to enter? ";
cin >> numCount;
for(ct = 1; ct <= numCount; ct += 1)
{
cout << "Enter Value from 0 to 20, inclusive: ";
cin >> Number;
if(Number > 20|| Number < 0)
for(int errorCt = 1; errorCt <= 4; errorCt += 1)
{
if(errorCt == 4)
{
cout << "You have had 3 attempts to enter a valid" <<
"number. \nPlease try this program again when you" <<
"are able to follow directions.";
cout <<"\nLBn\n"<<"L4P2LB.cpp\n"<<"11-05-12\n";
return 0;
}
cout << Number << "is not within range.\n" <<
"Please enter a number from 0 to 20: ";
cin >> Number;
} //end for loop
total += Number;
if(maxValue <= Number)
maxValue = Number;
if(Number <= minValue)
minValue = Number;
} //end for loop
cout << "The smallest number entered was " << minValue << endl;
cout << "The largest number you entered was " << maxValue << endl;
average = total/numCount;
cout << setprecision(3) << fixed << showpoint << "You entered " <<
numCount << " numbers. The average of these is " << average;
//Program ID
cout <<"\n" << "L4P2LB.cpp\n" << "11-05-12\n";
system ("pause");
return 0;
} // End main
谢谢你在前进的人谁可以引导我在正确的方向。不寻找任何人做我的工作我只需要帮助方向,如果没有别的或任何建议做什么。再次感谢。琳达
此外,我需要以某种方式第三次和退出后正确暂停。如果我把第二个停顿放在它不会工作,所以我也错过了一些明显的东西!
第一个问题,我看到的是,你没有初始化几个变量。
您应该初始化两个minValue
和maxValue
变量的东西,这将在任何情况下覆盖在第一循环(通常为“正/负无穷大”,如<limits>
提供),或者只是在第一次迭代设置既要Number
,而不管它们的当前价值。所以,我建议更换
if(maxValue <= Number)
maxValue = Number;
if(Number <= minValue)
minValue = Number;
与
if(maxValue <= Number || ct == 1)
maxValue = Number;
if(Number <= minValue || ct == 1)
minValue = Number;
解决这一问题作为ct == 1
将在第一次迭代是真实的。
这就是说,你检查0..20
范围条件的错误变量。你在Number
变量上检查它,但你应该检查numCount
变量。 但是你也没有尊重变量要存储的“数字号”应该是Number
,所以你做了检查了正确的变量,但是用错了读入输入。这应该可以解决这个问题(我在cin >>...
线变化的变量名称+移动主循环外的检查):
cout << "How many numbers would you like to enter? ";
cin >> Number;
if(Number > 20|| Number < 0)
{
for(int errorCt = 1; errorCt <= 4; errorCt += 1)
...
if(errorCt == 4)
{
cout << "You have had 3 attempts to enter a valid" <<
"number. \nPlease try this program again when you" <<
"are able to follow directions.";
cout <<"\nLBn\n"<<"L4P2LB.cpp\n"<<"11-05-12\n";
return 0;
}
cout << Number << "is not within range.\n" <<
"Please enter a number from 0 to 20: ";
cin >> Number;
} //end for loop
}
for(ct = 1; ct <= Number; ct += 1)
{
...
}
...
这是功课?如果是这样,请添加标签。 – stefan
@stefan:家庭作业标签现在被删除:http://meta.stackexchange.com/questions/147100 –
您需要做的第一件事是正确缩进您的代码。就目前而言,很难看到程序的“流程”,因为很难说出if语句,for语句等内容。没有人会在努力阅读代码时让他们的眼睛变得紧张。 –