Value of Count pointer is not changing - c

I am passing the pointer of count in all function but its value does not change and remain 0
int binarysearch(int a[],int l,int r,int k,int *count)
{
int m = (r+l)/2;
if (r > l)
{
if (a[m] > k){
*count++;
return binarysearch(a,l,m-1,k,count);
}
if (a[m] < k)
{
*count++;
return binarysearch(a,m+1,r,k,count);
}
if (a[m] == k)
{
*count++;
return m;
}
}
if (r <= l)
{
*count++;
if (k>a[l]) return l+1;
else return l;
}
}
void insertati (int a[],int i,int *count)
{
int t = binarysearch(a,0,i-1,a[i],count);
int y = a[i],j = i-1;
while (j >= t)
{
a[j+1] = a[j];
j--;
}
a[j+1] = y;
}
void insetionsort(int a[],int n,int *count)
{
for (int i = 1; i < n; ++i)
{
insertati(a,i,count);
}
}
int main()
{
int n,count =0;
scanf("%d",&n);
int arr[n];
for (int i = 0; i <n; ++i)
{
scanf("%d",&arr[i]);
}
insetionsort(arr,n,&count);
printf("%d\n",count);
for (int i = 0; i <n; ++i)
{
printf("%d ",arr[i]);
}
return 0;
}

Surround dereferencing with parentheses:
int binarysearch(int a[],int l,int r,int k,int *count) {
int m = (r+l)/2;
if (r > l)
{
if (a[m] > k){
(*count)++;
return binarysearch(a,l,m-1,k,count);
}
if (a[m] < k)
{
(*count)++;
return binarysearch(a,m+1,r,k,count);
}
if (a[m] == k)
{
(*count)++;
return m;
}
}
if (r <= l)
{
(*count)++;
if (k>a[l]) return l+1;
else return l;
}
}

Related

design an algorithm to determine whether there exists such a key which is equal to the summation of other two keys in the array

