موقع طلبة جامعة البحرين

يمكنك تصفح الموقع كزائر ولكن ندعوك لتسجيل عضوية خاصة بك لتحصل على كافة الصلاحيات مثل تنزيل ملفات المكتبة وقراءة تعليقات هيئة التدريس وغيرها. يمكنك الحصول على عضوية مجانية بالضغط على زر تسجيل. إذا قمت بالتسجيل مسبقا فيمكنك الضغط على زر دخول.

تسجيل دخول
  • قسم المكتبة الجامعية مغلق مؤقتا بعد الاستهداف التي تعرضت له بعض خدمات أمازون في البحرين، جاري استرجاع الملفات ونقلها إلى سيرفرات جديدة وسيتم بعدها اعادة افتتاح المكتبة

ITCS112 - ITCS104 - ITCS102 للفصل الصيفي 2014 - 2015

شباب هذا محاولتي للحل في Problem 4 in Pointers .. البرنامج يشتغل بس ما يرجع شئ .. جيكوا ليي وين المشكله وإذا في خطأ في الحل خصوصاً في الداينمك

PHP:
/*

Problem 4:
a.  Write a function LinearSeachPtr that takes as parameters a pointer to an array of integers, the
array size, and a key value. The function should search for the first occurrence of the key value in
the array and return its position. If the value is not found it should return -1. The prototype of the
function is:            int LinearSearchPtr(int *, int, int);

b.  Write a main function that includes the following statement:
b.1-  Write a statement to declare a pointer named arrPtr of type int.
b.2-  Write a statement to let arrPtr to point to a dynamic one-dimension array of 10 integers.
b.3-  Write a statement(s) to prompt the user to enter 10 integers and store them in the dynamic
array pointed by arrPtr.
b.4-  Write a statement(s) to search for the value (33) in the dynamic array pointed by arrPtr
using the function LinearSearchPtr defined in (a) and output an appropriate message to
show if the value 33 is found in the array or not.

*/

#include <iostream>
using namespace std;

int LinearSearchPtr(int *p,int size,int key)
{
    for(int i=0 ; i<size ; i++)
    {
        if(p[i]==key)
            return i;
    }
}

void printing(int *p, int size)
{
    for(int i=0 ; i<size ; i++)
    {
        cout<<p[i]<<" ";
    }
}

int main()
{
    int arr[10];
    int size=10;
    int key;
    int *p;
    int *arrPtr;

    p=&arr[0];

    arrPtr=new int[size];

    cout<<"Please Enter Array Values: ";
    for(int i=0 ; i<10 ; i++)
    {
        cin>>*(p+i);
    }

    cout<<"Please Enter A Value To Search: ";
    cin>>key;


    printing(p,size);

    cout<<endl;

    LinearSearchPtr(p,size,key);

    cout<<endl;

    printing(p,size);

    cout<<endl;

    system("pause");
    return 0;
}

سويت جذي في الـMain عشان أتأكد ان كود الفنكشن صح ولا لا

PHP:
    if(LinearSearchPtr(p,size,key))
    cout<<"Found lah!! Mabrook ya hlras"<<endl;

    else cout<<"Not Found lah!! .. Stupid search in your array ya aldeala!!"<<endl;

    cout<<endl;

وإذا اكتب له رقم موجود يطلع لي
PHP:
Found lah!! Mabrook ya hlras

وإذا اكتب له رقم غير موجود يطلع لي
PHP:
Not Found lah!! .. Stupid search in your array ya aldeala!!

..

يعني الكود صح .. بس الـreturn غلط إلي اسويه .. شوفوا ليي حل يا جماعه لا اخمبق بقوة
 
التعديل الأخير:
^
ما جيكت الكود كامل، بس من اللي شفته بالفنكشن انك مسوي return i
هذا راح يكون في حاله إذا كان الرقم موجود، لكن إذا ما كان موجود هنيه المشكلة
كل اللي عليك تسويه انك تضيف تحت اللوب
return -1

إذا خلص من اللوب والرقم مب موجود، فراح يرجع 1-

عموماً هذا الحل، حليته من فتره وللحين موجود عندي
شوفه وقارنه مع حلك
PHP:
#include <iostream>
using namespace std;

