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

لغه c++

Aya Alfaky

السؤال


4- Write a program that prompts the user to enter three integer values, and then outputs the values in numerical sequence separated by commas. So, if the user enters the values 10 4 6, the output should be 4, 6, 10. If two values are the same, they should just be ordered together. So, the input 4 5 4 should give 4, 4, 5.
 

عايزه البرنامج دا مكتوب بلغه الc++ لو ممكن

تم التعديل في بواسطة Aya Alfaky
رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

أهلًا بك،

هناك دالة في c++ اسمها sort تقوم بالترتيب من الصغير للكبير

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int intArray[3] = {0, 0, 0};

    cout << "input 3 integers values with spaces between them\n";
    cin >> intArray[0] >> intArray[1] >> intArray[2]; 

    //Since you are using std you should have this sort method
    sort(intArray, intArray + 3);

    cout << intArray[0] << ", " << intArray[1] << ", " << intArray[2];

    return 0;
}

 

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

  • 0

ممكن باستخدام sort & callback & classes, friend classes

#include <iostream>
#include <algorithm>


using namespace std;

class Item{
	public:
	friend class Test;

	private:
	int item;
	
	public:
	Item(){
		item=0;
	}

	int getItem(){
		return item;
	}
	void setItem(int _item){
		item=_item;
	}
};

class Test{
	private:
	static int itemCmp(Item item1, 
			Item item2)
	{
		if(item1.item<item2.item)
			return 1;
		return 0;
	}
	
	public:
	void getItemsFromSTDIN(Item item[]){
		int rd;
		int idx;

		cout<<"Now write the numbers: ";
		
		for (idx=0; idx<3; idx++){
			cin>>rd;
			item[idx].item=rd;
		}
	}
	
	void sort(Item item[]){
		std::sort(item, 
			item+3,
			itemCmp);
	}
	
	void print(Item item[]){
		cout
			<<item[0].item
			<<", "
			<<item[1].item
			<<", "
			<<item[2].item
			<<endl;
	}
};

int main(int argsCount, char **argsValues){
	
	Item item[3];
	Test test;
	
	test.getItemsFromSTDIN(item);
	test.sort(item);
	test.print(item);

    return 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.

  • إعلانات

  • تابعنا على



×
×
  • أضف...