i'm trying to solve this problem: given an array containing n keys determine whether there exists such a key that is equal to sum of other two keys in array. if yes, print them out.
I'm using mergesort to sort the array and then checking for keys. but (for loop) inside summation function somehow fails to increment every time. i've tried (while loop) and several other ways. nothing works. any ideas?
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
void merge_sort(int input_array[], int first_element, int last_element);
void merge(int input_array[], int first_element, int middle_element,
int last_element);
void find_summation(int input_array[], int first_element, int last_element);
int total_elements;
int main() {
int input_array[100];
printf("\nEnter number of elements in the array : ");
scanf("%d", &total_elements);
int i = 0;
printf("\nEnter %d array elements: ", total_elements);
while (i < total_elements) {
scanf("%d", &input_array[i]);
i++;
}
merge_sort(input_array, 0, total_elements - 1);
printf("\nSorted Array: ");
for (int i = 0; i < total_elements; i++) {
printf("%d ", input_array[i]);
}
printf("\n");
find_summation(input_array, 0, total_elements - 1);
printf("\n");
return 0;
}
void find_summation(int input_array[], int first_element, int last_element) {
bool found;
last_element = total_elements - 1;
int j = 2;
int current_num;
for (int j = 2; j <= last_element;) {
current_num = input_array[j];
while ((first_element < last_element)) {
int a = input_array[first_element];
int b = input_array[last_element];
int summation = a + b;
printf("summation %d\n", summation);
if (summation == current_num) {
found = true;
} else if (summation > current_num) {
last_element--;
} else if (summation < current_num) {
first_element++;
}
if (found) {
printf("\nKey: %d > sum of Keys: %d & %d", current_num, a,
current_num - a);
break;
}
}
}
}
void merge(int input_array[], int first_element, int middle_element,
int last_element) {
int m = (middle_element - first_element) + 1;
int n = last_element - middle_element;
int left_array[m];
int right_array[n];
for (int i = 0; i < m; i++) {
left_array[i] = input_array[first_element + i];
}
for (int j = 0; j < n; j++) {
right_array[j] = input_array[(middle_element + 1) + j];
}
int i = 0, j = 0, k = 0;
k = first_element;
while (i < m && j < n) {
if (left_array[i] <= right_array[j]) {
input_array[k] = left_array[i++];
} else {
input_array[k] = right_array[j++];
}
k++;
}
while (i < m) {
input_array[k++] = left_array[i++];
}
while (j < n) {
input_array[k++] = right_array[j++];
}
}
void merge_sort(int input_array[], int first_element, int last_element) {
if (first_element < last_element) {
int middle_element = (first_element + last_element) / 2;
merge_sort(input_array, first_element, middle_element);
merge_sort(input_array, middle_element + 1, last_element);
merge(input_array, first_element, middle_element, last_element);
} else
return;
}
Thank you everyone for guidance and support. Finally got the code to work on all the cases. original code had a lot of mistakes. including control flow and logical errors. i have fixed the code now. solution is posted below:
Test Case: 18 23 4 35 99 67 198 20 38 55 2 19 487 11 40 10 13 27 22
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
void merge_sort(int input_array[], int first_element, int last_element);
void merge(int input_array[], int first_element, int middle_element,
int last_element);
void find_summation(int input_array[], int first_element, int last_element);
int binary_search(int input_array[], int first_element, int last_element,
int difference);
int main() {
int total_elements;
int input_array[100];
printf("\nEnter number of elements in the array : ");
scanf("%d", &total_elements);
int i = 0;
printf("\nEnter %d array elements: ", total_elements);
while (i < total_elements) {
scanf("%d", &input_array[i]);
i++;
}
merge_sort(input_array, 0, total_elements - 1);
printf("\nSorted Array: ");
for (int i = 0; i < total_elements; i++) {
printf("%d ", input_array[i]);
}
printf("\n");
find_summation(input_array, 0, total_elements);
printf("\n");
return 0;
}
void find_summation(int input_array[], int first_element, int last_element) {
for (int j = 0; j <= last_element; j++) {
int current_num = input_array[j];
// printf("current_num and j: %d %d\n", current_num, j);
for (int i = 0; i < j; i++) {
int element = input_array[i];
int element_found =
binary_search(input_array, first_element, j, (current_num - element));
if (element_found > 0) {
if (element != (current_num - element))
printf("\nKey: %d > sum of Keys: %d & %d", current_num, element,
current_num - element);
}
}
}
}
int binary_search(int input_array[], int first_element, int last_element,
int difference) {
bool found;
int current_num;
int middle_element = (first_element + last_element) / 2;
if ((last_element >= first_element) && (first_element < middle_element)) {
// int middle_element = (first_element + last_element) / 2;
/*
while ((last_element - first_element) <= 2) {
return binary_search(input_array, middle_element + 1, last_element,
difference);
}*/
if (input_array[middle_element] == difference) {
return middle_element;
}
if (input_array[middle_element] > difference) {
return binary_search(input_array, first_element, middle_element - 1,
difference);
}
return binary_search(input_array, middle_element + 1, last_element,
difference);
}
return -1;
}
void merge(int input_array[], int first_element, int middle_element,
int last_element) {
int m = (middle_element - first_element) + 1;
int n = last_element - middle_element;
int left_array[m];
int right_array[n];
for (int i = 0; i < m; i++) {
left_array[i] = input_array[first_element + i];
}
for (int j = 0; j < n; j++) {
right_array[j] = input_array[(middle_element + 1) + j];
}
int i = 0, j = 0, k = 0;
k = first_element;
while (i < m && j < n) {
if (left_array[i] <= right_array[j]) {
input_array[k] = left_array[i++];
} else {
input_array[k] = right_array[j++];
}
k++;
}
while (i < m) {
input_array[k++] = left_array[i++];
}
while (j < n) {
input_array[k++] = right_array[j++];
}
}
void merge_sort(int input_array[], int first_element, int last_element) {
if (first_element < last_element) {
int middle_element = (first_element + last_element) / 2;
merge_sort(input_array, first_element, middle_element);
merge_sort(input_array, middle_element + 1, last_element);
merge(input_array, first_element, middle_element, last_element);
} else
return;
}

