Hi everyone:

I'm having problems constructing a loop and I'd appreciate any advice. This is probably a simple problem, but I just can't make it work! This isn't homework! I'm an old fart and I'm new to C++.

Here's what I want to do: take a vector and insert it as the column of a matrix.

Here's the problem: I have no problem doing this for a vector that has the same number of rows as the matrix. My problem is that the matrix contains extra rows. What I am having trouble doing is constructing a loop that puts the elements of the vector in the right rows of the matrix.

Specifically, I have not been able to construct a loop that divides my vector into three parts, then puts each part in the matrix with two empty matrix rows separating it from the next vector part. It would be great if someone could provide a simple example.

For example:
1) numRowsVector is the number of rows in the vector. Let's say it's 9.
numRowsMatrix is the number of rows in the matrix. It's 25.

2) numRowsVector needs to be divided into 3 parts (always--regardless of the number of elements it contains).

3) Then the first part of the vector needs to be put into the first column of the matrix. The first element of the vector should appear in matrix row = numRowsVector + 3 (indexing starts at 0):
E.g.,
numRowsMatrix [numRowsVector + 3] [1] = numRowsVector[0]

4) There should be two empty rows in the matrix that separate the first vector part from the second vector part. Then there should be two empty rows in the matrix that separate the second and third vector parts. Then the last rows of the matrix should be empty (the number of empty rows = numRowsVector + 2).

More detail: yesterday Fbody here at Daniweb kindly informed me that it is possible to use two iterators in one for loop. This solved one problem. But after trying many different things, I'm still stuck. Rather than post them all, I'm hoping that someone might be willing to post a simple example that will set me straight. Thank you.

Perhaps I'm missing something and oversimplifying, but does this not work?

for (int vi = 0, mi = 0; vi < numRowsVector && mi < numRowsMatrix; vi += 3, mi += 3)
{
    for (int i = 0; i < 3; i++)
    {
        m[mi][i] = v[vi + i];
    }
}

Hi Narue:

Thanks for your advice. Unfortunately, this doesn't work though.

I need to fill a matrix column with elements from a vector, but I need to skip certain rows/elements of the matrix.

For example, a vector has 9 elements. A matrix column has 25 rows. The first 3 elements of the vector must be placed into the seventh, eighth, and ninth rows. Then there must be two empty rows. Then the next three elements must be placed into the next three rows, then there must be two empty rows, then the final three elements must be placed.

I just can't figure out how to make a loop that does things at irregularly-spaced steps. If you have ever run into this, I would be very grateful for any pointers. Thanks again!

>Unfortunately, this doesn't work though.
How doesn't it work?

>I need to fill a matrix column with elements from a vector,
>but I need to skip certain rows/elements of the matrix.

My example does precisely that.

>For example, a vector has 9 elements. A matrix column has [...]
Okay, let's get more concrete. This is my vector:

{1,2,3,4,5,6,7,8,9}

This is my matrix:

{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0}

After the loop I gave you, this is the result:

{1,2,3},
{0,0,0},
{0,0,0},
{4,5,6},
{0,0,0},
{0,0,0},
{7,8,9},
{0,0,0},
{0,0,0}

How is that not what you want? If it's only a matter of placing vector items in the first column of each specified matrix row, the concept still works and only minor changes are necessary:

for (int vi = 0, mi = 7; vi < numRowsVector && mi < numRowsMatrix; vi += 3, mi += 5)
{
    for (int i = 0; i < 3; i++)
    {
        m[mi + i][0] = v[vi + i];
    }
}

So I can only conclude that you're either not understanding the code I offered, or you're being unintentionally obtuse in explaining the requirements.

It is likely that I am missing something here or my explanation is unclear...

I understand what you have done. Where I'm lost is, I don't just want ONE row filled at regular intervals. What I need is TWO rows SKIPPED at regular intervals. My vectors will have different numbers of rows for each calculation I do. But they will always have two rows left empty. For example:

{1,2,3},
{4,5,6},
{7,8,9},
{0,0,0},
{0,0,0},
{9,8,7},
{6,5,4},
{3,2,1},
{0,0,0}
{0,0,0}

Does this make sense?

@OP:
I think that either you don't understand how arrays are arranged or you're confusing the terms row and column and causing additional confusion. A 2-dimensional array is arranged thus in memory:

int anArray [9][3];  //declares a 3x9 "matrix"
         column:
         0  1  2
row 0: { 0, 0, 0 }
row 1: { 0, 0, 0 }
row 2: { 0, 0, 0 }
row 3: { 0, 0, 0 }
row 4: { 0, 0, 0 }
row 5: { 0, 0, 0 }
row 6: { 0, 0, 0 }
row 7: { 0, 0, 0 }
row 9: { 0, 0, 0 }

Based on this, what is the result you are looking for?

Unless you succeed in creating a multi-dimensional vector, vectors don't have "rows". They have "elements" stored sequentially in memory.

I misspoke. I should have used the term, elements. Sorry, I'm a newbie.

I have a vector. I have a matrix. The vector elements need to be stored in the matrix. A single vector goes into a single matrix column. However, transferring the vector elements into the matrix is not straightforward.

When the vector values are in the matrix, they need to be in groups of a certain size. Each group needs to be separated from the next group by two rows.

What my problem is, is I do not know how to write a loop that will do this. I know how to put values in an array or vector or whatever at an interval, but I don't know how to put an arbitrary number of values in an array or vector or whatever and also leave two spaces in the array that are blank at regular intervals.

Sorry--I don't know how else to describe this. If it still isn't clear, feel free to forget about it. Thanks for any help.

>What I need is TWO rows SKIPPED at regular intervals.
That's even easier. Now you're just filling in a two dimensional array using a one dimensional array, then incrementing the row counter by two at the end:

#include <iostream>

#define add_vector(m, rows, cols, next, v, vsize)\
do {                                             \
    int vi = 0;                                  \
    while (vsize - vi >= cols && next < rows)    \
    {                                            \
        for (int i = 0; i < cols; i++)           \
        {                                        \
            m[next][i] = v[vi++];                \
        }                                        \
        ++next;                                  \
    }                                            \
    next += 2;                                   \
} while (0)

int main()
{
    int v[9] = {1,2,3,4,5,6,7,8,9};
    int m[25][3] = {0};
    const int vsize = 9;
    const int rows = 25;
    const int cols = 3;
    int next = 7;

    add_vector(m, rows, cols, next, v, vsize);
    add_vector(m, rows, cols, next, v, vsize);
    add_vector(m, rows, cols, next, v, vsize);

    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            std::cout<< m[i][j] <<'\t';
        }

        std::cout<<'\n';
    }
}
commented: Thanks for your patience. +1

Narue:

This does it! I really appreciate all the time you and Fbody have spent. Thanks a lot to both of you for bearing with me!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.