row table pb

I would like to have the total of each row of a table.
How to do?
exemple

1 5 8 ==>14
2 4 1 ==> 7
7 3 2 ==>12


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
#include <iostream>
#include <vector>
#include <conio.h>
 
using namespace std;
  
int main() {
    int taille = 0;
    cin >> taille;
    do {
        cout << taille << "n'est pas impaire ";
        cout << "Tapez la taille du tableau : ";
        cin >> taille;
    } while (taille % 2 == 0);
  
    vector<int> tableau(taille * taille);
    for (int i = 0; i < taille * taille; ++i) {
        cout << "entrez le chiffre n " << (i % taille + 1)
             << " de la ligne n " << (i / taille + 1)  << endl;
        cin >> tableau[i];
             
    }
  
    for (int i = 0; i < taille * taille; ++i) {
        cout << tableau[i] << '\t';
        if ((i+1) % taille == 0) cout << endl;
    }
    
    getch();
   
   
  for (int i = 0; i < taille * taille; ++i)
       {
                
        cout << tableau[i] << '\t';
     
    }
    
    
  getch();
  
  
  
    getch();
    return 0;
}
@julieen03

Redid your for loop , that prints out the vector. Added in an int, to keep track of the totals, , prints total after displaying each line of vector and reset it to 0, after each printed line.


1
2
3
4
5
6
7
8
9
10
11
int total=0;
	for (int i = 0; i < taille * taille; ++i) 
	{
		total += tableau[i];
		cout << tableau[i] << '\t';
		if ((i + 1) % taille == 0)
		{
			cout << "\tTotals : " << total << endl;
			total = 0; //Reset to 0, for next line in vector
		}
	}
ok thanks.

What should i change for the colomn?

1
2
3
4
5
6
7
8
9
	for (int i = 0; i < taille*taille; ++i) 
	{
		total += tableau[i];
		cout << tableau[i] << '\t';
		if ((taille + taille*i]) % taille == 0)
		{
			cout << "\tTotals : " << total << endl;
			total = 0; //Reset to 0, for next line in vector
		}


And how could i compare the sum of each line?

1 5 6 ==>12
2 9 1 ==> 12
7 3 2 ==>12

line 1= line2 = line 3
@julieen03

Here's your program, with small improvements. First one, was the input for vector size. Now, it only informs you that you need an odd number input IF the inputted value was even.

Second, it now shows column values.

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Tab Tally.cpp : main project file.

#include <iostream>
#include <vector>
#include <conio.h>

using std::cout;
using std::cin;
using std::endl;
using std::vector;

int main()
{
	int taille = 0;
	cout << "Tapez la taille du tableau : ";
	do
	{
		cin >> taille;
		while (taille % 2 == 0)
		{
			cout << taille << " n'est pas impaire " << endl;
			cout << "Tapez la taille du tableau : ";
			cin >> taille;
		}
	} while (taille % 2 == 0);
	vector<int> tableau(taille * taille);
	for (int i = 0; i < taille * taille; ++i) {
		cout << "entrez le chiffre n " << (i % taille + 1)
			<< " de la ligne n " << (i / taille + 1) << endl;
		cin >> tableau[i];

	}

	// Show row totals

	int total=0;
	cout << endl << endl;
	for (int i = 0; i < taille * taille; ++i) 
	{
		total += tableau[i];
		cout << tableau[i] << '\t';
		if ((i + 1) % taille == 0)
		{
			cout << "\tRow Total : " << total << endl;
			total = 0; //Reset to 0, for next line in vector
		}
	}
	
	// Show column totals

	cout << endl;
	for (int j = 0; j < taille; j++)
	{
		total = 0;
		for (int i = j; i < taille * taille; i+=taille)
			total += tableau[i];
		
		cout << total << "\t";
	}
	cout << "\t: Column Totals" << endl;
	_getch();

	for (int i = 0; i < taille * taille; ++i)
	{
		cout << tableau[i] << ' ';
	}
	
	_getch();
	return 0;
}
ok thank you.



And how could i say if the sumn of both line and column is the same?

1 5 6 ==>12
2 9 1 ==> 12
7 3 2 ==>12

12 12 12

line 1= line2 = line 3 = column 1=column2=column3
==> cout << "congrat'! you've completed the exercice"


Last edited on
Are you trying to create a 'Magic Square' so all the rows and columns are equal, or just checking if row 1 is equal to column 1, row 2 is equal to column 2 and so on? Checking all would require a variable be set to the value of row 1, then checking total against that variable, and if they match, set a bool to true, and false, if it doesn't. At the end of all the checks, if the bool is still true, they all match, and cout << "congrat'! you've completed the exercice";
yes, a magic square :s . ouch ! it seems to be difficult.

Do you mean i should first check if all numbers are the same in rows and secondly check if numbers are the same in columns and then checking if the total of rows = total of columns?
Last edited on
@julieen03

