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

برنامج C++ لإجراء عمليات على المصفوفات

Anwar Shweiki

السؤال

Write c++ program that reads 10 elements and store them in array , then do the follwing:
1- find the average using function avgfun()
2- find the maximum using function maxfun()
3- sort the array in decending order using function sort()
4- search for an entered number x in the array and print its index if it is found, or not found , using function searchfun()

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

Recommended Posts

  • 1

مرحباً بك أنور،

يمكن تنفيذ المطلوب كالآتي:

#include <iostream>

using namespace std;

void avgfun(int numbers[], int length)
{
    int total = 0;
    for(int i = 0; i < length; i++)
        total += numbers[i];
    float average = (float) total / length;
    cout << average << endl;
}

void maxfun(int numbers[], int length)
{
    int max = numbers[0];
    for(int i = 0; i < length; i++)
    {
        if(max < numbers[i])
            max = numbers[i];
    }
    cout << max << endl;
}

void sort(int numbers[], int length)
{
    int array[length];

    for (int l = 0; l < length; l++)
        array[l] = numbers[l];

    for(int i = 0; i < length - 1; i++)
    {
        for(int j = 0; j < length - i - 1; j++)
        {
            if(array[j] < array[j + 1])
            {
                int tmp = array[j];
                array[j] = array[j + 1];
                array[j + 1] = tmp;
            }
        }
    }

    for(int i = 0; i < length; i++)
    {
        cout << array[i] << " ";
    }
    cout << endl;
}

void search(int numbers[], int length)
{
    int number;
    cout << "Number: ";
    cin >> number;
    for(int i = 0; i < 10; i++)
    {
        if(numbers[i] == number)
        {
            cout << "Number found at index " << i << endl;
            return;
        }
    }
    cout << "Not Found." << endl;
}

int main()
{
    int numbers[10], choice;

    cout << "Enter the numbers separated by spaces" << endl;

    for(int i = 0; i < 10; i++)
        cin >> numbers[i];

    cout << "1) Find the average value." << endl;
    cout << "2) Find the maximum value." << endl;
    cout << "3) Sort the array in descending order." << endl;
    cout << "4) Search for number." << endl;
    cout << "5) Exit." << endl;

    do {
        cout << "Choice: ";
        cin >> choice;
        switch(choice)
        {
            case 1:
                avgfun(numbers, 10);
                break;
            case 2:
                maxfun(numbers, 10);
                break;
            case 3:
                sort(numbers, 10);
                break;
            case 4:
                search(numbers, 10);
                break;
            case 5:
                return 0;
            default:
                cout << "Wrong Choice." << endl;
        } 
    } while(true);
}

 

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

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

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

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

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   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.

  • إعلانات

  • تابعنا على



×
×
  • أضف...