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 TMPCodePudding user response:
Question 1, find the smallest wrong logicint 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: