موقع طلبة جامعة البحرين

يمكنك تصفح الموقع كزائر ولكن ندعوك لتسجيل عضوية خاصة بك لتحصل على كافة الصلاحيات مثل تنزيل ملفات المكتبة وقراءة تعليقات هيئة التدريس وغيرها. يمكنك الحصول على عضوية مجانية بالضغط على زر تسجيل. إذا قمت بالتسجيل مسبقا فيمكنك الضغط على زر دخول.

تسجيل دخول
  • قسم المكتبة الجامعية مغلق مؤقتا بعد الاستهداف التي تعرضت له بعض خدمات أمازون في البحرين، جاري استرجاع الملفات ونقلها إلى سيرفرات جديدة وسيتم بعدها اعادة افتتاح المكتبة

ITCS112 - ITCS104 - ITCS102 للفصل الصيفي 2014 - 2015

لا أوكي. لأن هو ما بيرتب رقمين بعدين يجيك الكوندشن. أهو بيفتر ع اللستة كاملة داخل الفور لوب. بعدين بعد ما يخلص بيطلع ع الوايل و بيجيك اذا الفور سووت تغيير أو لا.إذا ما سوت يعني اللستة مرتبة ف بيطلع. :whistling:

:mellow: سو روحك ما شفت
شكلي صرت اعمى :RpS_lol: .. لكن ال for loop اسرع "
اذا كان فيها شرط او ان اللسته كلها بتترتب " :RpS_sleep: شوف الفرق بينهم وبتعرف :RpS_tongue:

 
:mellow: سو روحك ما شفت
شكلي صرت اعمى :RpS_lol: .. لكن ال for loop اسرع "
اذا كان فيها شرط او ان اللسته كلها بتترتب " :RpS_sleep: شوف الفرق بينهم وبتعرف :RpS_tongue:


أهو فعلا الفور لوب اسهل في الكتابة و كلشي. بس من ناحية الـ performance الـ while أسرع لانها ممكن توصل مثلا نص اللستة و تشوفها ترتبت و توقف المقارنات و تكمل البرنامج, أما الفور لوب بتكمل للنهاية حتى لو ترتبت. بس بيني و بينك حلك أصح, لان المغزى من الـ bubble sort اهو سهولة الكتابة و الفهم مو السرعة (أصلا هالـ algorithm بطيء حده و علة :RpS_lol:). يلا لكن أهم شي يشتغل الكود و الأسايمنت أستوى :RpS_sleep:
 
أهو فعلا الفور لوب اسهل في الكتابة و كلشي. بس من ناحية الـ performance الـ while أسرع لانها ممكن توصل مثلا نص اللستة و تشوفها ترتبت و توقف المقارنات و تكمل البرنامج, أما الفور لوب بتكمل للنهاية حتى لو ترتبت. بس بيني و بينك حلك أصح, لان المغزى من الـ bubble sort اهو سهولة الكتابة و الفهم مو السرعة (أصلا هالـ algorithm بطيء حده و علة :RpS_lol:). يلا لكن أهم شي يشتغل الكود و الأسايمنت أستوى :RpS_sleep:

