الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

ابي المساعده في برنامج magic square C++

مغلق
بدأه albronzy في 17 يونيو 2007 · 2 رد · 750 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

albronzy@hotmail.com

I need to fix this program , i've been tryingto fix it but it wast work

there are to major prblem in this program both of then in the outputs

the first it repeats the output twice

the second the range funftion doesnt work

my program is Magic Square

and here is the code it is in C++

#include<iostream>

#include<iomanip>

using namespace std;

bool checkColumns ( int theSquare[][4], const int numRows, const int magicvalue);

bool checkRows ( int theSquare[][4], const int numRows, const int magicvalue);

bool checkDiagonals ( int theSquare[][4], const int numRows, const int magicvalue);

bool checkRange ( int theSquare[][4], const int numRows, const int magicvalue );

int main()

{

int theSquare[4][4];

int numRows;

int magicvalue=34;

cout<<"The magic value for your square is 34, which means that every row, column and diagonal of your square must add up to that number."<<endl;

for (int row= 0; row<4; row++)

{

cout <<"Please enter the 4 values for row " <<row<< " , seperated by spaces: ";

for (int col=0; col<4; col++)

cin >> theSquare[row][col];

}

checkRows (theSquare, numRows, magicvalue);

checkColumns (theSquare, numRows, magicvalue);

checkDiagonals (theSquare, numRows, magicvalue);

checkRange (theSquare, numRows, magicvalue);

bool Diagonals= checkDiagonals (theSquare, numRows, magicvalue);

bool Rows = checkRows (theSquare, numRows, magicvalue);

bool Columns = checkColumns (theSquare, numRows, magicvalue);

bool Range = checkRange (theSquare, numRows, magicvalue);

if (Diagonals==true && Rows==true && Columns==true && Range==true)

cout << "MAGIC: YES" << endl;

else

cout << "MAGIC: NO" << endl;

return 0;

}

//////////////////////////////////////

bool checkRows( int theSquare[][4], const int numRows, const int magicvalue)

{

int row1=theSquare[0][0]+theSquare[0][1]+theSquare[0][2]+theSquare[0][3];

int row2=theSquare[1][0]+theSquare[1][1]+theSquare[1][2]+theSquare[1][3];

int row3=theSquare[2][0]+theSquare[2][1]+theSquare[2][2]+theSquare[2][3];

int row4=theSquare[3][0]+theSquare[3][1]+theSquare[3][2]+theSquare[3][3];

if (row1==34&&row2==34&&row3==34&&row4==34)

{

cout <<"ROWS: VALID"<<endl;

return true;

}

else

{

cout << "ROWS:";

if( row1!=magicvalue)

cout<<" 0 ";

if( row2!=magicvalue)

cout<<" 1 ";

if( row3!=magicvalue)

cout<<" 2 ";

if( row4!=magicvalue)

cout<<" 3 "<<endl;

return false;

}

}

//////////////////////////////////////////////////////////////////

bool checkColumns ( int theSquare[][4], const int numRows, const int magicvalue )

{

int col1= theSquare[0][0] + theSquare[1][0] + theSquare[2][0]+ theSquare[3][0];

int col2= theSquare[0][1] + theSquare[1][1] + theSquare[2][1]+ theSquare[3][1];

int col3= theSquare[0][2] + theSquare[1][2] + theSquare[2][2]+ theSquare[3][2];

int col4= theSquare[0][3] + theSquare[1][3] + theSquare[2][3]+ theSquare[3][3];

if ( col1==magicvalue&&col2==magicvalue&&col3==magicvalue&&col4==magicvalue)

{

cout <<"COLS: VALID"<<endl;

return true;

}

else

{

cout << "COLS:";

if( col1!=magicvalue)

cout<<" 0 ";

if( col2!=magicvalue)

cout<<" 1 ";

if( col3!=magicvalue)

cout<<" 2 ";

if( col4!=magicvalue)

cout<<" 3 "<<endl;;

return false;

}

}

/////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////

bool checkDiagonals ( int theSquare[][4], const int numRows, const int magicvalue )

