So today i tried to implement a quicksort. It's almost working but somehow skips one element.
example : 5 2 8 2 3 4 1 5 7 -5 -1 -9 2 4 5 7 6 1 4
output : -5 -1 -9 1 2 2 3 2 1 4 4 4 5 5 5 6 7 7 8
In this case it skips -9. Here is code of the function.
test = quicksort_asc(0,size,tab,size) // this is function call
int quicksort_asc(int l, int r, int tab[], int tabSize) //l is first index, r is last index, tabSize is tabsize-1 generally
{
int i,buffer,lim,pivot,flag=0;
if(l == r)
return 0;
lim = l-1;
pivot = tab[r-1];
printf("pivot:%d\n",pivot);
for (i = l; i <= r-1; ++i)
{
if(tab[i] < pivot) {
lim++;
buffer = tab[lim];
tab[lim] = tab[i];
tab[i] = buffer;
flag = 1;
}
}
if(flag == 0)
return 0;
buffer = tab[lim+1];
tab[lim+1] = pivot;
tab[r-1] = buffer;
quicksort_asc(l,lim+1,tab,lim+1);//left side
quicksort_asc(lim+1,tabSize,tab,tabSize);//right side
}
This is my array length count code. 100 is maximum size, 0 is a stop value.
Count is size.
int count=0;
for (int i = 0; i < 100; i++)
{
test = scanf("%d",&vec[i]);
if(vec[i] == 0) break;
count++;
}
return count;
It seems nobody hurries to help you.:)
For starters the last parameter is redundant.
int quicksort_asc(int l, int r, int tab[], int tabSize);
^^^^^^^^^^^
All you need is the pointer to the first element of the array and starting and ending indices.
Also instead of the type int for the indices it is better to use the type size_t. However as you are using the expression statement
lim = l-1;
then o'k let the indices will have the type int though you could use another approach without this expression statement.
So the function should be declared like
void quicksort_asc( int tab[], int l, int r );
The variable flag is redundant. When it is equal to 0 it means that all elements before the pivot value are greater than or equal to it. But nevertheless you have to swap the pivot value with the first element that is greater than or equal to the pivot.
This loop
for (i = l; i <= r-1; ++i)
has one iteration redundant. It should be set like
for (i = l; i < r-1; ++i)
This call
quicksort_asc(lim+1,tabSize,tab,tabSize);
^^^^^ ^^^^^^^
shall be substituted for this call
quicksort_asc( lim + 2, r, tab );
^^^^^^^ ^^
because the pivot value is not included in this sub-array.
Here is a demonstrative program.
#include <stdio.h>
void quicksort_asc( int tab[], int l, int r )
{
if ( l + 1 < r )
{
int lim = l - 1;
int pivot = tab[r - 1];
for ( int i = l; i < r - 1; ++i )
{
if ( tab[i] < pivot )
{
lim++;
int tmp = tab[lim];
tab[lim] = tab[i];
tab[i] = tmp;
}
}
tab[r - 1] = tab[lim + 1];
tab[lim + 1] = pivot;
quicksort_asc( tab, l, lim + 1 );
quicksort_asc( tab, lim + 2, r );
}
}
int main(void)
{
int a[] = { 5, 2, 8, 2, 3, 4, 1, 5, 7, -5, -1, -9, 2, 4, 5, 7, 6, 1, 4 };
const int N = ( int )( sizeof( a ) / sizeof( *a ) );
for ( int i = 0; i < N; i++ )
{
printf( "%d ", a[i] );
}
putchar( '\n' );
quicksort_asc( a, 0, N );
for ( int i = 0; i < N; i++ )
{
printf( "%d ", a[i] );
}
putchar( '\n' );
return 0;
}
Its output is
5 2 8 2 3 4 1 5 7 -5 -1 -9 2 4 5 7 6 1 4
-9 -5 -1 1 1 2 2 2 3 4 4 4 5 5 5 6 7 7 8
Related
In which way i can find indexes of my random numbers and store them.
Example:
300, 2, 43, 12, 0, 1, 90
Values -> 0 1 2 12 43 90 300
Indexes -> 0 1 2 3 4 5 6
So. Can i store instead of my values their indexes?
Like This
300 2 43 12 0 1 90
6 2 4 3 0 1 5
And will it possible for negative numbers also?
EDIT: (correction to my previously posted incorrect solution)
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int val;
int in0;
int in1;
} pair_t;
int cmpVal( const void *a, const void *b ) { return ((pair_t*)a)->val - ((pair_t*)b)->val; }
int cmpOrg( const void *a, const void *b ) { return ((pair_t*)a)->in0 - ((pair_t*)b)->in0; }
int main() {
int i;
int unsort[] = { 300, 2, 43, 12, 0, 1, 90 };
const int n = sizeof unsort/sizeof unsort[0];
// Make a copy in unsorted order including orginal sequence.
pair_t *worken = malloc( n * sizeof *worken );
for( i = 0; i < n; i++ )
worken[i].val = unsort[i], worken[i].in0 = i;
// Sort by value ascending
qsort( worken, n, sizeof pair_t, cmpVal );
// Register this sequence with each element
for( i = 0; i < n; i++ )
worken[i].in1 = i;
// Restore original sequence
qsort( worken, n, sizeof pair_t, cmpOrg );
// Copy the indices (of sorted version) to 'persistant' array.
int sorted[n] = { 0 };
for( i = 0; i < n; i++ )
sorted[i] = worken[i].in1;
// Toss 'working' buffer.
free( worken );
// List original sequence
for( i = 0; i < n; i++ )
printf( "%4d", unsort[ i ] );
putchar( '\n' );
// List corresponding indices (as if sorted)
for( i = 0; i < n; i++ )
printf( "%4d", sorted[ i ] );
putchar( '\n' );
return 0;
}
Output
300 2 43 12 0 1 90
6 2 4 3 0 1 5
Trivial assignment loop to "replace values with indices" in original array left out for clarity...
EDIT #2:
The OP suggests the unsorted array is to have its values replaced(!) with indices indicating the sort order.
This following does as much with the proviso that array values are not near the top end of values for ints.
#include <stdio.h>
#include <limits.h>
void show( int u[], size_t cnt ) { // Show current array values
for( size_t i = 0; i < cnt; i++ )
printf( "%4d", u[ i ] );
putchar( '\n' );
}
void oddSort( int u[], size_t cnt ) {
show( u, cnt );
// Succesively find and replace highest values with decreasing large int values.
int peak = INT_MAX;
for( size_t set = 0; set < cnt; set++ ) {
int maxID = 0;
while( u[maxID] >= peak ) maxID++; // find first non-replaced value
for( size_t i = maxID + 1; i < cnt; i++ )
if( u[i] < peak && u[i] > u[maxID] )
maxID = i;
u[maxID] = peak--;
}
// transpose down to 0, 1, 2...
for( size_t i = 0; i < cnt; i++ )
u[i] -= peak + 1;
show( u, cnt );
}
int main() {
{
int u[] = { 300, 2, 43, 12, 0, 1, 90 };
oddSort( u, sizeof u/sizeof u[0] );
}
putchar( '\n' );
{
// Test with negatives (coincidentally lowest value in first pos)
int u[] = { -256, 300, 2, 43, 12, 0, 1, 90 };
oddSort( u, sizeof u/sizeof u[0] );
}
return 0;
}
Output:
300 2 43 12 0 1 90
6 2 4 3 0 1 5
-256 300 2 43 12 0 1 90
0 7 3 5 4 1 2 6
/**
#brief Move all negative elements to end
EX: Given an unsorted array arr[] of size N having both negative and positive integers.
The task is place all negative element at the end of array
without changing the order of positive element and negative element.
Example 1:
Input :
N = 8
arr[] = {1, -1, 3, 2, -7, -5, 11, 6 }
Output :
1 3 2 11 6 -1 -7 -5
Example 2:
Input :
N=8
arr[] = {-5, 7, -3, -4, 9, 10, -1, 11}
Output :
7 9 10 11 -5 -3 -4 -1
*/
#include<stdio.h>
void shortArray(int array[], int stack[]){
int size = 8;
int left = 0;
int right = size;
for(int i=0; i<size; i++){
if(left = right) break;
volatile int value = array[i];
if(array[i] > 0){
stack[left] = value;
left ++;
}
if(array[i] < 0){
stack[right] = value;
right--;
}
}
}
//printarray
void printArray(int array[], int size){
for(int i=0; i<size; i++){
printf(" %d", array[i]);
}
}
int main(){
int array[] = {-5, 7, -3, -4, 9, 10, -1, 11};
int size = 8;
int stack[size];
shortArray(array, stack);
printArray(stack, size);
return 0;
}
my output:
0 0 0 0 0 0 0 0
i try to assign value to stack[] but it don't work right.
i dont know what wrong with it? can you tell me why? thank you!
sorry for my bad english :<
For starters the assignment sounds like
The task is place all negative element at the end of array
It means that you need to change the source array in place not to build a new array.
In this if statement
if(left = right) break;
there is used the assignment operator = instead of the equality operator ==. So if right initially is not equal to 0 then the loop breaks at once.
Using the qualifier volatile in this declaration
volatile int value = array[i];
has no any sense.
If the first element of the source array is negative then this if statement
if(array[i] < 0){
stack[right] = value;
right--;
}
invokes undefined behavior because the index right points to memory after the last element of the array stack.
And moreover with this approach the order of negative values will be broken
And the function shall not use the magic number 8. It should be able to deal with arrays of various sizes. So the function must accept the number of elements in the passed to it array.
The function can be defined for example the following way as it is shown in the demonstration program below
#include <stdio.h>
void partition( int a[], size_t n )
{
for (size_t i = 0, j = 0; i < n; )
{
while (j != n && !( a[j] < 0 )) j++;
if (!( j < i )) i = j;
while (i != n && a[i] < 0) i++;
if (i != n)
{
int tmp = a[i];
for ( size_t k = i; k != j; --k )
{
a[k] = a[k - 1];
}
a[j] = tmp;
i++, j++;
}
}
}
int main( void )
{
int a[] = { 1, -1, 3, 2, -7, -5, 11, 6 };
const size_t N = sizeof( a ) / sizeof( *a );
for (size_t i = 0; i < N; i++)
{
printf( "%d ", a[i] );
}
putchar( '\n' );
partition( a, N );
for (size_t i = 0; i < N; i++)
{
printf( "%d ", a[i] );
}
putchar( '\n' );
}
The program output is
1 -1 3 2 -7 -5 11 6
1 3 2 11 6 -1 -7 -5
I wrote the following function:
void negate_row(const int n, const int r, int *a)
{
if (r == 0)
{
printf("Matrix with negated row: ");
printf("\n");
for (int y = 0; y < 3; y++)
{
*(a + 3 * r + y) = *(a + 3 * r + y) *(-1);
printf("%d ", *(a + 3 * r + y));
}
printf("\n");
for (int y = 0; y < 3; y++)
{
*(a + 3 * r + y) = *(a + 3 * r + y) *(-1);
printf("%d ", *(a + 3 * (r + 1) + y));
}
printf("\n");
for (int y = 0; y < 3; y++)
{
*(a + 3 * r + y) = *(a + 3 * r + y) *(-1);
printf("%d ", *(a + 3 * (r + 2) + y));
}
printf("\n");
}
So basically what happens here is my function takes in a n X 3 matrix and negates a specific row using pointer arithmetic. I have been able to achieve that, I've also been able to figure out how to print that same matrix with the negated row. It's just the way I'm doing it is not efficient at all. I'd have to write an if statement for each row, ex if r == 0,1,2,3,4 etc... is there any way I can do this more efficiently?
Some clarifications: const int n decides the size of the matrix (n x 3), const int r decides what row is negated (0 <= r < n).
A second loop will help. I generally find pointer code a bit harder to read. Particularly with Matrix manipulation you might be better off using array syntax instead of pointer syntax.
for (int y = 0; y < 3; y++)
{
for (int x = 0; x < 3; x++)
{
printf("%d ", *(a + 3 * (r + x) + y));
}
printf("\n");
}
Particular case (3 rows):
int mul = 1, y = 0;
for (int x = 0; x < n; x++) {
mul = x == r ? -1 : 1;
y = (a + 3 * x);
printf("%d ", (*y) * mul);
printf("%d ", (*y + 1) * mul);
printf("%d ", (*y + 2) * mul);
printf("\n");
}
More generic (m rows):
int mul = 1;
for (int x = 0; x < n; x++) {
mul = x == r ? -1 : 1;
for (int y = 0; y < m; y++) {
printf("%d ", ((*(a + m * x + y)) * mul);
}
printf("\n");
}
Note: In new compilers there is also no difference in speed between array or pointer syntax.
You could write a function that accepts a one-dimensional array (a row of your matrix) and then using this function you could in a loop output its all rows or output a selected row.
Here is a demonstrative program.
#include <stdio.h>
void negate_row( const int *a, size_t n, int width )
{
if ( width < 1 ) width = 1;
for ( const int *p = a; p != a + n; ++p )
{
printf( "%*d ", width, -*p );
}
putchar( '\n' );
}
int main(void)
{
enum { M = 3, N = 4 };
int matrix[M][N] =
{
{ 0, 1, 2, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 }
};
for ( int ( *p )[N] = matrix; p != matrix + M; ++p )
{
negate_row( *p, N, 3 );
}
return 0;
}
The program output is
0 -1 -2 -4
-5 -6 -7 -8
-9 -10 -11 -12
As you showed a code where you are using pointers tp output elements of an array then in this demonstrative program I am also using pointers everywhere to access elements of an array.
The third parameter of the function specifies the width of the field for an outputted value.
To output the matrix in the reversed order of rows you can use the loop shown in the program blow.
#include <stdio.h>
void negate_row( const int *a, size_t n, int width )
{
if ( width < 1 ) width = 1;
for ( const int *p = a; p != a + n; ++p )
{
printf( "%*d ", width, -*p );
}
putchar( '\n' );
}
int main(void)
{
enum { M = 3, N = 4 };
int matrix[M][N] =
{
{ 0, 1, 2, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 }
};
for ( int ( *p )[N] = matrix + M; p != matrix; )
{
negate_row( *--p, N, 3 );
}
return 0;
}
The program output is
-9 -10 -11 -12
-5 -6 -7 -8
0 -1 -2 -4
You can generalize the function easily: as long as the row is within the matrix, your code works for any row. Also note that it is better to use a separate function to print the matrix.
#include <stdio.h>
void negate_row(const int n, const int r, int *a) {
if (r >= 0 && r < n) {
// negate row r
for (int col = 0; col < 3; col++) {
*(a + 3 * r + col) *= -1;
}
}
}
void print_matrix(const int n, int *a, const char *title) {
if (title) {
printf("%s:\n", title);
}
for (int row = 0; row < n; row++) {
for (int col = 0; col < 3; col++) {
printf("%d ", *(a + 3 * row + col));
}
printf("\n");
}
printf("\n");
}
int main() {
int matrix[5 * 3] = {
0, 1, 2,
3, 4, 5,
6, 7, 8,
9, 10, 11,
12, 13, 14,
};
print_matrix(5, matrix, "Matrix");
negate_row(5, 0, matrix);
print_matrix(5, matrix, "Matrix with negated row");
negate_row(5, 3, matrix);
print_matrix(5, matrix, "Matrix with two negated rows");
negate_row(5, 0, matrix);
negate_row(5, 3, matrix);
print_matrix(5, matrix, "Matrix back to origin");
return 0;
}
Output:
Matrix:
0 1 2
3 4 5
6 7 8
9 10 11
12 13 14
Matrix with negated row:
0 -1 -2
3 4 5
6 7 8
9 10 11
12 13 14
Matrix with two negated rows:
0 -1 -2
3 4 5
6 7 8
-9 -10 -11
12 13 14
Matrix back to origin:
0 1 2
3 4 5
6 7 8
9 10 11
12 13 14
I should build a function that gets an array and it's size and return a pointer
to new array (i need do create new array by using malloc and realloc) that find the identical numbers and duplicates them in the row
for example the array:{1,8,8,70,2,2,2,5,5,2} and size 10 suppose to return a pointer to this array
{1,8,8,8,8,70,2,2,2,2,2,2,5,5,5,5,2}. Any clue what's wrong with my code??
int * duplicateArray(int* arr, int n)
{
int g = 1;
int i,j=0;
int *p = (int*)(calloc)(n, sizeof(int));
assert(p);
for (i = 0; i < n-1; i++)
{
if (arr[i] == arr[i + 1])
{
p= (int*)(realloc)(p, n+g * sizeof(int));
n=n+g;
assert(p);
p[j] = arr[i];
j++;
p[j] = arr[i+1];
}
else
p[j] = arr[i];
j++;
}
return p;
}
The approach used by you when the memory is constantly reallocated is inefficient.
Also the function should return not only the pointer to the dynamically allocated array but also the number of elements in the allocated array. Otherwise the user of the function will not know how many elements are in the allocated array. To use a sentinel value for an integer dynamically allocated array is not a good idea.
I suggest to split the task into two separate tasks that will correspond to two separate functions..
The first function will count the number of repeated elements in a given array.
The second function will create dynamically an array with the specified size based on the returned value of the first function and copy elements of the source array to the newly created array.
Here is a demonstrative program
#include <stdio.h>
#include <stdlib.h>
size_t countRepeated( const int a[], size_t n )
{
size_t repeated = 0;
for ( size_t i = 0; i != n; )
{
size_t m = 1;
while ( ++i != n && a[i] == a[i-1] ) ++m;
if ( m != 1 ) repeated += m;
}
return repeated;
}
int * copyWithDuplication( const int a[], size_t n, size_t m )
{
int *result = m == 0 ? NULL : calloc( m, sizeof( int ) );
if ( result )
{
for ( size_t i = 0, j = 0; j != m && i != n; )
{
result[j++] = a[i++];
size_t k = 1;
while ( j != m && i != n && a[i] == a[i-1] )
{
result[j++] = a[i++];
++k;
}
if ( k != 1 )
{
while ( j != m && k-- ) result[j++] = a[i-1];
}
}
}
return result;
}
int main(void)
{
int a[] = { 1, 8, 8, 70, 2, 2, 2, 5, 5, 2 };
const size_t N = sizeof( a ) / sizeof( *a );
for ( size_t i = 0; i < N; i++ )
{
printf( "%d ", a[i] );
}
putchar( '\n' );
size_t m = N + countRepeated( a, N );
int *b = copyWithDuplication( a, N, m );
if ( b )
{
for ( size_t i = 0; i < m; i++ )
{
printf( "%d ", b[i] );
}
putchar( '\n' );
}
free( b );
return 0;
}
The program output is
1 8 8 70 2 2 2 5 5 2
1 8 8 8 8 70 2 2 2 2 2 2 5 5 5 5 2
And here is another more interesting demonstrative program.
#include <stdio.h>
#include <stdlib.h>
size_t countRepeated( const int a[], size_t n )
{
size_t repeated = 0;
for ( size_t i = 0; i != n; )
{
size_t m = 1;
while ( ++i != n && a[i] == a[i-1] ) ++m;
if ( m != 1 ) repeated += m;
}
return repeated;
}
int * copyWithDuplication( const int a[], size_t n, size_t m )
{
int *result = m == 0 ? NULL : calloc( m, sizeof( int ) );
if ( result )
{
for ( size_t i = 0, j = 0; j != m && i != n; )
{
result[j++] = a[i++];
size_t k = 1;
while ( j != m && i != n && a[i] == a[i-1] )
{
result[j++] = a[i++];
++k;
}
if ( k != 1 )
{
while ( j != m && k-- ) result[j++] = a[i-1];
}
}
}
return result;
}
int main(void)
{
int a[] = { 1, 8, 8, 70, 2, 2, 2, 5, 5, 2 };
const size_t N = sizeof( a ) / sizeof( *a );
for ( size_t i = 0; i < N; i++ )
{
printf( "%d ", a[i] );
}
putchar( '\n' );
size_t m = N + countRepeated( a, N );
for ( size_t i = 0; i < m; i++ )
{
int *b = copyWithDuplication( a, N, i + 1 );
if ( b )
{
for ( size_t j = 0; j < i + 1; j++ )
{
printf( "%d ", b[j] );
}
putchar( '\n' );
}
free( b );
}
return 0;
}
Its output is
1 8 8 70 2 2 2 5 5 2
1
1 8
1 8 8
1 8 8 8
1 8 8 8 8
1 8 8 8 8 70
1 8 8 8 8 70 2
1 8 8 8 8 70 2 2
1 8 8 8 8 70 2 2 2
1 8 8 8 8 70 2 2 2 2
1 8 8 8 8 70 2 2 2 2 2
1 8 8 8 8 70 2 2 2 2 2 2
1 8 8 8 8 70 2 2 2 2 2 2 5
1 8 8 8 8 70 2 2 2 2 2 2 5 5
1 8 8 8 8 70 2 2 2 2 2 2 5 5 5
1 8 8 8 8 70 2 2 2 2 2 2 5 5 5 5
1 8 8 8 8 70 2 2 2 2 2 2 5 5 5 5 2
Any clue what's wrong with my code??
If the parameter n is 1, then your program will allocate an array for 1 element of type int, but will write nothing to it. It won't copy anything from the input buffer.
You are accessing both the input arr and the output array p out of bounds, which causes undefined behavior. The loop
for (i = 0; i < n-1; i++)
will not count from 0 to the function parameter n minus 2, because n is being incremented inside the loop. This causes your loop to have more iterations than it is supposed to, which causes both the input and the output array to be accessed out of bounds.
Also, there doesn't seem to be much point in your variable g, as it never changes and always has the value 1.
The function prototype
int * duplicateArray(int* arr, int n);
does not seem meaningful, as the calling function has no way of knowing the size of the returned array. If you use the function prototype
void duplicateArray ( const int *p_input_array, int num_input, int **pp_output_array, int *p_num_output );
instead, the function duplicateArray can write the address of the new array to *pp_output_array and the number of elements in the array to *p_num_output. That way, the calling function will effectively be able to receive two "return values" instead of only one.
Here is my implementation of the function duplicateArray and also of the calling function:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
void duplicateArray( const int *p_input_array, int num_input, int **pp_output_array, int *p_num_output )
{
int i = 0, j = 0;
// In the most extreme case, the output array must be 2 times larger than
// the input buffer, so we allocate double the size of the input buffer.
int *p_output_array = (int*)malloc( num_input * 2 * sizeof(int) );
assert( p_output_array != NULL );
while ( i < num_input )
{
int num_repetitions;
int k = p_input_array[i++];
//count the number of repetitions
for ( num_repetitions = 0; i < num_input && p_input_array[i] == k; num_repetitions++, i++ );
if ( num_repetitions == 0 )
{
p_output_array[j++] = k;
}
else
{
for ( int l = 0; l < num_repetitions + 1; l++ )
{
p_output_array[j++] = k;
p_output_array[j++] = k;
}
}
}
//shrink the array to the actually needed size
p_output_array = (int*)realloc( p_output_array, j * sizeof(int) );
assert( p_output_array != NULL );
*pp_output_array = p_output_array;
*p_num_output = j;
}
int main()
{
int arr[] = { 1, 8, 8, 70, 2, 2, 2, 5, 5, 2 };
int *p;
int num;
duplicateArray( arr, sizeof(arr)/sizeof(*arr), &p, &num );
for ( int i = 0; i < num; i++ ) {
printf( "%d\n", p[i] );
}
free( p );
}
I am going through a TuturialsPoint algorithm and trying to run the C code with GCC. Would anyone know why my local output would be different that what is produced by an online C compiler?
#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]);
}
}
Expected output (from online gcc compiler)
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
The array elements after insertion :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 10
LA[4] = 7
LA[5] = 8
My local output:
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
The array elements after insertion :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
LA[5] = 6
I am using gcc version 8.2.0 (MinGW.org GCC-8.2.0-5)
You defined an array of exactly 5 elements.
int LA[] = {1, 3, 5, 7, 8};
So the valid range of indices to access elements of the array is [0, 5).
This array may not be enlarged. So using an index equal to or greater than 5 results in access and overwriting the memory beyond the array.
You need initially to define the array with the number of elements that allows to insert new elements apart from the 5 explicitly initialized elements.
What you mean is something like the following
#include <stdio.h>
int main(void)
{
enum { N = 10 };
int a[N] = { 1, 3, 5, 7, 8 };
size_t n = 0;
while ( a[n] != 0 ) ++n;
printf( "The original array elements are :" );
for ( size_t i = 0; i < n; i++ ) printf( "%d ", a[i] );
putchar( '\n' );
int item = 10;
size_t pos = 3;
size_t j = n;
if ( pos < j )
{
for ( ; j != pos; j-- )
{
a[j] = a[j-1];
}
}
a[j] = item;
++n;
printf( "The array elements after insertion : " );
for ( size_t i = 0; i < n; i++ ) printf( "%d ", a[i] );
putchar( '\n' );
return 0;
}
The program output is
The original array elements are :1 3 5 7 8
The array elements after insertion : 1 3 5 10 7 8
Note: This code snippet
if ( pos < j )
{
for ( ; j != pos; j-- )
{
a[j] = a[j-1];
}
}
can be substituted for this loop
for ( ; pos < j; j-- )
{
a[j] = a[j-1];
}