c++ Write two versions of a function that
returns a dynamic array consisting of all prime numbers less than
or equal to the int parameter n. You may declare the dynamic array
to have length n, even though this is a bit inefficient. In one
version of the function, return the number of primes via a separate
reference variable. In the other, use 0 as a sentinel value to
signify the end of the prime list. See below for the function
prototypes.
int* smallPrimes(int n, int& size); // number of primes
returned via size ref variable
int* smallPrimes(int n); // sentinel value indicates end of prime
list
Implement both of the functions in main with two input values from
the user.
#include <iostream>
using namespace std;
int* smallPrimes(int n, int& size) // number of primes returned via size ref variable
{
int *arr= new int[n];
int index=0,count=0;
//iterating in range
for(int i=2;i<=n;i++){
count=0;
//checking if is prime
for(int j=2;j<i;j++){
if(i%j==0)
count++;
}
if(count==0)
arr[index++]=i;
}
//adding the size
size=index;
return arr;
}
int* smallPrimes(int n){ // sentinel value indicates end of prime list
int *arr= new int[n];
int index=0,count=0;
//iterating in range
for(int i=2;i<=n;i++){
count=0;
//checking if is prime
for(int j=2;j<i;j++){
if(i%j==0)
count++;
}
if(count==0)
arr[index++]=i;
}
//adding sentinel value as 0
arr[index]=0;
return arr;
}
int main(){
int size;
int *p=smallPrimes(20,size);
for(int i=0;i<size;i++)
cout<<p[i]<<" ";
cout<<endl;
p=smallPrimes(30);
for(int i=0;p[i]!=0;i++)
cout<<p[i]<<" ";
}

Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me
c++ Write two versions of a function that returns a dynamic array consisting of all prime...
please help with this c++ question! Write a function that returns a dynamic array, of size n, composed of sequential odd integers starting from 1: 1,3,5,7, etc.. int* odd_array(int n) {
How to initialize a dynamic two-dimensional array with values? (C++) Here's my problem... # include using namespace std; int main() { // Q#5 - dynamic 2d arrays, indexing via subscript operator, pointer arithmetic // tic tac toe board is an array of int pointers // each int pointer in the board points to a row // declare a pointer to an array of int pointers (i.e. a pointer to a pointer of type int) const...
USING C++: Write a function that given a 2D dynamic array of integers, will return the number of elements whose absolute value is smaller than a given value x. The function should take as arguments (in this order): a pointer to the array (i.e., 2D dynamic array) the number of rows the number of columns the given value The function should return the number of elements smaller in absolute value than epsilon E.g. if A={{0.2, -0.1, 3.4},{4.4, 0, -2}} and...
In C++ Write a function int * return_matches(int a[], int & size,TEST t) which returns an array (allocated off of the heap in the function) containing only the elements of array a that pass the “test” t. The function iterates through all the elements of a, and constructs a new array containing all the elements that pass the test. When the parameter corresponding to “size” is passed in (by reference), it contains the size of the array a. In the...
Prime Number Programing in C Note: The program is to be written using the bitwise operation. Use an integer array (not a Boolean array) and a bit length (for instance 32 bits). In this program you will write a C program to find all prime numbers less than 10,000. You should use 10,000 bits to correspond to the 10,000 integers under test. You should initially turn all bits on (off) and when a number is found that is not prime,...
C++ problem with dynamic arrays is that once the array is created using the new operator the size cannot be changed. For example, you might want to add or delete entries from the array similar to the behavior of a vector. This project asks you to create a class called DynamicStringArray that includes member functions that allow it to emulate the behavior of a vector of strings. The class should have: A private member variable called dynamicArray that references a...
(C++ exercise) 5. Write a new Checkbook class that uses a dynamic array. Call the class a different name, since it will be different from the latest version that uses a static array. Start off with a dynamic array size of 2, made by the constructor. Every time the dynamic array becomes full and a new check has to be written, double the current size of the array. Write a doubleArray function for the class to do this, but place...
Implement and test a function that uses dynamic array to store students’ grades and calcuate the average grade. // This program demonstrates the use of dynamic array // it #include <iostream> #include <iomanip> using namespace std; int main() { float *grades; // a pointer used to point to an array int size = 2; grades = new float[size]; int count = 0; // track the number of grade/student float grade; // the grade of a student float average, total;...
PLEASE write in C Language, ( you can use array, recursive
function, Pointers, Dynamic call by refernce, Structure)
a) 70 marks] Write a program that Sorts a given integer array via Selection Sort obtained by rotation around 3. Searches for a key point in the rotated array in O(logn) time, where the rotation Rotates the sorted array around a random point, e.g., 1 2 345->34512 is point is known in advance. ts above by first finding the unknown rotation point...
8.9 Coding lab #5: create a dynamic array ADT and a singly linked list ADT. Honor Code Your answers to this homework must be your own work.You are not allowed to share your solutions.You may not engage in any other activities that will dishonestly improve your results or dishonestly improve or damage the results of others. Plagiarism Plagiarism is when you copy words, ideas, or any other materials from another source without giving credit. Plagiarism is unacceptable in any academic environment....