I am beginner in programming, and I have a problem. I have to create two dimensional array [5][3] ... Lets says this is example of part of this:
- 2 3 4
- 7 8 9
- 5 6 7
Then I have to get sum of lines and write it next to this :
- 2 3 4 9
- 7 8 9 24
- 5 6 7 18
Now, I have to sort arrays by this sum, so the result would look like this:
- 7 8 9 24
- 5 6 7 18
- 2 3 4 9
I dont know how to achieve this, this is my code:
#include <iostream>
#include <time.h>
using namespace std;
void tocke(int polje[5][3])
{
int vsota;
srand(time(NULL));
int sums[5];
for (int i = 0; i < 5; i++)
{
vsota = 0;
cout << endl;
cout << i + 1 << ". ";
for (int j = 0; j < 3; j++){
polje[i][j] = (rand() % 10 + 1);
vsota += polje[i][j];
sums[i] = vsota;
cout << polje[i][j] << " ";
}
}
}
void urejaj(int polje[5][3])
{
cout << "\n\n\n\n" << endl;
int sums[5];
int vsota ;
double temp;
for (int i = 0; i < 5; i++)
{
vsota = 0;
cout << endl;
cout << i + 1 << ". ";
for (int j = 0; j < 3; j++)
{
vsota += polje[i][j];
sums[i] = vsota;
if (sums[i] < sums[i+1])
{
temp = polje[i][j];
polje[i][j] = polje[i + 1][j];
polje[i + 1][j] = temp;
}
cout << polje[i][j] << " ";
}cout << sums[i];
}
}
int main()
{
int polje[5][3];
tocke(polje);
urejaj(polje);
cout << "\n";
system("pause");
return 0;
}
First function writes elements in field, and the second has to sort fields.