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

// If the user enters Y or y, the outer loop begins 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
Brack
  • 333
  • 2
  • 14
Will McKenzie
  • 21
  • 1
  • 8

2 Answers2

2

Multidimensional Array:

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

in order to iterate through a Multidimensional Array you need 2 loops

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

Parallel Array:

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

iterating:

for(i=0; i<Row1.length; i++){
       Row1[i]
       Row2[i]
       Row3[i]
}
inxoy
  • 3,484
  • 2
  • 13
  • 21
  • Your answer makes it seem as if the number of loops needed to iterate is the major difference between parallel and multidimensional arrays. But one could equally well access all three rows of the multidimensional array within one loop as in your second example. – le_m Dec 05 '17 at 16:45
  • Thanks. Number of loops is something I hadn't considered. – Brack Jun 24 '18 at 15:29
0

As @inxoy mentioned a multidimensional array is basically a matrix.

    # 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)]

A parallel array is a form of implicit data structure that uses multiple arrays to represent a singular array of records (from 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      ]
goosfraba
  • 80
  • 1
  • 8
  • so how adds your answer more value to mine? – inxoy Oct 24 '17 at 20:24
  • I just tried to explain in other words, maybe some people will understand straight away with your answer maybe they won't. I've included some references also to the proper definition of a parallel array while you just gave an example without any context. Just trying to help whoever comes back here – goosfraba Oct 24 '17 at 20:27
  • thanks and maybe the won't get the context with yours so. :D – inxoy Oct 24 '17 at 20:28
  • So, multidimensional adds columns where parallel are only rows, since multidimensional is more of a matrix? So, in theory I could use multidimensional when only using bookName bookCost and newPrice, but it would be a waste of memory since the extra arrays created would be wasted? – Will McKenzie Oct 24 '17 at 20:37
  • Also, I've seen many examples where the values are manually entered for multidimensional arrays, but how much coding would it take to enter those values via console or scanner? It seems like parallel is more simplistic, but I can't seem to grasp when to use one over the other. – Will McKenzie Oct 24 '17 at 20:57
  • It truly depends on the use case you are facing. I agree that parallel arrays are a lot more simplistic but maybe less maintainable and structured than multidimensional arrays. Maybe you can share some code samples of your use case and what you did and people can help on analysing your solution and giving you their thoughts on the best approach for it. – goosfraba Oct 24 '17 at 20:59
  • I added the Pseudocode I wrote. It asks to be wrote in parallel which I have already done, so it doesn't need changed, but I'm curious about using multidimensional and whether it would be worth my time. – Will McKenzie Oct 24 '17 at 21:16
  • Hi, In your use case I would say it is more explicit to use the parallel arrays because you explicitely know that the first array is for product names and the second one for product cost. That wouldn't be explicitely done with the multidimensional array. An option to workaround this would be to use an array of objects / dictionaries. Something like: products = [ {'name':'whatever_prod_1','code':'another_code_1'}, {'name':'whatever_prod_2','code':'another_code_2'} ] – goosfraba Oct 25 '17 at 07:02