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("=) =|%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.
CodePudding user response:
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("=) =|%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.
CodePudding user response:
Other non-pow()
problems:
Overflow
i * i <= num
will overflow when num
is a prime near INT_MAX
.
False primes
prime_check(any value 1 or less)
incorrectly returns true.
Fixed
With modest even test improvement.
bool prime_check(int num) {
if (num % 2 == 0) {
return num == 2;
}
for (i = 3; i <= num/i; i = 2) {
if (num % i == 0) {
return false;
}
}
return num >= 2;
}