【问题标题】:What are the differences between parallel and multidimensional arrays并行数组和多维数组有什么区别
【发布时间】:2018-04-05 18:25:36
【问题描述】:
//Ask the user to start the program (the outer loop)
output “To begin, enter Y or y. To end the program, enter the letter N:”
input getUserDecision

//如果用户输入Y或y,外循环开始 while (getUserDecision = "Y") OR (getUserDecision = "y")

  //Get the product names and costs
  //Can enter up to 10 products and their cost
for num count = 0 to 9 Step 1 //Declare count here

    output  “Enter product name:” //Prompt user
         input productName[count] //Input product name

         output “Enter product cost: $” //Prompt user 
         input productCost[count] //Input product price

  end for

【问题讨论】:

  • 代码是否与您的问题相关?

标签: multidimensional-array parallel-arrays


【解决方案1】:

正如@inxoy 提到的,多维数组基本上是一个矩阵。

    # list containing 3 lists, each of 4 items, all set to 1
    w, h = 4, 3;
    Matrix = [[1 for x in range(w)] for y in range(h)]

并行数组是一种隐式数据结构形式,它使用多个数组来表示单个记录数组(来自https://en.wikipedia.org/wiki/Parallel_array

    first_names   = ['Joe',  'Bob',  'Frank',  'Hans'    ]
    last_names    = ['Smith','Seger','Sinatra','Schultze']
    heights_in_cm = [169,     158,    201,      199      ]

【讨论】:

  • 那么如何为我的答案增加更多价值?
  • 我只是试着换一种说法,也许有些人会马上明白你的回答,也许他们不会。我还包含了一些关于正确定义并行数组的参考资料,而您只是给出了一个没有任何上下文的示例。只是想帮助回到这里的人
  • 谢谢,也许他们不会得到你的上下文。 :D
  • 所以,多维添加列,其中并行只是行,因为多维更像是一个矩阵?所以,理论上我可以在只使用 bookName bookCost 和 newPrice 时使用多维,但是这会浪费内存,因为创建的额外数组会被浪费?
  • 另外,我见过很多手动输入多维数组值的例子,但是通过控制台或扫描仪输入这些值需要多少编码?似乎并行更简单,但我似乎无法掌握何时使用其中一个。
【解决方案2】:

多维数组:

Table = [
         [1,2,3],
         [4,5,6],
         [7,8,9]
        ]

为了遍历多维数组,您需要 2 个循环

for(row in Table) {
    for(column in row) {
        //do smth       
    }
 }

并行数组:

Row1 = [1,2,3]
Row2 = [4,5,6]
Row3 = [7,8,9]

迭代:

for(i=0; i<Row1.length; i++){
       Row1[i]
       Row2[i]
       Row3[i]
}

【讨论】:

  • 您的回答看起来好像迭代所需的循环数是并行数组和多维数组之间的主要区别。但是,在第二个示例中,同样可以在一个循环中访问多维数组的所有三行。
  • 谢谢。循环数是我没有考虑过的。
猜你喜欢
  • 1970-01-01
  • 2010-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-07
相关资源
最近更新 更多