السلام عليكم و رحمة الله و بركاته...
لقد كتبت برنامجا و إحدى دوال هذا البرنامج لا تعمل 100٪ ، حيث عندما أدخل أكثر من رقمين القيمة المرجعة من الدالة تكون خطأ :(
bool allSorted(int x[], int N) //This function return true if
//all N integers in x are sorted
//in a descending order.
{
int cnst=0;
for(int i=0; i<N; i++)
{
if(x >= x[i+1])
cnst++;
}
if(cnst==N)
return 1;
else
return 0;
}و هذا البرنامج الكامل ...
#include <iostream>
using namespace std;
void ReadData(int x[], int N) //This function will read from
//the user N integers. The N
//integers will ba saved in the
//parameter array x.
{
cout<<"Data: ";
for (int i=0; i<N; i++)
{
cin>>x;
}
cout<<endl;
}
void printData(int x[], int N) //This function prints the first
//N integers in array x.
{
cout<<"Data is: ";
int i;
for (i=0; i<N-1; i++)
{
cout<<x<<',';
}
cout<<x<<'.'<<endl;
}
bool allEven(int x[], int N) //This function returns true if
//all N integers in x are even.
{
int cnst=0;
for (int i=0; i<N; i++)
{
if((x%2) == 0)
cnst++;
}
if(cnst == N)
return 1;
else
return 0;
}
bool allSorted(int x[], int N) //This function return true if
//all N integers in x are sorted
//in a descending order.
{
int cnst=0;
for(int i=0; i<N; i++)
{
if(x >= x[i+1])
cnst++;
}
if(cnst==N)
return 1;
else
return 0;
}
void computeSum(int x[], int y[], int N) //This function will
//set elements in
//array y such that
//the value of
//y(sub)k is equal
//to the sum of all
//positive integers
//<= x(sub)k
{
for(int i=0; i<N; i++)
{
int sum=0;
if (x<=0)
{
sum=0;
}
else
{
for (int j=0; j<=x; j++)
sum=sum+j;
}
y=sum;
}
}
int main()
{
int N,i;
cout<<"Size of data: ";
cin>>N;
int x[N],y[N];
cout<<endl;
ReadData(x, N);
printData(x, N);
if(allSorted(x, N) == 1)
cout<<"The data IS sorted."<<endl;
else
cout<<"The data IS NOT sorted."<<endl;
if(allEven(x, N) == 1)
cout<<"The data IS all even."<<endl;
else
cout<<"The data IS NOT all even."<<endl;
computeSum( x, y, N);
{
for (i=0; i<N-1; i++)
{
cout<<y<<',';
}
cout<<y<<'.'<<endl;
}
cout<<"Thank you.";
return 0;
}الرجاء المساعدة بأسرع وقت ممكن لأني لم أكتشف هذا الخطأ إلا متأخرا ، و تسليم الـassignment اليوم.