مثل ما قلت قبل الـ for loop اسهل واسرع بعد " اذا كان فيها شرط او ان اللسته كلها بتترتب " :rolleyes2:
وفايدة الـ Bubble Sort انها اسرع من السورت العادي
كود:
for(i=1;(i<=size)&&flag; i++)
{
flag = 0;
for (j=0;j<size-i;j++)
....
حاول ترسم شلون الفور لوب تشتغل و قارنها بالوايل لوب وبتفهم قصدي :smile2:
 
التعديل الأخير:
مثل ما قلت قبل الـ for loop اسهل واسرع بعد " اذا كان فيها شرط او ان اللسته كلها بتترتب " :rolleyes2:
وفايدة الـ Bubble Sort انها اسرع من السورت العادي
كود:
for(i=1;(i<=size)&&flag; i++)
{
flag = 0;
for (j=0;j<size-1;j++)
....
حاول ترسم شلون الفور لوب تشتغل و قارنها بالوايل لوب وبتفهم قصدي :smile2:
أي أوكي جدي الفور لووب ويا الكوندشن شغل تمام :RpS_love:
بس الببل سورت مو أسرع شي. أهي تقارن كل العناصر ويا بعض مرة ورا مرة. في وحدة نسيت أسمها موجودة في الكتاب تشوف اصغر شي و تبدله ويا أول عنصر و بعدين تصغر حجم اللستة --. و في الـ Binary searching and sorting اللي الحين مو حاضر في بالي شنهو بالضبط بس مشهور بسرعته. يعني الـ bubble sort أضعف الإيمان :RpS_lol:
 
شباب وبنات ... باجر عندنا كوز فيه جبتر 10 وقالت في الدليت والانسيرت ؟؟؟


قالت وحده كتابة فنكشن والثاني اوت بووت ..




الللي يقدر يفهمني اياهم ... وجزاكم الله كل خير ...
 
شباب وبنات ... باجر عندنا كوز فيه جبتر 10 وقالت في الدليت والانسيرت ؟؟؟


قالت وحده كتابة فنكشن والثاني اوت بووت ..




الللي يقدر يفهمني اياهم ... وجزاكم الله كل خير ...

بحاول أشرح اللي فاهمنه.
Disclaimer : انا ما أدرس هالمقرر ممكن معلوماتي تكون غلط.

أول شي الـ insertion. أفترضي عندش أري فيه عدد من الخانات, أول خمسين خانة منهم مشحونين فيهم أرقام, و انتين تبين تخلين رقم في الخانة 41 مثلا. طبعا تقدرين تقولين arr[41] = myData بس هالشي بيمحي اللي كان في 41 و هالشي ما نبيه. الطريقة الوحيدة أهي ان تزيحين كل العناصر اللي فوق 41 للأمام خطوة وحدة بحيث اللي كان arr[44] الحين يصير arr[45] و هالجدي. إذا فهمتين الفكرة سهل تطبقينها كودياً (كودياً كلمة جديدة من عندي :RpS_lol:) لأنه كله منطق (logic).

الـ algorithm مالنا عشان ندخل رقم بيكون جدي.
1- ناخذ الشي اللي بنضيفه (X) و مكانه (من اليوزر مثلا).
2-نباعد كل العناصر اللي بعد الموقع المطلوب خطوة وحدة.
3-نخلي الـ X في المكان المطلوب.

الخطوة 1 و 3 ما يبي ليهم كلام. الشغل في الخطوة 2. شلون نزيح العناصر في الأري. قبل ما أكتب الكود بحاول اشرحها عشان تكون مفهومة.
أول فكرة تجي في بالنا ان نسوي جذي :
كود:
arr[42] = arr[41];
arr43 = arr[42];
.
.

طبعاً هالشي غلط ليش ؟ لأنه و احنا ننقل من ال41 لل 42 اللي كان أساساً في 42 ضاع الحين, و إذا سوينا جدي الأرقام الجاية كلها بتكون قيمة 41.
الحل ان نخلي عندنا 2 variables مثلا نسميهم temp1 و temp2. نخزن في temp1 قيمة اللي في المكان اللي بنخلي فيه الرقم (pos+1) و نخلي الـ (pos+1 = pos). بعدين نرجع القيمة الأصلية لـ temp2 من temp1.جدي بالكلام يخربط واجد حتى أنا مادري ويش أكتب:schmoll:. بالكود هذا يصير شي جدي :
كود:
#include <iostream>
using namespace std;


void put_in(int[],int,int,int);
void print_array(int[],int);


int main(){
    const int size = 20;
    int list[size] = {2,5,1,3,6};
    
    cout<<"The initial array is : "<<endl;
    print_array(list,size);
    
    put_in(list,size,9,3);
    
    cout<<"Now the array is : "<<endl;
    print_array(list,size);


    return 0;
}


void print_array(int list[],int size){
    for(int i =0;i<size;i++){
        cout<<list[i]<<" ";
    }
}


void put_in(int list[],int list_size,int value,int pos){
    //Shifting the array
    
int temp1;
    int temp2 = list[pos];
    
    for(int i = pos; i < list_size - 1;i++){
        temp1 = list[i+1];
        list[i+1] = temp2;
        temp2 = temp1;
    }


    //Assigning the value
    list[pos] = value;
}

هذا الإنسرت. الديليت نفس الشي تقريبا بس الفرق ان بترجعينهم لـ ورا. إذا في شي مو عارفتنه أو غلط عندي خبريني :smile2: (مو أكيد أعرف :RpS_lol:)
 
تقدرووون تحلووووووون <><><



​
[FONT=&quot]Write a C++ program that includes the following functions:[/FONT]
​
· [FONT=&quot]A function named readList that takes as parameters: an array of integers and the array size. The function should prompt the user to input the array data. The function prototype is: void readList(int [ ], int);[/FONT]
​
[FONT=&quot] [/FONT]​
· [FONT=&quot]A function named printList that takes as parameters: an array of integers and the array size. The function should display the elements of the array. The function prototype is: void printList(int [ ], int);[/FONT]​
[FONT=&quot] [/FONT]​
· [FONT=&quot]A function named displayOdd that takes as parameters: an array of integers, and the array size. The function should display all odd numbers in the array. The function prototype is: void displayOdd (int [ ], int);[/FONT]​
[FONT=&quot] [/FONT]​
· [FONT=&quot]Write the main function to test the functions above.[/FONT]
​
​
 
تقدرووون تحلووووووون <><><



​
[FONT=&amp]Write a C++ program that includes the following functions:[/FONT]
​
· [FONT=&amp]A function named readList that takes as parameters: an array of integers and the array size. The function should prompt the user to input the array data. The function prototype is: void readList(int [ ], int);[/FONT]
​
· [FONT=&amp]A function named printList that takes as parameters: an array of integers and the array size. The function should display the elements of the array. The function prototype is: void printList(int [ ], int);[/FONT]​
· [FONT=&amp]A function named displayOdd that takes as parameters: an array of integers, and the array size. The function should display all odd numbers in the array. The function prototype is: void displayOdd (int [ ], int);[/FONT]​
· [FONT=&amp]Write the main function to test the functions above.[/FONT]
​

ما يصير حلول جاهزة .. على الأقل تحاولون وإذا في جزئية ما فهمتونها أو خطأ نساعدكم :smile2:
 
السلام عليكم ورحمة الله وبركاته

حبيت اسأل سؤال وبغيت مساعدتكم رحم الله والديكم

اذا عندي class
وفي داخله members

لنفرض
class StudentMarks
{privat:
string name;
float marks}​
ولنفرض ان عندنا 5 طلاب
السؤال : شلون ارتب الطلاب على حسب الدرجات بطريق استخدام bubble Sort


وشكراً لكم ,
 

Write the definition of a class named BankAccount which includes data members and the prototype of the function members as described below:
The data members of the class BankAccount are: AccountNumber (long), Name (string), and Balance (float).

The class BankAccount contains the following member functions:

  1. Set and get functions for all the data members.
  2. IncrementBalance that takes a value and adds it to balance.
  3. DisplayAccountDetails that prints (on the screen) all the information about the Account.

Write a main program to declare MyAccount as an object of type BankAccount.

  1. Call function Set to set MyAccount with your name and account number and 100BD in balance.
  2. Increment your balance by 50BD.
  3. print your account details again.

ممكن تفهموني السؤال؟
اللي فهمته بس مو متاكد.. في الاوتبوت اخر شي بيصير..
بينطبع الاكاونت ديتايلز(اكاونت نمبر - الاسم - والبلانس)
وبعدين البلانس يزيد 50 .. وينطبع الاكاونت الديتايلز(اكاونت نمبر - الاسم - البلانس+50)
 
بينطبع الاكاونت ديتايلز(اكاونت نمبر - الاسم - والبلانس)
وبعدين البلانس يزيد 50 .. وينطبع الاكاونت الديتايلز(اكاونت نمبر - الاسم - البلانس+50)
مثل ما قلت بالضبط ..
 
  • Like
التفاعلات: Ali.4
السلام عليكم ورحمة الله وبركاته

حبيت اسأل سؤال وبغيت مساعدتكم رحم الله والديكم

اذا عندي class
وفي داخله members

لنفرض
class StudentMarks
{privat:
string name;
float marks}​
ولنفرض ان عندنا 5 طلاب
السؤال : شلون ارتب الطلاب على حسب الدرجات بطريق استخدام bubble Sort


وشكراً لكم ,

اول حاجه تسوي لسته من 5 طلاب ..
كود:
StudentMarks students[5];
تكون الفنكشن جذي ..
كود:
void sort(StudentMarks list[],int size )
{
    string n;
    float m1,m2;
    StudentMarks temp;
    for(int i=1;i<size-1;i++)
    {
        for(int j=0;j<size-i;j++)
        {
            list[j].get(n,m1);
            list[j+1].get(n,m2);
            if(m2<m1)
            {
                temp=list[j];
                list[j]=list[j+1];
                list[j+1]=temp;
            }
        }
      }
}

وهذا الـ call :
كود:
sort(students,5);
 
امتحان المدترم لأي جبتر؟:whistling:
 
متى بكون امتحان المدتيرم تاريخ جم؟؟؟؟
 
هذا السؤال :

Assignment # 3.A: Struct (Records)
PART(A)
1. Write the definition of a structure type named nameType, which includes the following components: firstName, middleName and lastName.

2. Write the definition of a structure type named dateType, which include the following components: day, month, and year;

3. Write the definition of a structure type named personType, which include the following components: ID of type long, name of type nameType, and DateOfBirth of type dateType.

4. Write a function named getData that take as parameter: list: an array of type personType, and size: number of items in the Employees array. The function should prompt the user to enter the ID, name, and DateOfBirth of all Employees.

5. Write a function named printName that take as parameter: emp of type personType. The function should output the name (first name and last name) of emp.


6. Write a function named bornOnJuly that take as parameter:
a. Employees: an array of type personType.
b. size: number of items in the Employees array.
The function should print the names (first name and last name) of all Employees born on July. Use printName function to print the names.

PART(B)
Write a main program to do the following:
1. Declare an array named list of 5 elements of type personType.
2. Use the function getData to enter the data of list.
3. Call the function bornOnJuly to print the names of employees born on July.

Assignment # 3.A: Struct (Records)
PART(A)
1. Write the definition of a structure type named nameType, which includes the following components: firstName, middleName and lastName.

2. Write the definition of a structure type named dateType, which include the following components: day, month, and year;

3. Write the definition of a structure type named personType, which include the following components: ID of type long, name of type nameType, and DateOfBirth of type dateType.

4. Write a function named getData that take as parameter: list: an array of type personType, and size: number of items in the Employees array. The function should prompt the user to enter the ID, name, and DateOfBirth of all Employees.

5. Write a function named printName that take as parameter: emp of type personType. The function should output the name (first name and last name) of emp.


6. Write a function named bornOnJuly that take as parameter:
a. Employees: an array of type personType.
b. size: number of items in the Employees array.
The function should print the names (first name and last name) of all Employees born on July. Use printName function to print the names.

PART(B)
Write a main program to do the following:
1. Declare an array named list of 5 elements of type personType.
2. Use the function getData to enter the data of list.
3. Call the function bornOnJuly to print the names of employees born on July.

وين الاخطاء ..
HTML:
#include <iostream>#include <string>using namespace std;
Struct nameType {string firstName;string middleName;string lastName;
};
Struct dateType{int day;int month;int year;
};
Struct personType{long ID ;nameType name;dateType DateOfBirth ;
};
void getData( personType list[] ,int size ) {	for (int i=0 ; i<size; i++)	{		cout<<"Enter the ID & name :"<<endl;		cin>>ID>>name;        cout<<"Enter the Date Of Birth of all Employees(day,month,year):"<<endl;        cin>>day>>month>>year;    }}
void printName ( personType emp ){	cout<<"The first name and last name :"<<endl;	cout<<firstName<<lastName<<endl;
}
void bornOnJuly  ( personType Employees[] , int size )     for (int i=0 ; i<size; i++)	 {		 if (month==7)			 printName (emp);	 }


int main(){int list [5];int size=5;
getData (list , size);
bornOnJuly (Employees , size);


	system("pause");	return 0;	}

اتمنى رد سريع و أكون شاكر لكم :blush-anim-cl:
 
هذا السؤال :

Assignment # 3.A: Struct (Records)
PART(A)
1. Write the definition of a structure type named nameType, which includes the following components: firstName, middleName and lastName.

2. Write the definition of a structure type named dateType, which include the following components: day, month, and year;

3. Write the definition of a structure type named personType, which include the following components: ID of type long, name of type nameType, and DateOfBirth of type dateType.

4. Write a function named getData that take as parameter: list: an array of type personType, and size: number of items in the Employees array. The function should prompt the user to enter the ID, name, and DateOfBirth of all Employees.

5. Write a function named printName that take as parameter: emp of type personType. The function should output the name (first name and last name) of emp.


6. Write a function named bornOnJuly that take as parameter:
a. Employees: an array of type personType.
b. size: number of items in the Employees array.
The function should print the names (first name and last name) of all Employees born on July. Use printName function to print the names.

PART(B)
Write a main program to do the following:
1. Declare an array named list of 5 elements of type personType.
2. Use the function getData to enter the data of list.
3. Call the function bornOnJuly to print the names of employees born on July.

Assignment # 3.A: Struct (Records)
PART(A)
1. Write the definition of a structure type named nameType, which includes the following components: firstName, middleName and lastName.

2. Write the definition of a structure type named dateType, which include the following components: day, month, and year;

3. Write the definition of a structure type named personType, which include the following components: ID of type long, name of type nameType, and DateOfBirth of type dateType.

4. Write a function named getData that take as parameter: list: an array of type personType, and size: number of items in the Employees array. The function should prompt the user to enter the ID, name, and DateOfBirth of all Employees.

5. Write a function named printName that take as parameter: emp of type personType. The function should output the name (first name and last name) of emp.


6. Write a function named bornOnJuly that take as parameter:
a. Employees: an array of type personType.
b. size: number of items in the Employees array.
The function should print the names (first name and last name) of all Employees born on July. Use printName function to print the names.

PART(B)
Write a main program to do the following:
1. Declare an array named list of 5 elements of type personType.
2. Use the function getData to enter the data of list.
3. Call the function bornOnJuly to print the names of employees born on July.

وين الاخطاء ..
HTML:
#include <iostream>#include <string>using namespace std;
Struct nameType {string firstName;string middleName;string lastName;
};
Struct dateType{int day;int month;int year;
};
Struct personType{long ID ;nameType name;dateType DateOfBirth ;
};
void getData( personType list[] ,int size ) {    for (int i=0 ; i<size; i++)    {        cout<<"Enter the ID & name :"<<endl;        cin>>ID>>name;        cout<<"Enter the Date Of Birth of all Employees(day,month,year):"<<endl;        cin>>day>>month>>year;    }}
void printName ( personType emp ){    cout<<"The first name and last name :"<<endl;    cout<<firstName<<lastName<<endl;
}
void bornOnJuly  ( personType Employees[] , int size )     for (int i=0 ; i<size; i++)     {         if (month==7)             printName (emp);     }


int main(){int list [5];int size=5;
getData (list , size);
bornOnJuly (Employees , size);


    system("pause");    return 0;    }

اتمنى رد سريع و أكون شاكر لكم :blush-anim-cl:

أخوي عموما عندك غلطين. أول شي الـ declaration مال الـ struct. أول "S" لازم تكون lower case مو كابيتال. يعني "struct" مو "Struct".

ثاني غلط عندك أهو الطريقة اللي تدش ابها الـ variables اللي داخل الـ struct. عشان تدش مثلا الـ name مال الـ emp. لازم تخليها جذي :
كود:
emp.name;
يعن مو بس تخلي الـ name لازم تخلي قبله الأوبجكت ماله. هني اللي هو الـ emp. طبعا اذا عندك لستة من الـ Employees بتكون تقريبا شي جذي :
كود:
arrayOfEmployees[0].name;

في بعد ملاحظة أخيرة ان في مكان اللي تدخل الـ Birth date بيكون عندك دبل. يعني أول شي تدش الـ Employee بعدين تدش الـ dateType. بيكون شي جذي:
كود:
cin>>arrayOfEmployees[0].DateOfBirth.day;

أدري قلت أخر شي بس انتبهت لغلط بعد :RpS_lol:. في الفنكشن اللي اسمها bornOnJuly() ما خليت الـ parenthesis "{}" ف بيصير عندك غلط.

هذلين الأغلاط بس اعتقد. بالتوفيق :RpS_sleep:
 
ايش رايكم من بكره نراجع مراجعه جماعيه اهني ...
 
واحد من الجماعة هني طلب مني اني اشرح ليه جبتر 10 مال الكتاب و أخليه هني عشان أي أحد يقدر يستفيد من الشرح بعد. و فعلا أكو بشرحه. بس أول شي لازم أقول -كالعادة- اني ما أدري هالمقرر لحد الحين لأني أول سنة, بالتالي معلوماتي ممكن تكون قاصرة أو غلط. المهم إلى الشرح :shiny:

جبتر 10 يتكلم عن العمليات على الـ lists أو الـ arrays طبعا أعتبر ان اللي يقرا الشرح عارف شنو الأري و شلون يسويه و يتعامل معاه ...الخ. و بعد يتكلم الجبتر عن الـ vector Type اللي هو مثل الأري بس فيه شغلات غير.
العمليات اللي في جبتر 10 ع الأري أهي الـ searching و الـ sorting. الـ searching مثل ما الإسم يقول يعني ان عندي عنصر أبي أشوفه موجود في اللستة أو لا, و إذا موجود أبي أعرف موقعه شنهو. الـ sorting بعد من ما الإسم يقول يعني يرتب اللستة من الأكبر للأصغر أو بالعكس.
أول شي لازم نتعلم الـ sorting. في واااايد Algorithms حق السورتنق. هني في الكتاب بنتعلم الـ bubble sort و الـ selection sort و الـ insertion sort.

أولاً الـ bubble sort :
الـ bubble sort مشهور بسهولته لكن بعدم فعاليته. الببل سورت عموماً شلون ؟ جدي الـ algorithm ماله (ترتيب تصاعدي) :

1-ابدأ في أول عنصر في الستة و شوف اذا كان أكبر من اللي بعده.
2-إذا كان أكبر بدلهم, إذا كان أصغر لا تسوي شي.
3-انتقل للعنصر اللي بعده و عيد الخطوات 1, و 2 لين توصل للعنصر قبل الأخير في اللستة.
4-عيد الخطوات 1و2و3 على قد العناصر اللي في اللستة.

الحين شرح الخطوات:
أول شي انت هدفك انك ترتب العناصر من الأصغر للأكبر (تصاعديا), يعني الأرقام العودة تبي توديها في النهاية و الأرقام الصغيرة تبي تخليها في البداية. ف ويش تسوي ؟ تاخد كلحين عنصرين ورا بعض في اللستة و تشوف هل ترتيبهم صحيح (الأكبر صاير بعد الأصغر) ؟ إذا كان صحيح تخليهم مثل ما أهم, إذا ما كان صحيح تقوم تقلب موقعهم (بنيي لهالشغلة بعد شوي).
الخطوة الصلثة أهي انك تسوي هالشي لكل عنصر في اللستة, بس ليش في قبل الأخير مو الأخير ؟ لأنك انت بتبدي من العنصر رقم 0 و بتقارنه ويا العنصر 0+1. إذا وصلت للعنصر قبل الأخير n-1 و قارنته مع العنصر الأخير n يعني خلصت اللستة, لان لو ما خليت قبل الأخير جان بتخلي العنصر الأخير +1 و بيعتفس البرنامج لأن تتحاول تدش عنصر مو موجود في الأري. و بتدش للشي اللي صوبه في الـ memory و بتغيره :no:. (طبعا ممكن تبدي من العنصر 1 و تستخدم 1-1 بدل 0+1, بس ماليه داعي هالتعقيد و مابي أخربط الأولي و التالي (وايد اخربط الأوادم :RpS_lol:)).
الخطوة الأخير ترتب اللستة قد عدد العناصر اللي فيها. لأن تخيل عند اللستة هذي :
كود:
int list[]  = {10,2,5,1};
نمشي وحدة وحدة. أول شي العشرة و الإثنين بيتبدلون, بعدين العشرة و الخمسة, بعدين العشرة و الواحد. يعني صارت لستتنا جذي :
كود:
list = {2,5,1,10}
يعني ما ترتبت. بس ودينا العشرة في النهاية ! من جدي لو عدنا الكود اربع مرات ( عدد عناصر اللستة) بتترتب كاملها.

الحين فهمنا الـ algorithm نقدر نكتبه كود:
كود:
for(int i = 0; i < size - 1;i++)
        for(int j = 0 ;j < size -1;j++){
            if(list[j] > list[j+1]){
                int temp = list[j+1];
                list[j+1] = list[j];
                list[j] = temp;
            }
        }

مثل ما نلاحظ عندنا 2 فور لووب. الفور لووب الخارجية أهي الخطوة الرابعة. الفور لوب الداخلة تفتر عليهم و تبدل إذا كان يحتاج تبديل. في شغلة لكن, شنهو قصة الـ temp و كل هالحركات ؟ الحين لو سيدا قلنا :
كود:
list[j+1] = list[j];
                list[j] = list[j+1];
أول شي نعتبر ان list[j] = 1 و list[j+1] = 0. الحين أول خطوة تقول لينا ان j+1 = j. يعني مكان الصفر نخلي 1. انزين الحين شنو صارت اللستة ؟ list[j+1] = 1 و list[j] =1. الحين لو حاولنا الخطوة الثانية ويش بيصير ؟بنبدل 1 بـ 1. و الصفر وين راح ؟ صار ليه overwrite. هالش ما نبيه, ف نقوم نخلي مكان نخلي فيه الصفر للين نبدل عشان نرد نرجعه. هالمكان مسمنه هني temp.
في شي بعد لاحظته ان البعض دايما يخلي int temp مع ان ممكن يحتاج شي غير. اعتبر المثال هذا :
كود:
#include <iostream>
using namespace std;


int main(){
    const int size = 4;


    struct Person{
        string name;
        long cpr;
    };


    Person list[size];


    Person p1;
    p1.cpr = 123456322;
    p1.name = "Ahmed";


    Person p2;
    p2.cpr = 862543276;
    p2.name = "Mohammed";


    for(int i = 0; i < size;i++)
        for(int j = 0 ;j < size -1;j++){
            if(list[j].cpr > list[j+1].cpr){
                Person temp = list[j+1];
                list[j+1] = list[j];
                list[j] = list[j+1];
            }
        }


    return 0;
}

هني نفس ما نشوف في المثال الـ temp مو لازم ع طول يكون int. أحنا صدق هني نرتبهم ع حسب الـ cpr بس ما نبي نسيف بس الـ cpr في الـ temp, نبي نسيف الـ person كله. وايد شفت تأغلاط جذي قلت بنبه ع هالشغلة.
هذا ما لزم عن الـ bubble sort. بكمل الشرح مرة ثانية :smile2: انشالله بس ما عقدت الأمور زيادة عن اللزوم :RpS_lol:
 
التعديل الأخير:
السلام عليكم .. شخباركم :blushing:
شخبار الاجازة معاكم اللي باقي لها يوم وتخلص :tears:
المهم بدخل ف الموضوع دايركت .. امممم انا كتبت برنامج بس عندي اغلاط ومو عارفه وين :whistling:
هاذ البرنامج :
كود:
#include <iostream>#include <string>
using namespace std;
struct studenttype
{


	string studentFname , studentLname ;
	int testscore ;
	char grade ;
};
void readdata (studenttype list[],int size);
void assinggrade (studenttype list[],int size);
void findhighesttscore (studenttype list[],int size);
void printdata (studenttype list[],int size);
int main ()
{
	studenttype clas[20];
	readdata (clas , 3);
	assinggrade (clas , 3);
	findhighesttscore (clas , 3);
	printdata (clas , 3 );


	system ("pause");
}
void readdata (studenttype list[],int size)
{
	for (int i=0;i<size;i++)
	{
		cout<<"enter studentFname:"<<endl;
	    cin>>list[i].studentFname;
		cout<<"enter studentLname:"<<endl;
        cin>>list[i].studentLname;    
		cout<<"enter student test score:"<<endl;
		cin>>list[i].testscore;
	}
}
void assinggrade (studenttype list[],int size)
{
	  char a,b,c,d,f;


      for (int i=0;i<size;i++)
	  {
		  if (list[i].testscore>=90)
			{
				list[i].grade=a;
		  cout<<list[i].studentLname<<","<<" "<<list[i].studentFname<<" "<<list[i].testscore<<" "<<list[i].grade<<endl;
		  }
		  else if (list[i].testscore>=80)
		  {
			   list[i].grade=b;
		  cout<<list[i].studentLname<<","<<" "<<list[i].studentFname<<" "<<list[i].testscore<<" "<<list[i].grade<<endl;
		  }
		  else if (list[i].testscore>=70)
		  {
			   list[i].grade=c;
		  cout<<list[i].studentLname<<","<<" "<<list[i].studentFname<<" "<<list[i].testscore<<" "<<list[i].grade<<endl;
		  }
		  else if (list[i].testscore>=60)
		  {
			   list[i].grade=d;
		  cout<<list[i].studentLname<<","<<" "<<list[i].studentFname<<" "<<list[i].testscore<<" "<<list[i].grade<<endl;
		  }
		  else 
			   list[i].grade=f;
		  cout<<list[i].studentLname<<","<<" "<<list[i].studentFname<<" "<<list[i].testscore<<" "<<list[i].grade<<endl;
	  }
}
void findhighesttscore (studenttype list[],int size)
{
   int max = 0;
   for ( int i =0;i<size;i++)
   {
	   if (list[i].testscore>max)
		   max = list[i].testscore;
   }
}
void printdata (studenttype list[],int size)
{
	int max;
	 for ( int i =0;i<size;i++)
   {
	   if (list[i].testscore==max)
	   cout<<list[i].studentLname<<","<<" "<<list[i].studentFname<<endl;
	 }
}
 
عودة
أعلى أسفل