I need to take the elements from a one dimensional array (gamma[]) and put them into the first row of a two dimensional array (intStock[][]). I then need to set the remaining rows of inStock[][] to three times the previous Row. This also has to be done in a function which means both arrays have to be parameters, I can't use a pointer, but I'm sure I can use a reference parameter.
#include <iostream>
#include <cmath>
usingnamespace std;
const in COLUMNS = 4;
constint ROWS = 10;
void copyGamma(int tempArray1[], int a_size, int tempArray2[][COLUMNS], int ROWS);
int main()
{
int inStock[10][4],
gamma[4] = { 11, 13, 15, 17 };
int Array_Size = 4;
copyGama(gamma, Array_Size, inStock, ROWS);
for (int i = 0; i < 10; i++)
{
for (int j = i; j < 4; j++)
{
cout << "\tinStock Index: " << j << "Element: " << inStock[i][j];
}
cout << "\n";
}
return 0;
}
void copyGamma(int tempArray1[], int a_size, int tempArray2[][COLUMNS], int ROWS)
{
for (int i = 0; i < a_size; i++)
{
tempArray1[i];
for (int j = i; j < ROWS; j++)
{
for (int k = 0; k < COLUMNS; k++)
{
tempArray2[j][k];
}
}
}
/*
The elements from array gamma should go into the first row
of the 2d Array inStock and then the remaining rows of inStock need
to be set to three times the previous row of inStock.
How do I go about doing this?
*/
}
When I run the above code I get an output that shows the values stored in array gamma did not go into the inStock array and each line that's printed drops 1 column.
Example:
index 0: element: -8267547625 index 1: element: -8267547625 index 2: element: -8267547625
hehe today is not my day, I made a typo the print statement that produces this output is not coming from gamma it's coming from inStock so I eddied that bit.
Why do you have three nested loops in copyGamma?
It should do two operations:
1. Copy elements from teampArray to somewhere.
2. Fill in (remaining) elements of a 2D array.
Neither needs tripple loop. Latter needs two loops.
Why do you have three nested loops in copyGamma?
It should do two operations:
1. Copy elements from teampArray to somewhere.
2. Fill in (remaining) elements of a 2D array.
Why? Because I'm not sure how to process 2d arrays, so by this comment I see that it can be done in two operations, thank you for pointing that out.
1. So I should create another temporary array to store those values so that I can eliminate the nested loops? Do you have a sugesstion as to how to do this?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
for (int i = 0; i < a_size; i++)
{
int copyArray[4];
copyArray[i] = tempArray1[i];
cout << "copy index: " << i << " element: " << copyArray[i] << endl;
}
/* Copies elements in tempArray1[] into copyArray[] Produces the following output, which
is correct.
copy index: 0 element: 11
copy index: 1 element: 13
copy index: 2 element: 15
copy index: 3 element: 17
*/
//How can I put those values into a 2d Array?
You still have three nested loops in the copyGama.
Loop on lines 42-45 is inside loop of 40(-46), which is inside 34(-48).
Compare that to two nested loops on lines 18-25 that succeed in iterating through every element of the 2D array.
Lets digress and look at a different mess that is brewing. Lines 5-6 define two constants. Fine. Where are they used? Whera are they not used?
Lines 12, 13, 14, 18, 20, and 36 do use magic values4 and 10. Why?
Are the Array_Size, a_size and COLUMNS entirely unrelated?
Is it intuitive to call an argument of function with name "ROWS", when the global scope does already have a constant names "ROWS".
The copyArray is unnecessary. The tempArray1 is not changed within copyGama(). You set value of copyArray on line 37 and use it on lines 37 and 44, but do not change it. You could use element tempArray1 directly.