int LinearSearchPtr(int *intArray, int size, int key);
int main()
{
    int *arrPtr;
    arrPtr = new int[10];
    int K;

    cout<<"Enter 10 integer values:"<<endl;
    for(int i=0; i<10; i++)
    {
        cout<<"Value #"<<i+1<<":\t";
        cin>>arrPtr[i];
    }

    K=LinearSearchPtr(arrPtr, 10, 33);

    if(K==-1)
        cout<<"The key is not found"<<endl;
    else
        cout<<"Found and its position is: "<<K<<endl;

    delete [] arrPtr;
    arrPtr=NULL;

    system("pause");
    return 0;
}
int LinearSearchPtr(int *intArray, int size, int key)
{
    for(int i=0; i<size; i++)
        if(intArray[i]==key)
            return i;
    return -1;
}
 
^
ما جيكت الكود كامل، بس من اللي شفته بالفنكشن انك مسوي return i
هذا راح يكون في حاله إذا كان الرقم موجود، لكن إذا ما كان موجود هنيه المشكلة
كل اللي عليك تسويه انك تضيف تحت اللوب
return -1

إذا خلص من اللوب والرقم مب موجود، فراح يرجع 1-

عموماً هذا الحل، حليته من فتره وللحين موجود عندي
شوفه وقارنه مع حلك
PHP:
#include <iostream>
using namespace std;

int LinearSearchPtr(int *intArray, int size, int key);
int main()
{
    int *arrPtr;
    arrPtr = new int[10];
    int K;

    cout<<"Enter 10 integer values:"<<endl;
    for(int i=0; i<10; i++)
    {
        cout<<"Value #"<<i+1<<":\t";
        cin>>arrPtr[i];
    }

    K=LinearSearchPtr(arrPtr, 10, 33);

    if(K==-1)
        cout<<"The key is not found"<<endl;
    else
        cout<<"Found and its position is: "<<K<<endl;

    delete [] arrPtr;
    arrPtr=NULL;

    system("pause");
    return 0;
}
int LinearSearchPtr(int *intArray, int size, int key)
{
    for(int i=0; i<size; i++)
        if(intArray[i]==key)
            return i;
    return -1;
}

نفس اللحل اخوي .. بس ماعرف وين المشكله .. المهم إذا عندك حل Problem 5 .. و شلون نحله
 
ما قدرت احل هذا السؤال، طلب 2D dynamic array
الـArray لازم القيم اللي فيها تكون من نفس النوع لكن بالسؤال فيه string & double & integer
لازم تحويل لنوع القيم الموجودة اعتقد
 
ما قدرت احل هذا السؤال، طلب 2D dynamic array
الـArray لازم القيم اللي فيها تكون من نفس النوع لكن بالسؤال فيه string & double & integer
لازم تحويل لنوع القيم الموجودة اعتقد

لا بن عمي انت تتكلم عن Problem 6

انا اقولك Problem 5
 
مادري والله يمكن انت التتوريال عندك اسئلة زيادة
هذا السؤال هو Problem 5 عندي
و Problem 6 عبارة عن سؤال output

اكتب لي السؤال إذا ما عليك أمر
 
مادري والله يمكن انت التتوريال عندك اسئلة زيادة
هذا السؤال هو Problem 5 عندي
و Problem 6 عبارة عن سؤال output

اكتب لي السؤال إذا ما عليك أمر

لا تشيل هم .. الحين احطهم افا عليك

و بخصوص الـtow Dim Array .. بتكون جذي

Filling function
PHP:
void fill(int **p, int rowSize, int columnSize)
{
for (int row = 0; row < rowSize; row++)
{
cout << "Enter " << columnSize << " number(s) for row "
<< "number " << row << ": ";
for (int col = 0; col < columnSize; col++)
cin >> p[row][col];
cout << endl;
}
}

Printing function
PHP:
void print(int **p, int rowSize, int columnSize)
{
for (int row = 0; row < rowSize; row++)
{
for (int col = 0; col < columnSize; col++)
cout << setw(5) << p[row][col];
cout << endl;
}
}

Main Function
PHP:
int main()
{
int **board; 

int rows;
int columns; 

cout << "Line 4: Enter the number of rows "
<<"and columns: ";
cin >> rows >> columns; 
cout << endl; 

//Create the rows of board
board = new int* [rows]; 

//Create the columns of board
for (int row = 0; row < rows; row++) 
board[row] = new int[columns];

//Insert elements into board
fill(board, rows, columns); 

cout << "Line 11: Board:" << endl; 

//Output the elements of board
print(board, rows, columns); 

system("pause");
return 0;
}
 
