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

اريد توضيح اخطائي في البرنامج

بدأه Mr.Raed في 7 يونيو 2011 · 4 رد · 455 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمة الله وبركاته

طلب مني الدكتور مشروع نهائي للفصل هذا وهذا هو

السوال

Mid-term Project

Write a C++ program to read, save, sort, search and display the students’ ids, student names and marks of N students.

N=7 should be declared as a constant in your program. You must use arrays to store the students’ IDs, names and marks.

Note: an enhanced version of this program can be achieved after studying functions and structs.

Your program should display and execute a menu with the following options.

A switch statement must be used to execute the following menu options.

Menu

---------

1. Clear

2. Register a Student (Id, Name, Mark)

3. Display the student with a highest mark

4. Display the marks of all students

5. Display the percentage of the total mark received by each student

6. Sort all marks in ascending order

7. Sort all marks in descending order

8. Search for a student with a given id

9. Search for a student with a given name

10. Search for a student with a given mark

11. Exit

Explanation

1. Clear

This option initializes all the elements of the arrays to -1 to indicate that array is empty and names to null.

2. Register a Student (Id, Name, Mark)

This option reads and stores the student’s id, student’s name and student’s marks. The marks (float numbers) are entered from the keyboard.

3. Display the student with a highest mark

This option displays the student id, name and mark for a student with a highest mark.

4. Display the marks of all students

This option displays the students’ IDs, names, marks of all students.

5. Display the percentage of the total mark received by each student

This option displays the percentage of the total mark for all students.

6. Sort all marks in ascending order

This option sorts all marks in ascending order. You can use any sorting algorithm which uses at least two while loops and one if statement.

7. Sort all marks in descending order

This option sorts all marks in descending order. You can use any sorting algorithm which uses at least two while loops and one if statement.

8. Search for a student with a given id

This option searches for a student with a given id. The program asks the user to enter the id using the keyboard and searches for it. If an appropriate student is found, the program displays the message “student found” and displays the id, name and the mark otherwise it displays a message “no student found”.

9. Search for a student with a given name

This option searches for a student with a given name. The program asks the user to enter the name using the keyboard and searches for it. If an appropriate student is found, the program displays the message “student found” and displays the id, name and mark otherwise it displays a message “no student found”.

10. Search for a student with a given mark

This option searches for a student with a given mark. The program asks the user to enter the mark using the keyboard and searches for it. If an appropriate student is found, the program displays the message “student found” and displays the id, name and mark otherwise it displays a message “no student found”.

11. Exit

The program should display the message “Thanks for using this program”, and exit.

The whole program should work in a loop to enable the user to select an item from menu until the user chooses to exit.

وهاذي محاولتي للحل لكن اريد احد يوضح لي مالاخطاء في الحالة الثالثة

لان اريد ان اكمل البرنامج وارجوكم لان اختبارنا النهائي يوم السبت ويجب ان اخلص منه لكي يبقى لي فرصة لذاكر

هذا حلي

# include < iostream >
# include < string > 
using namespace std ;

