I'm having some trouble with my recursive division generated maze. I know there's something wrong about the recursion, but I just can't find a way to solve it.
I also know the code is really big and not optimized at all, but I'm working better on this issue later.
The red maze is what I'm generating and the purple one is what I'm trying to do:
What should I be doing to correct that?
Tnks.
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#define MAX 41
#define FUNDO 0
#define PAREDE 1
#define SAIDA 2
#define SOLUCAO 3
int num_aleat(int min, int max)
{
return rand()%(max-min+1)+min;
}
void paredes(int m[][MAX], int FINAL_i, int FINAL_j, int INICIO_i, int INICIO_j)
{
int i=0, j=0;
for(i=0; i<MAX; i++)
{
if (i!=FINAL_i && i!=INICIO_i)
m[i][j]=PAREDE;
}
i=0;
for(j=0; j<MAX; j++)
{
if (j!=INICIO_j)
m[i][j]=PAREDE;
else
m[i][j]=SAIDA;
}
j=0;
for(i=0; i<MAX; i++)
{
if (j!=FINAL_j && j!=INICIO_j)
m[i][j+MAX-1]=PAREDE;
}
i=0;
for(j=0; j<MAX; j++)
{
if (j!=FINAL_j)
m[i+MAX-1][j]=PAREDE;
else
m[i+MAX-1][j]=SAIDA;
}
j=0;
}
void pardede_gener(int m[][MAX], int min, int max, int x, int dir)
{
int i=0, passagem=0;
switch (dir)
{
case 1:
passagem=num_aleat(min, max);
printf("\nc\n");
for(i=min; i<=max; i++)
{
if(i!=passagem)
m[i][x]=PAREDE;
printf("\nd_%i_%i\n", i, x);
}
return;
break;
case 2:
passagem=num_aleat(min, max);
for(i=min; i<=max; i++)
{
if(i!=passagem)
m[x][i]=PAREDE;
}
return;
break;
}
return;
}
void div(int m[][MAX], int i, int j, int max_i, int max_j, int dir)
{
int parede_i=MAX, parede_j=MAX, flag=0;
if(dir==1)
{
while(1)
{
parede_j=num_aleat(j, max_j);
if (parede_j%2==0 && m[i+1][parede_j]==FUNDO && m[i+1][parede_j-1]!=PAREDE && m[i+1][parede_j+1]!=PAREDE || m[i+2][parede_j]!=PAREDE)
break;
else
break;
}
if(m[i+1][parede_j]!=PAREDE && m[i+1][parede_j-1]!=PAREDE && m[i+1][parede_j+1]!=PAREDE)
{
pardede_gener(m, i+1, max_i-1, parede_j, dir);
dir=2;
div(m, i, j, max_i, parede_j, dir);
div(m, i, parede_j, max_i, max_j, dir);
}
else
return;
}
else if(dir==2)
{
while(1)
{
parede_i=num_aleat(j, max_j);
if (parede_i%2==0 && m[parede_i][j+1]==FUNDO && m[parede_i-1][j+1]!=PAREDE && m[parede_i+1][j+1]!=PAREDE || m[parede_i][j+2]==PAREDE)
break;
else
break;
}
if(m[parede_i][j+1]!=PAREDE && m[parede_i-1][j+1]!=PAREDE && m[parede_i+1][j+1]!=PAREDE)
{
pardede_gener(m, j, max_j-1, parede_i-1, dir);
dir=1;
div(m, i, j, parede_i, max_j, dir);
div(m, parede_i, j, max_i, max_j, dir);
}
else
return;
}
}
bool resolve(int *m, int i, int j, int fi, int fj)
{
*(m + i*MAX + j)=SOLUCAO;
if(i==fi-1 && j==fj)
return true;
if (i+1<MAX && *(m + (i+1)*MAX + j)==FUNDO)
{
if(resolve(m, i+1, j, fi, fj))
return true;
}
if (i-1>=0 && *(m + (i-1)*MAX + j)==FUNDO)
{
if(resolve(m, i-1, j, fi, fj))
return true;
}
if (j+1<MAX && *(m + i*MAX + (j+1))==FUNDO)
{
if(resolve(m, i, j+1, fi, fj))
return true;
}
if (j-1>=0 && *(m + i*MAX + (j-1))==FUNDO)
{
if(resolve(m, i, j-1, fi, fj))
return true;
}
*(m + i*MAX + j)=FUNDO;
return false;
}
int main()
{
int lab[MAX][MAX];
int FINAL_i=0, FINAL_j=0, INICIO_i=0, INICIO_j=0, i=0, j=0;
srand(time(NULL));
FINAL_i=MAX-1;
while (INICIO_j%2==0 || FINAL_j%2==0)
{
INICIO_j=rand()%(MAX-2)+1;
FINAL_j=rand()%(MAX-2)+1;
}
zero(lab);
paredes(lab, FINAL_i, FINAL_j, INICIO_i, INICIO_j);
div(lab, 0, 0, MAX-1, MAX-1, 1);
// resolve(*lab, 1, INICIO_j, FINAL_i, FINAL_j);
return 0;
}
Related
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;
}
In this code I try to create three arrays. With these three arrays I successflly tried to make the union. But when I want to make an Intersection and the difference I couldn't.
#include <stdio.h>
int main()
{
int dizi1Sinir, dizi1Deger; // ilk alt kumenin degiskenleri
int dizi2Sinir, dizi2Deger; // ikinci alt kumenin degiskenleri
int dizi3Sinir, dizi3Deger; // ucuncu alt kumenin degiskenleri
// BIRINCI ALT KUME TANIMLAMA ISLEMLERI
printf("ilk alt kumen kac elemanli olsun?\n");
scanf("%d", &dizi1Sinir);
int alt_kume_1[dizi1Sinir]; // ilk alt kume
for(dizi1Deger = 0; dizi1Deger < dizi1Sinir; dizi1Deger++)
{
printf("dizinin elemanlarini gir.\n %dnci eleman = ", dizi1Deger + 1);
scanf("%d", &alt_kume_1[dizi1Deger]);
}
// -------------------------------------------------------------------------------
//IKINCI ALT KUME TANIMLAMA ISLEMLERI
printf("ikinci alt kumen kac elemanlı olsun?\n");
scanf("%d", &dizi2Sinir);
int alt_kume_2[dizi2Sinir]; // ikinci alt kume
for(dizi2Deger = 0; dizi2Deger < dizi2Sinir; dizi2Deger++)
{
printf("dizinin elemanlarini gir.\n %dnci eleman = ", dizi2Deger + 1);
scanf("%d", &alt_kume_2[dizi2Deger]);
}
//UCUNCU ALT KUME TANIMLAMA ISLEMLERI
printf("ucuncu alt kumen kac elemanli olsun?\n");
scanf("%d", &dizi3Sinir);
int alt_kume_3[dizi3Sinir]; // ucuncu alt kume
for(dizi3Deger = 0; dizi3Deger < dizi3Sinir; dizi3Deger++)
{
printf("dizinin elemanlarini gir.\n %dnci eleman = ", dizi3Deger + 1);
scanf("%d", &alt_kume_3[dizi3Deger]);
}
//---------------------------------
int azami=dizi1Sinir+dizi2Sinir+dizi3Sinir;
int birlesim[azami];
int i;
for(i=0;i<azami;i++)
{
if(i<dizi1Sinir)
birlesim[i]=alt_kume_1[i];
else if(i-dizi1Sinir<dizi2Sinir)
birlesim[i]=alt_kume_2[i-dizi1Sinir];
else
birlesim[i]=alt_kume_3[i-dizi1Sinir-dizi2Sinir];
}
//-----------------------------
//Ayni Elemanlari Sil (-1)
for(i=0;i<azami;i++)
{
int tempSayi=birlesim[i];
int j;
for(j = 0;j<i;j++)
{
if(birlesim[j]==tempSayi)
{
birlesim[i]=-1;
}
}
}
printf("\nbirlesim = ");
for(i = 0; i < azami; i++)
{
if(birlesim[i]!=-1)
printf("%d ", birlesim[i]);
}
int main() {
//************************
printf("\ninsertion dizi_1 and dizi_2 : ");
intersection_two_arrays(alt_kume_1,dizi1Sinir,alt_kume_2,dizi2Sinir);
printf("\ninsertion dizi_1 and dizi_3 : ");
intersection_two_arrays(alt_kume_1,dizi1Sinir,alt_kume_3,dizi3Sinir);
printf("\ninsertion dizi_2 and dizi_3 : ");
intersection_two_arrays(alt_kume_2,dizi2Sinir,alt_kume_3,dizi3Sinir);
printf("\ninsertion dizi_1 dizi_2 and dizi_3 : ");
intersection_three_arrays(alt_kume_1,dizi1Sinir,alt_kume_2,dizi2Sinir,alt_kume_3,dizi3Sinir);
//...etc
//*************************
printf("\nDifference dizi_1 - dizi_2 : ");
differenceX_Y(alt_kume_1,dizi1Sinir,alt_kume_2,dizi2Sinir);
printf("\nDifference dizi_2 - dizi_1 : ");
differenceX_Y(alt_kume_2,dizi2Sinir,alt_kume_1,dizi1Sinir);
printf("\nDifference dizi_2 - dizi_3 : ");
differenceX_Y(alt_kume_2,dizi2Sinir,alt_kume_3,dizi3Sinir);
printf("\nDifference dizi_1 - dizi_2 and dizi_3 : ");
differenceX_YandZ(alt_kume_1,dizi1Sinir,alt_kume_2,dizi2Sinir,alt_kume_3,dizi3Sinir);
//...etc
//*************************
}
function 1 : Intersection of two sets
void intersection_two_arrays(int kume_1[], int sinir1 , int kume_2[] , int sinir2)
{
int i,j;
int value;
if(sinir1>sinir2) value=sinir2;
else value=sinir1;
int hold[value];
int h=0;
for(i=0; i<sinir1; i++) {
for(j=0; j<sinir2; j++) {
if(kume_1[i] == kume_2[j]) {
hold[h++] = kume_1[i];
continue;
}
}
}
if(h==0) {
printf("There is not common value");
}
else {
for(i=0; i<h; i++) {
printf("%d ", hold[i]);
}
}
}
function 2 : Intersection of three sets
void intersection_three_arrays(int kume_1[], int sinir1 , int kume_2[] , int sinir2,
int kume_3[], int sinir3)
{
int i,j;
int value;
if(sinir1>sinir2) value=sinir2;
else value=sinir1;
int hold[value];
int h=0;
for(i=0; i<sinir1; i++) {
for(j=0; j<sinir2; j++) {
if(kume_1[i] == kume_2[j]) {
hold[h++] = kume_1[i];
continue;
}
}
}
intersection_two_arrays(hold,h, kume_3,sinir3);
}
function 3 : Difference X-Y
void differenceX_Y(int kume_1[], int sinir1 , int kume_2[] , int sinir2)
{
int i,j;
int value;
if(sinir1>sinir2) value=sinir1;
else value=sinir2;
int hold[value];
int h=0;
int findEqual=0;
for(i=0; i<sinir1; i++) {
for(j=0; j<sinir2; j++) {
if(kume_1[i] == kume_2[j]) {
findEqual++;
continue;
}
}
if(findEqual==0) hold[h++] = kume_1[i];
findEqual=0;
}
if(h==0) {
printf("There is not different value.");
}
else {
for(i=0; i<h; i++) {
printf("%d ", hold[i]);
}
}
}
function 4 : Difference X-(Y and Z)
void differenceX_YandZ(int kume_1[], int sinir1 , int kume_2[] , int sinir2, int
kume_3[], int sinir3)
{
int i,j;
int value;
if(sinir1>sinir2) value=sinir1;
else value=sinir2;
int hold[value];
int h=0;
int findEqual=0;
for(i=0; i<sinir1; i++) {
for(j=0; j<sinir2; j++) {
if(kume_1[i] == kume_2[j]) {
findEqual++;
continue;
}
}
if(findEqual==0) hold[h++] = kume_1[i];
findEqual=0;
}
differenceX_Y(hold, h , kume_3, sinir3);
}
I wrote a C code for a Game of Life simulation. My program works perfectly fine for case 1 which is my random array. But it doesn't work when I try to load an array out of a .txt file. I mean it gives me an output but it doesn't output an array with '*' or ' ' it gives me this:
Does someone know what my mistake is?
Here´s my code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <Windows.h>
#define ED 25
#define ZD 25
// - FUNCTIONS
void print_matr(char matr[ED][ZD], int n, int m);
void create_matr(char matr[ED][ZD], int n, int m, int *alz, int prozent);
void check_neighbours(char matr[ED][ZD], int n, int m, char lager[ED][ZD], int *alz);
void ausfuehren(char matr[ED][ZD], int n, int m, char lager[ED][ZD], int *alz);
void load_matr(char matr[ED][ZD], int n, int m, int filenr, int *alz);
int Menu(char *text, int anz, char *prompt);
int check_rules(char matr[ED][ZD], int n, int m, char lager[ED][ZD], int lx, int ly, int alz);
int Einlesen(char *prompt, int min, int max, int versuche);
// -MAIN-
int main() {
const int n = ED; const int m = ZD; //1.Dim/2.Dim
char matr[ED][ZD];
char lager[ED][ZD];
int alz; //Anzahl lebender Zellen
int prozent;
switch (Menu("\n1 - random\n2 - load array 1\n3 - load array 2\n4 - load array 3\n\n0 - exit", 5, "\n >>>> ")) {
case 0: exit(0); break;
case 1:
prozent = Einlesen("Percentage? ", 0, 100, 5);
create_matr(matr, n, m, &alz, prozent);
print_matr(matr, n, m);
ausfuehren(matr, n, m, lager, &alz);
break;
case 2:
load_matr(matr, n, m, 1,&alz);
print_matr(matr, n, m);
ausfuehren(matr, n, m, lager, &alz); break;
case 3:
load_matr(matr, n, m, 2,&alz);
print_matr(matr, n, m);
ausfuehren(matr, n, m, lager, &alz); break;
case 4:
load_matr(matr, n, m, 3, &alz);
print_matr(matr, n, m);
ausfuehren(matr, n, m, lager, &alz);
break;
}
return 0;
}
// -PRINT
void print_matr(char matr[ED][ZD], int n, int m) {
for (int lx = 0; lx < n; lx++) {
for (int ly = 0; ly < m; ly++) {
printf(" %c", matr[lx][ly]);
}
printf("\n");
}
printf("\n\n");
}
int Einlesen(char *prompt, int min, int max, int versuche) {
int zahl;
fflush(stdin);
if (!versuche) {
exit(0);
}
printf(prompt);
if (!scanf("%d", &zahl)) {
printf(" *** Bitte Zahl eingeben! ***\n");
return Einlesen(prompt, min, max, versuche - 1);
}
if ((zahl<min) || (zahl>max)) {
printf(" *** [%d,%d]! ***\n", min, max);
return Einlesen(prompt, min, max, versuche - 1);
}
return zahl;
}
int Menu(char *text, int anz, char *prompt) {
printf(text);
return Einlesen(prompt, 0, anz, 5);
}
void create_matr(char matr[ED][ZD], int n, int m, int *alz, int prozent) {
for (int ln = 0; ln < n; ln++) {
for (int lm = 0; lm < m; lm++) {
if (rand()%100 <= prozent) {
matr[ln][lm] = '*';
(*alz)++;
}
else { matr[ln][lm] = ' ';}
}
}
}
int check_rules(char matr[ED][ZD], int n, int m, char lager[ED][ZD], int lx, int ly, int l_anzlz) {
//Rule 1
if (matr[lx][ly] == '*') {
if ((l_anzlz == 0) || (l_anzlz == 1) || (l_anzlz > 3)) {
lager[lx][ly] = ' ';
}
//Rule 2
else if ((l_anzlz == 2) || (l_anzlz == 3)) {
lager[lx][ly] = '*';
}
}
//Rule 3
else if (matr[lx][ly] == ' ') {
if (l_anzlz == 3) {
lager[lx][ly] = '*';
}
else lager[lx][ly] = ' ';
}
return 1;
}
void check_neighbours(char matr[ED][ZD], int n, int m, char lager[ED][ZD], int *alz) {
int counteq = 0;
int l_anzlz;
(*alz)=0;
for(int lx = 0; lx < n; lx++) {
for(int ly = 0; ly < m; ly++) {
l_anzlz = 0;
for(int la = -1; la <= 1; la++) {
for(int lb = -1; lb <= 1; lb++) {
if((la==0)&&(lb==0)) continue;
if((lx==0)&&(la==-1)) continue;
if((lx==n-1)&&(la==1)) continue;
if((ly==0)&&(lb==-1)) continue;
if((ly==m-1)&&(lb==1)) continue;
if (matr[lx + la][ly + lb] == '*') {
l_anzlz++;
}
}
}
check_rules(matr, n, m, lager, lx, ly, l_anzlz);
}
}
//printf("lager\n");
//print_matr(lager, n, m);
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++) {
if (matr[i][j] == lager[i][j]) {
counteq++;
}
matr[i][j] = lager[i][j];
if (matr[i][j] == '*') (*alz)++;
}
}
if (counteq == (n*m)) (*alz)=0;
}
// -EXECUTE
void ausfuehren(char matr[ED][ZD], int n, int m, char lager[ED][ZD], int *alz) {
char eing;
int fors;
fors = Einlesen(" [0] or [1]?\n", 0, 1, 2);
if(fors==1) printf(" 'q' to end \n");
while ((*alz) > 0) {
check_neighbours(matr, n, m, lager, alz); /
print_matr(matr, n, m);
if (fors == 0) {
Sleep(300);
}
else if (fors == 1) {
fflush(stdin);
scanf("%c", &eing);
if(eing=='q') break;
}
}
}
void load_matr(char matr[ED][ZD], int n, int m, int filenr, int *alz) {
FILE *fp;
char c[4];
(*alz)=0;
if (filenr == 1) {
fp = fopen("matrix1.txt", "rt");
}
else if (filenr == 2) {
fp = fopen("matrix2.txt", "rt");
}
else if (filenr == 3) {
fp = fopen("matrix3.txt", "rt");
}
if(fp==0){
printf("Couldnt open File!");
exit(-1);
}
if(fgets(c, 5, fp)==NULL){
printf("Error\n");
exit(-1);
}
n=(int)(*c);
if(fgets(c, 5, fp)==NULL){
printf("Error\n");
exit(-1);
}
m=(int)(*c);
for (int lx = 0; lx < n; lx++) {
for (int ly = 0; ly < m; ly++) {
int star = fgetc(fp);
if (star == 42) { //42=* | ASCII
matr[lx][ly] = '*';
(*alz)++;
}
else if (star == 32) { //32= | ASCII
matr[lx][ly] = ' ';
}
else if (star == 10) { //10=\n | ASCII
lx--; ly = m - 1;
}
else break; //EOF
}
}
fclose(fp);
}
Would appreciate any help!
Thanks!
Your problem is into the load_matr function:
This is a possible solution, that make it works when the input file is properly formatted (it is not a solution fault tolerant), and many bad things, but it works.
void clear_matr(char matr[ED][ZD])
{
for (int row = 0; row < ED; row++)
for (int col = 0; col < ZD; col++)
matr[row][col] = ' ';
}
void load_matr(char matr[ED][ZD], int n, int m, int filenr, int *alz) {
FILE *fp;
char c[16];
(*alz)=0;
memset(c, 0, sizeof(c));
if (filenr == 1) {
fp = fopen("matrix1.txt", "rt");
}
else if (filenr == 2) {
fp = fopen("matrix2.txt", "rt");
}
else if (filenr == 3) {
fp = fopen("matrix3.txt", "rt");
}
if (fp == 0) {
printf("Couldnt open File!");
exit(-1);
}
if (fgets(c, 5, fp) == NULL) {
printf("Error\n");
exit(-1);
}
n = atoi(c);
if (fgets(c, 5, fp) == NULL) {
printf("Error\n");
exit(-1);
}
m = atoi(c);
clear_matr(matr);
for (int ly = 0; ly < m; ly++) {
for (int lx = 0; lx < n; lx++) {
int star = fgetc(fp);
if (star == '\n')
star = fgetc(fp);
if (star == 42) { //42=* | ASCII
matr[ly][lx] = '*';
(*alz)++;
}
else if (star == 32) { //32= | ASCII
matr[ly][lx] = ' ';
}
else break; //EOF
}
}
fclose(fp);
}
I have coded a 16*16 sudoku solver using the backtracking algorithm in c. The program takes input from the user using a text file. The unfilled positions in the matrix are filled with a 0. Works perfectly fine.
I want to further optimize the code for time and space complexity. Please suggest some methods or ways to optimize the code.
#include <stdio.h>
#include<time.h>
#define N 16
int isAvailable(int sudoku[16][16], int row, int col, int num)
{
int i, j;
for(i=0; i<16; i++){
if( (sudoku[row][i] == num) || ( sudoku[i][col] == num ) )
return 0;
}
int rowStart = (row/4) * 4;
int colStart = (col/4) * 4;
for(i=rowStart; i<(rowStart+4); ++i)
{
for(j=colStart; j<(colStart+4); ++j)
{
if( sudoku[i][j] == num )
return 0;
}
}
return 1;
}
int fillsudoku(int sudoku[16][16], int row, int col)
{
int i;
if( row<16 && col<16 )
{
if( sudoku[row][col] != 0 )
{
if( (col+1)<16 )
return fillsudoku(sudoku, row, col+1);
else if( (row+1)<16 )
return fillsudoku(sudoku, row+1, 0);
else
return 1;
}
else
{
for(i=0; i<16; ++i)
{
if( isAvailable(sudoku, row, col, i+1) )
{
sudoku[row][col] = i+1;
if( (col+1)<16 )
{
if( fillsudoku(sudoku, row, col+1) )
return 1;
else
sudoku[row][col] = 0;
}
else if( (row+1)<16 )
{
if( fillsudoku(sudoku, row+1, 0) )
return 1;
else
sudoku[row][col] = 0;
}
else
return 1;
}
}
}
return 0;
}
else
{
return 1;
}
}
int main()
{
clock_t tic=clock();
int a,i,j;
int sudoku[N][N];
FILE *f;
f=fopen("s16.txt","r");
for(i=0;i<N;i++){
for(j=0;j<N;j++)
{
fscanf(f,"%d",&sudoku[i][j]);
}
}
if( fillsudoku(sudoku, 0, 0) )
{
for(i=0; i<16; ++i)
{
for(j=0; j<16; ++j){
printf("%d ", sudoku[i][j]);
if(sudoku[i][j]<10){
printf(" ");
}
if(j%4==3){
printf("|");
}
}
printf("\n");
if(i%4==3){
printf("----------------------------------------------------");
}
printf("\n");
}
}
else
{
printf("\n\nNO SOLUTION\n\n");
}
clock_t toc=clock();
printf("\nTotal time elasped is %f", (double)(toc - tic) / CLOCKS_PER_SEC);
return 0;
fclose(f);
}
I am trying to create program to add two matrixes. After typing input like [12 4] program crashes when function strcat starts.
I have no idea what is wrong. func.h consists of stdio.h, _mingw.h,stdlib.h and string.h
#include"func.h"
void string_to_table(double **matrix, char inpt[])/*problematic function*/
{
printf("convertion start");
int i=strlen(inpt);
printf("%i",i);
int j=1;
int k=0,l=0;
char num[128];
double converted=0;
printf("breakpoint1");
while(j<(i-1))
{
if(inpt[j]==' ')
{
printf("first if");
converted=atof(num);
num[0]='\0';
matrix[k][l]=converted;
++l;
printf("breakpoint2");
}
else if(inpt[j]==';')
{
printf("second if");
converted=atof(num);
num[0]='\0';
matrix[k][l]=converted;
++k;
l=0;
}
else
{
printf("third if");
strcat(num,inpt[j]);/*place when everything crashes*/
}
++j;
}
printf("convert0 end");
}
void add_matrix(double **matrix1, double **matrix2,int i,int j)
{
int k=0;
int l=0;
while(k<i)
{
while(l<j)
{
matrix1[k][l]+=matrix2[k][l];
++l;
}
l=0;
++k;
}
int matrixproccesing(int *i,int *j, char m[])/*sprawdzanie poprawnosci wejscia*/
{
printf("macro start");
int columnnum=0,rownum=0,x=0,piv=0,check=0;
int textsize=strlen(m);
printf("%i",i);
printf("loop start");
printf("%i",textsize);
while(x<(textsize-1))
{
printf("%i",x);
printf("\n");
if(x==0)/*czy poczatek to [*/
{
if(m[x]!='[')
return 0;
}
else if(x==(textsize-2))/*czy koniec to]*/
{
printf("kohec");
if(m[x]==']')
break;
return 0;
}
else if((m[x]>47&&m[x]<58)||(m[x]==' ')||(m[x]=='.')||(m[x]==';')||(m[x]=='-'))/*czy liczba*/
{
if(m[x]==';')/*czy ilosc liczb w rzedzie taka sama*/
{
if(check==0)
{
check=columnnum;
}
else if(check!=columnnum)
{
return 0;
}
printf("colnum");
columnnum=0;
rownum++;
}
else if(m[x]==' ')/*czy nowa liczba/kolumna */
{
columnnum++;
}
}
++x;
}
*i=(check+1);
*j=(columnnum+1);
printf("macro end");
return 1;
}
int is_same_size(int a, int b,int c ,int d)/*test rozmiaru*/
{
if((a==c)&(b==d))
return 1;
return 0;
}
void print_da_matrix(double **matrix, int i, int j)
{
int k=0,l=0;
printf("[ ");
while(k<i)
{
while(l<j)
{
printf("%f",matrix[k][l]);
printf(" ");
}
printf(";");
l=0;
if(k<(i-1))
++k;
}
printf("]");
}
void release_the_memory(double **matrix, int i)
{
int k=0;
while(k<i)
{
free(matrix[k]);
++k;
}
free(matrix);
matrix=NULL;
}
}
int main()
{
int i=0,j=0,m1=0,m2=0,tabcr=0;
char matrix[512];
fgets(&matrix,511,stdin);
double **matrix1;
double **matrix2;
if(!matrixproccesing(&i,&j,matrix))
{
printf("zle wejscie");
return 0;
}
matrix1=(double**)malloc(i*sizeof(double *));
while(tabcr<j)
{
matrix1[tabcr]=(double*)malloc(j*sizeof(double));
++tabcr;
}
string_to_table(matrix1,matrix);
printf("\n");
printf("podaj druga macierz");
fgets(&matrix,511,stdin);
if(!matrixproccesing(&m1,&m2,matrix))
{
printf("zle wejscie");
return 0;
}
tabcr=0;
if(!is_same_size(i,j,m1,m2))
{
printf("matrixes have different size.");
return 0;
}
matrix2=(double**)malloc(i*sizeof(double *));
while(tabcr<j)
{
matrix2[tabcr]=(double*)malloc(j*sizeof(double));
++tabcr;
}
string_to_table(matrix2,matrix);
add_matrix(matrix1,matrix2,i,j);
/* print_da_matrix(matrix1,i,j);
release_the_memory(matrix1,i);
release_the_memory(matrix2,i);*/
return 0;
}