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

اريد تصحيح البرنامج

مغلق
بدأه Fibonacci في 13 يونيو 2007 · 4 رد · 384 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

البرنامج عقدني و لف راسي

ابي احد يصححه لي و يشوف ايش الخطأ فيه

كود البرنامج:

#include<iostream>

#include<cmath> //for square root function

#include<iomanip> //included in case you use formatting for output

using namespace std;

void makeSieve ( bool sieve[], const int sieveSize);

void workSieve ( bool sieve[], const int sieveSize);

void findBoundries ( int &lower, int &upper, const int sieveSize );

void showTwins (const bool sieve[], const int lower, const int upper);

int main()

{

const int SIZE = 120001;

bool sieve ;

int low, high;

makeSieve ( sieve, SIZE );

workSieve ( sieve, SIZE );

findBoundries ( low, high, SIZE );

showTwins ( sieve, low, high );

return 0;

}

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

void makeSieve ( bool sieve[], const int sieveSize)

{

sieve [0]=0;

sieve [1]=0;

for ( int x=2; x<sieveSize; x++)

{

sieve[x]=1;

}

}

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

void workSieve ( bool sieve [], const int sieveSize)

{

for (int y=2; y <= sqrt ( sieveSize ); y++ )

{

if (sieve [y]==1)

{

for(int z=y*2; z<sieveSize ;z=z+B)

{

sieve[z]=0;

}

}

}

}

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

void findBoundries ( int &lower, int &upper, const int sieveSize )

{

cout<<"Please enter an upper boundary and a lower boundary and I will print all of the\nprime twin pairs between those boundaries.\n"<<endl;

cout<<"Please enter the upper boundary (between 1 and 120000): ";

cin>>upper;

while (upper<=0 ||upper>=120001)

{

cout<<"Please enter the upper boundary (between 1 and 120000): ";

cin>>upper;

}

cout<<"Please enter the lower boundary (between 1 and 120000): ";

cin>>lower;

while (lower<=0 ||lower>=120001)

{

cout<<"Please enter the lower boundary (between 1 and 120000): ";

cin>>lower;

}

if (lower>upper)

{

do

{

cout<<"\nYour upper boundary cannot be smaller than your lower boundary"<<endl;

cout<<"Please enter the upper boundary (between 1 and 120000): ";

cin>>upper;

while (upper<=0 ||upper>=120001)

{

cout<<"Please enter the upper boundary (between 1 and 120000): ";

cin>>upper;

}

cout<<"Please enter the lower boundary (between 1 and 120000): ";

cin>>lower;

while (lower<=0 ||lower>=120001)

{

cout<<"Please enter the lower boundary (between 1 and 120000): ";

cin>>lower;

}

}

while(lower>upper);

}

cout<<"\nHere are all of the twin prime pairs in the range "<<lower<<" to "<<upper<<", one pair per line:\n"<<endl;

}

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

void showTwins (const bool sieve[], const int lower, const int upper)

{

int count =0;

for ( int c=lower; c<=upper-2 ; c++ )

{

if ( sieve[c]==1 && sieve[c+2]==1 )

{

cout<<c<<" and "<<c+2<<endl;

count++;

}

}

cout<<"\n\nThere were "<<count<<" twin prime pairs displayed between "<<lower<<" and "<<upper<<endl;

}

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

#2

ماهو عمل البرنامج بالضبط ؟

#3

عندك خطأ syntax وحيد وهو في الدالة هذي :

void workSieve ( bool sieve [], const int sieveSize)
{

		for (int y=2; y <= sqrt ( sieveSize ); y++ )
		{
				if (sieve [y]==1)
				{
						for(int z=y*2; z<sieveSize;z=z+B)
						{
								sieve[z]=0;
						}

				}

		}


}

المتغير B لم تقم بتعريفه من قبل ابداً .

احتمال كبير يكون فيه اخطاء منطقية ، لكن انت في البداية علمنا وش يسوي هذا البرنامج ، وش الفايده منه ؟

#4

مشكور على تصحيح الخطأ اخوي مجلد جديد

طبعا في خطا للحين موجود في السطر 40 تقريباً

و هنا الخطأ اوفر لود تعبت اعدل مو راضي يشتغل معاي

18.cpp: In function `void workSieve(bool*, int)':

18.cpp:40: call of overloaded `sqrt(const int&)' is ambiguous

/usr/include/bits/mathcalls.h:157: candidates are: double sqrt(double)

/usr/include/c++/3.2.3/cmath:465: long double std::sqrt(long

double)

/usr/include/c++/3.2.3/cmath:461: float std::sqrt(float)

البرنامج يدخل عددين و يطلع البرايم نمبرز

يعني يطلع الاعداد اللي ما بينهم اللي ما تقبل القسمه الا على نفسها و على واحد مثل

5,7,13,etc.....

تم تعديل هذه المشاركة بواسطة Fibonacci في 14 يونيو 2007 في 11:33

#5

اتوقع ان الدالة sqrt ما تقبل الا double وليس int ، غير نوع المتغير sieveSize الى double وشف

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

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