int main ( ) 
	{
	struct student 
		{
	string name  ;  ,id; 
	float mark;
		};
	student x[7] ; 
	bool E;

	cout << " This is Menu please Enter the number of the chosie\n " ;
	cout << " 1. Clear \n " ; 
cout << "2. Register a Student (Id, Name, Mark)\n " ;
cout<<"3:display the student with a highest mark\n";
cout<<"4:display the mark student\n";
cout<<"5:display the percentage of the total mark received by each student\n";
cout<<"6:sort all mark in ascending order\n";
cout<<"7:sort all mark in descending order\n";
cout<<"8:search for a student with give id\n ";
cout<<"9:search for a student with give name\n ";
cout<<"10:search for a student with give mark \n";
cout<<"11:Exit\n";
E = 1;
while(E){

	int chosie ;
	cin>> chosie ;
switch ( chosie )
	{
	case 1:
	cout << " 1. Clear \n ";
	for ( int i = 0 ; i<7 ; i++ )
		{	x.name = " null " ;
	    x.id = " null "  ;
		x.mark = 0 ;
		}
	break;
	case 2:
	cout << "2. Register a Student (Id, Name, Mark)\n " ;

	for ( int i = 0 ; i<7 ; i ++ )
		{cout << " please enter your name " ;
		cin >> x.name ;
		cout << " please enter your id " ;
		cin >> x.id ;
		cout << " please enter your mark " ;
		cin >> x.mark ;
		if ( x.mark >  100 ) 
			x.mark = 100 ; 
		}
	break; 
	case 3 :
	cout<<"3:display the student with a highest mark\n";
       float mark
	for (int k=0 ; k<7 ; k++)      
			{              if (mark[k]> max)                  max =mark [k];          }

       cout << x.name << endl << x.id << endl << max << endl  ; 
	}



	break;
	case 4 :
    cout<<"4:display the mark student\n";
	break;
	case 5 :
	cout<<"5:display the percentage of the total mark received by each student\n";
	break;
	case 6 :
	cout<<"6:sort all mark in ascending order\n";
	break;
	case 7 : 
	cout<<"7:sort all mark in descending order\n";
	break;
	case 8 :
	cout<<"8:search for a student with give id\n ";
	break;
	case 9 :
	cout<<"9:search for a student with give name\n ";
	break;
	case 10 :
	cout<<"10:search for a student with give mark \n";
	break;
	case 11 :
	if (chosie == 11 )	
     E = 0;
	cout << "\nThanks for using this program";
    break;
    default :
	cout << " please chosie from this menue " ;
	}
	}

return 0;



	}
1f557e95a4.jpg
#2

اوك بنتظارك حورية متى ما فضيت بس ان شاء الله قبل وقت التسليم

1f557e95a4.jpg
#3

واضح أنك كنت مرهق عند مراجعة case 3 والتي تحوي على مشكلتك.

لقد قمت بالتصريح عن متغير جديد باسم mark من نوع float داخل الحالة 3، وطبعاً أنقصت الفاصلة المنقوطة في نهاية السطر لكن ليست هذه هي المشكلة،

مشكلتك هي أنك لم تستخدم المصفوفة x (من نوع البنية student) التي أنشأتها للطلاب في بداية البرنامج لمقارنة علامات الطلاب المسجلة فيها..

float max = x[0].mark;
int offset = 0;
for (int i = 0; i < 7-1; i++)
{
	if (x[i+1].mark > max)
	{
		max = x[i+1].mark;
		offset = i+1;
	}
}
cout << x[offset].name << endl << x[offset].id << endl << max << endl;

نقوم باعتبار أول علامة في المصفوفة هي أعلى علامة، ونقارنها بما يليها، فإن كانت التالية أكبر جعلناها أعلى علامة ونتابع في مقارنتها مع ما يليها وهكذا حتى نهاية المصفوفة (انتبه إلى أن نهاية المصفوفة سيكون هو حجم المصفوفة-1 لأننا نستخدم x[i+1] وذلك حتى لا نخرج خارج مجال تعريف المصفوفة)

كما يمكن كتابته بالشكل:

float max = x[0].mark;
int offset = 0;
for (int i = 1; i < 7; i++)
{
	if (x.mark > max)
	{
		max = x.mark;
		offset = i;
	}
}
cout << x[offset].name << endl << x[offset].id << endl << max << endl;

لإلغاء الحاجة لتنفيذ عملية + غير ضرورية من أجل حساب قيمة i، لكن انتبه إلى بداية ونهاية الحلقة في هذه الحالة واختلافها عن السابقة.

1

Do as I say, not as I do

We are Anonymous. We are Legion. We don't forgive. We don't forget

#4

اهلآ اخوي واختي

انظرو

# include < iostream >    
# include < string >     
using namespace std ;    


        struct student     
                {    
        string name ,id; //هنا الايدي سترنق لانه في حالة اذا حطينا صفر ما يختصر انتقر لان على اليسار ما يحسب الصفر مثلا     

        float mark[10];    
                };    

