0

Here's the question I've been given:

Create a 4X3 integer array and fill it, column by column using loops, with the odd numbers starting with 1. In a separate, one dimensional array, store the average of each column of the 4X3 array. Output the 4X3 array (as a 4X3 array) and output the average of each column underneath each column. Label these as the average.

I just learned that I can make an array like this: N[4][3], but should I even do that here? I feel like setting up an array for each range: 1-4, 5-8 & 9-12, would work better so I can average them at the end.

I'm unfamiliar with multidimensional arrays, so please let me know if this is where I should start (I realize this isn't my idea, where I split them up that I mentioned earlier. I'm still unsure about this, in case you can't tell).

int X, N[3][4]={1};

for(X=1; X<12; X++) {
    N[X]= N[X-1]+2;
}

As far as I know, the tables are auto-filled, where each row is filled in order, rather than each column. How do I fill them in columns? I think that the splitting-them-up strategy would do this for me, correct me if I'm wrong.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Santino
  • 23
  • 5
  • By definition, a 4x3 array is a multi-dimensional array, so yes - you have to use the type of array you've indicated. You're going to need to go back to your lessons for the proper syntax for filling the array, based on the loop you've posted. Just as a heads up: be careful not to exceed the bounds of the array when indexing using `x + 1`, – Ken White Nov 26 '20 at 01:08
  • 2
    Tip: Put your iterator variables inside the loop, and also use *lower case* for variables, like `for (int x = 1; x < 12; x++)`. – tadman Nov 26 '20 at 01:09
  • 1
    Since this is a multi-dimensional array you need to reference it like `N[i][j]` where `i` and `j` are the indexes. It's not a singular array per-se. – tadman Nov 26 '20 at 01:10
  • @KenWhite the lesson I got on multidimensional arrays didn't appear to be very relevant/helpful for this problem, that's why I'm here lol. would you mind expanding on what you mean by not exceeding the bounds of the array? like keeping it less than 12? Also: im not gonna lie which array are you referring to, the one i described with multiple small arrays or the one I actually coded out? – Santino Nov 26 '20 at 01:24
  • @tadman wait not to sound stupid but what iterator variable are you referring to when you say I should include it in my loop? As for the i/j portion, I should like create an array for both i and j?? incase you can't tell I'm pretty lost – Santino Nov 26 '20 at 01:27
  • If you're navigating a 2D array you should be navigating it two-dimensionally, where `i` and `j` are arbitrary but conventional names for such iterators. You declared it as `N[m][n]` so you need `N[i][j]` for access, not just `N[i]` which yields a type of `int[4]`. – tadman Nov 26 '20 at 01:29
  • All of the concepts related to multimensional arrays should be fully explained in every C++ textbook, but unfortunately stackoverflow.com is not really a good replacement for a C++ textbook. If you want to learn how to use them, your textbook will be the best resource for you to look at. Is there anything ***specific*** in your textbook, regarding this, that's unclear to you? – Sam Varshavchik Nov 26 '20 at 01:37
  • @SamVarshavchik problem is I don't have a textbook, my professor releases notes that are on the topic of what the question is, but the example she uses is less confusing than this – Santino Nov 26 '20 at 02:21
  • C++ is the most complicated general purpose programming language in use today. Unfortunately, without a textbook that explains C++ in a guided, structured, organized approach, with plenty of sample code examples accompanied with explanations, it will be rather difficult to learn it and understand it. I don't know what kind of a class does not use a textbook, but it seems to be a rather unproductive way to learn C++. – Sam Varshavchik Nov 26 '20 at 02:25
  • @SamVarshavchik yeah tell me about it... my best guess is its been thrown off because of covid – Santino Nov 26 '20 at 02:45
  • @Santino while I wouldn't explicitly encourage piracy, you'd be amazed how far you can get by googling [the name of the book you want](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) plus "pdf" – alter_igel Nov 26 '20 at 03:24

1 Answers1

0

An important thing to keep in mind is that when you're using a for loop to access elements of a one-dimensional array defined like this for example for(int i=0; i<n; i++), i is the index of the array that you're using to access a specific element.

In the case of a two-dimensional array you would need 2 for loops (one nested inside the other) - the first will be for the index of the row, and another which will be for the index of the column. Here's an example of how you'd populate a two-dimensional array (iterating column by column) with dimensions MxN where all elements would be zero:

int matrix[M][N];
for(int j=0; j<N; j++) {
   for(int i=0; i<M; i++) {
       //j is an index for the column
       //i is an index for the row
       matrix[i][j] = 0;
   }
}

To populate it with odd numbers, you could use a separate variable initialized on 1, and increase its value by 2 for each new element in the matrix.

D. D.
  • 97
  • 1
  • 2
  • 10