how would i write a code that will take the values of one array and copy it to another?

ex: array1 and array2 have 100 elements

so would it be:

array1 [100] = array2 [100]

or does that just pass a reference?

Recommended Answers

All 8 Replies

What you wrote will only chage 101'th element of array1 with 101'th element of array2.

To copy arrays, you have to write function that will do that for each element

Do it in a loop

for(int i(0); i < SIZE_OF_ARRAY; ++i)
   array1[i] = array2[i];

and how would i take the sums of 2d array?
for the cols and rows?

ex sample [2][3]

With a nested loop.

for (int i(0); i < X_SIZE; ++i)
   for(int ii(0); i < Y_SIZE; ++ii)
      iSum += Array[ii][i];

X_Size is the size of the first dimension, Y_Size is the size of the second, and sum is an integer initialized to 0

i though it could also be done with out a nested loop.

btw-i want the sum for the rows and cols separatly. (so it shud display 2 results)

Lol. The sum of all rows = the sum of all cols. If you wanted to total each row or col seperately, that would be different. But it would display the number of rows + the number of cols amount of results (ie not 2, unless your taking about a 1x1 array >.< ).

how bout this?
assuming int days [29][5];

//sum row
for (int col = 0; col < 5; col++)
  total += days [row][col];

//sum col
for (int row = 0; row < 29; row++)
  total += days [row][col];

my compiler is not workin today so im downloading the software right now-so i need you to check in the meantime :D

Think of it this way, your code will only result in values being added for squares with a letter, while ignoring all the other values. If this is what's wanted then you are indeed right, but then what's the point of the rest of the data in the array? (assuming you reset cols to 0 after the first loop)


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.