experchange > learn.c-c++

Paul Reid (01-20-20, 11:31 PM)
Hi all,
I am currently studying a tutorial on algorithms and data structures on tutorialspoint. The code to insert into an array is given below, in C:

#include <stdio.h>

main() {
int LA[] = {1,3,5,7,8};
int item = 10, k = 3, n = 5;
int i = 0, j = n;

printf("The original array elements are :\n");

for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, LA[i]);
}

n = n + 1;

while( j >= k) {
LA[j+1] = LA[j];
j = j - 1;
}

LA[k] = item;

printf("The array elements after insertion :\n");

for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, LA[i]);
}
}

This code seems wrong as it accesses out of bounds array indexes in the while loop. Should I continue to use this tutorial or is it feeding me incorrect information?
Stefan Ram (01-20-20, 11:58 PM)
Paul Reid <pchristor> writes:
>#include <stdio.h>
>main() {
> int LA[] = {1,3,5,7,8};
> int item = 10, k = 3, n = 5;
> int i = 0, j = n; ....
> while( j >= k) {
> LA[j+1] = LA[j];


n == 5 and j == n after initialization, so
j == 5 at the start of the loop quoted above.

Then, LA[ j + 1 ] and LA[ j ] are evaluated, so,
LA[ 6 ] and LA[ 5 ] are evaluated.

However, after "int LA[] = {1,3,5,7,8}", we may
evaluated only LA[ 0 ] to LA[ 4 ].

This is a major error indicating that the author
himself is someone who is just starting to learn C.

The code also has stylistic issues.

The classic book by Kernighan and Ritchie is dated,
but even this should be a much better guide to C
(and some simple algorithms).

To learn C check out,

Modern C - Jens Gustedt

, and for algorithms,

Algorithm Design Manual - Skiena
Introduction to Algorithms - Thomas Cormen et al
Stefan Ram (01-21-20, 12:20 AM)
Paul Reid <pchristor> writes:
>#include <stdio.h>
>main() {
> int LA[] = {1,3,5,7,8};


The following program still has both stylistic and security
issues and lacks generality of the algorithm, but it should
at least avoid the issue the bad evaluations for the special
case given.

main.c

#include <stdio.h>

int print( int const array[], int const used )
{ if( used )
{ int const r = printf( "{ " );
if( r != 2 )return r;
int r1 = 0, r2 = 0;
for( int i = 0; i < used; ++i )
{ r1 = printf( "%d", array[ i ]);
if( r1 < 0 )return r1;
if( i < used - 1 )
{ r2 = printf( ", " );
if( r2 < 0 )return r2; }}
int const r3 = printf( " }" );
return r3 < 0 ? r3 : r + r1 + r2 + r3; }
else return printf( "{}" ); }

int main( void )
{ int array[] ={ 1, 3, 5, 7, 8, -1 };
int used = 5;

print( array, used ); puts( "" );

int const insert_position = 3;
for( int i = used; i > insert_position; --i )
array[ i ]= array[ i - 1 ]; ++used;

int const insert_value = 10;
array[ insert_position ]= insert_value;

print( array, used ); puts( "" ); }

transcript

main.c: In function 'main':
main.c:26:4: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
26 | for( int i = used; i > insert_position; --i )
| ^~~
main.c:27:32: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
27 | array[ i ]= array[ i - 1 ]; ++used;
| ^~

{ 1, 3, 5, 7, 8 }
{ 1, 3, 5, 10, 7, 8 }
Ben Bacarisse (01-21-20, 03:18 AM)
Paul Reid <pchristor> writes:

> I am currently studying a tutorial on algorithms and data structures
> on tutorialspoint. The code to insert into an array is given below, in
> C:


<cut awful code>
> This code seems wrong as it accesses out of bounds array indexes in
> the while loop. Should I continue to use this tutorial or is it
> feeding me incorrect information?


Ditch it. The code is wrong in so many ways. I don't recall ever seeing
a decent tutorial about anything on tutorialspoint.
Paul Reid (01-21-20, 10:11 PM)
On Tuesday, January 21, 2020 at 1:18:58 AM UTC, Ben Bacarisse wrote:
> <cut awful code>
> Ditch it. The code is wrong in so many ways. I don't recall ever seeing
> a decent tutorial about anything on tutorialspoint.
> --
> Ben.


Hi Ben.
I have also found tutorialspoint tutorials very undecent in the past, unfortunately my institution promotes them. I was in slight disbelief when I seen this piece of code disguised as a tutorial, what I could only describe asa sacrilege to decent coders but I doubted my judgement as i have not doneand c/c++ coding for many years.
Thankyou for the reply.
Francis Glassborow (01-31-20, 03:57 PM)
On 21/01/2020 20:11, Paul Reid wrote:
> On Tuesday, January 21, 2020 at 1:18:58 AM UTC, Ben Bacarisse wrote:
> Hi Ben.
> I have also found tutorialspoint tutorials very undecent in the past, unfortunately my institution promotes them. I was in slight disbelief when I seen this piece of code disguised as a tutorial, what I could only describe as a sacrilege to decent coders but I doubted my judgement as i have not done and c/c++ coding for many years.
> Thankyou for the reply.

If your institution is promoting bad tutorials it is time they got
called out on it particularly if they are pretending to instruct others
for money.

Francis
Similar Threads