Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does the pow function reduce the length of my loop in C

i have this code:

#include <stdio.h>
#include <math.h>
int check_menu(int menu1)
{
    if (menu1 > 3 || menu1 < 0)
        return 1;
    else
        return 0;
}

void menu()
{
    int choice, count = 0, count2;
    do {
        printf("----MENU----\n0 -> Exit\n1 -> Prime time\n2 -> Calander calculating\n3 -> Matrix printing\n");
        scanf_s("%d", &choice);
        count++;
        if (choice < 0 || choice > 3)
            printf("%d/5 errors\n", count);
    } while (check_menu(choice) == 1 && count <= 4);
    determine(choice);
}
int prime_check(int num) {
    int i, is_prime = 1;
    for (i = 2; i * i <= num; i++) {
        if (num % i == 0) {
            is_prime = 0;
        }
    }
    return is_prime;
}
void prime_total(int num) {
    int prime, germain, marsenne, couss, cousb, twins, twinb, fermat, loop, i;
    for (i = -9, loop = num - 9; loop <= num + 9; loop++, i++) {
        prime =prime_check(loop),germain=prime_check(2*loop+1),marsenne=prime_check(pow(2, loop)-1),twins=prime_check(loop+2),twinb=prime_check(loop-2),couss=prime_check(loop+4),cousb=prime_check(loop-4);// prime
        if (prime == 0)
            germain = 0, marsenne = 0, cousb = 0, couss = 0, twins = 0, twinb = 0, fermat = 0;
        printf("%3d)  %3d|%d|%d|%d|%d|%d|%d|%d|\n",i, loop, prime, germain, marsenne, twinb, twins, cousb, couss);
    }
    menu();
}
int Prime_time() {
    int num;
    do {
        printf("enter a number (1 - 1000000):\n");
        scanf_s("%d", &num);
    } while (num < 1 || num > 1000000);
    prime_total(num);
}

int determine(int choice) {
    if (choice == 1)
        Prime_time();
}
void main()
{
    menu();
}
}

the problem im having is with the prime_total function the thing is that whenever i use the pow function to pass a parameter to prime_check then it dosent call menu and gets stuck midway through the loop.
when i dont use pow then it works fine.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your problem is in this piece of code:

void prime_total(int num) {
    int prime, germain, marsenne, couss, cousb, twins, twinb, fermat, loop, i;
    for (i = -9, loop = num - 9; loop <= num + 9; loop++, i++) {
        prime =prime_check(loop),germain=prime_check(2*loop+1),marsenne=prime_check(pow(2, loop)-1),twins=prime_check(loop+2),twinb=prime_check(loop-2),couss=prime_check(loop+4),cousb=prime_check(loop-4);// prime
        if (prime == 0)
            germain = 0, marsenne = 0, cousb = 0, couss = 0, twins = 0, twinb = 0, fermat = 0;
        printf("%3d)  %3d|%d|%d|%d|%d|%d|%d|%d|\n",i, loop, prime, germain, marsenne, twinb, twins, cousb, couss);
    }
    menu();
}

If you consider the manual page for the pow() function:

NAME
     pow -- power function

SYNOPSIS
     #include <math.h>

     double
     pow(double x, double y);

     long double
     powl(long double x, long double y);

     float
     powf(float x, float y);

Notice that int is not the type used in either calling or handling a return from any of these. In fact, by using pow() you are expecting a double as a return value.

To find these types of errors, you might consider compiling your code with:

gcc -wall

This will enable all warnings. It is best practice to eliminate all warnings from any compiled code and compile with all warnings on.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading