C ++我可以在这里使用数组来缩短代码吗?

PC游戏王

这是我在这里的第一篇文章,所以请不要因为我的愚昧而杀了我。

我最近制作了一个有趣的程序,可以输入大量数字,并给出平均值,虽然不是很有用,但我想我会看看是否可以。如果有人可以向我解释如何使用数组而不是大量变量来改善代码,但仍然可以达到相同的目的,甚至可能更有效,那我将非常乐意。

我的代码如下所示:

#include "stdafx.h"
#include <iostream>

using namespace std;

int main() {

    int q1;
    int q2;
    int q3;
    int q4;
    int q5;
    int q6;
    int q7;
    int q8;
    int q9;
    int q10;
    int q11;
    int q12;

    int f;
    //Used for the total of all values

    int t;
    //Used for the total to be divided

    int a;
    //Used for dividing the numbers.
    cout << "We will be finding a mean. Enter the amount of numbers that will be        entered, the maximum is 12: ";

    cin >> a;

    cout << "Now enter what numbers you want to find the mean for, because the maximum is 12, if you have less than 12, enter 0 for the rest: ";
    cin >> q1;
    cin >> q2;
    cin >> q3;
    cin >> q4;
    cin >> q5;
    cin >> q6;
    cin >> q7;
    cin >> q8;
    cin >> q9;
    cin >> q10;
    cin >> q11;
    cin >> q12;

    f = q1 + q2 + q3 + q4 + q5 + q6 + q7 + q8 + q9 + q10 + q11 + q12;

    cout << f / a << '\n';


    system("pause");
}

任何建议都非常感谢!这是在Visual Studio中制作的,以防万一您需要知道。

马特·C

当然,数组可以使您的生活更轻松!

这是使用数组完成与上述相同任务的方法:

#include "stdafx.h"
#include <iostream>

using namespace std;

int main() {

    int totalNums;
    cout << "We will be finding a mean.\n";
    cout << "You can only enter up to 12 numbers;

     // Declare an array to hold 12 int's
    int nums[12];

    // i will count how many numbers have been entered
    // sum will hold the total of all numbers
    int i, sum = 0;
    
    for(i = 0; i < 12; i++) {
        cout << "Enter the next number: ";
        cin >> nums[i];
        sum += nums[i];
    }    

    cout << "The mean is: " << (sum / totalNums) << '\n';


    //Try to avoid using system!
    system("pause");
}

但是,为什么要使用数组?

将数字加到总数后,无需保留任何数字,那么为什么要使用数组呢?

您可以不使用数组而只用一个数字变量来完成相同的任务!

#include "stdafx.h"
#include <iostream>

using namespace std;

int main() {

    int totalNums;
    cout << "We will be finding a mean.\n";
    cout << "Enter the amount of numbers that will be entered: ";
    cin >> totalNums;

    // i will count how many numbers have been entered
    // sum will hold the total of all numbers
    // currentNum will hold the last number entered
    int i, sum = 0, currentNum = 0;
    
    for(i = 0; i < totalNums; i++) {
        cout << "Enter the next number: ";
        cin >> currentNum;
        sum += currentNum;
    }    

    cout << "The mean is: " << 1.0 * sum / totalNums << '\n';


    //Try to avoid using system!
    system("pause");
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

C 代码不断闪烁错误,想知道我是否可以在这里得到帮助?

我的 C++ 合并排序代码不起作用。我在这里缺少什么?

C语言新手-我在这里做不安全的事情吗?

C-呼叫getcwd在这里引起错误吗?

由于我不能在 C# 中使用带有隐式变量的 lambda 函数,我可以在这里替换什么?

c中的链表(我在这里做错了什么?)

我可以在这里不使用线程同步吗?

我可以在这里使用.select(&)方法吗?

需要帮助缩短我的代码!!(菜鸟在这里)

Objective-C,为什么我需要在这里而不是在这里放星号

C ++ std :: move在这里不好?

C++ 互斥体可以交叉方法/变量吗?如果是这样,为什么它在这里不起作用?

多重定义,首先在这里定义C代码错误

我可以在这里避免模板递归吗?

我可以在这里重载<<操作符吗?

我可以在这里摆脱嵌套循环吗?

我可以在这里避免沮丧吗?

我可以在这里制作Thread.stop()吗?

我在这里用什么代替 int ?C# CMD

我可以在这里使用for循环,而不用编写数百行代码。

复制交换习语 - 我们可以在这里使用动态转换操作吗?

.map 不是一个函数(我真的没有在这里使用数组吗?)

可以在这里使用ggplot的构面吗?

我不能在这里使用graphshortestpath函数吗?

在c ++中使用operator关键字在这里意味着什么?

方法级联可以在这里吗?

我的代码在这里怎么了?

我在这里泄漏吗?

c++ 为什么 cout 在这里如此重要?