Home > Enterprise >  Make an array multiply only the columns behind it, going one at a time until the last one and save t
Make an array multiply only the columns behind it, going one at a time until the last one and save t

Time:12-03

I'm making a program in C that factors any number using primes and saves these primes, multiplying them you find all the divisors of a number. But I can't make an array that multiplies the previous columns and saves the results. follow the example


60 / 2
30 / 2
15 / 3
5 / 5

divisors = 2, 2, 3, 5  

now i need`add 1 to array  array {1, 2, 2, 3, 5}

i need this now    start colune 2    {1, 2}      2 * 1 = 2 save. 
next                     colune 3    {1, 2, 2}   2 * 1 = 2 but we already have 2 so don't save it.
continue 2 * 2 = 4 save.
                         colune 4    {1, 2, 2, 3} 3 * 1 = 3 save, 3 * 2 = 6 save, 3 * 4 = 12 save.
                         colune 5    {1, 2, 2, 3, 5}  5 * 1 = 5 save, 5* 2 = 10, 5 * 4 = 20 save, 5 * 3= 15 save, 5 * 6 = 30 save, 5 * 12 = 60 save. 

now we found all divisors of 60 =  1, 2, 3, 4, 5, 6,  10 ,12 , 15,20, 30, 60.


It is important to mention that I need the program to be like this, I know there are other ways... but I only need this one, I have been unable to complete it for 1 week

video to help https://www.youtube.com/watch?v=p0v5FpONddU&t=1s&ab_channel=MATEMÁTICAFORALLLUISCARLOS

my program so far

#include <stdlib.h>
#include <stdio.h>

int N = 1;

int verificarPrimo(int numero);

int main()
{

    int num = 60, i, primo = 1, resultados[N], j = 1;

    for (i = 0; i < 60; i  )
    {
        if (primo == 1)
        {
            resultados[N - 1] = primo;
            i = 2;
            primo = i;
        }

        if (verificarPrimo(i))
        {
            while (num % i == 0)
            {
                num = num / i;
                resultados[N] = i;
                N  ;
            }
        }
    }

    for (i = 1; i < N; i  )
    {
        printf("%d \n", resultados[i]);
    }
}

int verificarPrimo(int primo)
{
    int i;

    if (primo <= 1)
        return 0;
    for (i = 2; i <= primo / 2; i  )
    {
        if (primo % i == 0)
            return 0;
    }
    return 1;
}

CodePudding user response:

I tried out your code and ran into some issues with how the results were being stored. First off, the results array is being initially defined as an array with a size of "1", and that it not what you probably want.

int num = 60, i, primo = 1, resultados[N], j = 1;

With that in mind and determining the spirit of this project, following is tweaked version of the code to test for one or more values and their factors.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int verificarPrimo(int primo)
{
    int sq = sqrt(primo)   1;   /* Usual checking for a prime number is from '2' to the square root of the number being evaluated */

    if (primo <= 1)
        return 0;
    for (int i = 2; i < sq; i  )
    {
        if (primo % i == 0)
            return 0;
    }
    return 1;
}

int main()
{
    int N = 0;
    int num = 0, entry = 0, resultados[100];            /* The results array needs to be defined with some value large enough to contain the assorted factors a number might have */

    printf("Enter a number to evaluate for factors: "); /* Using a prompt to allow various values to be tested */
    scanf("%d", &entry);

    num = entry;

    if (verificarPrimo(num))                            /* Catchall in case the entered number is a prime number */
    {
        printf("This number is a prime number and has no factors other than one and itself\n");
        return 0;
    }

    resultados[0] = 1;  /* Normally the value '1' is implied in a list of factors, so these lines could be omitted */
    N = 1;

    for (int i = 2; i < entry; i  )
    {
        if (verificarPrimo(i))
        {
            while (num % i == 0)
            {
                num = num / i;
                resultados[N] = i;
                N  ;
            }
        }
    }

    printf("Factors for %d\n", entry);

    for (int i = 0; i < N; i  )
    {
        printf("%d ", resultados[i]);
    }

    printf("\n");

    return 0;
}

Some items to point out in this tweaked code.

  • In the prime number verification function, it is usually customary to set up a for loop in testing for prime numbers to go from the value of "2" to the square root of the number being tested. There usually is no need travel to one half of the number being tested. For that, the #include <math.h> statement was added (FYI, "-lm" would need to be added to link in the math library).
  • Instead of defining the results array with a value of one element, an arbitrary value of "60" was chosen for the holding the possible number of results when evaluating factors for a given value. Your original code had the potential of storing data past the end of the array and causing a "smashing" error.
  • The value of "1" is usually left out of the list of factors for a number, but was left in as the initial result value. This might be left out of the completed code.
  • An additional entry field was added to allow for user entry to be tested to give the code some flexibility in testing numbers.
  • A test was also added to see if the entered number is itself a prime number, which would only have factors of "1" and itself.

Following is some sample terminal output testing out your original value of "60" along with some other values.

@Dev:~/C_Programs/Console/Factors/bin/Release$ ./Factors 
Enter a number to evaluate for factors: 60
Factors for 60
1 2 2 3 5 
@Dev:~/C_Programs/Console/Factors/bin/Release$ ./Factors 
Enter a number to evaluate for factors: 63
Factors for 63
1 3 3 7 
@Dev:~/C_Programs/Console/Factors/bin/Release$ ./Factors 
Enter a number to evaluate for factors: 29
This number is a prime number and has no factors other than one and itself

Give that a try to see if it meets the spirit of your project.

  • Related