并行和多维数组之间有什么区别

时间:2017-10-24 20:14:33

标签: multidimensional-array parallel-arrays

//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

2 个答案:

答案 0 :(得分:1)

多维数组:

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]
}

答案 1 :(得分:0)

正如@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      ]