Checking if an element is in the array or not infinite loop

I'm trying to see if a number is in the array or not in logn time but this function executes
else if(arr[(r-l)/2] < n)
all the time and it becomes an infinite loop. Why is that?
int exists(int n, int *arr, int l, int r){
if(l == r){
if(arr[l] == n){
return 1;
}
else{
return 0;
}
}
else if(arr[(r-l)/2] == n){
return 1;
}
else if(arr[(r-l)/2] > n){
return exists(n, arr, l, (r-l)/2);
}
else if(arr[(r-l)/2] < n){
return exists(n, arr, (r-l)/2, r);
}
}
int main(){
node *root = NULL;
int arr[5] = {1,2,3,4,5};
printf("%d", exists(5, arr, 0, 4));
}
well, if
else if (arr[(r-l)/2] == n) {
is being executed, then it is because
if (l == r) {
is false
It looks like you are trying to do a binary search, if so, your index computations are all wrong.
int exists(int n, int *arr, int l, int r) {
if (l <= r) {
int mid = (l + r) / 2;
if (arr[mid] == n) {
return 1;
} else if (arr[mid] > n) {
return exists(n, arr, l, mid - 1);
} else {
return exists(n, arr, mid + 1, r);
}
}
return 0;
}

Comparing sorting methods in C

I have to compare these 3 sorting functions (bubble, insertion, and selection), in their worst, best, and the random possible way to sort an array (when talking about the idea of numbers of comparison).
I built and ran the program. The compiler is working, everything seems fine. The problem is that after the cmd window appears, nothing happens. The program is not returning anything (neither 0 (because of return 0) or writing sth in the file). How can I fix it?
#include <stdio.h>
#include <stdlib.h>
int b[11000];
int w[11000];
int r[11000];
int n;
int comp;
int attr;
FILE*f;
//CREAREA SIRURILOR
//best, worst, random
void sir(int n)
{ int i;
for(i=0; i<n; i++)
{
b[i] = i;
r[i] = rand() % n;
w[i] = n-i-1;
}
}
//INSERTION SORT
void insertion (int a[1000])
{
int j, i, index;
comp = 0;
attr = 0;
for (i = 0; i < n; i++)
{
index = a[i];
j = i;
attr+=1;
while ((j > 0) && (a[j-1] > index))
{ comp++;
a[j] = a[j-1];
j = j-1;
attr+=1;
}
while ((j > 0) || (a[j-1] > index))
{ comp++;
}
a[j] = index;
attr++;
}
fprintf(f,"\t%d\t", comp);
fprintf(f,"\t%d\t", attr);
fprintf(f,"\t%d\t", attr+comp);
}
//Selection Sort
void selection (int a[1000])
{
int j, i, min, temp;
comp = 0;
attr = 0;
for (i = 0; i < n-1; i++)
{
min = i;
for (j = i+1; j < n; j++){
comp++;
if (a[j] < a[min])
min = j;
}
temp = a[i];
a[i] = a[min];
a[min] = temp;
attr+=3;
}
fprintf(f,"\t%d\t", comp);
fprintf(f,"\t%d\t", attr);
fprintf(f,"\t%d\t", attr+comp);
}
//Bubble Sort
void bubblesort (int array[2000])
{
int sorted;
int swap,d;
int c;
comp = 0;
attr = 0;
for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0; d < n - c - 1; d++)
{
comp++;
if (array[d] > array[d + 1])
{
swap = array[d];
array[d] = array[d + 1];
array[d + 1] = swap;
attr += 3;
}
}
}
fprintf(f,"\t %d\t", comp);
fprintf(f,"\t %d\t", attr);
fprintf(f,"\t%d\t", attr+comp);
}
void InsertionSort()
{
insertion(b);
insertion(r);
insertion(w);
}
void SelectionSort()
{
selection(b);
selection(r);
selection(w);
}
void BubbleSort()
{
bubblesort(b);
bubblesort(r);
bubblesort(w);
}
int main ()
{
f = fopen("iesire.txt","w+");
if(f == NULL)
{
printf("Error!");
exit(-1);
}
fprintf(f,"\n Bubble Sort\t\n");
fprintf(f,"\tBest\t\t\t\t\t\tAverage\t\t\t\t\t\tWorst\t\t\n");
fprintf(f,"\tComparatii\t\tAtribuiri\t\tComp+Atr\t\tComparatii\t\tAtribuiri\t\tComp+Atr\t\tComparatii\t\tAtribuiri\t\tComp+Atr\n");
for (n = 100; n<=10000; n+=500)
{
sir(n);
BubbleSort();
fprintf(f,"\n");
}
fprintf(f,"\n\n Selection Sort\t\n");
fprintf(f,"\tBest\t\t\t\t\t\tAverage\t\t\t\t\t\tWorst\t\t\n");
fprintf(f,"\tComparatii\t\tAtribuiri\t\tComp+Atr\t\tComparatii\t\tAtribuiri\t\tComp+Atr\t\tComparatii\t\tAtribuiri\t\tComp+Atr\n");
for (n = 100; n<=10000; n+=500)
{
sir(n);
InsertionSort();
fprintf(f,"\n");
}
fprintf(f,"\n\n Insertion Sort\t\n");
fprintf(f,"\tBest\t\t\t\t\t\tAverage\t\t\t\t\t\tWorst\t\t\n");
fprintf(f,"\tComparatii\t\tAtribuiri\t\tComp+Atr\t\tComparatii\t\tAtribuiri\t\tComp+Atr\t\tComparatii\t\tAtribuiri\t\tComp+Atr\n");
for (n = 100; n<=10000; n+=500)
{
sir(n);
SelectionSort();
fprintf(f,"\n");
}
fclose(f);
printf(" Result: 'iesire.txt'.");
return 0;
}