{

int dia1 = theSquare[0][0] + theSquare[1][1] + theSquare[2][2]+ theSquare[3][3];

int dia2= theSquare[0][3] + theSquare[1][2] + theSquare[2][1]+ theSquare[3][0];

if ( dia1==magicvalue && dia2==magicvalue)

{

cout <<"DIAG: VALID"<<endl;

return true;

}

else

{

cout<<"DIAG: ";

if( dia1!=magicvalue)

cout<<"0 ";

if( dia2!=magicvalue)

cout<<"1"<<endl;;

return false ;

}

}

////////////////////////////////////////////////////

bool checkRange ( int theSquare[][4], const int numRows, const int magicvalue )

{

int range[17];

int foo=0;

cout<<"RANG: ";

for(int bar=0; bar<4; bar++)

{

for(int j=0; j<4; j++)

{

if(theSquare[bar][j]>16 || theSquare[bar][j]<1)

{

cout<<theSquare[bar][j]<<" ";

foo++;

}

else

if(range[theSquare[bar][j]]== j )

{

cout<<theSquare[bar][j]<<" ";

foo++;

}

else

range[theSquare[bar][j]]= j;

}

}

if (foo > 0)

{ cout << endl;

return false;

}

else

{

cout<<" Valid"<<endl;

}

return true;

}

#2

اول شي نكتب الكود يطريقة منظمة مشان يوضح لاي حد بقراه

#include<iostream>
#include<iomanip>
using namespace std;
bool checkColumns ( int theSquare[][4], const int numRows, const int magicvalue);
bool checkRows ( int theSquare[][4], const int numRows, const int magicvalue);
bool checkDiagonals ( int theSquare[][4], const int numRows, const int magicvalue);
bool checkRange ( int theSquare[][4], const int numRows, const int magicvalue );
int main()
{

int theSquare[4][4];
int numRows;
int magicvalue=34;

cout<<"The magic value for your square is 34, which means that every row, column and diagonal of your square must add up to that number."<<endl;
for (int row= 0; row<4; row++)
{
cout <<"Please enter the 4 values for row " <<row<< " , seperated by spaces: ";
for (int col=0; col<4; col++)
cin >> theSquare[row][col];
}

checkRows (theSquare, numRows, magicvalue);
checkColumns (theSquare, numRows, magicvalue);
checkDiagonals (theSquare, numRows, magicvalue);
checkRange (theSquare, numRows, magicvalue);

bool Diagonals= checkDiagonals (theSquare, numRows, magicvalue);
bool Rows = checkRows (theSquare, numRows, magicvalue);
bool Columns = checkColumns (theSquare, numRows, magicvalue);
bool Range = checkRange (theSquare, numRows, magicvalue);
if (Diagonals==true && Rows==true && Columns==true && Range==true)


cout << "MAGIC: YES" << endl;
else
cout << "MAGIC: NO" << endl;


return 0;




}
//////////////////////////////////////
bool checkRows( int theSquare[][4], const int numRows, const int magicvalue)
{
int row1=theSquare[0][0]+theSquare[0][1]+theSquare[0][2]+theSquare[0][3];
int row2=theSquare[1][0]+theSquare[1][1]+theSquare[1][2]+theSquare[1][3];
int row3=theSquare[2][0]+theSquare[2][1]+theSquare[2][2]+theSquare[2][3];
int row4=theSquare[3][0]+theSquare[3][1]+theSquare[3][2]+theSquare[3][3];
if (row1==34&&row2==34&&row3==34&&row4==34)
{
cout <<"ROWS: VALID"<<endl;
return true;
}
else
{
cout << "ROWS:";
if( row1!=magicvalue)
cout<<" 0 ";
if( row2!=magicvalue)
cout<<" 1 ";
if( row3!=magicvalue)
cout<<" 2 ";
if( row4!=magicvalue)
cout<<" 3 "<<endl;
return false;
}
}
//////////////////////////////////////////////////////////////////
bool checkColumns ( int theSquare[][4], const int numRows, const int magicvalue )
{
int col1= theSquare[0][0] + theSquare[1][0] + theSquare[2][0]+ theSquare[3][0];
int col2= theSquare[0][1] + theSquare[1][1] + theSquare[2][1]+ theSquare[3][1];
int col3= theSquare[0][2] + theSquare[1][2] + theSquare[2][2]+ theSquare[3][2];
int col4= theSquare[0][3] + theSquare[1][3] + theSquare[2][3]+ theSquare[3][3];
if ( col1==magicvalue&&col2==magicvalue&&col3==magicvalue&&col4==magicvalue)
{
cout <<"COLS: VALID"<<endl;
return true;
}
else
{
cout << "COLS:";
if( col1!=magicvalue)
cout<<" 0 ";
if( col2!=magicvalue)
cout<<" 1 ";
if( col3!=magicvalue)
cout<<" 2 ";
if( col4!=magicvalue)
cout<<" 3 "<<endl;;
return false;
}
}
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
bool checkDiagonals ( int theSquare[][4], const int numRows, const int magicvalue )
{

int dia1 = theSquare[0][0] + theSquare[1][1] + theSquare[2][2]+ theSquare[3][3];
int dia2= theSquare[0][3] + theSquare[1][2] + theSquare[2][1]+ theSquare[3][0];


if ( dia1==magicvalue && dia2==magicvalue)
{
cout <<"DIAG: VALID"<<endl;
return true;
}
else
{
cout<<"DIAG: ";
if( dia1!=magicvalue)
cout<<"0 ";
if( dia2!=magicvalue)
cout<<"1"<<endl;;
return false;
}
}

