I'm a beginner and I have to print the letter "N" out of #'s.
So far I can only print the |\ , so I'm still missing the last 'leg'.
I don't actually know how I got so far as this.. If anyone can help me or explain!!
Here is my code:
#include <iostream>
using namespace std;
int main()
{
int i, j;
for (i = 1; i <= 9; i++)
{
cout << "#";
for (j = 1; j <= 12; j++)
{
if (i == j)
{
cout << "#";
}
else
{
cout << " ";
}
}
cout << endl;
}
return 0;
}
for (i = 1; i <= 9; i++) //prints one line at a time
{
cout << "#";
for (j = 1; j <= 9; j++)
{
if (i == j) cout << "#"; //Diagonal part
else cout << " ";
}
cout << "#"; // <<< You missed this
cout << endl;
}
Little more elegant (using only one for-loop):
for (i = 1; i <= 9; i++)
{
string s = "#";
s.append(i-1, ' ' );
s +='#';
s.append(9-i, ' ' );
s +='#';
cout << s << endl;
}
i would go for the "Cheeting" way printing the exact thing without mangling with loops.
cout << "## #" << endl
cout << "# # #" << endl
cout << "# # #" << endl
cout << "# # #" << endl
cout << "# # #" << endl
cout << "# # #" << endl
cout << "# ##" << endl
easy as pie.
for(int y=0; y<9;y++){
for(int i=0; i<9; i++){
if((i==8&&y==0) or(i==8&&y==8) ){std::cout<<" ";}
if(i==0 or i==8){std::cout<<"#";}else{std::cout<<" ";};
if(i>0 && i<8){if(i==y){std::cout<<"#";std::cout<<" ";}else{std::cout<<" ";};};
};std::cout<<"\n";};
Related
I wrote the below code (for homework) that is counting letters and numbers and generates a frequency table.
My question is: how to stop the frequency generation when the letter or number does not exist?
With the code I wrote, the program is counting every letter that are being fed to it but is also publishing a line for every possible letter/number in the ASCII code.
I hope I asked my question right and I appreciate any help or advice!
#include <iostream>
#include <string>
using namespace std;
int countingLetters(string someWords);
int countingNumbers(string someWords);
int frequency(string someWords, double totalChar);
int main() {
string someWords;
cout << "Write a some words: " << endl;
getline(cin, someWords);
cout << "You wrote:" << someWords << '\n';
cout << "Your sentence has " << someWords.length() << " characters." << '\n';
double totalChar = someWords.length();
cout << "Your sentence has " << countingLetters(someWords) << " letters." << '\n';
cout << "Your sentence has " << countingNumbers(someWords) << " numbers." << '\n';
cout << "Frequency of signs and letters :" << endl;
frequency(someWords, totalChar);
return 0;
}
int countingLetters(string someWords) {
int count = 0;
for (int i = 0; i < someWords.length(); i++) {
if (someWords[i] >= 'a' && someWords[i] <= 'z')
count++;
}
return count;
}
int countingNumbers(string someWords) {
int count = 0;
for (int i = 0; i < someWords.length(); i++) {
if (isdigit(someWords[i]) != 0)
count++;
}
return count;
}
int frequency(string someWords, double totalChar) {
cout << " Letter" << '\t' << "Antal" << '\t' << "Procent" << endl;
int frequency[255]={0};
for (int i = 0; i < someWords.length(); i++) {
char c = someWords[i];
if (isdigit(c) != 0)
frequency[c]++;
if (isalpha(c) != 0)
frequency[c]++;
}
for (int i = 0; i < sizeof(frequency); i++) {
if(frequency[i]>0)
cout << '\t' << static_cast<char>(i) << '\t' << frequency[i] << '\t' << frequency[i]/totalChar << endl;
}
return 0;
}
sizeof(frequency) is the size of the frequency array in bytes. You want the number of elements which is the size in bytes if the array divided by the size in bytes of one array element:
This is the number of elements of the frequency array:
sizeof(frequency) / sizeof(frequency[0])
But as you are using C++ you shouln't use raw arrays but std::array:
#include <array>
...
int frequency(string someWords, int totalChar) {
cout << " Letter" << '\t' << "Antal" << '\t' << "Procent" << endl;
std::array<int, 255> frequency{0};
for (int i = 0; i < someWords.length(); i++) {
char c = someWords[i];
if (isdigit(c) != 0)
frequency[c]++;
if (isalpha(c) != 0)
frequency[c]++;
}
int x = frequency.max_size();
for (int i = 0; i < frequency.max_size(); i++) {
if (frequency[i]>0)
cout << '\t' << static_cast<char>(i) << '\t' << frequency[i] << '\t' << frequency[i] / totalChar << endl;
}
return 0;
}
There is still room for improvement.
I've been trying to overcome this problem for a few hours now and I seem to have one approach to the situation. It seems that the use of selection statements worked in creating the table necessary. Although there are formatting issues.
I'd like to know if there was a way to create the same table
using only nested for-loops as mentioned by our professor.
Are the selection statements necessary or can we implement a system of nested for loops to acquire the same results?
The image below is the required table:
But the image below is what I have:
Below is my code:
for (int i = 0; i <= numChoice; ++i)
{
if (i == 0)
{
for (int k = 1; k <= numChoice; ++k)
{
cout << " " << k;
}
cout << "\n";
}
else
{
cout << i << " | ";
for (int j = 1; j <= numChoice; ++j)
{
if (j*i <= 9)
{
cout << " " << j*i << "|";
}
else if (j*i > 9 && j*i <= 100)
{
cout << " " << j*i << "|";
}
else if (j*i > 99 && j*i <= 999)
{
cout << " " << j*i << "|";
}
}
cout << "\n";
for (int k = 0; k <= numChoice; ++k)
{
if (k == 0)
{
cout << "-|";
}
else
{
cout << "----|";
}
}
cout << "\n";
}
}
The following code uses no if else constructs. The formatting can be got by using setw, used for setting the width of integers.Following code produces perfect output.
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int i,j;
cout<<" "<<1;//5 space chars
for(i = 2;i <= 10;++i)
cout<<" "<<i;//4 space chars
cout<<endl;
cout<<" ----|";
for(i = 2;i <= 10;++i)
cout<<"----|";
cout<<endl;
for(i = 1;i <= 10;++i)
{
cout<<setw(2)<<i<<"|";
for(j = 1;j <= 10;++j)
cout<<setw(4)<<j*i<<"|";
cout<<endl;
cout<<" -|----";
for(j = 2;j <= 9;++j)
cout<<"|----";
cout<<"|----|";
cout<<endl;
}
return 0;
}
#FranticCode. I'm also in the same class as you and was having problems with this homework assignment as well. I still don't understand it, but I figured out how to manipulate Sumeet's code to give us correct format. The ONLY thing I am having a problem with now is adding an empty space AFTER the first multiplication table and before the menu redisplay. I'll share what I have and maybe you can figure it out. Still going to ask the professor to review chapter 5 because I would like to learn it rather than just submit the homework.
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
char userSelection;
int numForTable;
int col;
int row;
do
{
cout << "MENU" << endl
<< "a) Generate Multiplication Table" << endl
<< "q) Quit the program" << endl
<< "Please make a selection: ";
cin >> userSelection;
if (userSelection == 'a')
{
cout << "Please enter a number for your multiplication table: " << endl;
cin >> numForTable;
while (numForTable < 1 || numForTable > 10)
{
cout << "Please enter a number between 1 & 10." << endl;
cin >> numForTable;
}
cout << "\n"
<< "MULTIPLICATION TABLE: " << numForTable << "'s" << endl
<< "\n"
<< " " << 1;
for (col = 2; col <= numForTable; ++col)
cout << " " << col;
cout << endl;
cout << " ----|";
for (col = 2; col <= numForTable; ++col)
cout << "----|";
cout << endl;
for (col = 1; col <= numForTable; ++col)
{
cout << setw(2) << col << "|";
for (row = 1; row <= numForTable; ++row)
cout << setw(4) << col * row << "|";
cout << endl;
cout << " -|----";
for (row = 2; row <= numForTable - 1; ++row)
cout << "|----";
cout << "|----|";
cout << endl;
}
}
else if (userSelection != 'q')
{
cout << "Invalid Selection\n" << endl;
}
else if (userSelection == 'q')
{
cout << " You have chosen to quit the program. Thank you for using!" << endl;
}
}
while (userSelection != 'q');
//system("PAUSE");
return 0;
}
got curious to see if i could add the lines as easy as i claimed, it took a bit of fiddling, but here's the result (updated code below to also have lines).
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int counter;
int counter2;
int amount;
cout << " |-----------------------------------------------------------|" << endl; // first line of table.
for(counter=1;counter<11;counter++){ // the 2 for lines create our 2 dimensional table
for(counter2=1;counter2<11;counter2++){
cout << " | " << setw(3) << counter*counter2; // setw(3) is a function of <iomanip>,
//setting minimum width to 3 for numbers.
}
cout << " |" << endl; // this here is being added to the end of each line and starts a new line.
cout << " |-----------------------------------------------------------|" << endl; // this is being inserted between each line, and starts a new line.
}
return 0;
}
Use the following construct:
for (int i=0; i<=numChoice; i++) // display first row of numbers
cout <<"\t" << i << "\t";
cout << "\n";
for (int i=0; i <=numChoice; i++) {
cout << i << "\t";
for (int j=0; j <=numChoice; j++)
cout << i*j << "\t";
cout << "\n";
}
I have project in banker algorithm implement in c++
But I have small mistake :') in resource request
I didn't know what the mistake. The first if statement doesn't work :(
#include<iostream>
#include<vector>
using namespace std;
int main() {
int work[4];
int allocation[5][4];
int max[5][4];
int need[5][4];
int p, pr, r, a, aval[4], req[4];
bool state[5], test;
vector < int > avl;
//----------------------------------------
test = true;
for (int i = 0; i < 4; i++)
work[i] = aval[i];
for (int i = 0; i < 5; i++)
state[i] = false;
//----------------------------enter p r---------------------------------
cout << "Enter the number of processes in the system :";
cin >> p;
cout << "\nEnter the number of recourses :";
cin >> r;
//---------------------enter alloc---
cout << "\nEnter the allocation " << endl;
if (r = 1)
{
cout << "\t A \n \t ";
}
else if (r = 2)
{
cout << "\t A B \n \t ";
}
else if (r = 3)
{
cout << " A B C\n \t ";
}
else if (r = 4)
{
cout << " A B C D\n \t ";
}
for (int i = 0; i < p; i++)
{
cout << endl << "\t" << "P" << i << ":";
for (int j = 0; j < 4; j++)
{
cin >> allocation[i][j];
cout << " ";
}
}
//-----------------------------entet max----------------
cout << "\nEnter the MAX" << endl;
if (r = 1)
cout << " A \n \t ";
else if (r = 2)
cout << " A B \n \t ";
else if (r = 3)
cout << " A B C\n \t ";
else if (r = 4)
cout << " A B C D\n \t ";
for (int i = 0; i < p; i++)
{
cout << endl << "P" << i << ":";
for (int j = 0; j < 4; j++)
{
cin >> max[i][j];
need[i][j] = max[i][j] - allocation[i][j];
}
}
//-----------------enter ava--------------
cout << "\nEnter the avaliable number : " << endl;
for (int i = 0; i < 4; i++)
{
cin >> aval[i];
cout << " ";
}
//-----------------enter request--------------
cout << "\nEnter the number of process want be request : ";
cin >> pr;
cout << "\nEnter the request number : " << endl;
for (int i = 0; i < 4; i++)
{
cin >> req[i];
cout << " ";
}
//-----------------------------------COUT---------------------
cout << endl << "There are " << p << " processes in the system." << endl << endl;
cout << "There are " << r << " resource types." << endl << endl;
//----------------------------------cout allocation---------------
cout << " The allocation Matrix : " << endl << endl;
cout << "\t A B C D";
for (int i = 0; i < p; i++)
{
cout << endl << "\tP" << i << ":";
for (int j = 0; j < 4; j++)
{
cout << allocation[i][j] << " ";
}
cout << endl;
}
//----------------------------------cout max---------------
cout << endl << " The Max Matrix is... " << endl << endl;
cout << "\t A B C D";
for (int i = 0; i < p; i++)
{
cout << endl << "\tP" << i << ":";
for (int j = 0; j < 4; j++)
{
cout << max[i][j] << " ";
}
cout << endl;
}
//-------------------------cout need-------------------------------------------
cout << endl << " The Need Matrix is... " << endl << endl;
cout << "\t A B C D";
for (int i = 0; i < p; i++)
{
cout << endl << "\tP" << i << ":";
for (int j = 0; j < 4; j++)
{
cout << need[i][j] << " ";
}
cout << endl;
}
//----------------------------- cout aval ---------------------
cout << endl << "The Available Vector is..." << endl << endl;
cout << "A B C D" << endl;
for (int i = 0; i < 4; i++)
{
cout << aval[i] << " ";
}
//-----------------------------------SAFE STATE-----------------------
int k = 0;
for (k = 0; k < p; k++) {
if (state[k] == false) {
test = false;
for (int j = 0; j<r; j++) {
if (need[k][j] > work[j])
break;
if (need[k][j] <= aval[j])
test = true;
}
}
}
if (test == true) {
for (int j = 0; j < r; j++)
{
work[j] = work[j] + allocation[k][j];
}
state[k] = true;
cout << endl << endl << "THE SYSTEM IS IN A SAFESTATE!" << endl;
}
if (test == false) {
state[k] = false;
cout << endl << endl << "THE SYSTEM IS NOT IN A SAFE STATE!";
}
//-----------------------------------request------------------------
cout << "\nThe Request Vector is..." << endl;
cout << " A B C D" << endl;
cout << pr << ":";
for (int i = 0; i < 4; i++)
{
cout << req[i] << " ";
}
bool test2 = false;
for (int i = 0; i < p; i++) {
if (pr == p) {
for (int j = 0; j < 4; j++)
{
if (req[j] <= avl[j] && req[j] <= need[i][j])
{
test2 = true;
}
else
{
break;
}
}
if (test2 = true)
{
for (int n = 0; n < r; n++)
{
aval[n] = aval[n] - req[n];
allocation[i][n] = allocation[i][n] + req[n];
need[i][n] = need[i][n] - req[n];
}
cout << "THE REQUEST CAN BE GRANTED!" << endl << endl;
cout << "The Available Vector is...";
cout << "A B C D" << endl;
for (int x = 0; x < r; x++)
{
cout << aval[x] << " ";
}
}
else
{
cout << "THE REQUEST CANNOT BE GRANTED!" << endl << endl;
}
}
}
//------------------------------------------------------------------------------
system("pause");
return 0;
}
When checking if two primitive types are equal, you need to use "==" instead of "="
e.g, change your if statements from
if ( r = 1 )
to
if (r == 1)
Apart from above
work[i] = aval[i]; - aval[i] - has not been initialised
Read up on switch instead of r == 1 do this etc. ( I took into account the above statement and the chained if statements
Ditto with = true. Learn the difference between comparison and assignment
Perhaps learn how to use the debugger
You need to use cout << flush so that the output is sent.
.... I could add others - this is enough to get on with
Here is the whole working program. I made some changes in it and made the code more easy to understand.
int p, r;
bool test;
vector < int > avl;
//----------------------------enter p r---------------------------------
system("clear");
cout << "Enter the number of processes in the system: ";
cin >> p;
cout << "Enter the number of recourses: ";
cin >> r;
//test
int allocation[p][r];
int max[p][r];
int need[p][r];
int aval[r];
int state[p];
test = true;
//test
//---------------------enter alloc---
system("clear");
cout << "\nEnter the allocation " << endl;
if (r == 1)
{
cout << "\t A \n \t ";
}
else if (r == 2)
{
cout << "\t A B \n \t ";
}
else if (r == 3)
{
cout << "\t A B C \n \t ";
}
else if (r == 4)
{
cout << "\t A B C D \n \t ";
}
for (int i = 0; i < p; i++)
{
cout << endl << "\t" << "P" << i << ": ";
for (int j = 0; j < r; j++)
{
cin >> allocation[i][j];
cout << " ";
}
}
system("clear");
//-----------------------------entet max----------------
cout << "\nEnter the MAX" << endl;
if (r == 1)
cout << "\t A \n \t ";
else if (r == 2)
cout << "\t A B \n \t ";
else if (r == 3)
cout << "\t A B C\n \t ";
else if (r == 4)
cout << "\t A B C D\n \t ";
for (int i = 0; i < p; i++)
{
cout << endl << "\t" << "P" << i << ": ";
for (int j = 0; j < r; j++)
{
cin >> max[i][j];
need[i][j] = max[i][j] - allocation[i][j];
}
}
system("clear");
//-----------------enter ava--------------
cout << "\nEnter the avaliable number : " << endl;
cout<<"\tAvail: ";
for (int i = 0; i < r; i++)
{
cin >> aval[i];
cout << " ";
}
//-----------------------------------COUT---------------------
system("clear");
system("clear");
cout << "There are " << p << " processes in the system." << endl;
cout << "There are " << r << " resource types." << endl << endl;
//----------------------------------cout allocation---------------
cout << " The allocation Matrix : " << endl << endl;
if (r == 1)
cout << "\t A \n \t ";
else if (r == 2)
cout << "\t A B \n \t ";
else if (r == 3)
cout << "\t A B C\n \t ";
else if (r == 4)
cout << "\t A B C D\n \t ";
for (int i = 0; i < p; i++)
{
cout << endl << "\tP" << i << ": ";
for (int j = 0; j < r; j++)
{
cout << allocation[i][j] << " ";
}
cout << endl;
}
//----------------------------------cout max---------------
cout << endl << " The Max Matrix is... " << endl << endl;
if (r == 1)
cout << "\t A \n \t ";
else if (r == 2)
cout << "\t A B \n \t ";
else if (r == 3)
cout << "\t A B C\n \t ";
else if (r == 4)
cout << "\t A B C D\n \t ";
for (int i = 0; i < p; i++)
{
cout << endl << "\tP" << i << ": ";
for (int j = 0; j < r; j++)
{
cout << max[i][j] << " ";
}
cout << endl;
}
//-------------------------cout need-------------------------------------------
cout << endl << " The Need Matrix is... " << endl << endl;
for(int i = 0; i < p; i++)
{
for(int j = 0; j < r; j++)
{
need[i][j] = max[i][j] - allocation[i][j];
}
}
if (r == 1)
cout << "\t A \n \t ";
else if (r == 2)
cout << "\t A B \n \t ";
else if (r == 3)
cout << "\t A B C\n \t ";
else if (r == 4)
cout << "\t A B C D\n \t ";
for (int i = 0; i < p; i++)
{
cout << endl << "\tP" << i << ": ";
for (int j = 0; j < r; j++)
{
cout << need[i][j] << " ";
}
cout << endl;
}
//----------------------------- cout aval ---------------------
cout << endl << "The Available Vector is..." << endl << endl;
cout<<"\tAvail: ";
for (int i = 0; i < r; i++)
{
cout << aval[i] << " ";
}
cout<<endl;
//--------SAFESTATE----------SAFESTATE----------SAFESTATE-------------------
int count = p;
cout<<endl;
cout<<"Safe sequence: ";
do{
for(int loop_var = 0; loop_var < p; loop_var++)
{
test = false;
for (int j = 0; j < r; j++) {
if(state[loop_var] == true)
{
break;
}
if(need[loop_var][j] > aval[j])
{
test = false;
state[loop_var] = false;
break;
}
else
{
test = true;
}
}
if((test))
{
count--;
state[loop_var] = true;
for(int sb = 0; sb < r; sb++)
{
aval[sb] = aval[sb] + allocation[loop_var][sb];
}
if(count == 0)
{
cout<<"P"<<loop_var<<" ";
}
else if(count > 0)
{
cout<<"P"<<loop_var<<"-->";
}
}
}
}while(count != 0);
cout<<endl;
//--------SAFESTATE----------SAFESTATE----------SAFESTATE-------------------
cout<<endl;
cout<<"The new available vector is: ";
for(int i = 0; i < r; i++)
{
cout<<aval[i]<<" ";
}
cout << endl << endl;
return 0;
The following code
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
bool checkPerm(unsigned long long x){
vector<unsigned long long> tester;
string strx = to_string(x);
int sizestrx = strx.size();
int counter = 1;
cout << "x is " << strx << " and its permutations are ";
while (next_permutation(strx.begin(), strx.end())){
cout << strx << " ";
unsigned long long stoipermstrx = stoi(strx);
tester.push_back(stoipermstrx);
}
cout << endl;
int sizetester = tester.size();
for (int j = 2; j <= 6; j++){
cout << "j is " << j << ' ';
for (int k = 0; k < sizetester; k++){
if (j*x == tester[k]){
cout << "counter increased because x, counter " << x << " " << counter << endl;
counter++;
if (counter == 6){
cout << "Number is " << x << endl;
return true;
}
break;
}
}
//cout << "Number " << x << " failed" << endl;
return false;
}
return true;
}
int main(){
unsigned long long x = 1;
for (double i = 0; ; i++){
cout << i << endl;
while (x < 1.67*pow(10, i)){
if (i == 5)
cout << x << endl;
if (checkPerm(x)){
cin.get();
}
x++;
}
x = pow(10, (i + 1));
}
cin.get();
}
has the following problems in this piece of code:
cout << "x is " << strx << " and its permutations are ";
while (next_permutation(strx.begin(), strx.end())){
cout << strx << " ";
unsigned long long stoipermstrx = stoi(strx);
tester.push_back(stoipermstrx);
}
cout << endl;
int sizetester = tester.size();
for (int j = 2; j <= 6; j++){
cout << "j is " << j << ' ';
for (int k = 0; k < sizetester; k++){
if (j*x == tester[k]){
cout << "counter increased because x, counter " << x << " " << counter << endl;
counter++;
if (counter == 6){
cout << "Number is " << x << endl;
return true;
}
break;
}
}
//cout << "Number " << x << " failed" << endl;
return false;
}
Here the output will be "j is j x is x and its permutations are (permutations of x)". HOWEVER, the console should print "x is x and its permutations are (permutations) j is j". The following sample output is given:
j is 2 x is 1355 and its permutations are 1535 1553 3155 3515 3551 5135 5153 531
5 5351 5513 5531
j is 2 x is 1356 and its permutations are 1365 1536 1563 1635 1653 3156 3165 351
6 3561 3615 3651 5136 5163 5316 5361 5613
It appears there are two (minor) things about this. One, you are not looking at the value of sizetester before printing the value of j, and you are not printing a newline after the value of j. This means you are displaying the value of j for the previous loop at the beginning of your line for the current 'x'. If I understand what your code is supposed to be doing, it seems to be doing it correctly -- it's just the way that the output is getting displayed that makes it confusing.
Try this:
int sizetester = tester.size();
for (int j = 2; j <= 6; j++){
if (sizetester){ // <-- added test (see below)
cout << "j is " << j << '\n'; // <-- added newline
} // <--
The test against sizetester suppresses spurious printings of values for j - you later test that (k < sizetester) anyway. The newline just prevents values of j from starting the line for the next values of x, which appears to be the cause of the confusing output.
I have a program that takes in a set of 16 numbers and prints them out in a grid of 4x4. I must then check each row, column and diagonal add up to the same number, however I can't do this part, as I have no idea how that would work. Can anyone help?
current code:
void getNumbers(int numbers[]){
int idx;
for(int x = 0; x < 17; x++){
cout << "Please enter a number: " << endl;
cin >> idx;
numbers[x] = idx;
}
cout << " " << numbers[0] << " " << numbers[1] << " " << numbers[2] << " " << numbers[3] << endl;
cout << " " << numbers[4] << " " << numbers[5] << " " << numbers[6] << " " << numbers[7] << endl;
cout << " " << numbers[8] << " " << numbers[9] << " " << numbers[10] << " " << numbers[11] << endl;
cout << " " << numbers[12] << " " << numbers[13] << " " << numbers[14] << " " << numbers[15] << endl;
}
remove any syntax error if you see, the overall code must work! Enjoy ;-)
bool magicSquare(){
int idx;
int numbers[4][4];
cout << "Please enter your numbers: " << endl;
for(unsigned int i=0; i<4; i++) {
for(unsigned int j=0; j<4; j++) {
cin >> idx;
numbers[i][j] = idx;
}
}
// Checking
for(unsigned int i=0; i<4; i++) {
int row_sum = 0;
for(unsigned int j=0; j<4; j++) {
row_sum+= numbers[i][j];
}
int col_sum = 0;
for(unsigned int j=0; j<4; j++) {
col_sum+= numbers[j][i];
}
int diag_sum_left = numbers[0][0] + numbers[1][1] + numbers[2][2];
int diag_sum_right = numbers[0][2] + numbers[1][1] + numbers[2][0];
if ((col_sum != 15) ||
(row_sum !=15) ||
(diag_sum_left != 15) ||
(diag_sum_right != 15) )
return false;
}
cout << "Your answer is correct:" << endl;
for(unsigned int i=0; i<4; i++) {
for(unsigned int j=0; j<4; j++)
cout << numbers[i][j] << "\t";
cout << endl;
}
return true;
}
You can just write all the sums and the rules directly
Like this:
int horizontal1 = numbers[0]+ numbers[1]+ numbers[2]+ numbers[3];
int horizontal2 = numbers[4]+ numbers[5]+ numbers[6]+ numbers[7];
int horizontal3 = numbers[8]+ numbers[9]+ numbers[10]+numbers[11];
int horizontal4 = numbers[12]+numbers[13]+numbers[14]+numbers[15];
int vertical1 = numbers[0]+numbers[4]+numbers[8]+ numbers[12];
int vertical2 = numbers[1]+numbers[5]+numbers[9]+ numbers[13];
int vertical3 = numbers[2]+numbers[6]+numbers[10]+numbers[14];
int vertical4 = numbers[3]+numbers[7]+numbers[11]+numbers[15];
int diagonal1 = numbers[0]+numbers[5]+numbers[10]+numbers[15];
int diagonal2 = numbers[3]+numbers[6]+numbers[9]+ numbers[12];
// The result of check would be in this variable
bool result = horizontal1 == horizontal2 &&
horizontal1 == horizontal3 &&
horizontal1 == horizontal4 &&
horizontal1 == vertical1 &&
horizontal1 == vertical2 &&
horizontal1 == vertical3 &&
horizontal1 == vertical4 &&
horizontal1 == diagonal1 &&
horizontal1 == diagonal2;
Put how to check aside, you have something wrong in the posted code:
You mentioned that you have 16 numbers but you are actually asking for 17. If your numbers array has size = 16, then you will have index out of bound error.
for(int x = 0; x < 17; x++){
//^^^should be 16, put them into numbers array with numbers[0] to numbers[15]
cout << "Please enter a number: " << endl;
cin >> idx;
numbers[x] = idx;
}
For checking, the brute force way is to check for rows,columns then diagonal separately.