Find pset3 "\expected an exit code of 0, not 1"

all mighty community of CS50ers on StackOverflow!
I have coded the sort and search functions of pset3, find, but I just don't get why I get this message. It is as if in the while loop the first if were never executed.
Any hints?
#include <cs50.h>
bool search(int value, int values[], int n)
{
if (n < 1)
{
return false;
}
else
{
int start = 0, end = (n-1);
while (end >= start)
{
int median = (end - start) / 2;
if (median == value)
{
return true;
}
else if (values[median] < value)
{
start = values[median + 1];
}
else
{
end = values[median - 1];
}
}
return false;
}
}
void sort(int values[], int n)
{
int temp;
for (int i = 0; i < n; i++)
{
int smallest_index = i;
for (int j = i + 1; j < n; j++)
{
if (values[i] > values[j])
smallest_index = j;
}
temp = values[smallest_index];
values[smallest_index] = values[i];
values[i] = temp;
}
}
Click Here to See Error Message from Check50

C sudoku backtracking solver precheck function

This is my first post on StackOverflow, so I apologize if I'm doing something wrong. I'm relatively new to C, so I'm sure my code is fairly ugly and hacky in places, however the majority of the code does what I expect it to. I'm having trouble with a precheck method that I'm using to check a sudoku board before I begin feeding it through my solver-logic. I'm redirecting input from a text file with strings that look like
4.....8.5.34.........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4......
4.....8.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.5......
4.....8.5.3..........7......2.....6.....8.4......1...x...6.3.7.5..2.....1.4......
417369825632158947958724316825437169791586432346912758289643571573291684164875293
417369825632158947958724316825437169791586432346912758289643.71573291684164875293
Each string is (ideally) 81 characters with just digits 1-9 and '.'s. I parse a string into a temp char array, and then use the method fillBoard to transfer the chars in the temp array into a 2d int array. Once this is complete, I call my precheck method. If the filled board doesn't pass the row, column, and box checks, the precheck method returns a one, indicating that the puzzle is not solvable (meaning an error message should be printed and that the program should move on to the next string). For some reason, my precheck method is returning one even for strings that should be solvable. I'm not sure why this is. Any help would be appreciated. Thanks.
#include <stdio.h>
#include <string.h>
int alphaError = 0;
struct Point findEmpty(int board[9][9]);
int usedInBox(int board[9][9], int boxStartRow, int boxStartCol, int num);
int positionSafe(int board[9][9], int row, int col, int num);
int usedInCol(int board[9][9], int col, int num);
int usedInRow(int board[9][9], int row, int num);
int solvePuzzle(int board[9][9]);
int precheck(int board[9][9]);
int main()
{
char c;
int charCount = 0;
int i = 0;
char tempStr[100000];
int board[9][9];
while((fscanf(stdin, "%c", &c)) != EOF)
{
printf("%c", c);
if(c != '\n')
{
if(isalpha(c))
{
alphaError = 1;
}
tempStr[i] = c;
i++;
charCount++;
}
else
{
if(charCount != 81 || alphaError == 1)
{
printf("Error\n\n");
i = 0;
charCount = 0;
alphaError = 0;
}
else
{
fillBoard(board, tempStr);
printBoard(board);
if(precheck(board) == 1)
{
printf("Error\n\n");
}
else
{
if(solvePuzzle(board) == 1)
{
printBoard(board);
}
else
{
printf("No solution\n\n");
}
}
i = 0;
charCount = 0;
}
}
}
return 0;
}
struct Point
{
int x;
int y;
} point;
struct Point findEmpty(int board[9][9])
{
struct Point point1;
point1.x = -1;
point1.y = -1;
int row, col;
for(row = 0; row < 9; row++)
{
for(col = 0; col < 9; col++)
{
if(board[row][col] == 0)
{
point1.x = col;
point1.y = row;
}
}
}
return point1;
}
int usedInBox(int board[9][9], int boxStartRow, int boxStartCol, int num)
{
int row, col;
for(row = 0; row < 3; row++)
{
for(col = 0; col < 3; col++)
{
if(board[row + boxStartRow][col + boxStartCol] == num)
{
return 1;
}
}
}
return 0;
}
int positionSafe(int board[9][9], int row, int col, int num)
{
if((usedInRow(board, row, num)) == 0 && (usedInCol(board, col, num)) == 0 && (usedInBox(board, (row-row%3), (col-col%3), num)) == 0)
{
return 1;
}
else
{
return 0;
}
}
int usedInCol(int board[9][9], int col, int num)
{
int row;
for(row = 0; row < 9; row++)
{
if(board[row][col] == num)
{
return 1;
}
}
return 0;
}
int usedInRow(int board[9][9], int row, int num)
{
int col;
for(col = 0; col < 9; col++)
{
if(board[row][col] == num)
{
return 1;
}
}
return 0;
}
int solvePuzzle(int board[9][9])
{
int num;
struct Point point2;
point2 = findEmpty(board);
if(point2.x == -1)
{
return 1;
}
for(num = 1; num <= 9; num++)
{
if(positionSafe(board, point2.y, point2.x, num) == 1)
{
board[point2.y][point2.x] = num;
if(solvePuzzle(board) == 1)
{
return 1;
}
board[point2.y][point2.x] = 0;
}
}
return 0;
}
void printBoard(int board[9][9])
{
int i, j;
for (i = 0; i < 9; i++)
{
for (j = 0; j < 9; j++)
{
printf("%d", board[i][j]);
}
}
printf("\n\n");
}
void fillBoard(int board[9][9], char tempStr[100000])
{
int i, j;
int k = 0;
for(i = 0; i < 9; i++)
{
for(j = 0; j < 9; j++)
{
if(tempStr[k] == '.')
{
board[i][j] = 0;
}
else
{
board[i][j] = (tempStr[k] - '0');
}
k++;
}
}
}
int precheck(int board[9][9])
{
int i, j, num;
for(i = 0; i < 9; i++)
{
for(j = 0; j < 9; j++)
{
if(board[i][j] != 0)
{
num = board[i][j];
if(positionSafe(board, i, j, num) == 0)
{
return 1;
}
}
}
}
return 0;
}
So you are using precheck on an already filled board? That might be the problem because usedInCol, usedInRow and usedInBlock will return 1 if the value is already present. So precheck should be used only while filling the board, not after. It will always return 1 if you check values you take from the already filled board.

Resources