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

ممكن شرح حل السؤال

بدأه rwad2000 في 16 يونيو 2009 · 1 رد · 659 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

ممكن حد يشرح حل السؤال التالي أو يعطيني حل تاني للسؤال يكون ابسط باكون ممنون

Use a single-subscripted array to solve the following problem. Read in 20 numbers, each of which is between 10 and 100,

inclusive. As each number is read, print it only if it is not a duplicate of a number already read. Provide for the "worst case" in which

all 20 numbers are different. Use the smallest possible array to solve this problem.

و حله كالتالي

#include <iostream>

using std::cout;

using std::endl;

using std::cin;

#include <iomanip>

using std::setw;

int main()

{

const int SIZE = 20;

int a[ SIZE ] = { 0 }, subscript = 0, duplicate, value;

cout << "Enter 20 integers between 10 and 100:\n";

for ( int i = 0; i < SIZE; ++i ) {

duplicate = 0;

cin >> value;

for ( int j = 0; j < subscript; ++j )

if ( value == a[ j ] ) {

duplicate = 1;

break;

}

if ( !duplicate )

a[ subscript++ ] = value;

}

cout << "\nThe nonduplicate values are:\n";

for ( i = 0; a[ i ] != 0; ++i )

cout << setw( 4 ) << a[ i ];

cout << endl;

return 0;

}

تم تعديل هذه المشاركة بواسطة rwad2000 في 16 يونيو 2009 في 21:01

#2

السلام عليكم

حل آخر ممكن أن يكون أبسط

#include <iostream>
using namespace std;
int IsFound(int arr[],int n,int val)
{
	int c=0;
	for(int i=0;i<n;i++)
	{
		if(arr==val)
			c++;
	}
	return c;
}
int main()
{
	const int size=10;
	int a[size];
	int num,i;
	int s=0;
	for( i=0;i<10;i++)
	{
		cin>>num;
		if(!IsFound(a,i,num))
			a[s++]=num;

	}
	cout<<" the nonduplicated members :: \n";
	for (i=0;i<s;i++)
	{
		cout<<a<<"  ";
	}
cout << endl;
return 0;
}

اللهم صل على سيدنا محمد وعلى آله وصحبه وسلم

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