////////////////////////////////////////////////////

bool checkRange ( int theSquare[][4], const int numRows, const int magicvalue )
{

int range[17];
int foo=0;
cout<<"RANG: ";
for(int bar=0; bar<4; bar++)
{
for(int j=0; j<4; j++)
{
if(theSquare[bar][j]>16 || theSquare[bar][j]<1)
{
cout<<theSquare[bar][j]<<" ";
foo++;
}
else
if(range[theSquare[bar][j]]== j )
{
cout<<theSquare[bar][j]<<" ";
foo++;
}
else
range[theSquare[bar][j]]= j;
}
}

if (foo > 0)
{ cout << endl;
return false;
}
else
{
cout<<" Valid"<<endl;
}
return true;


}

مع هيك تم الكود بخوف :s

مشان هيك هيه مرفق بالملف بس بعد التعديل :)

هلا الكود اكيد رح يطبع ال output twice

coz u called the functions twice

استدعيت الاقترانات لوحدها في البداية ثم عند تعريف المتغيرات

على كل بس بكفي تشطب الجزء الاول اللي فيه استدعاء الاقترانات

هادا الشي اللي انا ساويته بالملف المرفق

اما قصة ال Range فمو فاهمة شو بدك

سلام

magic_square.cpp

تم تعديل هذه المشاركة بواسطة white soul في 17 يونيو 2007 في 18:04

Computer Engineer

2007

"اللهم يا مثبت القلوب ثبت قلبي على دينك "

ولدتك امك باكيا والناس حولك يضحكون سرورا

فافعل لنفسك ان تكون اذا بكوا في يوم موتك ضاحكا مسرورا

#3

مشكوره اختي على التجاوب ,,,, الحمد لله اشتغل معاي اوكي في النقطه اللي صلحتيها

اما موضوع الRange انا بشرحه لك

هو يختبر الاعداد اللي تكون من 0 الى 16 في الادخال انا كان اي عدد فوق اصغر من صفر او اكبر من 16 يعطيني في المخرجات الاعداد الزايده , ولو مثلا دخلته اعداد من 0 الى 16 و فيها منها مكرر راح يطبع لي المكرر منها بعددها

مثال

المدخلات

1 2 3 4

4 9 17 4

-1 5 10 13

1 16 1 1

Range= 1 1 1 4 4 17 -1

اما ان كان صحيح فيطبع لي

Range= VALID

هذا الموضوع مغلق.

مواضيع مشابهة

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…