2 d Arrays


d) Write the definition of the function copyGamma that sets the elements of the first row of
inStock to 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.


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
#include <ctime>
#include <cstdlib>.
#include <iostream>
using namespace std ;
void CopyGamma ( int inStock[] , int gamma [] , int InstouckArrayRows , int InstouckArrayCoulms , int GammaArraySize) ;
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};


CopyGamma(inStock[10],gamma,10,4,4) ;


return 0 ;

}



void CopyGamma( int inStock[10] , int gamma[] , int InstouckArrayRows , int InstouckArrayCoulms, int GammaArraySize )

{


  for (int i = 0 ; i < GammaArraySize ; i++ )
          inStock[0][i] = gamma[i]  ;



   for ( int i = 1 ; i <= InstouckArrayRows ; i++ )
      inStock[i][InstouckArrayCoulms] = 3*(gamma[GammaArraySize])  ;

}




When i call this function as CopyGamma(inStock,gamma,10,4) ; i have got some errors , i just want to know where is the error thanks !!!

Last edited on
Please use code tags (the <> button next to the entry window, put your code inside it) and try to leave out the line numbers because the forum adds them automatically, and these things really help with readability. The problem lies in where you begin your CopyGamma function. Unlike a one dimensonal array, which you can use with gamma[], with a 2d array you need to tell it the size of the first dimension. I haven't tried it, but I believe it will work if you change it to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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};

    srand (time(0)) ;
    InputArray(alpha,20) ;
    doubleArray ( alpha , beta , 20 ) ;
    CopyGamma(inStock,gamma,10,4) ;
    return 0 ;
}

void CopyGamma( int inStock[10][] , int gamma [] , int InstouckArrayRows , int InstouckArrayCoulms, int GammaArraySize )
{
    for (int i = 0 ; i < GammaArraySize ; i++ )
        inStock[0][i] = gamma[i] ;

    for ( int i = 1 ; i <= InstouckArrayRows ; i++ )
        inStock[i][InstouckArrayCoulms] = 3*(gamma[GammaArraySize]) ;
}
.
IT also appears that on line 12, where you call on CopyGamma, that you are not supplying enough parameters. You need to decide which positions of inStock to provide, which position of gamma to provide, and 3 integers, not 2.
Example:
CopyGamma(inStock[alpha][beta],gamma[3],10,4,7) ;
Not that these are the values you probably want, but this is how you would call the function as you have it set up now.
Last edited on




thank you for answering
but now i have an error in line number 19
i would like to post the code again but i cant do it in a blue box like what u did.
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
#include <ctime>
#include <cstdlib>.
#include <iostream>
using namespace std ;
void setZero(int anyaaray[] , int arrSize) ;
void InputArray( int alpha[] , int AlphaarrSize ) ;
void doubleArray ( int alpha[] , int beta[] , int arraSize ) ;
void CopyGamma ( int inStock[] , int gamma [] , int InstouckArrayRows , int InstouckArrayCoulms , int GammaArraySize) ;
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};

srand (time(0)) ;
InputArray(alpha,20)  ;
doubleArray (  alpha ,  beta , 20  ) ;
CopyGamma(inStock[10],gamma,10,4,4) ;


return 0 ;

}



void CopyGamma( int inStock[10] , int gamma[] , int InstouckArrayRows , int InstouckArrayCoulms, int GammaArraySize )

{


  for (int i = 0 ; i < GammaArraySize ; i++ )
          inStock[0][i] = gamma[i]  ;



   for ( int i = 1 ; i <= InstouckArrayRows ; i++ )
      inStock[i][InstouckArrayCoulms] = 3*(gamma[GammaArraySize])  ;

}








ok now the problems are in lines #36 and #41



come on guys please help
You may wish to invest some time into learning how to read compiler error messages. Most problems that we get on this problem can be solved as such.

inStock is not passed as a two-dimensional array on line 30. Therefore, your attempts to dereference it twice as such on lines 36 and 41 will yield compilation errors.

Also, a warning. Line 41 has an out-of-bounds error. Can you spot it?

-Albatross
thanks for answering





i tried to write it as void CopyGamma( int inStock[10][] , int gamma[] , int InstouckArrayRows , int InstouckArrayCoulms, int GammaArraySize )


