Take elements from 1d Array and put them into 2d array

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.

My question is how do I this?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <cmath>
using namespace std;

const in COLUMNS = 4;
const int 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

(i'd put more up but it gets cut off here so)
Last edited on
What output? Your program does not have any output statements. (Well, your code doesn't even compile.)

Your function copyGamma() does not do anything concrete. Lines 26 and 31 do nothing.
yeah i'm sorry I added that in a rush, As for it not compiling it does on mine so i'm not sure why it wouldn't work for you.

EDIT: Rand program to get full output.


inStock Index: 0Element: -858993460 inStock Index: 1Element: -858993460 inStock Index: 2Element: -858993460 inStock Index: 3Element: -858993460
inStock Index: 1Element: -858993460 inStock Index: 2Element: -858993460 inStock Index: 3Element: -858993460
inStock Index: 2Element: -858993460 inStock Index: 3Element: -858993460
inStock Index: 3Element: -858993460

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.
Last edited on
What do the lines 35 and 40 do?

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?




UPDATE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include<cmath>
using namespace std;

const int COLUMNS = 4; 
const int ROWS = 10; 

void copyGama(int tempArray1[], int a_size, int tempArray2[][COLUMNS], const int ROWS);

int main()
{
 int inStock[10][4],
 gamma[4] = { 11, 13, 15, 17 },
 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 copyGama(int tempArray1[], int a_size, int tempArray2[][COLUMNS], const int ROWS) 
{ 
	
	for (int i = 0; i < a_size; i++)
	{ 	
		int copyArray[4];
		copyArray[i] = tempArray1[i];
		cout << "copy index: " << i << " elemenet: " << copyArray[i] << endl;

		for (int j = i; j < ROWS; j++)
		{
			for (int k = 0; k < COLUMNS; k++)
			{
				tempArray2[j][k] = copyArray[i];
			}
		}

	}
}


Okay so I got part of right, The elements go into the array and when output produce this.

EDIT cleaned output:

11111111
131313
1515
17

What I now need to figure out is how to make sure that these elements only go into the first row, any suggestions on that?
Last edited on
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 values 4 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.
Wow.

Just wow.

Don't bother wasting any more of your own time I'm not going to ever use this site again lol
Topic archived. No new replies allowed.