0

this is my first time posting on here, and I may not be doing this the right way, sorry in advance.

I am having trouble understanding how to copy two separate one dimensional arrays into one multidimensional array. It is part of my assignment, but I already turned it in and am probably going to lose points in it, but it's ok. As long as I get to understand how it's done, I'd still be happy.

The specific part I am having trouble with is section 5, I got everything else down, maybe it's because its been a long day, but I just can't seem to get it. I will post the whole assignment as well as my code. Thank you for taking the time to read all this if you did.

Write a C++ program that tests the function main and the functions discussed in parts 1 through 7. (Add additional functions, such as printing a two-dimensional array, as needed.).

Consider the following function main:

<code>int main()
{
     int inStock[10][4];
     int alpha[20];
     int beta[20];
     int gamma[4] = {11, 13, 15, 17};
     int delta[10] = {3, 5, 2, 6, 10, 9, 7, 11, 1, 8};
     .
     .

     .
}</code>
  1. Write the definition of the function setZero that initializes any one-dimensional array of type int to 0 (alpha and beta).

  2. Write the definition of the function inputArray that prompts the user to input 20 numbers and stores the numbers into alpha.

  3. Write the definition of the function doubleArray that initializes the elements of beta to two times the corresponding elements in alpha. Make sure that you prevent the function from modifying the elements of alpha.

  4. Write the definition of the function copyGamma that sets the elements of the first row of inStock from gamma and the remaining rows of inStock to three times the previous row of inStock. Make sure that you prevent the function from modifying the elements of gamma.

5. Write the definition of the function copyAlphaBeta that stores alpha into the first five rows of inStock and beta into the last five rows of inStock. Make sure that you prevent the function from modifying the elements of alpha and beta.

  1. Write the definition of the function printArray that prints any one-dimensional array of type int.

  2. Write the definition of the function setInStock that prompts the user to input the elements for the first column of inStock. The function should then set the elements in the remaining columns to two times the corresponding element in the previous column, minus the corresponding element in delta.

Here is my code:

<code>
#include <iostream>
using namespace std;

int setZero(int alpha[], int beta[]);
int inputArray(int alpha[]);
int doubleArray(const int alpha[], int beta[]);
int copyGamma(const int gamma[], int inStock[][4]);
void copyAlphaBeta(int inStock[][4], const int numberOfRows, const int 
alpha[], const int beta[], int numSize);
void printArray(int print[], int n);
int setInStock(int inStock[][4],const int delta[]);


int main()
{
    int inStock[10][4];
    int alpha[20];
    int beta[20];
    int gamma[4]={11,13,15,17};
    int delta[10]={3,5,2,6,10,9,7,11,1,8};

    setZero(alpha, beta);
    cout << "Alpha after initialization" << endl;
    printArray(alpha, 20);

    inputArray(alpha);
    cout << "Alpha after reading 20 numbers" << endl;
    printArray(alpha, 20);

    doubleArray(alpha, beta);
    cout << "Beta after a call to doubleArray" << endl;
    printArray(beta, 20);

    copyGamma(gamma, inStock);

    copyAlphaBeta(inStock, 10, alpha, beta, 20);
    for (int row=0; row<10;row++)
        for (int col=0; col<4; col++)
        {
            if (col%4==0)
                cout << endl;
            cout << inStock[row][col] << '\t';
        }
    cout << endl;

    setInStock(inStock, delta);

    return 0;
}

int setZero(int alpha[], int beta[])
{
    for (int i=0; i<20; i++)
    {
        alpha[i]=0;
        beta[i]=0;
    } 
    cout << endl;
}
int inputArray(int alpha[])
{
    cout << endl;
    cout << "Enter 20 integers" << endl;
    for (int i=0; i<20; i++)
    {
        cin >> alpha[i];
    }
    cout << endl;
    return 0;
}
int doubleArray(const int alpha[], int beta[])
{
    cout << endl;

    for (int i=0; i<20; i++)
    {
        beta[i]=alpha[i]*2;
    }
    cout << endl;
    return 0;
}
int copyGamma(const int gamma[], int inStock[][4])
{
    cout << endl;
    cout << "inStock after a call to copyGamma" << endl;
    for (int row=0;row<10;row++)
    for (int column=0;column<4;column++)
    {
        if (row==0)
            inStock[row][column]=gamma[column];
        else
            inStock[row][column]=3*inStock[row-1][column];
    }
    cout << endl;
    for (int r=0;r<10;r++)
    for (int c=0; c<4;c++)
    {
        if (c==0)
            cout << endl;
        cout << inStock[r][c] << '\t';
    } 
    cout << endl;
    return 0;
 }
void copyAlphaBeta(int inStock[][4], const int numberOfRows, const int 
alpha[], const int beta[], int numSize)
{
    cout << endl;
    cout << "inStock after a call to copyAlphaBeta" << endl;

    for(int r=0;r<numberOfRows;r++)
    {
        for(int c=0;c<4;c++)
        {
            for (int counter=0; counter<numSize; counter++)
            {
                if(r<5)
                {
                    inStock[r][c]=alpha[counter];
                }
                else if (r>=5)
                {
                    inStock[r][c]=beta[counter];
                }
            }
        }
    }

    cout << endl;
}
void printArray(int print[], int n)
{
    cout << endl;
    for (int i=0;i<n;i++)
        cout << print[i] << " ";
    cout << endl;
}
int setInStock(int inStock[10][4],const int delta[])
{

    int input;
    cout << endl;
    cout << "Enter 10 integers" << endl;

    for (int r=0;r<10;r++)
    for (int c=0;c<4;c++)
    {
        if(c==0)
        {
            cin >> inStock[r][c];
        }
        else
            inStock[r][c] = 2 * inStock[r][c-1] - delta[r];
    }
    for (int row=0; row<10;row++)
    for (int col=0; col<4; col++)
    {
        if (col%4==0)
            cout << endl;

        cout << inStock[row][col] << " ";
    }
}

TL;DR - Need help understanding past homework, I just want to know the proper way to do it. Also please critique, looking for ways to improve. Thank you!

Edit: The problem I have is with this part:

  1. Write the definition of the function copyAlphaBeta that stores alpha into the first five rows of inStock and beta into the last five rows of inStock. Make sure that you prevent the function from modifying the elements of alpha and beta.

and my code relating to this portion of the question is:

<code>
void copyAlphaBeta(int inStock[][4], const int numberOfRows, const int 
alpha[], const int beta[], int numSize)
{
    cout << endl;
    cout << "inStock after a call to copyAlphaBeta" << endl;

    for(int r=0;r<numberOfRows;r++)
    {
        for(int c=0;c<4;c++)
        {
            for (int counter=0; counter<numSize; counter++)
            {
                if(r<5)
                {
                    inStock[r][c]=alpha[counter];
                }
                else if (r>=5)
                {
                    inStock[r][c]=beta[counter];
                }
            }
        }
    }

    cout << endl;
}</code>

Thank you for the links, I am not allowed to use vectors at the moment, but I am definitely going to study up on them.

1
  • 1
    Could you narrow the question and the code to just the relevant parts? Commented Feb 5, 2018 at 0:02

2 Answers 2

1

First of all, if you're allowed to use std::vector you should use it. Read up on std::vector here for more details.

Vector provides a dynamically allocated (in the back) chunk of memory that can "expand" and change at runtime depending on adding/deleting elements. You might want to learn to use a vector as they are very useful.

However, you don't really need a vector to do your copying. You can use std::copy to do this for you. More info for it here: std::copy

You can use it to take 3 inputs for your case. First two are the beginning and end pointers, the last input is the starting pointer for the destination. You should be able to figure out what they are for your project.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for that, I will definitely read up on std::copy, it will help me in the future. I am currently not allowed to use vector, but I will also read up on that too.
0

In your nested for-loops, the last loop is wrong and it will repeatedly put different values of alpha[count] and beta[count] into a same inStock[r][c] element.

You can remove this loop and change the code like

for(int r=0;r<numberOfRows;r++)
    {
        for(int c=0;c<4;c++)
        {
                if(r<5)
                {
                    inStock[r][c]=alpha[c]; // assuming size of alpha is 4
                }
                else if (r>=5)
                {
                inStock[r][c]=beta[c]; //assuming size of beta is 4
                }
         }
    }

In this code, you have assumed inStock has 4 columns, these 4 columns of each row, are going to be filled with 4 elements of alpha and beta arrays.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.