Not really too difficult.

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
48
int checks=0;
for (int i = 0; i < taille * taille; ++i) 
	{
		cout << "entrez le chiffre n " << (i % taille + 1)
			<< " de la ligne n " << (i / taille + 1) << endl;
		cin >> tableau[i];
		if (i<taille)
			checks += tableau[i]; // Get total value of row 1
	}

	// Show row totals

	int total=0;
	bool same = true;
	cout << endl << endl;
	for (int i = 0; i < taille * taille; ++i) 
	{
		total += tableau[i];
		cout << tableau[i] << '\t';
		if ((i + 1) % taille == 0)
		{
			cout << "\tRow Total : " << total << endl;
			if (checks != total)
				same = false; // Set to false only if they're different
			total = 0; //Reset to 0, for next line in vector
		}
	}
	
	// Show column totals

	cout << endl;
	for (int j = 0; j < taille; j++)
	{
		total = 0;
		for (int i = j; i < taille * taille; i+=taille)
			total += tableau[i];
		
		cout << total << "\t";
		if (checks != total)
			same = false; // Same here..  Set to false only if they're different
	}
	cout << "\t: Column Totals" << endl;

	if (same) // All rows and columns, match in value
		cout << endl << "Congrats'! You've completed the exercice.." << endl;
	else // They don't.. 
		cout << endl << "Not a Magic Square.. Sorry!!" << endl << endl;
	_getch();
Ok thanks. And last thing: how could i do if i want the player do not enter the same number twice in tableau[i]? Maybe do ... While (i!=tableau[i]);
Last edited on
@julieen03

Hopefully, you study this program, and learn from it. Here's how you check for duplicate inputs.

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
int taille = 0;
int number = 0;
int checks = 0;
cout << "Tapez la taille du tableau : ";
do
{
	cin >> taille;
	while (taille % 2 == 0)
	{
		cout << taille << " n'est pas impaire " << endl;
		cout << "Tapez la taille du tableau : ";
		cin >> taille;
	}
} while (taille % 2 == 0);

vector<int> tableau(taille * taille);
for (int i = 0; i < taille * taille; ++i)
{
	tableau[i] = -1;
}
bool ok = true;
for (int i = 0; i < taille * taille; ++i)
{
	cout << "entrez le chiffre n " << (i % taille + 1)
		<< " de la ligne n " << (i / taille + 1) << endl;
	do
	{
		ok = true;
		cin >> number;
		for (int x = 0; x < taille * taille; x++)
		{
			if (tableau[x] == number)// Run thru all entries, search for a duplicate
			{
				cout << "Sorry, that number has already been used. Re-enter a different choice" << endl;
				ok = false;
			}
		}
	} while (!ok);
      tableau[i] = number;// Only if number not used previously, assign tableau[i] to number
	// i increases by one, only when ok is found to be true
	if (i < taille)
		checks += tableau[i];
}
// Rest of program below, unchanged 
Last edited on
ok THANK YOU
Last edited on
may u explain why tableau[i]=-1 ? i do not understand

1
2
3
4
5
vector<int> tableau(taille * taille);
for (int i = 0; i < taille * taille; ++i)
{
	tableau[i] = -1;
}


and what do u think about this algorithm?
Variables taille, number, checks en integer
start
show (« tapez la taille du tableau »)
do
	read taille
	while taille%2 == 0
 show (taille) (« n’est pas impair »)
show (« Tappez la taille du tableau »)
read taille
End while 
while taille%2 ==0

Variable total, same ;
	For i from 0 to taille*taille
		Tableau[i]=-1
for i from  0 to taille*taille
show (« entrez le chiffre n » i%taille+1 « de la ligne »1/taille+1)
do
	read nombre
	for x from 0 to taille*taille
		if tableau[x]==number
		show (« ce chiffre a déjà été saisi ! Entrez un autre chiffre »)
while ok = false

	Variables total, same en integer
	for i from 0 to taille*taille
Total +=tableau[i]
show tableau[i]
if i+1%taille ==0
		show (« la somme des lignes est de :», total)
	if  checks !=total
Same = false
Total  0
		For row from 0 to taille
		Total  0
For column allant de 0 à 2
show (total)
show (« la somme des colonnes est de »)
if same = true
show (« Bravo ! Vous avez trouvé une des solutions »)
else
show (« Ce n’est pas un carré magique »)
end
Last edited on
@julieen03

I set the vector with a known value that would not be inputted by the user. Without assigning a value, when you type in a number, it might be equal to one of the values in the vector already, when it was created. You could use a zero instead, but I wasn't sure what values were allowed in the vector, so I made them all less than zero.

As the algorithm, I really don't understand it. Sorry. Maybe someone else can comment on it.
To me, it looks more like pseudo code, and not an algorithm.
Last edited on
ok. May u show me how the algorithm should be? It's the first algorithm i try to write :S
i added diagonals, but it don't work when i try

2 7 6 ==> 15
9 5 1 ==> 15
4 3 8 ==> 15

column 15 15 15
diagonal 15 15

[code
Last edited on
Topic archived. No new replies allowed.