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?

Dani AI

Generated

, array1[100] = array2[100]; does not copy both arrays. It tries to write a single element at index 100, which is out of range for a 100-element array (valid indices are 0..99). Also, C-style arrays cannot be assigned wholesale (array1 = array2; is illegal). Either copy element-wise or use the standard library. A simple and safe one-liner is std::copy(array2, array2 + 100, array1); (include <algorithm>). Even better, prefer std::array<int,100> or std::vector<int> so you can just do a1 = a2;.

For your 2D totals, you can compute row sums and column sums in one pass. This avoids reusing the same total and accidentally mixing results.

#include <array>

constexpr int R = 29, C = 5;
int days[R][C] = { /* ... */ };

std::array<int, R> rowSum{};  // zero-initialized
std::array<int, C> colSum{};

for (int r = 0; r < R; ++r)
  for (int c = 0; c < C; ++c) {
    int v = days[r][c];
    rowSum[r] += v;
    colSum[c] += v;
  }

// rowSum[r] holds the sum of row r; colSum[c] holds the sum of column c

Notes and gotchas:

  • The grand total of all elements is the same whether you add by rows or by columns, but per-row and per-column totals are different arrays of results, not single numbers (as hinted).
  • Reset accumulators before each new computation, and ensure your loop bounds match the actual sizes.
  • Watch for typos in nested loops; the inner loop should compare and increment its own index.
  • If you ever store non-trivial objects (not plain ints), use std::copy or container assignment; avoid memcpy.

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)

XAAAAAAAAA
B0000000000
B0000000000
B0000000000
B0000000000
B0000000000

Be a part of the DaniWeb community

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