C error: expected declaration or statement at end of input - c

Alright, so from what I've seen, it seems these errors usually show up whenever there's a semicolon missing, or a misplaced curly-brace or something of that nature. I'm surely just missing it somehow, but I'm not able to find where the error is, or if it is due to an error of that sort. This error in pointing toward line 235, so the end of the last function.
This is the code
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAX 100
void display_menu();
int check_option(int option);
int check_size(int size);
void initialize_2Darray(int x[][MAX], int size);
void print_2Darray(int x[][MAX], int size);
void initialize_1Darray(int y[], int size);
void print_1Darray(int y[], int size);
int search_min(int x[][MAX], int r, int c, int size);
int count_match(int x[][MAX], int y[], int size, int r);
int closest_row(int x[][MAX], int y[], int size);
void sort_1Darray(int y[], int size);
void sort_2Darray(int x[][MAX], int size);
int main()
{
int size, r, c, option;
int exit = 0; //exits program when = 1
int x[MAX][MAX], y[MAX];
srand(time(NULL));
printf("Enter the size: ");
scanf("%d", &size);
while(check_size(size) == 0) {
printf("\nInvalid input, enter the size of the array again: ");
scanf("%d", &size);
}
while(exit != 6) {
initialize_2Darray(x, size);
initialize_1Darray(y, size);
display_menu();
scanf("%d", &option);
while(check_option(option) == 0) {
printf("Invalid option, enter again: ");
scanf("%d", &option);
}
//Search Min Operation
if(option == 1) {
print_2Darray(x, size);
printf("Enter the row: ");
scanf("%d", &r);
printf("\nEnter the col: ");
scanf("%d", &c);
printf("The smallest number present in row %d and col %d is %d", r, c, search_min(x, r, c, size));
}
//Count Matches Op.
else if(option == 2) {
printf("Count Matches Operation\n\n2D array\n");
print_2Darray(x, size);
printf("\n1D array\n");
print_1Darray(y, size);
printf("\nEnter the row: ");
scanf("%d", &r);
if(count_match(x, y, size, r) > 0)
printf("There are %d matches from 1D array present in 2D array", count_match(x, y, size, r));
else
printf("There are no numbers from 1D array present in 2D array");
}
//Closest Row Op.
else if(option == 3) {
printf("\nClosest Row Operation\n\n2D array\n");
print_2Darray(x, size);
printf("\n1D array\n");
print_1Darray(y, size);
printf("Row closest to the 1D array is row %d", closest_row(x, y, size));
}
//Sort 1D Array Op.
else if(option == 4) {
printf("Sort 1D Array Operation\n\n1D Array before sorting:\n");
print_1Darray(y, size);
sort_1Darray(y, size);
printf("\n\n1D Array after sorting:\n");
print_1Darray(y, size);
}
//Sort 2D Array Op.
else if(option == 5) {
printf("\nSort 2D Array Option\n\n2D Array before sorting:\n");
print_2Darray(x, size);
sort_2Darray(x, size);
printf("\n\n2D Array after sorting:\n");
print_2Darray(x, size);
}
//Exit
else if(option == 6)
exit = 6;
}
return 0;
}
void display_menu()
{
printf("\nArray operations, your options are:\n\n1: Search Min\n2: Count Matches\n3: Closest Row\n4: Sort 1D Array\n5: Sort 2D Array\n6: Exit\nEnter the operation you want to perform: ");
}
int check_option(int option)
{
if(option > 0 && option < 7)
return 1;
else
return 0;
}
int check_size(int size)
{
if(size > 0 && size <= 100)
return 1;
else
return 0;
}
void initialize_2Darray(int x[][MAX], int size)
{
int i, s; //counters
for(i=0; i < size; i++) {
for(s=0; s < size; s++) {
x[i][s] = rand()%10;
}
}
}
void print_2Darray(int x[][MAX], int size)
{
int i, s; //counters
for(i=0; i < size; i++) {
printf("\n");
for(s=0; s < size; s++) {
printf("%d ", x[i][s]);
}
}
printf("\n");
}
void initialize_1Darray(int y[], int size)
{
int i, r;
for(i=0; i < size; i++) {
r = rand()%10;
y[i] = r;
}
}
void print_1Darray(int y[], int size)
{
int i;
//Prints array values until (s)ize is reached
for(i=0; i < size; i++) {
printf("%d ", y[i]);
}
}
int search_min(int x[][MAX], int r, int c, int size)
{
int i, j; //counters
int min = 9;
for(i=0; i < size; i++) {
if(x[r][i] < min) {
min = x[r][i];
}
}
for(j=0; j < size; j++) {
if(x[j][c] < min) {
min = x[j][c];
}
}
return min;
}
int count_match(int x[][MAX], int y[], int size, int r)
{
int i, j, count;
for(i=0; i < size; i++) {
for(j=0; j < size; j++) {
if(y[i] == x[r][j])
count++;
}
return count;
}
int closest_row(int x[][MAX], int y[], int size)
{
int l, i, j; //counters
int sum = 0;
int dif = 0;
int row, totaldif; //best matching row & total dif
for(l=0; l < size; l++) {
for(i=0; i < size; i++) {
for(j=0; j < size; j++) {
dif = abs(y[j] - x[i][j]);
sum += dif;
}
if(sum < totaldif) {
totaldif = sum;
row = i;
}
sum = 0;
}
}
return row;
}
void sort_1Darray(int y[], int size)
{
int a, b, temp;
//Loops through the array, swapping values until in proper order of ascension
for(a = 0; a < size; a++) {
for(b = 0; b < size-1; b++) {
if(y[b] > y[b+1]) {
temp = y[b+1];
y[b+1] = y[b];
y[b] = temp;
}
}
}
}
void sort_2Darray(int x[][MAX], int size)
{
int a, b, c, temp;
//Loops through the array, swapping values until in proper order of ascension
for(c=0; c < size; c++) {
for(a = 0; a < size; a++) {
for(b = 0; b < size-1; b++) {
if(x[a][b] > x[a][b+1]) {
temp = x[a][b+1];
x[a][b+1] = x[a][b];
x[a][b] = temp;
}
}
}
}
}

Run your code through indent. It can help you find this sort of problem very quickly.
Here's the output from your code. As you can see, the problem is caused by a missing brace at the end of count_match():
int
count_match(int x[][MAX], int y[], int size, int r)
{
int i , j, count;
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
if (y[i] == x[r][j])
count++;
}
return count;
}
/*** CLOSING BRACE MISSING HERE ***/
int closest_row(int x[][MAX], int y[], int size){
int l , i, j;
//counters

Related

Permutation function print array but the array that returns is empty

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");
}
}

