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>
Write the definition of the function setZero that initializes any one-dimensional array of type int to 0 (alpha and beta).
Write the definition of the function inputArray that prompts the user to input 20 numbers and stores the numbers into alpha.
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.
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.
Write the definition of the function printArray that prints any one-dimensional array of type int.
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:
- 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.