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
}
}
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
}
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.
// 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;
}
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?
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();
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
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
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.