Home > Back-end >  Enter a positive integer n in the input number n, it is concluded that their minimum number (I this
Enter a positive integer n in the input number n, it is concluded that their minimum number (I this

Time:09-22

 
Int Min (int * arr, int length)//from inside an array to find the smallest number of

{

Int TMP=0;

int i=0;

Int j=1;

for (int i=0; i
{

If (arr [I] & gt; + 1=arr [I])

{

TMP=arr (I + 1),

}

The else

TMP=arr [I];

}

Return the TMP;

}

Int main ()

{
int a;

Printf (" enter a positive integer n: ");

The scanf (" % d ", & amp; a);

Int b [a];

Printf (" input n integers: ");

for (int i=0; i {
The scanf (" % d ", & amp; B [I]);

}

Int length=sizeof (b)/sizeof (b [0]);

Printf (" % d \ n ", Min (length, b));

}


CodePudding user response:

Certainly not, min function should be inside the TMP first assignment arr [0], then compared, with TMP and current arr [I] than, smaller than TMP is assigned to TMP

CodePudding user response:

Question 1, find the smallest wrong logic
 int Min (int * arr, int length)//from inside an array to find the smallest number of 

{

Int TMP=0;

int i=0;

Int j=1;
TMP=arr [0].

for (int i=0; i
{

//if (arr [I] & gt; + 1=arr [I])
If (arr [I] & gt; TMP) here is the comparison of TMP and arr [I], not to compare an array of two adjacent elements, what do you want to assume an array {0}, more adjacent array elements is 0 s and 1 s, TMP=0 (minimum), but compare the adjacent elements 1 and 2, TMP was changed to 1 again, because it's 1 and 2, you forgot to 0, so comparing TMP directly by the

{

//TMP=arr (I + 1),
TMP=arr [I];

}

//else

//TMP=arr [I];

}

Return the TMP;

}


Question 2
Int b [a];//not the length of the array variable declarations, to int int [100] or with dynamic application memory b * b=(int *) malloc (sizeof (int) * a) (remember after the free memory)
In addition, the length not recalculate, directly in the int length=a

CodePudding user response:

 # include & lt; Stdio. H> 
Int Min (int * arr, int length)//from inside an array to find the smallest number of

{

Int TMP=0;

int i=0;

Int j=1;
TMP=arr [I];//-- -- -- -- -- -- -- -- -- -- add code -- -- -- -- -- -- -- -- -- -- --
//-- -- -- -- -- -- -- -- -- -- -- -- - the following code has been modified -- -- -- -- -- -- -- -- -- -- -- -- --
for (int i=0; i
{

If (arr [I + 1]
{

TMP=arr (I + 1),

}

}

Return the TMP;

}
Int main ()
{
int a;

Printf (" enter a positive integer n: ");

The scanf (" % d ", & amp; a);

Int * b=new int [a];//-- -- -- -- -- -- -- -- to dynamically allocated memory -- -- -- -- -- -- -- -- --

Printf (" input n integers: ");

for (int i=0; i {
The scanf (" % d ", & amp; B [I]);//-- -- -- -- -- -- -- % d space behind the removed -- -- -- -- -- -- --

}


Printf (" % d \ n ", Min (b, a));//-- -- -- -- -- -- -- the length directly to a -- -- -- -- -- -- -- -- -- --
The delete [] b;//-- -- -- -- -- -- -- -- -- free memory -- -- -- -- -- -- -- -- --
return 0;
}

VS2015 c + + environment to run the results

CodePudding user response:

refer to the second floor qybao response:
1 problem, find the smallest logic not
 int Min (int * arr, int length)//from inside an array to find the smallest number of 

{

Int TMP=0;

int i=0;

Int j=1;
TMP=arr [0].

for (int i=0; i
{

//if (arr [I] & gt; + 1=arr [I])
If (arr [I] & gt; TMP) here is the comparison of TMP and arr [I], not to compare an array of two adjacent elements, what do you want to assume an array {0}, more adjacent array elements is 0 s and 1 s, TMP=0 (minimum), but compare the adjacent elements 1 and 2, TMP was changed to 1 again, because it's 1 and 2, you forgot to 0, so comparing TMP directly by the

{

//TMP=arr (I + 1),
TMP=arr [I];

}

//else

//TMP=arr [I];

}

Return the TMP;

}


Question 2
Int b [a];//not the length of the array variable declarations, to int int [100] or with dynamic application memory b * b=(int *) malloc (sizeof (int) * a) (remember after the free memory)
In addition, the length not recalculate, directly in the int length=a


 int Min (int * arr, int length)//from inside an array to find the smallest number of 

{
Int TMP=arr [0];

For (int j=1; j{

If (tmp> Arr [j])
TMP=arr [j];


}

Return the TMP;

}

Int main ()

{
int a;


Printf (" enter a positive integer n: ");

The scanf (" % d ", & amp; a);

Int * b=(int *) malloc sizeof (int) * a);


Printf (" input n integers: ");

for (int i=0; i {
The scanf (" % d ", & amp; B [I]);

}

Printf (" % d \ n ", Min (b, a));
Free (b);
}


Thank you for your advice, I felt as if there are errors, the scanf after I input all the data point enter, or have no reaction, is I changed the code above

CodePudding user response:

You so more, such as 1, 2, 3, 4, the first TMP=1. The second TMP=2, has been changed, should be above the for loop TMP=arr [0]. First value set a variable as an array, and then the comparison, if less than behind the assignment of values to TMP TMP is small, you write, have given an array? When I=length, arr [I + 1] is not to this value

CodePudding user response:

The
reference 3 floor MianHou response:
 # include & lt; Stdio. H> 
Int Min (int * arr, int length)//from inside an array to find the smallest number of

{

Int TMP=0;

int i=0;

Int j=1;
TMP=arr [I];//-- -- -- -- -- -- -- -- -- -- add code -- -- -- -- -- -- -- -- -- -- --
//-- -- -- -- -- -- -- -- -- -- -- -- - the following code has been modified -- -- -- -- -- -- -- -- -- -- -- -- --
for (int i=0; i
{
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related