The question is how to delete elements whichs their values equal to an integer x.
In c++, i'm using reference and it works ok, but then when i changed to C, it can't release output.
Here is my code.
Please let me know what I'm wrong, thank you guys !
#include <stdio.h>
#include <conio.h>
void Input_array(int a[], int n)
{
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
}
void Output_array(int a[], int n)
{
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
}
int find_pos(int a[], int n, int x)
{
for (int i = 0; i < n; i++)
{
if (a[i] == x)
{
return i;
}
}
return -1;
}
void delete_pos(int a[], int *n, int pos)
{
int cnt=0;
for (int i = pos; i < *n - 1; i++)
//{
a[i] = a[i + 1];
*n=*n-1;
}
void delete_elmts(int a[], int *n, int x){
for(int i=0; i<n;){
if (a[i]==x)
{
delete_pos(a, *n, i);
}
else
i++;
}
}
int main()
{
int n, x;
scanf("%d%d", &n, &x);
int a[n];
Input_array(a, n);
delete_elmts(a, &n, x);
Output_array(a, n);
}
Use the correct types
Use standard functions like memcpy or memmove as they are much more efficient
void Input_array(size_t size, int *arr)
{
for (size_t i = 0; i < size; i++)
arr[i] = i;
}
void Output_array(size_t size, int *arr)
{
for (size_t i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
size_t delete_pos(const size_t size, const size_t pos, int *arr)
{
size_t newsize = size;
if(arr && size && pos < size)
{
memmove(arr + pos, arr + pos + 1, sizeof(*arr) * (size - pos));
newsize--;
}
return newsize;
}
size_t delete_elmts(const size_t size, const size_t pos, size_t nelements, int *arr)
{
if(arr && nelements && pos < size - 1)
{
if(pos + nelements > size) nelements = size - pos;
memmove(arr + pos, arr + pos + nelements, sizeof(*arr) * (size - pos - nelements));
}
return size - nelements;
}
#define SIZE 10
int main()
{
size_t size = SIZE;
int a[SIZE];
for(size_t i = 0; i < SIZE + 2; i ++)
{
Input_array(SIZE, a);
size = delete_pos(SIZE, i, a);
Output_array(size, a);
}
for(size_t i = 0; i < SIZE + 2; i ++)
{
for(size_t len = 0; len < SIZE + 1; len++)
{
printf("pos = %2zu, len = %2zu: ", i, len);
Input_array(SIZE, a);
size = delete_elmts(SIZE, i, len, a);
Output_array(size, a);
}
printf("\n");
}
}
Related
I need some help with this function that make permutations from an array pointer in input.
Here is the code.
#include <stdio.h>
#include <stdlib.h>
void input_array(int *arr, int size);
void print_array(int *arr, int size);
void array_to_matrix(int **matrix, int *arr, int row, int col);
void print_matrix(int **matrix, int row, int col);
int factorial(int n)
{
if (n == 0)
return 1;
else
return (n * factorial(n - 1));
}
void swap(int *x1, int *x2)
{
int x = *x1;
*x1 = *x2;
*x2 = x;
}
int *permute(int *new_arr, int st, int ls)
{
int i = 0;
if (st == ls) {
// int k;
// for (k = 0; k < ls; k++)
// {
// printf("%d ", new_arr[k]);
// }
// printf("\n");
return new_arr;
} else {
for (i = st; i < ls; i++)
{
swap(new_arr + st, new_arr + i);
permute(new_arr, st + 1, ls);
swap(new_arr + st, new_arr + i);
}
}
return new_arr;
}
int main()
{
int m, n, *arr, **mat;
int size, i;
//INIZIO CALCOLO MATRICE DI ARRAY INIZIALE (matrice dei costi)
//inserisci il numero di colonne, che corrisponde al numero di cittÃ
printf("Enter the number of columns(n):");
if ((scanf("%d", &n) != 1) || (n < 1)) {
puts("invalid value for columns");
return -1;
}
m = n;
//m=numero di righe n=numero di colonne
size = m * n;
if (((arr = malloc(size * sizeof(int))) == NULL) ||
((mat = malloc(m * sizeof(int))) == NULL)) {
puts("not enough memory");
exit(-1);
}
for (i = 0; i < m; ++i) {
if ((mat[i] = malloc(n * sizeof(int))) == NULL) {
puts("not enough memory");
exit(-1);
}
}
input_array(arr, size);
print_array(arr, size);
array_to_matrix(mat, arr, m, n);
print_matrix(mat, m, n);
for (i = 0; i < m; ++i)
free(mat[i]);
free(mat);
//INIZIO CALCOLO PERMUTAZIONI (matrice dei percorsi possibili)
int nrighe = factorial(n);
int *new_array;
int st = 0, k = 0, j;
if (((new_array = malloc((n * nrighe) * sizeof(int))) == NULL) ||
((mat = malloc((n * nrighe) * sizeof(int))) == NULL)) {
puts("not enough memory");
exit(-1);
}
for (i = 0; i < m; ++i) {
if ((mat[i] = malloc((n * nrighe) * sizeof(int))) == NULL) {
puts("not enough memory");
exit(-1);
}
}
for (j = 1; j < n + 1; j++) {
new_array[k] = j;
printf("newarray[%d", k);
printf("]= %d", j);
k++;
}
free(arr);
if ((arr = malloc((n * nrighe) * sizeof(int))) == NULL) {
puts("not enough memory");
exit(-1);
}
printf("\nPermutations of dimension: %d\n", n);
arr = permute(new_array, st, n);
k = 0;
for (k = 0; k < n; k++) {
printf("%d ", arr[k]);
}
printf("\n");
array_to_matrix(mat, new_array, nrighe, n);
print_matrix(mat, nrighe, n);
free(arr);
free(new_array);
return 0;
}
void input_array(int *arr, int size)
{
int i;
for (i = 0; i < size; i++) {
printf("Enter element a[%d]", i);
if (scanf("%d", &arr[i]) != 1) {
int c;
puts("invalid value, redo");
/* flush invalid value up to the end of line */
while ((c = getchar()) != '\n') {
if (c == EOF) {
puts("EOF, abort");
exit(-1);
}
}
i -= 1;
}
}
}
void print_array(int *arr, int size)
{
int i;
printf("\n 1D array is as follows : \n");
for (i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
}
void array_to_matrix(int **matrix, int *arr, int row, int col)
{
int i, j, k = 0;
for (i = 0; i < row; i++) {
for (j = 0;j < col; j++) {
matrix[i][j] = arr[k++];
}
}
}
void print_matrix(int **matrix, int row, int col)
{
int i, j;
printf("\n 2D matrix is as follows : \n");
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
When I print the array like the section in the comments it works. It prints all the permutations that it has found. The problem is when I try to print it from the return function, in the main() function, i take the array returned from the permute() function and after with a for loop I try to print it, but it that says it's empty; the loop doesn't print the corrects values. I can't understand why. Can someone help me please?
permute does compute and can display all permutations of the array passed as argument, but it cannot return them separately. If you want to store all permutations into a matrix, you should pass the destination array along with the index this way:
int permute(int *arr, int st, int ls, int *new_array, int k)
{
int i;
if (st == ls) {
/* store a new permutation */
for (i = 0; i < ls; i++) {
new_array[k++] = arr[i];
}
} else {
for (i = st; i < ls; i++) {
swap(arr + st, arr + i);
k = permute(arr, st + 1, ls, new_array, k);
swap(arr + st, arr + i);
}
}
return k;
}
Here is a modified version of your program with more bug fixes and including a version of permute that can handle duplicates:
#include <stdio.h>
#include <stdlib.h>
void input_array(int *arr, int size);
void print_array(const int *arr, int size);
int **allocate_matrix(int rows, int cols);
void free_matrix(int **mat, int rows, int cols);
void array_to_matrix(int **matrix, const int *arr, int rows, int cols);
void print_matrix(int **matrix, int rows, int cols);
int factorial(int n) {
if (n <= 0)
return 1;
else
return n * factorial(n - 1);
}
void swap(int *x1, int *x2) {
int x = *x1;
*x1 = *x2;
*x2 = x;
}
int permute(int *arr, int st, int ls, int *dest, int k) {
if (st == ls) {
/* store a new permutation */
for (int i = 0; i < ls; i++) {
dest[k++] = arr[i];
}
} else {
k = permute(arr, st + 1, ls, dest, k);
for (int i = st + 1; i < ls; i++) {
/* eliminate permutations of identical elements */
if (arr[st] != arr[i]) {
swap(arr + st, arr + i);
k = permute(arr, st + 1, ls, dest, k);
swap(arr + st, arr + i);
}
}
}
return k;
}
int main() {
int n, nrighe, size, *arr, **mat;
//INIZIO CALCOLO MATRICE DI ARRAY INIZIALE (matrice dei costi)
//inserisci il numero di colonne, che corrisponde al numero di cittÃ
printf("Enter the number of columns(n): ");
if (scanf("%d", &n) != 1 || n < 1) {
fprintf(stderr, "invalid value for columns\n");
exit(1);
}
#if 1
nrighe = n;
//nrighe = number of rows, n = number of columns
size = nrighe * n;
if (((arr = calloc(size, sizeof(*arr))) == NULL)
|| ((mat = allocate_matrix(nrighe, n)) == NULL)) {
fprintf(stderr, "not enough memory\n");
exit(1);
}
input_array(arr, size);
print_array(arr, size);
array_to_matrix(mat, arr, nrighe, n);
print_matrix(mat, nrighe, n);
free_matrix(mat, nrighe, n);
#endif
//INIZIO CALCOLO PERMUTAZIONI (matrice dei percorsi possibili)
nrighe = factorial(n);
size = nrighe * n;
if (((arr = calloc(size, sizeof(*arr))) == NULL)
|| ((mat = allocate_matrix(nrighe, n)) == NULL)) {
fprintf(stderr, "not enough memory\n");
exit(1);
}
for (int i = 0; i < n; i++) {
arr[i] = i + 1;
printf("newarray[%d] = %d\n", i, i + 1);
}
printf("\nPermutations of dimension: %d\n", n);
permute(arr, 0, n, arr, 0);
array_to_matrix(mat, arr, nrighe, n);
print_matrix(mat, nrighe, n);
free(arr);
free_matrix(mat, nrighe, n);
return 0;
}
void input_array(int *arr, int size) {
for (int i = 0; i < size; i++) {
printf("Enter element a[%d]: ", i);
if (scanf("%d", &arr[i]) != 1) {
int c;
fprintf(stderr, "invalid value, redo\n");
/* flush invalid value up to the end of line */
while ((c = getchar()) != '\n') {
if (c == EOF) {
fprintf(stderr, "EOF, abort\n");
exit(1);
}
}
i -= 1;
}
}
}
void print_array(const int *arr, int size) {
printf("\n 1D array is as follows:\n");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
/* allocate a matrix of m rows and n columns initialized to 0 */
int **allocate_matrix(int rows, int cols) {
int **mat;
if ((mat = calloc(rows, sizeof(*mat))) == NULL)
return NULL;
for (int i = 0; i < rows; ++i) {
if ((mat[i] = calloc(cols, sizeof(*mat[i]))) == NULL) {
while (i-- > 0)
free(mat[i]);
free(mat);
return NULL;
}
}
return mat;
}
void free_matrix(int **mat, int rows, int cols) {
if (mat) {
for (int i = 0; i < rows; ++i) {
free(mat[i]);
}
free(mat);
}
}
void array_to_matrix(int **matrix, const int *arr, int rows, int cols) {
for (int i = 0, k = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = arr[k++];
}
}
}
void print_matrix(int **matrix, int rows, int cols) {
printf("\n 2D matrix is as follows:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
What is the problem? Why is there always a fixed value of comparisons and swaps? And am I correct in counting swaps and comparisons in principle? I mean, do I accidentally count them in the wrong places? Maybe in some places it is not necessary to do this? Please help me because I really can't figure out what's wrong
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void mergeAbstract(int *a, int lb, int split, int ub, int *count, int *swaps)
{
int size = ub - lb + 1;
int *sub = (int *)malloc((ub + 1) * sizeof(int));
int i = 0, j = size - 1, k = lb;
int s;
for (s = 0; s < split + 1 - lb; s++) sub[s] = a[lb + s];
for (s = split + 1 - lb; s < size; s++) sub[s] = a[ub - (s - (split + 1 -lb))];
while (i <= j) {
(*count) += 2;
if (sub[i] > sub[j]) {
(*swaps)++;
a[k] = sub[j];
j--;
} else {
(*swaps)++;
a[k] = sub[i];
i++;
}
k++;
}
(*count)++;
free(sub);
}
void mergeSort(int *a, int lb, int ub, int *count, int *swaps) {
long split;
if (lb < ub) {
split = (lb + ub) / 2;
mergeSort(a, lb, split, count, swaps);
mergeSort(a, split + 1, ub, count, swaps);
mergeAbstract(a, lb, split, ub, count, swaps);
}
}
void arrprint(int *arr, int n) {
printf("%d", *arr);
int i;
for (i = 1; i < n; i++) printf(" %d", arr[i]);
puts("");
}
int main(int argc, char *argv[]) {
int n = 10;
int *arr = NULL;
arr = (int *)malloc(n * sizeof *arr);
int c = 0;
int *count = &c;
int h = 0;
int *swaps = &h;
srand(time(NULL));
int s;
for (s = 0; s < n; s++)
arr[s] = rand() % 50;
arrprint(arr, n);
mergeSort(arr, 0, n - 1, count, swaps);
arrprint(arr, n);
free(arr);
puts("");
printf("Comparison ans swaps : %d %d\n", *count ,*swaps);
return 0;
}
This is a code for multiplying two square matrices in C.
What is the difference between using malloc and calloc in void multiply() function?
When using malloc, I am getting garbage values, but calloc is providing the right answer.
Only the first row is outputting garbage values so is it an issue with the way malloc allocates space in the heap as compared to calloc?
#include <stdio.h>
#include <stdlib.h>
int *getArray(int);
void display(int *, int, int);
void multiply(int *, int *, int);
int main() {
int n;
printf("enter dimension of square matrix:\n");
scanf("%d", &n);
int *arr1;
int *arr2;
arr1 = getArray(n);
display(arr1, n, n);
printf("\n now give input for next array");
arr2 = getArray(n);
display(arr2, n, n);
printf("\n\n\n");
multiply(arr1, arr2, n);
return 0;
}
int *getArray(int n) {
int *arr = (int *)malloc(n * n * sizeof(int));
printf("\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", (arr + i * n + j));
}
}
/*for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf(" %d ", *(arr + i * n + j));
}
printf("\n");
}*/
return arr;
}
void display(int *arr, int row, int col) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
printf(" %d ", *(arr + i * row + j));
}
printf("\n");
}
}
void multiply(int *arr1, int *arr2, int n) {
int *arr = (int *)calloc(n * n, sizeof(int));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
*(arr + i * n + j) += (*(arr1 + i * n + k)) * (*(arr2 + k * n + j));
}
}
}
printf("product of above matrices = \n\n");
display(arr, n, n);
}
The only functional difference between allocating memory with malloc() and with calloc() for the same size, assuming the size computation is accurate, is the latter initializes the block to all bits 0, whereas the former does not.
All bits 0 means all int values in the array are initialized to 0.
The inner loop in the multiply function only increments the element at row i and column j, therefore the function relies on implicit initialization of the array elements to 0. calloc() does that, but not malloc() so you definitely need to use calloc().
Also note these remarks:
in display the computation for the offset of the matrix element at row i column j should be printf(" %5d ", *(arr + i * col + j));
multiply should return arr and display() should be called in the main function.
you should check for scanf(), malloc() and calloc()` failure
you should free allocated memory
pointer arguments to objects that are not modified by the function should be const qualified so the function can be called with a pointer to a const object.
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
int *getArray(int);
void display(const int *, int, int);
int *multiply(const int *, const int *, int);
int main() {
int n;
printf("enter dimension of square matrix:\n");
if (scanf("%d", &n) != 1)
return 1;
printf("\n now give input for the first array");
int *arr1 = getArray(n);
if (!arr1)
return 1;
display(arr1, n, n);
printf("\n now give input for the second array");
int *arr2 = getArray(n);
if (!arr2)
return 1;
display(arr2, n, n);
printf("\n\n\n");
int *arr = multiply(arr1, arr2, n);
if (!arr)
return 1;
printf("product of above matrices = \n\n");
display(arr, n, n);
free(arr1);
free(arr2);
free(arr);
return 0;
}
int *getArray(int n) {
int *arr = malloc(sizeof(int) * n * n);
if (arr == NULL)
return NULL;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (scanf("%d", (arr + i * n + j)) != 1) {
free(arr);
return NULL;
}
}
}
return arr;
}
void display(const int *arr, int row, int col) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
printf(" %5d ", *(arr + i * col + j));
}
printf("\n");
}
}
int *multiply(const int *arr1, const int *arr2, int n) {
int *arr = calloc((size_t)n * n, sizeof(int));
if (arr == NULL)
return NULL;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
*(arr + i * n + j) += (*(arr1 + i * n + k)) * (*(arr2 + k * n + j));
}
}
}
return arr;
}
The below code works fine except for a one dimensional array I am trying to pass to main() from the function order_coef. I believe I am doing something wrong in the line where I specify *ordered_array=*array;, but not really sure what it is. Some help would be very much appreciated. Thank you!
#include <stdio.h>
#define COEF 5
int save_coef (int i, int *array);
int order_coef (int i, int *array, int *ordered_array);
int save_x (int x);
int resolve_polinomyal (int *array, int x, int i);
int main()
{
int array[COEF], x = 1, i = 0, *ordered_array=0;
printf("Please, enter 5 coefficients:\n");
save_coef (i, array);
order_coef (i, array, ordered_array);
for (i = 0; i < COEF; ++i)
printf("\n%d\n", ordered_array[i]);
printf("Please, enter the value of x:\n");
save_x (x);
printf("%d", resolve_polinomyal (array, x, i));
return 0;
}
int save_coef (int i, int *array)
{
for (i = 0; i < COEF; ++i)
{
scanf("%d", &array[i]);
}
return 0;
}
int order_coef (int i, int *array, int *ordered_array)
{
int j, a;
for (i = 0; i < COEF; ++i)
{
for (j = i + 1; j < COEF; ++j)
{
if (array[i] > array[j])
{
a = array[i];
array[i] = array[j];
array[j] = a;
}
}
}
*ordered_array=*array;
return 0;
}
int save_x (int x)
{
scanf("%d", &x);
return 0;
}
int resolve_polinomyal (int *array, int x, int i)
{
for (i = 5-1; i > 0; --i)
{
array[i-1] = array[i] * x + array[i-1];
}
return array[0];
}
The first problem is you do not allocate for ordered_array.
When you declare
int array[COEF], x = 1, i = 0, *ordered_array=0;
it means you initialized ordered_array = NULL.
You can use the static or dynamic allocation:
int ordered_array[COEF];
//
int *ordered_array = malloc(COEF*sizeof(int));
The second problem is *ordered_array=*array not copy array to array. It assigns first value of array to first value of ordered_array.
If you want to copy the value from an array to another one, you should use memcpy or a loop to copy value by value.
For example:
// copy the values of array to ordered_array
memcpy(ordered_array, array, COEF * sizeof(int));
Based on #Hitokiri comment I am pasting here the final solution with my code updated:
#include <stdio.h>
#define COEF 5
int save_coef (int i, int *array);
int order_coef (int i, int *array, int *ordered_array);
int save_x (int x);
int resolve_polinomyal (int *array, int x, int i);
int main()
{
int array[COEF], x = 1, i = 0, ordered_array[COEF];
printf("Please, enter 5 coefficients:\n");
save_coef (i, array);
order_coef (i, array, ordered_array);
for (i = 0; i < COEF; ++i)
printf("\n%d", ordered_array[i]);
printf("\nPlease, enter the value of x:\n");
save_x (x);
printf("%d", resolve_polinomyal (array, x, i));
return 0;
}
int save_coef (int i, int *array)
{
for (i = 0; i < COEF; ++i)
{
scanf("%d", &array[i]);
}
return 0;
}
int order_coef (int i, int *array, int *ordered_array)
{
int j, a;
for (i = 0; i < COEF; ++i)
{
for (j = i + 1; j < COEF; ++j)
{
if (array[i] > array[j])
{
a = array[i];
array[i] = array[j];
array[j] = a;
}
}
}
for (i = 0; i < COEF; ++i)
ordered_array[i] = array[i];
return 0;
}
int save_x (int x)
{
scanf("%d", &x);
return 0;
}
int resolve_polinomyal (int *array, int x, int i)
{
for (i = 5-1; i > 0; --i)
{
array[i-1] = array[i] * x + array[i-1];
}
return array[0];
}
What my program does is that it takes an array of numbers that were read in from a file and sorts them with the selection sort and bubble sort methods. When it's sorted via the bubble sort method, the array lists one number twice in a row. It's also always the second number that gets copied. I checked to see if for whatever reason the number was actually getting passed to the new array twice, but it isn't. I also tried a different input file, same thing happens in the same place.This also cuts off the last number in the list. I don't see anything obvious in my code that's causing that to happen.
The list that the program calls is as follows:
10
50
78
83
92
100
0
4
72
3
19
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int findMin(int arr[], int start, int size);
void printArr(int arr[], int size);
void selectionSort(int arr[], int size);
void bubbleSort(int arr[], int size);
int main(int argc, char *argv[])
{
if(argc != 2)
{
printf("Syntax Error: ./<exec> <infile>\n");
exit(1);
}
FILE *ifp = NULL;
ifp = fopen(argv[1], "r");
if(ifp == NULL)
{
printf("Could not open %s for reading\n", argv[1]);
exit(1);
}
int counter;
int j = 0;
fscanf(ifp, "%d", &counter);
int array[counter];
int arrB[counter];
for(j = 0; j < counter; ++j)
{
fscanf(ifp, "%d", &array[j]);
}
for(j = 0; j < counter; j++)
{
arrB[j] = array[j];
}
int size = sizeof(array) / sizeof(int);
printf("Before: ");
printArr(array, size);
selectionSort(array, size);
bubbleSort(arrB, size);
fclose(ifp);
return 0;
}
int findMin(int arr[], int start, int size)
{
int i = 0;
int minLoc = start;
int minVal = arr[minLoc];
for ( i = start + 1; i < size; ++i)
{
if (arr[i] < minVal)
{
minVal = arr[i];
minLoc = i;
}
}
return minLoc;
}
void printArr(int arr[], int size)
{
int i = 0;
for(i = 0; i < size; ++i)
printf("%3d ", arr[i]);
printf("\n");
}
void selectionSort(int arr[], int size)
{
int i = 0;
int minLoc = 0;
int tmp = 0;
for(i = 0; i < size; ++i)
{
minLoc = findMin(arr, i, size);
tmp = arr[i];
arr[i] = arr[minLoc];
arr[minLoc] = tmp;
}
printf("** Selection Sort **\n After: ");
printArr(arr, size);
}
void bubbleSort(int arr[], int size)
{
int i = 0;
int j = 0;
int tmp = 0;
for(j = 0; j < size; j++)
{
for(i = 0; i < size; ++i)
{
if(arr[i] > arr[i+1])
{
tmp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = tmp;
}
}
}
printf("** Bubble Sort **\n After: ");
printArr(arr, size);
}
bubbleSort() accesses outside array bounds. Thus undefined behavior (UB).
for(j = 0; j < size; j++) {
for(i = 0; i < size; ++i) {
if(arr[i] > arr[i+1]) { // Here code access outside bounds when i = size - 1
... Code swaps arr[i], arr[i+1];
}
}
}
Instead
for(j = 0; j < size; j++) {
for(i = 1; i < size; ++i) {
if(arr[i-1] > arr[i]) {
... Code swaps arr[i-1], arr[i];
}
}
}