【问题标题】:For loop for differences column n + 1 and column n对于差异列 n + 1 和列 n 的 for 循环
【发布时间】:2020-06-10 10:20:42
【问题描述】:

我想创建一个 for 循环: - 计算每一列的总和 - 从列 x+1 中减去列 x 的值

我以前从未做过 for 循环,所以我有点卡在这里。 Example of the columns with the values

谁能帮帮我?

【问题讨论】:

  • 嘿@Wouter 欢迎堆栈溢出。从您的问题来看,您似乎是编程新手,所以我认为您自己先尝试问题会更好,如果这不起作用,请在此处发布该解决方案,以便我们为您提供帮助。祝你好运
  • 您想使用哪种语言?您的数据值是否来自文本文件?
  • 我使用 R,我的数据值来自 .csv 文件
  • 我建议从这个开始:datamentor.io/r-programming/for-loop

标签: loops for-loop sum col


【解决方案1】:

这里有一些入门代码,以防您选择使用 C。

int columnSum( int colNum )
{
    int theSum = 0;
    //  TBD
    return theSum;
}

int main( int argc, char* argv[] )
{
    int firstCol = 0;
    int lastCol = 4;
    for ( int col = firstCol; col <= lastCol; col++ )
    {
        int sum = columnSum( col );
    }
    //  You probably want to save those sums above into an array.
    //  Here, you could do your subtractions with your saved sums.
}

【讨论】:

    【解决方案2】:
    1/22/2020 1/23/2020 1/24/2020 1/25/2020 1/26/2020 1/27/2020 1/28/2020 1/29/2020 1/30/2020 
      555       653       941      1434      2118      2927      5578      6166      8234 
    

    所以基本上我想减去 653 - 555、941 - 653 等,但不是手动。

    【讨论】:

    • 您在此处发布的内容最好作为对原始帖子的编辑,而不是作为答案。这是一个澄清,但不是真正的答案。
    猜你喜欢
    • 2018-09-08
    • 2021-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    相关资源
    最近更新 更多