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

طلب بسيط " برنامج يقوم بترتيب 10 قيم تصاعدياً " array

بدأه mo3az في 13 أبريل 2010 · 6 رد · 885 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

هل لي بطلب كود لـ برنامج " يقوم بترتيب 10 قيم

حيث نحن من يقوم بإدخالها ،، ثم يقوم بترتيبها تصاعدياً من الأصغر للـ أكبر عن طريق الـ Array ...

بلـغة + + c

سي بـَلس بـَس ...

ومشكوِرَين سَلفاً . . .

مع الإح ـترآم ..

#2
#include <iostream>
using std::cout;
using std::endl;
using std::cin ;
#include <iomanip>
using std::setw;

int main()
  {
     const int arraySize = 10; // size of array a
     int data[ arraySize ] = { 0 };
     int insert; // temporary variable to hold element to insert

     for ( int i = 0; i < arraySize; i++ ) {
        cout << "please enter number " << i+1 << ": " ;
        cin >> data ;
     }

      // output original array
     cout << "Unsorted array:\n";
     for ( int i = 0; i < arraySize; i++ )
        cout << setw( 4 ) << data[ i ];


     // insertion sort
     // loop over the elements of the array
     for ( int next = 1; next < arraySize; next++ )
     {
        insert = data[ next ]; // store the value in the current element

        int moveItem = next; // initialize location to place element

        // search for the location in which to put the current element
        while ( ( moveItem > 0 ) && ( data[ moveItem - 1 ] > insert ) )
        {
           // shift element one slot to the right
           data[ moveItem ] = data[ moveItem - 1 ];
           moveItem--;
        } // end while

        data[ moveItem ] = insert; // place inserted element into the array
     } // end for

     cout << "\nSorted array:\n";

     // output sorted array
     for ( int i = 0; i < arraySize; i++ )
        cout << setw( 4 ) << data[ i ];

     cout << endl;
     return 0; // indicates successful termination
  } // end main
#3

وهنا باستخدام array عادي بدون linked list

#include <iostream>

using namespace std;

int main()
{
   int arr[10],i,j,t;
 cout<<"Enter the Elements\n";
	 for(i=0;i<10;i++)
		 cin>>arr;

//Sorting Elements;
 for(i=0;i<10;i++)
 {
  for(j=1;j<10;j++)
  {
    if (arr[j-1]>arr[j])
	 {
	 	t=arr[j-1];
		arr[j-1]=arr[j];
		arr[j]=t;
	 }
  }
 }

 for(i=0;i<10;i++)
   cout<<"  "<<arr;
    system("PAUSE");
}

تحياتي

تم تعديل هذه المشاركة بواسطة omani في 13 أبريل 2010 في 19:55

1

OMANI FOR EVER

-------------------------------------------------

post-12787-12780191801974.jpg

-------------------------------------------------

#4

السلام عليكم هذا حل أعتقد أنه أبسط قليلاً و الشرح موجود عليه

#include<iostream.h>
void main()
{
	int arr[10]={0};
	cout<<"enter 10 numbers .please\n-------------------------------"<<endl;
	for(int i=0;i<10;i++)
	{
		cout<<"number"<<i+1<<":"<<endl;
		cin>>arr;
	}
	for(int k=0;k<9;k++)//
	{
		for(int m=0;m<9-k;m++)
		{
			if(arr[m]>arr[m+1])
			{int temp;
			temp=arr[m];        
				arr[m]=arr[m+1];            
				arr[m+1]=temp;                

			}//end if
		}//end for
	}//end for
			cout<<"new array is:{";
			for(int n=0;n<10;n++)
			{
				cout<<arr[n]<<",";
			}
			cout<<"}"<<endl;
	}

عدد الاحتمالات القصوى لمقارنة عنصر مع ما يليه هو من الصفر إلى حجم المصفوفة -1 و يمكنك اثبات ذلك احتمالياً .

و لقد وضعنا متغير temp من أجل تخزين قيمة العنصر السابق لأن قيمته سيوضع بعدها قيمة العنصر اللاحق و قيمة العنصر اللاحق تُستبدل بقيمة المتغير temp .

عذرا أخي العماني و لكن يبدو أننا وضعنا الحل سوياً لذلك أنا اعتذر منك إن كان ذلك أزعجك و لكن و الله ماكنت أعلم أنك ستضع نفس الطريقة .

تم تعديل هذه المشاركة بواسطة yahya91 في 13 أبريل 2010 في 20:22

#5
yahya91 كتب:

عذرا أخي العماني و لكن يبدو أننا وضعنا الحل سوياً لذلك أنا اعتذر منك إن كان ذلك أزعجك و لكن و الله ماكنت أعلم أنك ستضع نفس الطريقة .

بالعكس أخي الكريم : التفاعل من خلال اعضاء المنتدى يسعدني دوماً .. وردك يدل على تفاعلك مع المنتدى وأعضاءه .

تقبل تحياتي

OMANI FOR EVER

-------------------------------------------------

post-12787-12780191801974.jpg

-------------------------------------------------

#6

حل سريع للغايه :) :

#include <cstdlib>
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;

int main(int argc, char *argv[])
{
    vector<int> arrays;

    cout << "Enter Integers (CTRL-Z to Stop) : " ;
    copy(istream_iterator<int>(cin),istream_iterator<int>(),back_inserter(arrays));

    cout << "Before Sorting:" << endl;
    copy(arrays.begin(),arrays.end(),ostream_iterator<int>(cout," "));
    cout << endl;

    sort(arrays.begin(),arrays.end());

    cout << "After Sorting:" << endl;
    copy(arrays.begin(),arrays.end(),ostream_iterator<int>(cout," "));
    cout << endl;

    system("PAUSE");
    return EXIT_SUCCESS;
}
1

http://informatic-ar.com منصة تعليمية عربية في علوم الحاسب والبرمجة

https://moalfat.com  للكتب الالكترونية والكورسات التعليمية

Everything we see now is just an engineering solution based on old science

#7

مشكورِين جداً يا جماعة ، ويعطيكم ألف عآفية على جهودكم المباركــــَة ..

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

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…