So here is the function:
int VerifTri(int t[], int g[], int n, int m)
{ int k, l; int sorted, sorted2;
sorted = 1; sorted2 = 1;
for (k=0; k<n; k )
{
if( t[k] > t[k 1] )
sorted=0;
}
for (l=0; l<m; l )
{
if (g[l] > g[l 1])
{
sorted2=0;
}
}
if ((sorted == 1) && (sorted2 == 1))
return 1;
else
return 0;
}
There is no syntax error, but the code is just wrong: I used it in a program and I entered two sorted arrays and it still returned 0.
CodePudding user response:
Assuming that n
is a length of t
and m
is a length of g
you can put
int VerifTri(int t[], int g[], int n, int m)
{
int k;
for (k = 0; k < n - 1; k )
if ( t[k] > t[k 1] )
return 0;
for (k = 0; k < m - 1; k )
if ( g[k] > g[k 1] )
return 0;
return 1;
}
Please, note the range: k == [0 .. n - 1)
otherwise the last comparison will be incorrect one
t[n - 1] > t[n]
Same for g
array
When we found any array being unsorted we can return 0
: if an array is not sorted, both of them can't be sorted
CodePudding user response:
what is wrong with this function i wrote to see if two arrays are sorted in ascending order, in C?
The function is initially wrong independent of its body because there is no great sense to define such a function instead of defining a function that checks whether an array is sorted.
Having such a function you can check in the caller whether two arrays are sorted the following way
int sorted = VerifTri( t, n ) && VerifTri( g, m );
Or you can even check whether three arrays are sorted like
int sorted = VerifTri( a1, n1 ) && VerifTri( a2, n2 ) && VerifTri( a3, n3 );
Moreover the parameters of the function that denote arrays should have qualifier const
because the passed arrays are not changed within the function. And the parameters that denote sizes of the arrays should have the type size_t
.
Within the for loops there is an attempt to access memory beyond the arrays in these if statements
if( t[k] > t[k 1] )
and
if (g[l] > g[l 1])
when k
is equal to n-1
or when l
is equal to m-1
.
And the loops should be interrupted as soon as a previous element of the array is greater than the next element of the array.
So your function should look for example the following way
int VerifTri( const int a[], size_t n )
{
size_t i = 0;
if ( i != n )
{
while ( i < n && !( a[i] < a[i-1] ) );
}
return i == n;
}
And if you want to check whether two arrays are sorted then you can just write as shown above the expression
VerifTri( t, n ) && VerifTri( g, l )
If you want to check whether one of the arrays is sorted then you can write the expression
VerifTri( t, n ) || VerifTri( g, l )
Using your original function you will be unable to do this and have to write one more function.
CodePudding user response:
There are some problems in your code, some benign, some major:
the code is badly indented: this makes it hard to read, with more places for bugs to hide.
the loops are incorrect: assuming
n
is the length oft
andm
is the length ofg
, the last comparison will reference an element beyond the end of the arrays.the code is inefficient: you could return 0 as soon as any comparison evaluates to false.
the name
l
for a variable is risky as it looks dangerously similar to1
in common fixed width fonts.the prototype could be improved: the array pointers should be defined as
const
since the function does not modify their contents.your function is too specific: calling a simpler function that checks a single array is simpler and less confusing.
Here is a modified version:
int VerifTri(const int t[], const int g[], int n, int m) {
for (int i = 1; i < n; i ) {
if (t[i - 1] > t[i])
return 0;
}
for (int i = 1; i < m; i ) {
if (g[i - 1] > g[i])
return 0;
}
return 1;
}
For illustration, here is a simpler function to test a single array:
int VerifTri(const int *t, size_t n) {
for (; n --> 1; t ) {
if (t[0] > t[1])
return 0;
}
return 1;
}