اذهب إلى المحتوى
  • 0

لغه السي بلس بلس

حمامه السلام

السؤال

Write a Name_pairs class holding three members (name, age, size) where name is an array of 
strings, age is an array of ints, and size is the size of name and age arrays. Write a destructor and 
constructor that accepts one input argument which the size. Provide an input operation (method) d  . 
read_names that reads a series of names. Provide a read_ages operation that prompts the user 
for an age for each name. Provide a print operation that prints all the (name[i], age[i]) pairs (one 
per line) in the order determined by the name array. Provide a sort operation that sorts the name
array in alphabetical order and reorganizes the age array to match. Create a C++ program and let 
user enter 5 names and ages and then use print method to print them. Sort the name array and 
print it again. 
Notes: 
1- Sort the names in name according to the first character only and sort only for the first five 
letters. i.e, A, B, C, D, and E. 
2- For example, enter the following names: Baraa, Ahmed, Emad, Diaa, and Chloe.

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

مرحباً ..
يمكنك عمل البرنامج بهذه الطريقة بلغة c++ 

#include <iostream>
using namespace std;

// تعريف الكلاس
class Name_pairs  {

    // تعريف خصائص الكلاس
public:
    int size;
    string *names;
    int *age;

    // constructor
public:
    Name_pairs(int size) {
        cout <<"Constructor is called" <<endl;
        this->size = size;
        this->names = new string[size];
        this->age = new int [size];
    }

    // destructor
public:
    ~Name_pairs() {
        cout <<"\nDestructor is called" <<endl;
        delete []names;
        delete  []age;
    }

    // تعريف الميثود المسؤولة عن قراءة الأسماء
    void read_names() {
        for (int i = 0; i < this->size; i++) {
            cout << "Enter name " << (i+1) << ": "<<endl;
            cin >> this->names[i];
        }
    }

    // تعريف الميثود المسؤولة عن قراءة الأعمار
    void read_ages() {
        for (int i = 0; i < this->size; i++) {
            cout << "Enter age " << (i+1) << ": "<<endl;
            cin >> this->age[i];
        }
    }

    // طباعة السجلات
    void print() {
        for (int i = 0; i < this->size; i++) {
            cout << "record " << (i+1) << ": (" << this->names[i] << ", " << this->age[i] << ")"<<endl;
        }
    }

    // selection sort (select the min)
    void sort() {
        int i, min, j, tmp_age;
        string tmp_name;

        for(i = 0; i < this->size - 1; i++) {
            min = i;
            for (j = i; j < this->size; j++)
                if(this->names[j][0] < this->names[min][0])
                    min = j;

            tmp_name = this->names[i];
            this->names[i] = this->names[min];
            this->names[min] = tmp_name;

            tmp_age = this->age[i];
            this->age[i] = this->age[min];
            this->age[min] = tmp_age;
        }

    }

};

int main() {

    // create instance from Name_paires class
    Name_pairs obj(5);


    obj.read_names(); // read names
    obj.read_ages(); // read ages

    cout << "\nRecords Before Sorting: " << endl;
    obj.print(); // print records before sorting

    obj.sort(); // call sort method
    cout << "\nRecords After Sorting: " << endl;
    obj.print(); // print records after sorting
    
}

و عند إدخال الأسماء المطلوبة أي:
Baraa, Ahmed, Emad, Diaa, and Chloe و الأعمار التالية: 20, 21, 22, 23, 24
ستظهر النتيجة بهذا الشكل 
cpp_sort_1.thumb.PNG.7f830dad7f2d1b591287422afa475bd2.PNG

cpp_sort_2.thumb.PNG.f6a0e14ad492cba00ae9f9f0599a9dbd.PNG

يمكنك تجربة البرنامج من خلال هذا الرابط

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

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0

شكرا على هاي النصيحه 

بس اني طبقت البرنامج ويطلع خطا بالكود وكنت ما عامله دليت فلذلك صار عندي مشكله بالكود ولهذا اجيت هنا حتى اشوف شنو الخطا الي عندي 

وشكرا 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...