C programming - Sodoku solution (backtracking)

the Question is: to get from the user sodoku board and if there is a solution to print it, if not to print no solution!
the solution of the soduko: two identical numbers mmust not appear on the same line;
two identical numbers must not appear in the same colum.
I worte a program that works perfectly when I put the soduko board and the size (global parametes-as shown un my code) but when I tried to receive from the user it took so much time to run the solution and sometimes it didn't retun anything. I would like to understand why?!
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
int matrix[5][5] = {
{4,2,0,0,5},
{2,0,0,1,3},
{5,0,1,2,0},
{0,0,3,0,2},
{0,0,0,0,0},
};
void print_sudoku()
{
int i,j;
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
{
printf("%d ",matrix[i][j]);
}
printf("\n");
}
}
int number_unassigned(int *row, int *col)
{
int num_unassign = 0;
int i,j;
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
{
if(matrix[i][j] == 0)
{
*row = i;
*col = j;
num_unassign = 1;
return num_unassign;
}
}
}
return num_unassign;
}
int is_safe(int n, int r, int c)
{
int i;
for(i=0;i<SIZE;i++)
{
if(matrix[r][i] == n)
return 0;
}
for(i=0;i<SIZE;i++)
{
if(matrix[i][c] == n)
return 0;
}
return 1;
}
int solve_sudoku()
{
int row;
int col;
if(number_unassigned(&row, &col) == 0)
return 1;
int i;
for(i=1;i<=SIZE;i++)
{
if(is_safe(i, row, col))
{
matrix[row][col] = i;
if(solve_sudoku())
return 1;
matrix[row][col]=0;
}
}
return 0;
}
int main()
{
if (solve_sudoku())
print_sudoku();
else
printf("No solution!\n");
return 0;
}
and this is whe code that I used to ask the user to enter a sodoku board:
int** ReadSoduko(int n) {
int** matrix = (int**) malloc((sizeof(int*)) * n);
for(int i = 0; i < n; ++i) {
matrix[i] = malloc(sizeof(int) * n);
}
printf("\nEnter your soduko board:\n");
for(int i = 0; i < n; ++i) {
printf("Enter row [%d]: ", i);
for(int j = 0; j < n; ++j) {
scanf("%d", &matrix[i][j]);
}
}
return matrix;
}
I suspect your problem is related to one thing: #define SIZE that you are probably forgetting to update when reading dynamically. Since you use SIZE in your loops, if that is not the real size of your matrix, then it probably won't work. I've changed 2 lines and added 3 other (the lines with comments at the end). Try it now.
#include <stdio.h>
#include <stdlib.h>
int SIZE; //changed here
int** matrix; //changed here
void print_sudoku()
{
int i, j;
for (i = 0; i < SIZE; i++)
{
for (j = 0; j < SIZE; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int number_unassigned(int* row, int* col)
{
int num_unassign = 0;
int i, j;
for (i = 0; i < SIZE; i++)
{
for (j = 0; j < SIZE; j++)
{
if (matrix[i][j] == 0)
{
*row = i;
*col = j;
num_unassign = 1;
return num_unassign;
}
}
}
return num_unassign;
}
int is_safe(int n, int r, int c)
{
int i;
for (i = 0; i < SIZE; i++)
{
if (matrix[r][i] == n)
return 0;
}
for (i = 0; i < SIZE; i++)
{
if (matrix[i][c] == n)
return 0;
}
return 1;
}
int solve_sudoku()
{
int row;
int col;
if (number_unassigned(&row, &col) == 0)
return 1;
int i;
for (i = 1; i <= SIZE; i++)
{
if (is_safe(i, row, col))
{
matrix[row][col] = i;
if (solve_sudoku())
return 1;
matrix[row][col] = 0;
}
}
return 0;
}
int** ReadSoduko(int n) {
int** matrix = (int**)malloc((sizeof(int*)) * n);
for (int i = 0; i < n; ++i) {
matrix[i] = (int*) malloc(sizeof(int) * n);
}
printf("\nEnter your soduko board:\n");
for (int i = 0; i < n; ++i) {
printf("Enter row [%d]: ", i);
for (int j = 0; j < n; ++j) {
scanf("%d", &matrix[i][j]);
}
}
return matrix;
}
int main()
{
printf("Size of matrix: "); //added this
scanf("%d", &SIZE); //added this
matrix = ReadSoduko(SIZE); //added this
if (solve_sudoku())
print_sudoku();
else
printf("No solution!\n");
return 0;
}
And don't forget that the matrix that you have declared statically doesn't have a solution! I have tested the same matrix, just replacing the 2 in first line by a 0 and it worked here.

One dimensional array being passed into the main() function

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];
}

Array stores same number form a list of numbers twice

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];
}
}
}