int main ( )     

        {    

        student x[7] ;    
        int lastFreePlace=0;    
        bool E=true;       

while(E){    
                        cout<<"\n=====================\n";          
                        cout << " This is Menu please Enter the number of the chosie\n " ;    
                        cout << " 1. Clear \n " ;     
                        cout << "2. Register a Student (Id, Name, Mark)\n " ;    
                        cout<<"3:display the student with a highest mark\n";    
                        cout<<"4:display the marks of all students\n";    
                        cout<<"5:display the percentage of the total mark received by each student\n";    
                        cout<<"6:sort all mark in ascending order\n";    
                        cout<<"7:sort all mark in descending order\n";    
                        cout<<"8:search for a student with give id\n ";    
                        cout<<"9:search for a student with give name\n ";    
                        cout<<"10:search for a student with give mark \n";    
                        cout<<"11:Exit\n";    
                        cout<<"\n=====================\n";     
        int chosie ;    
        cin>> chosie ;    
switch ( chosie )    
        {    
        case 1:    
                {    
                cout << " 1. Clear \n ";    
        for ( int i = 0 ; i<7 ; i++ )    
                {    
                x.name = " null " ;    
                x.id = " null "  ;    
                for (int j=0; j<10; j++)    
                                x.mark[j] = 0 ;  
                }    
                lastFreePlace=0;   
                                 break;    
                }    
                case 2:    
                        {    

                                cout << "2. Register a Student (Id, Name, Mark) " ;    
                                char anotherStu;    
                                do    
                                {    
                                        int markLastPlace=0;    
                                        if (lastFreePlace >= 7)    
                                        {    
                                                cout<<"\nError! More than 7 students";    
                                                break;    
                                        }    
                                        else    
                                        {    
                                                cout << "\nStudent's name: " ;    
                                                cin >> x[lastFreePlace].name ;    
                                                cout << "Student's ID: " ;    
                                                cin >> x[lastFreePlace].id ;    

                                                cout << "Student's marks:";    
                                                char repeat;    
                                                do    
                                                {    
                                                        cin >> x[lastFreePlace].mark[markLastPlace] ;    
                                                        markLastPlace++;    

                                                        cout<<"another mark? (y/n): ";    
                                                        cin>>repeat;    
                                                }    
                                                while( (repeat=='y') && (markLastPlace <10) );  

                                                lastFreePlace++;    

                                        }    
                                        cout<<"\nRegister another student? (y/n): ";    
                                        cin>>anotherStu;    
                                }    
                                while( (lastFreePlace < 7) && (anotherStu == 'y') );    

                                break;     
                        }    

        case 3 :      
                  {      
                          cout<<"3:display the student with a highest mark\n";      
                          float highest=x[0].mark[0];      
                          int highestID=0;      
                         for (int k=0 ; k<7 ; k++)          
                         {    
                                 for (int m=0; m<10; m++)    
                                 {    
                                        if (x[k].mark[m] > highest)      
                                        {    
                                                highestID=k;     
                                                highest= x[k].mark[m];    
                                        }    
                                 }    
                         }      
                         cout<<endl<<"name: "<<x[highestID].name ;      
                        cout  << endl<<"ID: "<<x[highestID].id  ;    
                         cout <<endl<<"Mark: "<<highest;                    
                           break;      
                  }    



        case 4 :    
                cout<<"4:display the marks of all students\n";      



        case 5 :    
        cout<<"5:display the percentage of the total mark received by each student\n";    
        break;    
        case 6 :    
        cout<<"6:sort all mark in ascending order\n";    
        break;    
        case 7 :     
        cout<<"7:sort all mark in descending order\n";    
        break;    
        case 8 :    
        cout<<"8:search for a student with give id\n ";    
        break;    
        case 9 :    
        cout<<"9:search for a student with give name\n ";    
        break;    
        case 10 :    
        cout<<"10:search for a student with give mark \n";    
        break;    
        case 11 :    
        if (chosie == 11 )          
        E = 0;    
        cout << "\nThanks for using this program";    
        break;    
        default :    
        cout << " please chosie from this menue " ;    
        }    
        }    

return 0;    

        }

رجاء خااااااص

اريد احد يساعدني في المتطلبات الباقية 4 الى 10

1f557e95a4.jpg
#5

ننظر إلى ماذا؟

بخصوص باقي الطلبات، هلاّ وضعت محاولاتك حتى تستفيد من تصحيح أخطائك؟

Do as I say, not as I do

We are Anonymous. We are Legion. We don't forgive. We don't forget

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