شباب عندي سؤال حليته بس طلعت لي بعض المشاكل ..
السؤال يطلب مني تكوين array من 5 خانات.. وادخال ارقام موجبة بها .. بعدها يطلب مني ادخال رقم من الارقام الي في الـ array واذا كان هو الرقم الاول يطبع لي انه الرقم الاول ويطبع لي الرقم الذي بعده ، واذا كان الرقم الاخير يطبع لي الرقم الذي قبله و يطبع لي انه الرقم الاخير واذا كان الرقم ليس الاول وليس الاخير يطبع لي الرقم الي قبله والذي بعده.. واذا كان ليس موجود في الـ array يطبع لي انه غير موجود في الـ array وللخروج من الـ loop علي طباعة رقم -1 .. وهذا مثال لـ input & output
Enter a positive number: 10
Enter a positive number: 20
Enter a positive number: 15
Enter a positive number: 25
Enter a positive number: 40
Enter a number to search for (use negative to stop): 15
The number before 15 is 20
The number after 15 is 25
Enter a number to search for (use negative to stop): 70
70 is not found
Enter a number to search for (use negative to stop): 40
The number before 40 is 25
40 is the last number
Enter a number to search for (use negative to stop): 10
10 is the first number
The number after 10 is 20
Enter a number to search for (use negative to stop): -1
هذا هو الحل لكن مع اخطاء .. ممكن تصححون لي الاخطاء؟
#include <iostream.h>
int main()
{
int a[5];
int c;
for (int b=0; b<=4; b++)
{
cout<<"enter a positive number: ";
cin>>a;
}
cout<<"enter a number to search for (use negative to stop): ";
cin>>c;
for (; c!=-1;)
{
switch (c)
{
case a[0] : cout<<c<<" is the first number"<<"The number after "<<a[0]<<" is "<<a[2];
break;
case a[1] : cout<<"The number before "<<c<<" is "<<a[0]<<endl<<"The number after "<<c<<" is "<<a[2]<<endl;
break;
case a[2] : cout<<"The number before "<<c<<" is "<<a[1]<<endl<<"The number after "<<c<<" is "<<a[3]<<endl;
break;
case a[3] : cout<<"The number before "<<c<<" is "<<a[2]<<endl<<"The number after "<<c<<" is "<<a[4]<<endl;
break;
case a[4] : cout<<"The number before "<<c<<" is "<<a[3]<<endl<<c<<" is the last number"<<endl;
break;
cout<<c<<" is not found";
}
cout<<"enter a number to search for (use negative to stop): ";
cin>>c;
}
return 0;
}