Home > Software engineering >  1) Create dynamically filled array in a function. 2) Return the array content
1) Create dynamically filled array in a function. 2) Return the array content

Time:01-04

I was doing a prime no# class (runs a function to check if inout is prime) and tried to take it a little further by printing what the prime numbers are for inputs that are prime. I managed to hack together an inelegant solution as you can see in the code, by wedging a for-loop in the middle of a cout.

I want to write a function that would create an array of all numbers that input num can be divided by, and then call the function in my main func.

int main() {
    
    int num;
    
    cout<<"Enter a number and we'll check if it's prime: ";
    cin>>num;
    
    isPrime(num);
    
    if (isPrime(num) == true)
        cout<<"Number is prime!";
    else
    {
        cout<<"Not a prime number :( ";
        cout<<"Infact, "<<num<<" can be divided by: ";
        for(int i=2; i<num; i  ) {
            if(num % i == 0)
            cout<<i<<", ";
        }
        
        //What I want:> cout<<"Infact, "<<num<<" can be divided by: "<<arrayOfDiiders;
    }
    return 0;
}

The below is quasi human language/c code, written to describe what I wanted to achieve.

int allDividers(int num) {

int arrayOfDiiders[]; //the array i wanted to make
for(int i=2; i<num; i  ) {
    if(num % i == 0)
    // add i to the arrayOfDiiders[];
}

return arrayOfDiiders[];
}

Other func that assertions if user input is prime number. Included incase relevant.

bool isPrime(int num){
    int countDivs = 0; 
    for(int i=1; i<=num; i  ) {
        if (num % i == 0) 
            countDivs  ;
    }
    if (countDivs == 2) 
        return true;
    else 
        return false;
} 

Thanks in advance.

CodePudding user response:

I managed to hack together an inelegant solution as you can see in the code, by wedging a for-loop in the middle of a cout.

Actually, this solution is much more elegant since it avoids the unnecessary cost of a dynamic array.

  1. Create dynamically filled array in a function. 2) Return the array content

You can do it like this:

std::vector<int> v; // a dynamic array
// fill the vector here
return v;

CodePudding user response:

You can use a std::vector:

#include <vector>
std::vector<int> factors;
for(int i=2; i<num; i  ) {
    if(num%i==0) {
        factors.push_back(i);
    }
}
for(int i:factors) cout<<i;

CodePudding user response:

Actually you can combine both your requirements(checking if a num is prime ad getting all the dividors) into the same function.

I would do it this way

#include <iostream>
#include <set>
#include <cmath>

using namespace std;

static void getDividors(int num, set<int> &divs)
{
    divs.insert(1);
    divs.insert(num);

    if (num == 2)
        return;

    // It is enough to check until the sqrt num..
    int sqRootNum = floor(sqrt(num));

    for (int i = 2; i <= sqRootNum; i  ) {
        if (num % i == 0) {
            divs.insert(i);
            divs.insert(num/i);
        }
    }
}

int main() {
    
    int num;
    
    cout<<"Enter a number and we'll check if it's prime: ";
    cin>>num;

    set<int> divs;
    getDividors(num, divs);
    bool isPrime = divs.size() > 2 ? false : true;
    if (isPrime)
        cout<<"Number is prime!";
    else
    {
        cout<<"Not a prime number :( ";
        cout<<"Infact, "<<num<<" can be divided by: ";
        for (const auto &n : divs) {
            cout<< n <<" ";
        }
    }

    return 0;
}

The complexity of above code is O(sqrt(n)) which makes it very efficient too.

For insertion of elements, i am using set instead of vectors as it will simplify the logic as to not check for duplicates when adding into vector and also will be ordered(elements added to set will be in the ascending order) making it easy to nicely print them.

  •  Tags:  
  • Related