xrom
Well-Known Member
- تاريخ التسجيل
- 23 مارس 2012
- المشاركات
- 154
- المجموعة
- ذكر
- الدفعة الدراسية
- معلومة خاصة
- الكلية
- كلية تقنية المعلومات
شباب هذا محاولتي للحل في Problem 4 in Pointers .. البرنامج يشتغل بس ما يرجع شئ .. جيكوا ليي وين المشكله وإذا في خطأ في الحل خصوصاً في الداينمك
سويت جذي في الـMain عشان أتأكد ان كود الفنكشن صح ولا لا
وإذا اكتب له رقم موجود يطلع لي
وإذا اكتب له رقم غير موجود يطلع لي
..
يعني الكود صح .. بس الـreturn غلط إلي اسويه .. شوفوا ليي حل يا جماعه لا اخمبق بقوة
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 غلط إلي اسويه .. شوفوا ليي حل يا جماعه لا اخمبق بقوة
التعديل الأخير:
