【问题标题】:Storing data into array将数据存储到数组中
【发布时间】:2021-04-27 08:26:19
【问题描述】:

我是一个自学者,我正在参加免费的在线课程。我正在尝试将 everyOther 的值放入 an array 中,以便以后可以访问它。我浏览了互联网,但找不到任何足智多谋的东西。你能告诉我如何将 everyOther 的输出值存储到一个数组中。提前致谢。

#include <cs50.h>
#include <stdio.h>
#include <math.h>

long countDigit(long long n);

int main(void)
{
    long n;

   //This is asking for the input
    do
    {
        n = get_long("Number: ");
    }
    while(!(countDigit(n)>13));

    //Checksum math
    long everyOther = 0;

    while(n > 0)
    {
        long lastNumber = n/10;
        everyOther = lastNumber % 10;
        n = n / 100;
        printf("%li\n", everyOther);
    }

}

//This function helps us with the counting of the number
long countDigit(long long n) {
  return floor(log10(n) + 1);
}

【问题讨论】:

  • 标记 C 和 C++ 时要小心。它们是不同的语言。它们都起源于 1980 年代的 C,但都偏离了那个共同的祖先。
  • 声明一个数组,声明一个初始化为 0 的索引,将 everyOther 存储到当前索引处的数组中,然后增加索引。您对这些步骤中的哪一个有困难?我很难相信您找不到任何将值存储到数组中的示例。
  • @kaylum 如何将 everyOther 存储到当前索引处的数组中?
  • my_array[index] = everyOther;
  • 理想情况下,您应该从一本好书学习 C++。它涵盖了数组等基础知识!

标签: c++ arrays c cs50 storing-data


【解决方案1】:

使用std::vector:

#include <cs50.h>
#include <math.h>
#include <stdio.h>
#include <vector>

long countDigit(long long n);

int main(void) {
  long n;

  // This is asking for the input
  do {
    n = get_long("Number: ");
  } while (!(countDigit(n) > 13));

  // Checksum math
  long everyOther = 0;

  std::vector<long> destination;

  while (n > 0) {
    long lastNumber = n / 10;
    everyOther = lastNumber % 10;
    n = n / 100;
    printf("%li\n", everyOther);

    destination.push_back(everyOther);
  }
}

// This function helps us with the counting of the number
long countDigit(long long n) { return floor(log10(n) + 1); }

当您迭代随机数的数字时,很难有效地使用std::array,因为它在编译时设置了大小。但是,您可以创建一个巨大的数组,并将其用作缓冲区。

#include <array>
#include <cs50.h>
#include <math.h>
#include <stdio.h>

#define BUFFERSIZE 65536

long countDigit(long long n);

int main(void) {
  long n;

  // This is asking for the input
  do {
    n = get_long("Number: ");
  } while (!(countDigit(n) > 13));

  // Checksum math
  long everyOther = 0;

  std::array<long, BUFFERSIZE> destination;

  size_t i = 0;
  while (n > 0) {
    long lastNumber = n / 10;
    everyOther = lastNumber % 10;
    n = n / 100;
    printf("%li\n", everyOther);

    destination[i++] = everyOther;
  }
}

// This function helps us with the counting of the number
long countDigit(long long n) { return floor(log10(n) + 1); }

【讨论】:

    【解决方案2】:

    如果我是你,我会在顶部 #include &lt;vector&gt;,然后声明 std::vector&lt;long&gt; vectorArray;。然后,您可以将值放入向量中,该向量是一个动态大小的数组。应该说这是一个 C++ 特性,在 C 中不起作用,但由于这篇文章被标记为两者,我假设它会很好。

    // #include <cs50.h>
    #include <iostream>
    #include <stdio.h>
    #include <math.h>
    #include <vector>
    
    long countDigit(long long n);
    
    int main(void)
    {
        long n = 0; // initialise n
    
        //This is asking for the input
        do
        {
            std::cin >> n; // using this as input
        }
        while(!(countDigit(n)>13));
    
        //Checksum math
        long everyOther = 0;
    
        std::vector<long> vectorArray;
        
        while(n > 0)
        {
            long lastNumber = n/10;
            everyOther = lastNumber % 10;
            vectorArray.push_back( everyOther ); // this appends everyOther to the end of the array
            n = n / 100;
            printf("%li\n", everyOther);
        }
    
        for ( auto x : vectorArray )
            std::cout << x << ' ';      // sample printout of vector's contents
        
    }
    
    //This function helps us with the counting of the number
    long countDigit(long long n) {
      return floor(log10(n) + 1);
    }
    
    

    【讨论】:

      【解决方案3】:

      C++ 数组,一旦创建了一定长度,就不能扩展或收缩。在最大化空间效率方面,这是一个优势,但通常会限制您的代码。

      在您的情况下,由于您正在接受长度不可预测 (>13) 的用户输入,因此您可能会创建一个没有足够空间的数组。

      你有两个选择:

      1. 对用户的输入制定规则并相应地创建数组,例如,如果您确保最多有 20 位,那么您的数组长度不能超过 20

        long lastNumber = n/10; everyOther = lastNumber % 10;code.

      everyother 放入数组中的方法是首先通过long foo [20] 创建一个长度为例如20 的数组,然后每次使用foo[i] = everyother 将每个人都转移到数组中,记住在@ 上保持计数器987654325@ 迭代到下一个单元格。

      1. 使用vector 代替数组,它基本上是系统为您处理空间限制的动态数组。这不是最有效的,但却是万无一失的。要使用它,首先#include &lt;vector&gt;,然后简单地用vector&lt;long&gt; foo; 声明一个向量 然后在 while 循环的每次迭代中使用 foo.push_back(everyother) 添加每个人。

      要重用这两种方法,只需调用foo[index_of_position] 以提取存储的所有其他信息。

      请记住,您尝试存储在这些结构中的每个数据类型都必须与创建它们的类型相匹配(大多数情况下)。例如,vector&lt;int&gt; 不会接受 string 对象。

      【讨论】:

      • 我在 c 中做,而数组方法,我在做增量 'storing[i] = everyOther;'使用计数器,它不会存储它们。
      • @Samjohns081998 你能详细说明一下吗? C 语言语法与 C++ 略有不同,它可能不会应用我在此处编写的完全相同的代码。您可以尝试在 C++ 中进行测试吗?或者你能告诉我它给你什么错误吗?编译错误?你先创建了一个数组吗?
      • 当我尝试打印数组时,这是它显示的内容,Element[1] = 0 Element[2] = 0 Element[3] = 0 Element[4] = 0 Element[5 ] = 0
      • @Samjohns081998 似乎您创建了一个数组但没有存储它们。将代码放在错误的位置可能是一个问题。你可以在everyOther = lastNumber % 10; 之后立即尝试foo[i] = everyother 吗?当然,用 i 代替迭代。使用 for 循环或其他。
      猜你喜欢
      • 2012-01-13
      • 2012-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      • 2021-04-09
      • 2011-07-19
      相关资源
      最近更新 更多