简体   繁体   English

在基本C ++中需要有关如何正确遍历零件并找到最小值的帮助

[英]Need help in basic C++ regarding how to properly loop through part and finding smallest value

Hi I'm needing some help. 嗨,我需要一些帮助。 I'm in a intro to programming class and we are using c++. 我正在介绍编程课,我们正在使用c ++。 I am hoping someone can help me with an assignment that was due yesterday (I understand not to expect miracle responses but a girl can always try). 我希望有人可以帮我完成昨天的作业(我知道不要期待奇迹般的反应,但是女孩总是可以尝试的)。

I'm having two problems that I know of. 我有两个我知道的问题。 The first is regarding the smallest value. 首先是关于最小值。 The big one is in trying to make it loop for requirements of three times but not lose out on my total count. 最重要的是尝试使其循环达到三倍的需求,但又不输给我。 I cannot use arrays or anything I haven't learned yet which is why I've posted this. 我不能使用数组或任何我还没有学到的东西,这就是为什么我发布了这个。 I've seen similar problems and questions but they have ended up with answers too complex for current progress in class. 我见过类似的问题和问题,但最后得出的答案太复杂,无法满足当前的课堂学习要求。 So here is the problems instructions: 因此,这里是问题说明:

Instructions 1) Write a program to find the average value, the largest value, and the smallest value of a set of numbers supplied as input from the keyboard. 说明1)编写一个程序,以查找作为键盘输入提供的一组数字的平均值,最大值和最小值。 The number of values in the data set must be in the range 0 to 20, inclusive. 数据集中的值的数量必须在0到20之间(包括0和20)。 The user will first enter the number of values in the data set(use variable int Number). 用户将首先在数据集中输入值的数量(使用变量int Number)。 Give the user 3 attempts at entering Number in the range given. 给用户3次尝试在给定范围内输入数字。 If the value for Number entered is out of this range, write an error message but continue. 如果输入的数字值超出此范围,请写一条错误消息,然后继续。 If the user does not enter a valid value for Number within the 3 attempts print an error message and terminate the program. 如果用户在3次尝试中都没有输入有效的Number值,则打印一条错误消息并终止程序。

2) Format only the output for the Average value to 3 decimal places when printed. 2)打印时,仅将“平均值”的输出格式化为小数点后3位。

3) The values in the data set entered as input can be any value positive, negative, or zero. 3)在输入作为输入数据组中的值可以是任何值正,负或零。

4) Make the program output readable(see the example below). 4)使程序输出可读(请参见下面的示例)。 (Note: that you will notprint out the input values that were entered in this program like you normally are required to do. This is because we have not covered the “tool” needed to do so yet in our studies). (请注意:您将不会像通常需要的那样打印出在程序中输入的输入值。这是因为在我们的研究中,我们尚未涵盖这样做所需的“工具”)。

Below will be the output from the execution of your program: (using these values in order for the data set --> 19.0 53.4 704.0 -15.2 0 100.0 ) 下面是程序执行的输出:(使用这些值以获取数据集-> 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

Here is my poor excuse at the solution: 这是我在解决方案上的可怜借口:

#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

Thank you in advance to anyone who can steer me in the right direction. 预先感谢任何可以引导我朝正确方向发展的人。 Not looking for anyone to do my work I just need help in direction if nothing else or any suggestions as to what to do. 不找人做我的工作,如果没有其他建议或关于如何做的任何建议,我只需要指导方面的帮助。 Thanks again. 再次感谢。 Lynda 琳达

Also I need somehow to pause after the third time and exit properly. 我还需要某种方式在第三次之后暂停并正确退出。 If I put the second pause in it won't work so am I missing something obvious there too! 如果我在其中添加第二个暂停将不起作用,那么我是否也错过了明显的东西!

The first problem I see is that you didn't initialize a couple of variables. 我看到的第一个问题是您没有初始化几个变量。

You should either initialize both minValue and maxValue variables with something which will overwritten in every case in the first loop (typically "positive/negative infinity", as provided by <limits> ), or just set both to Number in the first iteration, regardless of their current value. 您应该使用在每种情况下都会在第一个循环中覆盖的值(通常是“正/负无穷大”,由<limits> )来初始化minValuemaxValue变量,或者在第一次迭代中都将它们都设置为Number ,无论其当前值。 So I'd suggest to fix this by replacing 所以我建议通过替换来解决此问题

    if(maxValue <= Number)
        maxValue = Number;
    if(Number <= minValue)
        minValue = Number;

with

    if(maxValue <= Number || ct == 1)
        maxValue = Number;
    if(Number <= minValue || ct == 1)
        minValue = Number;

as ct == 1 will be true in the first iteration. 因为ct == 1在第一次迭代中为true。

That said, you check the 0..20 range condition on the wrong variable. 也就是说,您检查了错误变量的0..20范围条件。 You check it on the Number variable, but you should check the numCount variable. 您在Number变量上进行了检查,但应检查numCount变量。 But you also didn't respect the requirement that the variable to store the "number of numbers" should be Number , so you did check the correct variable, but used the wrong to read the input into. 但是,您也没有遵守存储“数字数量”的变量应为Number ,因此您确实检查了正确的变量,但是使用了错误的输入输入。 This should fix this issue (I changed the variable name in the cin >>... line + moved the check outside your main loop): 这应该可以解决此问题(我在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)
{    
    ...
}
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM