jeudi 1 septembre 2016

I am trying to execute this code in C++ to find and print all the factors of a number in sorted manner but unable to understand the error ?

How can I solve it without using vectors ? The logic to find & sort all the factors of a number is correct, there is error in return statement , i think.

vector<int> Solution::allFactors(int n) 
{
    // Do not write main() function.
    // Do not read input, instead use the arguments to the function.
    // Do not print the output, instead return values as specified
    // Still have a doubt. Checkout http://ift.tt/2bWF24c for more details


int j=0 ,i,value,k;
int arr[10];

int z = sqrt(n) ;

    for(i=1;i<=z;i++)
    {
    if(n%i==0)

{
  arr[j++] = i ;
if(i!=n/i)
  arr[j++] = n/i ;
}
}


for(i=1;i<j;i++)
{
  value= arr[i] ;
  k=i-1 ;
while(arr[i-1]>value && i>0)
{
arr[i]=arr[i-1] ;
  i-- ;
}
arr[i] = value ;
}
return arr[10] ;
}

Aucun commentaire:

Enregistrer un commentaire