Home > Back-end >  The c language
The c language

Time:09-21

Verify whether n can be decomposed into two prime Numbers multiplication, is to return 1, no returns 0; Through the pointer x, y return decomposition of two prime Numbers
For example 111 x=3=3 * 37 * and * y=37, the function returns the value 1

Why come out the debug an assertion failed






#include
#include
Int prime (int m)
{
Int k, flag=1;
For (k=2; KIf (m % k==0)
Flag=0;
return flag;
}
Int fun (int n, int x, int * y)
{
Int k, flag=0;
Int k2.
K=2;
Do {
K2=n/k;
If (k * k2==n)
If (prime (k) & amp; & Prime (k2) & amp; & K{
Flag=1;
* x=k;
* y=k2.
break;
}
K +=1;
}
While (k
return flag;
}
The main ()
{
int a,b; The FILE * out;
Out=fopen (" C: \ \ KSWJJ \ \ 2454999999010001 \ \ out dat ", "w");
If (fun (111, & amp; A, & amp; B)) {
Printf (" * 111=% d % d \ n ", a, b);
Fprintf (out, "111=% d * % d \ n", a, b);
}
If (fun (11111, & amp; A, & amp; B)) {
Printf (" * 11111=% d % d \ n ", a, b);
Fprintf (out, "11111=% d * % d \ n", a, b);
}
If (fun (1111111, & amp; A, & amp; B)) {
Printf (" * 1111111=% d % d \ n ", a, b);
Fprintf (out, "1111111=% d * % d \ n", a, b);
}
The fclose (out);
getchar();

}

CodePudding user response:

Where the assert? Said illegal access

CodePudding user response:

A and b to initialize a try

CodePudding user response:

Int fun (int n, int x, int * y)
{
Int k, flag=0;
Int k2.
K=2;
Do {
K2=n/k;
If (k * k2==n)
If (prime (k) & amp; & Prime (k2) & amp; & K{
Flag=1;
* x=k;//here seems to use wild pointer, can't let the pointer to a local variable, because when the end of the function call will undo the local variables of memory space, and then you could use the pointer points outside the function in the variable, it is no good,
* y=k2.//it is
break;
}
K +=1;
}
While (k
return flag;
}

CodePudding user response:

The main ()//here seems should be int main ()
{
int a,b; The FILE * out;
Out=fopen (" C: \ \ KSWJJ \ \ 2454999999010001 \ \ out dat ", "w");
If (fun (111, & amp; A, & amp; B)) {
Printf (" * 111=% d % d \ n ", a, b);
Fprintf (out, "111=% d * % d \ n", a, b);
}

CodePudding user response:

reference 4 floor Rage Your Dream. The DS response:
main ()//it seems should be int main ()
{
int a,b; The FILE * out;
Out=fopen (" C: \ \ KSWJJ \ \ 2454999999010001 \ \ out dat ", "w");
If (fun (111, & amp; A, & amp; B)) {
Printf (" * 111=% d % d \ n ", a, b);
Fprintf (out, "111=% d * % d \ n", a, b);
}


The main function is some should do not have what his error
If I were int fun under the wild pointer problem should be how to fix it?

CodePudding user response:

refer to the second floor CHXCHXKKK response:
a and b to initialize a try

Content of the main function is to own
I only make int fun content

CodePudding user response:

The
reference 1/f, Simple, Soft reply:
where assert? Said there illegal access

The compiled output is correct but will pop up the debug an assertion failed
The result is 0

CodePudding user response:

reference ink dong 5 floor response:
Quote: refer to 4th floor Rage Your Dream. The DS response:

The main ()//here seems should be int main ()
{
int a,b; The FILE * out;
Out=fopen (" C: \ \ KSWJJ \ \ 2454999999010001 \ \ out dat ", "w");
If (fun (111, & amp; A, & amp; B)) {
Printf (" * 111=% d % d \ n ", a, b);
Fprintf (out, "111=% d * % d \ n", a, b);
}


The main function is some should do not have what his error
If I were int fun under the wild pointer problem should be how to fix it?


 
/* k, k2 is defined as a static local variable */
#include
#include
Int prime (int m)
{
Int k, flag=1;
For (k=2; KIf (m % k==0)
Flag=0;
return flag;
}
Int fun (int n, int x, int * y)
{
Static int k, flag=0;
Static int k2.
K=2;
Do {
K2=n/k;
If (k * k2==n)
If (prime (k) & amp; & Prime (k2) & amp; & K{
Flag=1;
* x=k;
* y=k2.
break;
}
K +=1;
}
While (k
return flag;
}
The main ()
{
int a,b; The FILE * out;
Out=fopen (" C: \ \ KSWJJ \ \ 2454999999010001 \ \ out dat ", "w");
If (fun (111, & amp; A, & amp; B)) {
Printf (" * 111=% d % d \ n ", a, b);
Fprintf (out, "111=% d * % d \ n", a, b);
}
If (fun (11111, & amp; A, & amp; B)) {
Printf (" * 11111=% d % d \ n ", a, b);
Fprintf (out, "11111=% d * % d \ n", a, b);
}
If (fun (1111111, & amp; A, & amp; B)) {
Printf (" * 1111111=% d % d \ n ", a, b);
Fprintf (out, "1111111=% d * % d \ n", a, b);
}
The fclose (out);
getchar();

}

I suggest to use static local variables, because define k, k2 and static is to inform the compiler, will be stored in the program of static storage area rather than on the stack space, so that you can solve this function after the end of the variable storage space haven't release,

I think this is a small program, if you can also try to use global variables, but if a large project, is not recommended to use k, k2 for global variables, because to define a global variable has many shortcomings, the most obvious disadvantage is that destroyed this variable access range (the variables defined in the function, not only controlled by this function),

I feel so pointer... The two references in the past, and then in the fun function for assignment, the two references that can be referenced in the main function of the value of the variable is changed,
  • Related