السلام عليكــم ورحمـة الله وبركاتــه ،،
كيف حااالكم ؟؟
هذا سؤال فيه
Array & Pointer
Write a C++ program that reads a sequence of at maximum50 integer elements from the user, and tests if it is a prime number or not.
Output of the program:
How many numbers would you like to enter? 3
Enter a number1:5
Enter a number2:6
Enter a number3:7
5 is a prime.
6 is not a prime.
7 is a prime.
v In your program you must Defined the following :
:
Ø In main define :
- Array size as const variable with value of 50.
- An array numbers to save a sequence of integer elements, using the const variable.
- n variable use to save the numbers of element in an array. Make sure that n is a positive number <= 50.
- A pointer Ptr that points to the first element in the Array numbers.
Ø Global functions:
- readArray (int* Nptr, int n): that reads n elements from the user. Make sure that n is a positive number <= 50
- Is_Prime (int num): that tests the number if it is prime, return true if not return false. "Use a pointer ".
- PrintArray (const int *Nptr, int n): that prints n elements of an array and print the number is prime or not.
وهذا حلي :::
#include<iostream>
using namespace std;
void read_Array (int* Ptr, int n)
{
cout<< " *** Enter sequence of integer numbers **** \n" ;
for ( int i =0 ; i < n ; i ++ )
{
cout << " Number " << i+1 << " : " ;
cin >> *Ptr ;
Ptr++ ;
}
}
int Sum (const int*Ptr, int n)
{
int sum =0 ;
for ( int i =0 ;i < n ; i++ )
{
if ( i % 2 == 0 )
sum += *Ptr ;
Ptr ++ ;
}
cout << endl ;
return sum ;
}
void PrintArray (const int *Nptr, int n)
{
cout << "Your Array : " ;
for ( int i =0 ;i < n ; i++ )
{
cout << *Nptr << " " ;
Nptr ++ ;
}
cout << endl ;
}
int main()
{
const int size = 50 ;
int n ;
int numbers;
int* DataPtr = numbers ;
cout << " How many numbers would you like to enter ? " ;
cin >> n ;
while (n > 50 )
{
cout << " erorr ! please enter size with less than or equal to 50 " << endl;
cin >> n ;
}
read_Array (DataPtr , n );
PrintArray (DataPtr , n);
cout << " The sum of all elements (with even index) in the previous Arryy is : " << Sum (DataPtr , n ) << endl;
return 0 ;
}