60 331
Assignments Done
98%
Successfully Done
In April 2018

Answer to Question #48764 in C++ for John

Question #48764
Read an array of integers of 20 positive values
Then do the following processes:
1- Store all prime and even numbers from the array in a new array then display the values of the new array on screen.
2- Display on screen the square root of the numbers stored in the new array
3- Delete all the non-prime numbers from the array, and then display the values of the array on screen
(Don’t forget that the array may have a repetition in its values)
Expert's answer
#include <stdlib.h>
#include <iostream>  
#include <stdio.h>
#include <cstdlib> 
#include <cmath>
using namespace std;
bool isprime(int n)
{
    if(n == 1) 
        return false;
    
    for(int d = 2; d * d <= n; d++)
{         
        if(n % d == 0) 
            return false;
}
   
    return true;
}
void DisplaySqrt(int mas[], int n)
{
for (int i = 0; i < n; i++)
cout<<sqrt(mas[i])<<" ";
}
void DeleteNonPrime(int mas[], int &n)
{
for (int i = 0; i < n; i++)
{
if (!isprime(mas[i]))
{
for (int j = i; j < n - 1; j++)
{
mas[j] = mas[j + 1];
}
--n; --i;
}
}
}
int main()
{
int array1[2000];
int array2[2000];
// cout<<"Enter 20 numbers : ";
int sizeArray2 = 0;
int sizeArray1 = 20;
cout<<"Enter the 20 numbers : ";
for (int i = 0; i < 20; i++)
{
cin>>array1[i];
array1[i] = i + 1;
if (isprime(array1[i]) || array1[i] % 2 == 0 )
{
array2[sizeArray2] = array1[i]; ++sizeArray2;
}
}
cout<<endl<<endl<<endl; 
cout<<"1 - All prime and even numbers from the array in a new array :  "<<endl<<endl;
for (int j = 0; j < sizeArray2; j++)
{
cout<<array2[j]<<" ";
}
cout<<endl<<endl<<endl; 
cout<<"2 - Square root of the numbers stored in the new array : "<<endl<<endl;
DisplaySqrt(array2, sizeArray2); cout<<endl<<endl<<endl; 
cout<<"3 - Delete all the non-prime numbers from the array : "<<endl<<endl;
DeleteNonPrime(array1, sizeArray1); 
    for (int j = 0; j < sizeArray1; j++)
{
cout<<array1[j]<<" ";
}
cout<<endl<<endl<<endl; 
system("pause");
return 0;
}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions