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

سؤال حول المصفوفات

بدأه l7ool في 17 مايو 2012 · 1 رد · 654 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

انا أستخدم برنامج c++ وحليت بعض الأسئله لكن بعضها لم يتنفذ لي بالشكل الصحيح الكومبايلر يقول صحيح لكن عند التنفيذ يكون هناك اخطاء او اخطاء في الرن تايمPosted Image

السؤال هذا حول المصفوفات

Write a program that reads from the user two words (as array of characters) the program

then copies the shortest word into an array and prints the word to the user along with number of characters in the word.

As an example: Word1: “example” Word2: “tutorial”

The program would output: the shortest word is “tutorial” with 8 characters.

وهذا هو حلي ولا أعلم مالمشكله ؟

#include<iostream>
using namespace std;

const int size=15;

int main()
{
char a1[size];
char a2[size];
int j;
int i;
cout<<"enter firts word :"<<endl;
for(i=0;i<=size;++i);{
cin>>a1;}

cout<<"enter secound word :"<<endl;

for(j=0;j<=size;++j);
{cin>>a2[j];
}
if (a1<a2) 
cout<<"the shortest word is" <<a1<<" with"<< i<< "characters"<<endl;
else
cout<<"the shortest word is" <<a2<<" with"<< j<< "characters"<<endl;
system("pause");
return 0;
}

شاكره ومقدره المساعده والله يعطيكم العافيه :)

#2

إقرأي الأخطاء التي يُرسلها المُصرف compiler. لديك أخطأ في الإعراب في جملة for , هذا الجزء :

for(j=0;j<=size;++j);
{cin>>a2[j];
}

خطأ. بنية جملة for تأتي بالصيغ :

for ( X ; Y ; Z ) {
    // code ;
}

for ( X ; Y ; Z )
    // code ;

for ( X ; Y ; Z );

أعيدي مراجعة حلقات التكرار (وحاولي تنسيق كتابتك ﻻداعي لحشر الأكواد).

هذا البرنامج بعد التعديل :

#include<iostream>
using namespace std;

// note [1]
#define size 256

int main() {

    char a1[size];
    char a2[size];
    int j;
    int i;

    // note [2]
    cout << "Enter the first word  : ";
    cin.width(255);
    cin >> a1;

    cout << "Enter the second word : ";
    cin.width(255);
    cin  >> a2;


    // note [3]
    for( i = 0 ; a1 != '\0' ; i++  );
    for( j = 0 ; a2[j] != '\0' ; j++  );

    if (i < j) 
        cout << "The shortest word is '" << a1 << "' with " << i << " characters" << endl;
    else
        cout << "The shortest word is '" << a2 << "' with " << j << " characters" << endl;

    system("pause");
    return 0;
}

1 - إستخدام التوجيهات أفضل من إستخدام المتغيرات هنا , وأعطي مساحة أكبر للنصوص.

2 - ﻻحاجة لحلقة تكرار للقراءة من المستخدم. يمكنك إستخدام width لأخذ عدد معين من الحروف من المستخدم, وﻻتنسي أن تضعي مساحة للنهاية السلسلة النصية '0\'.

3 - حلقتي تكرار لزيادة قيمة i و j ( لحساب عدد الأحرف دون إستخدام دالة خارجية) طالما لم نصل لنهاية النصّ '0\'.

1

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