PHP:
Problem 5:   
Write a function named FirstOccurence that takes as parameters a pointer to an array of chars, the 
array size and a character Ch. The function should return a pointer that point to the first occurrence of 
Ch in the string. If Ch is not found the function should return NULL. The prototype of the function 
is: 
        char * FirstOccurence (char arr[], int size, char Ch)

PHP:
Problem 7:   
What is the output of the following programs? 
Output - 1 
int main ( ) { 
double *p,*q; 
double i=2.3, k =4.3; 
p=new double; 
*p=i;   q=p; 
cout<<*p<<" "<<*q<<endl; 
i=8.4; 
cout<<*p; 
p=&i;   i=5.2; 
cout<<"  "<<*p<<endl; 
*q=k; 
cout<<*p<<" "<<*q<<endl; 
return 0; 
} 
Output – 2 
int main( ) 
{ 
 int *ptr1, *ptr2,*ptr3; 
 int x=1, y=1; 
 ptr1=&x;  ptr2=new int[3]; ptr3=ptr2; 
 *ptr3=10; 
 for(int i=0;i<2;i++) 
   { ptr2[i]=y*x+*ptr3; 
     ptr2[*ptr1+i]=ptr2[i]+*ptr1; 
     y+=*ptr1; 
   } 
 cout<<x<<" "<<y<<" "<<*ptr1<<endl; 
 for(int i=0;i<3;i++) 
    cout<<ptr2[i]<<" "; 
return 0;    
}
 
Problem 5: Write a function named FirstOccurence that takes as parameters a pointer to an array of chars, the array size and a character Ch. The function should return a pointer that point to the first occurrence of Ch in the string. If Ch is not found the function should return NULL. The prototype of the function is: char * FirstOccurence (char arr[], int size, char Ch) احد عنده مشكله في حل هالسؤال ؟
 
Problem 5: Write a function named FirstOccurence that takes as parameters a pointer to an array of chars, the array size and a character Ch. The function should return a pointer that point to the first occurrence of Ch in the string. If Ch is not found the function should return NULL. The prototype of the function is: char * FirstOccurence (char arr[], int size, char Ch) احد عنده مشكله في حل هالسؤال ؟

انااااااااااا :whistling:

ويش سالفته ؟.
 
وعندي سؤال بعد شلون نستخدم الـoverloading ؟ .. اعرف احله بس شلون نسنخدمه في المين و شلون نسوي له excute عشان يطلع في الـoutput ؟!!
 
انااااااااااا :whistling:

ويش سالفته ؟.


If we have dynamic array , by default the pointer points to first index

الFunction نطرش لها بوينتر يؤشر على array و size و ch
ch هو اي حرف نحدده
راح نبحث بأستخدام seq. search
ولما نحصل نغير مكان اللي قاعد يؤشر عليه الى مكان اللي حصلنا فيه الحرف
ولما مانحصله نقول البوينتر يؤشر الى NULL

اتمنى واضح
 
وعندي سؤال بعد شلون نستخدم الـoverloading ؟ .. اعرف احله بس شلون نسنخدمه في المين و شلون نسوي له excute عشان يطلع في الـoutput ؟!!

نفرض عندنا
PHP:
student obj1;
student obj2;
student obj3;
الاول والثاني فيهم data والثالث فاضي
نبي نخلي في الثالث مجموع الاول والثاني

PHP:
obj3 = obj1+job2 ;
بمجرد كتبت + وانا كاتب له كود ( يعتبر انا استخدمت overloading داخل main )
لان لو ماكتب الكود الخاص بـ +
بعطيك error
 


If we have dynamic array , by default the pointer points to first index

الFunction نطرش لها بوينتر يؤشر على array و size و ch
ch هو اي حرف نحدده
راح نبحث بأستخدام seq. search
ولما نحصل نغير مكان اللي قاعد يؤشر عليه الى مكان اللي حصلنا فيه الحرف
ولما مانحصله نقول البوينتر يؤشر الى NULL

اتمنى واضح

تسسسسلم يالغالي بس ما عليك امر ابي الحل وشلون نسوي call لفنكشن بالبوينت مالها ؟! ؟؟ لو تلاحظ اهي صايره جذي *function name
 
لان بترجع pointer
قلنا صوب char *
لان مابترجع حرف ، بترجع pointer من نوع char

الحل

PHP:
   char *  FirstOccurence (char *p, int size, char Ch){
              for(int i=0;i<size;i++)
              if(p[i]==Ch)
              return p=&p[i];     
                 
              return p=NULL;//if not found
              }

اما الcall بشكل عادي
PHP:
p=FirstOccurence(p,size,ch);

اسندناها الى متغير لانها retrun function
 
ممكن احد يساعدني في هالسؤال

(Overloading)


Consider the following class named Sentence.
class Sentence
{
private:
string words[25]; // An array that represents the words of the sentence
intSentenceLength; //Number of words in the sentence
public:
Sentence ( ); //Initialize SentenceLength to zero
voidReadSentence( ); // Toread the length and words of the sentence
voiddisplaySentence ( ); //Display Sentence
};

PART(A)

Overload the = = operator (equal) as a member function of the class Sentence. Two sentences (S1 and S2) are considered to be equal if they have the same SentenceLength and the words of S1 equal to the corresponding words in S2.



PART(B):

In the main function:
- Declare two objects named S1 and S2 of type Sentence.

- Use the ReadSentence function to read the length and words of S1 and S2.

- Compare if S1 and S2 are equal and output an appropriate message.



حليت جزء منه

#include<iostream>
using namespace std;
class Sentence
{
private:
string words[25];
int SentenceLength;
public:
Sentence ( );
void ReadSentence( );
void displaySentence ( );
bool operator==(sentence);
};
bool sentence::operator==(const sentence & y)const
{
if(y.SentenceLength==SentenceLength)

return true ;
else
return false ;
}
 
احد يشرح لنا البوينتر من البداية لو سمحتو ..
 
(Overloading)


Consider the following class named Sentence.
class Sentence
{
private:
string words[25]; // An array that represents the words of the sentence
intSentenceLength; //Number of words in the sentence
public:
Sentence ( ); //Initialize SentenceLength to zero
voidReadSentence( ); // Toread the length and words of the sentence
voiddisplaySentence ( ); //Display Sentence
};

PART(A)

Overload the = = operator (equal) as a member function of the class Sentence. Two sentences (S1 and S2) are considered to be equal if they have the same SentenceLength and the words of S1 equal to the corresponding words in S2.



PART(B):

In the main function:
- Declare two objects named S1 and S2 of type Sentence.

- Use the ReadSentence function to read the length and words of S1 and S2.

- Compare if S1 and S2 are equal and output an appropriate message.



بليز احد يساعدني
 
لان بترجع pointer
قلنا صوب char *
لان مابترجع حرف ، بترجع pointer من نوع char

الحل

PHP:
   char *  FirstOccurence (char *p, int size, char Ch){
              for(int i=0;i<size;i++)
              if(p[i]==Ch)
              return p=&p[i];     
                 
              return p=NULL;//if not found
              }

اما الcall بشكل عادي
PHP:
p=FirstOccurence(p,size,ch);

اسندناها الى متغير لانها retrun function

انا ما شفتها كلها بس جذي انت غيرت باريميتر إلي معطينا اياه هو في السؤال

char arr you turn it to char *p

عموماً خوك انا سويتها جذي واعتقد ضبطت

PHP:
#include <iostream>
using namespace std;

 char *FirstOccurence (char arr[], int size, char Ch)
 {

    cout<<"Enter A Char To Search: ";
     cin>>Ch;
     for (int i=0 ; i<size ; i++)
     {
         if(arr[i]==Ch)
         {
             cout<<i;
                  break;
         }
     }


 }

int main()
{
    int size=20;
    char Ch;
    char arr[20]="How Are You?";

    cout<<"How Are You?"<<endl;

    cout<<(FirstOccurence(arr,size,Ch));

    return 0;
}
 
انا ما شفتها كلها بس جذي انت غيرت باريميتر إلي معطينا اياه هو في السؤال

char arr you turn it to char *p

عموماً خوك انا سويتها جذي واعتقد ضبطت

PHP:
#include <iostream>
using namespace std;

 char *FirstOccurence (char arr[], int size, char Ch)
 {

    cout<<"Enter A Char To Search: ";
     cin>>Ch;
     for (int i=0 ; i<size ; i++)
     {
         if(arr[i]==Ch)
         {
             cout<<i;
                  break;
         }
     }


 }

int main()
{
    int size=20;
    char Ch;
    char arr[20]="How Are You?";

    cout<<"How Are You?"<<endl;

    cout<<(FirstOccurence(arr,size,Ch));

    return 0;
}

لان البراميتر اللي معطيك اياه غلط :smile:
لا اخوي الfunction اللي مسويها غلط وكلش الفكره بعيده
وترى يعطي عندي data غلط
عادتا Return function ماتقبل تسوي داخل cout
 
عودة
أعلى أسفل