C Program - pointer array multiplication

This program is supposed to take 2 arrays and perform the dot product on each of the elements in the array.
My program is fine if the index of n is less than 5; however, once the index of the array is greater than 5 only the first element in the first array is wrong ( I checked by adding a printf statement in the function). I don't know how to fix this bug.
#include <stdio.h>
void multi_vec(int *v1, int *v2, int *v3, int n);
int main(void)
{
int n, i;
int v1[n];
int v2[n];
int v3[n];
printf("Enter the length of the two vectors\n");
scanf("%d", &n);
printf("Enter numbers for the first array\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &v1[i]);
}
printf("Enter numbers for the second array\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &v2[i]);
}
multi_vec(v1, v2, v3, n);
for (i = 0; i < n; i++) {
printf("%d", v3[i]);
}
printf("\n");
return 0;
}
void multi_vec(int *v1, int *v2, int *v3, int n)
{
int i;
for (i = 0; i < n; i++) {
*(v3+i) = *(v1+i) * *(v2+i);
}
}
Correct code
#include <stdio.h>
void multi_vec(int *v1, int *v2, int *v3, int n);
int main(void)
{
int n, i;
printf("Enter the length of the two vectors\n");
scanf("%d", &n);
int v1[n],v2[n],v3[n]; //you didn't initialize n
printf("Enter numbers for the first array\n"); //printf statements had extra ',n'
for (i = 0; i < n; i++) {
scanf("%d", &v1[i]);
}
printf("Enter numbers for the second array\n"); //printf statements had extra ',n'
for (i = 0; i < n; i++) {
scanf("%d", &v2[i]);
}
multi_vec(v1, v2, v3, n);
for (i = 0; i < n; i++) {
printf("%d ", v3[i]);
}
printf("\n");
return 0;
}
void multi_vec(int *v1, int *v2, int *v3, int n)
{
int i;
for (i = 0; i < n; i++) {
*(v3+i) = *(v1+i) * *(v2+i);
}
}

Resources