and also for the prototype
void CopyGamma ( int inStock[][] , int gamma [] , int InstouckArrayRows , int InstouckArrayCoulms , int GammaArraySize) ;



it gives me an error
like

"deceleration of inStock as multidimentional array must have bounds for all dimensions except the first "
Last edited on
That's exactly what it says in the tin. inStock needs to have a bound (the number in the bracket for your first attempted declaration) for all the dimensions except the first (leftmost one).

-Albatross


ok






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
#include <ctime>
#include <cstdlib>.
#include <iostream>
using namespace std ;

void CopyGamma ( int inStock[][4] , int gamma [] , int InstouckArrayRows , int InstouckArrayCoulms , int GammaArraySize) ;
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};


CopyGamma(inStock[][4],gamma,10,4,4) ;


return 0 ;

}



void CopyGamma( int inStock[][4] , int gamma[] , int InstouckArrayRows , int InstouckArrayCoulms, int GammaArraySize )

{


  for (int i = 0 ; i < GammaArraySize ; i++ )
          inStock[0][i] = gamma[i]  ;



   for ( int i = 1 ; i <= InstouckArrayRows ; i++ )
      inStock[i][InstouckArrayCoulms] = 3*(gamma[GammaArraySize])  ;

}





the problem now in calling the function
Last edited on
heeeeeeeeeeeeeeeeeeeeeeeeellllllllllllllllllllllllllllllllllllllllllppppppppppppppppppppp meeeeeeeeee pleeeeeeeaseeeeee
Patience, young grasshopper. Posts like your last one tend to make people less likely to help.

You are dealing with 2D arrays, which requires some magic.

Before I can help further, though, I need to know what exactly you are trying to do to inStock with the CopyGamma() function.


Your major problems are:

- syntax errors (for example, line 21 CopyGamma(inStock,gamma,10,4,4);

- off-by-one errors (for example, 3*(gamma[GammaArraySize]) is the first element past the end of the gamma array.

The names you have chosen for things are not helping, either. You should name things in a way that helps you understand what they are and what they do.

Post back, okay?
hello Duoas ,



i have inStock arrays which is a matrix . with 10 rows and 4 columns !


first row : i want to copy gamma to the first row

the remaining 9 rows i want to copy 3* (gamma )

thats the problem


Last edited on
So InstouckArrayCoulms and GammaArraySize have to be the same? Or is it possible they are different?

And you still haven't answered what 3*(gamma) is.


The name of your function is not very specific. Even something bland like "SetFromGamma" would be less misleading.

And your array names are still just a, b, c... no useful information at all.

When dealing with multi-variable-sized multi-dimensional arrays, you have to treat the array as flattened, and manually calculate the offset. A helper function to do that:

1
2
3
4
5
6
7
8
9
10
size_t index_2d( size_t rows, size_t cols, size_t row, size_t col )
{
  // You don't have to check this stuff... and how you do depends on you. 
  // Here I just throw an exception, but you can return (size_t)(-1) or whatever 
  // you think effective.
  if ((row > rows) or (col > cols)) throw 1;  

  // Here's our equation
  return (row * cols) + col;
}

Now:

1
2
3
4
5
6
7
8
9
10
11
void WeirdAssignmentFunction( int* inStock, int rows, int cols, int* gamma, int size )
{
  if (cols != size) throw 1;  // or some other error?

  for (int c = 0; c < cols; c++)
    inStock[ index_2d( rows, cols, 0, c ) ] = gamma[ c ];

  for (int r = 1; r < rows; r++)
  for (int c = 0; c < cols; c++)
    inStock[ index_2d( rows, cols, r, c ) ] = 3 * gamma[ c ];
}

And, for good measure, a little C++ helper to call the function properly. It also requires gamma[] to have the same number of columns as inStock[][].

1
2
3
4
5
template <int Rows, int Cols>
void WeirdAssignmentFunction( int (&a)[ Rows ][ Cols ], int gamma[ Cols ] )
{
  WeirdAssignmentFunction( &a[ 0 ][ 0 ], Rows, Cols, gamma, Cols );
}

Call it easily enough:

 
  WeirdAssignmentFunction( inStock, gamma );

Hope this helps.

[edit] I don't know if I got the function right. Is that what you wanted?
Last edited on
ohhhhh my godd it finnaly works thanks Duoas your awesome


i really appreciate your time




thanksalot
Topic archived. No new replies allowed.