So here's another interesting task. I know, i'm on the right path, just something doesn't work here. Basicly I have to sum rows and columns of 2d array and then store the results in separate arrays, as far as the code is now, can see it quite clearly. any advice?!
#include <iostream>
using std::cout;
int main()
{
int array[6][7], columntotal[6] = {0}, rowtotal[7] = {0}; //I think descriptive names are good.
int x, y, z = 1; //x & y are just used to iterate around loops, z will be used to fill up the array with some numbers.
for(x = 0; x < 6; ++x) //Just fill up the array with some numbers.
{
for(y = 0; y < 7; ++y)
{
array[x][y] = z;
++z;
}
}
for(x = 0; x < 6; ++x) //Sum the columns.
{
for(y = 0; y < 7; ++y)
{
columntotal[x] = columntotal[x] + array[x][y];
}
}
for(x = 0; x < 6; ++x) //Output the value of each column.
{
cout << columntotal[x] << "\n";
}
return 0;
}
That will sum columns. I'll leave it to you to figure out how to sum the rows. Btw, you are not using any functions from <cmath> so no need to include that header file.
As for the problems in your original code:
1 2 3 4
sum1+=array[x];
sum2+=array[y];
When accessing the elements in a 2d array, you must always refer to both dimensions to get the information.
sum1 += array[x][0];
Would be valid for example.
1 2
strcpy (array1,sum1);
strcpy (array2,sum2);
The way you are using strcpy here is incorrect. Also, strcopy requires inclusion of <cstring>. Hmm anyway I suggest reading about what strcopy does: http://www.cplusplus.com/reference/cstring/strcpy/
And not using it in this program, because you don't need to.
You should also avoid using:
system ("pause");
Try perhaps to use getchar() or something and try not to use system("anything").
Great! thanks all your advices helped to get sums properly now and sort out all the mess in my code :) but still I have to sleep on this! This is a task for school therefore i have teachers rules to follow, yes i can use <cstring>, guess that would make things easier, functions there are quite useful.
Why? Its just school homework. If its there, and easy to use... whats the problem?
IRL, you won't use console for output. (Whens the last time you've seen a console application on sale?). So, using system("anything") won't hurt u once you get out of classrooms either...
Why? Its just school homework. If its there, and easy to use... whats the problem?
http://www.cplusplus.com/forum/articles/11153/
It's never too early to start good coding habits, unless you want to be the guy responsible for your company's million dollar law suit because of a vulnerability you neglected. You can never take security serious enough and part of the problem starts with people learning unsafe habits during their education.
threeright wrote:
IRL, you won't use console for output. (Whens the last time you've seen a console application on sale?). So, using system("anything") won't hurt u once you get out of classrooms either...
I take it you're a windows-user. On Unix/Linux, which are still the main part of the servers out there, command line utilities are the norm rather than exceptions.
Mats really thank you! This is how it looks now: and it all works. Question about system thing - what to use to pause programm? cin.get() and getch () doesnt work for me and havent found nothing else really, and i do work on windows 7 :D and antivirus is going crazy :D