Trying to find number of possible paths going through all points exactly once starting at the center of a 5x5 array. Diagonal movement is also allowed - arrays

#include <iostream>
using namespace std;
long long int acc = 0;
bool isValid(int x, int y, int m[5][5])
{
if(x > -1 && x < 5 && y > -1 && y < 5 && m[x][y] == 0)
{
return true;
}
else
{
return false;
}
}
void start(int x, int y, int m[5][5],int count)
{
if(isValid(x,y,m))
{
count++;
if(count == 25)
{
acc++;
}
else
{
m[x][y] = 1;
start(x+1,y,m,count);
start(x-1,y,m,count);
start(x,y+1,m,count);
start(x,y-1,m,count);
start(x+1,y+1,m,count);
start(x-1,y-1,m,count);
start(x+1,y-1,m,count);
start(x-1,y+1,m,count);
}
}
}
int main()
{
int map[5][5];
for(int i = 0; i < 5; i++)
{
for(int j = 0;j < 5; j++)
{
map[i][j] = 0;
}
}
start(2,2,map,0);
cout<<acc;
}
or another code which i tried
#include <iostream>
#include <vector>
using namespace std;
long long int acc = 0;
class Point
{
public:
int row;
int column;
int count;
int map[5][5];
};
vector<Point> pts;
bool isValid(Point *b)
{
if(b->column > -1 && b->column <5 && b->row > -1 && b->row < 5 && b->map[b->column][b->row] != 1)
{
return true;
}
else
{
return false;
}
}
void start(vector<Point> A)
{
if(A.size() == 0)
{
cout<<acc;
}
Point *temp = &A.back();
A.pop_back();
if(isValid(temp))
{
temp->map[temp->column][temp->row] = 1;
temp->count = temp->count + 1;
if(temp->count == 25)
{
acc++;
}
else
{
Point *p = new Point;
for(int i = 0; i < 5; i++)
{
for(int j = 0;j < 5; j++)
{
p->map[i][j] = temp->map[i][j];
}
}
p->column = temp->column + 1;
p->row = temp->row;
p->count = temp->count;
A.push_back(*p);
Point *q = new Point;
for(int i = 0; i < 5; i++)
{
for(int j = 0;j < 5; j++)
{
q->map[i][j] = temp->map[i][j];
}
}
q->column = temp->column - 1;
q->row = temp->row;
q->count = temp->count;
A.push_back(*q);
Point *r = new Point;
for(int i = 0; i < 5; i++)
{
for(int j = 0;j < 5; j++)
{
r->map[i][j] = temp->map[i][j];
}
}
r->column = temp->column;
r->row = temp->row + 1;
r->count = temp->count;
A.push_back(*r);
Point *s = new Point;
for(int i = 0; i < 5; i++)
{
for(int j = 0;j < 5; j++)
{
s->map[i][j] = temp->map[i][j];
}
}
s->column = temp->column;
s->row = temp->row - 1;
s->count = temp->count;
A.push_back(*s);
Point *t = new Point;
for(int i = 0; i < 5; i++)
{
for(int j = 0;j < 5; j++)
{
t->map[i][j] = temp->map[i][j];
}
}
t->column = temp->column + 1;
t->row = temp->row + 1;
t->count = temp->count;
A.push_back(*t);
Point *u = new Point;
for(int i = 0; i < 5; i++)
{
for(int j = 0;j < 5; j++)
{
u->map[i][j] = temp->map[i][j];
}
}
u->column = temp->column - 1;
u->row = temp->row - 1;
u->count = temp->count;
A.push_back(*u);
Point *v = new Point;
for(int i = 0; i < 5; i++)
{
for(int j = 0;j < 5; j++)
{
v->map[i][j] = temp->map[i][j];
}
}
v->column = temp->column + 1;
v->row = temp->row - 1;
v->count = temp->count;
A.push_back(*v);
Point *w = new Point;
for(int i = 0; i < 5; i++)
{
for(int j = 0;j < 5; j++)
{
w->map[i][j] = temp->map[i][j];
}
}
w->column = temp->column - 1;
w->row = temp->row + 1;
w->count = temp->count;
A.push_back(*w);
}
}
cout<<A.size();
start(A);
}
int main()
{
pts.clear();
Point *p = new Point;
p->map[2][2] = 0;
p->column = 2;
p->row = 2;
p->count = 0;
cout<<p<<endl;
pts.push_back(*p);
start(pts);
}
the first runs for about 150 iterations and then outputs 0, to indicate no completepaths, which is definitely wrong.
the second seems an error in pointers and addresses, which i still cannot get my head around.

Related

Why when I try to run this code online it always generates runtime error/run error. but when I run it in command prompt it works [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed yesterday.
Improve this question
This code is a program that will move the index position of several names according to the size of the toga that each participant has, and according to the name that the user wants to change.
#include <stdio.h>
#include <string.h>
typedef struct
{
char nama[100];
char ukuran[100];
} toga;
int main()
{
int n, m;
scanf("%d%d", &n, &m);
toga matriks[n][m];
int jumlah[n][m];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
scanf("%s%s", &matriks[i][j].nama, &matriks[i][j].ukuran);
if(strcmp(matriks[i][j].ukuran, "XS") == 0)
{
jumlah[i][j] = 32;
}
else if(strcmp(matriks[i][j].ukuran, "S") == 0)
{
jumlah[i][j] = 36;
}
else if(strcmp(matriks[i][j].ukuran, "M") == 0)
{
jumlah[i][j] = 37;
}
else if(strcmp(matriks[i][j].ukuran, "L") == 0)
{
jumlah[i][j] = 38;
}
else if(strcmp(matriks[i][j].ukuran, "XL") == 0)
{
jumlah[i][j] = 40;
}
else if(strcmp(matriks[i][j].ukuran, "XXL") == 0)
{
jumlah[i][j] = 42;
}
else if(strcmp(matriks[i][j].ukuran, "XXXL") == 0)
{
jumlah[i][j] = 45;
}
}
}
maybe this part is the problem?
toga ditukar1[50][50];
toga ditukar2[50][50];
int cek2 = 0;
for (int i = 0; i < 50; i++)
{
for(int j = 0; j < 1; j++)
{
scanf("%s", &ditukar1[i][0].nama);
if(strcmp(ditukar1[i][0].nama, "Selesai") == 0)
{
j = 1;
i = 50;
}
else
{
scanf("%s ", &ditukar1[i][1].nama);
// cek1++;
}
}
cek2++;
}
cek2 -= 1;
// printf("ini cek1 : %d\n", cek1);
// printf("ini cek2 : %d\n", cek2);
toga matriks_baru[m][n];
for (int i = 0, a = 0; i < m && a < m; i++, a++)
{
for (int j = 0, b = 0; j < n && b < n; j++, b++)
{
strcpy(matriks_baru[a][b].nama ,matriks[j][i].nama);
}
}
int max_len[m];
max_len[0] = 0;
int terpanjang[m];
int a = 0;
for (int i = 0; i < m; i++)
{
max_len[a] = 0;
for (int j = 0; j < n; j++)
{
if(strlen(matriks_baru[i][j].nama) > max_len[a])
{
max_len[a] = strlen(matriks_baru[i][j].nama);
}
}
a++;
}
printf("Posisi awal :\n");
for(int i = 0; i < n; i++)
{
a = 0;
for(int j = 0; j < m; j++)
{
if(j != m-1)
{
printf("%s ", matriks[i][j].nama);
}
else
{
printf("%s", matriks[i][j].nama);
}
if(j != m-1)
{
for(int k = 0; k < (max_len[a]) - (strlen(matriks[i][j].nama)); k++)
{
printf(" ");
}
}
a++;
}
printf("\n");
}
printf("\n");
int baris1, baris2;
int kolom1, kolom2;
int k = 0;
int l = 0;
maybe this part is the problem?
here I will check whether the name entered by the user whose index you want to exchange is in the matrix
for(int l = 0; l < cek2; l++)
{
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
for(int a = 0; a < n; a++)
{
for(int b = 0; b < m; b++)
{
if(strcmp(ditukar1[l][0].nama, matriks[i][j].nama) == 0)
{
baris1 = i;
kolom1 = j;
if(strcmp(ditukar1[l][1].nama, matriks[a][b].nama) == 0)
{
// printf("%d\n", l);
baris2 = a;
kolom2 = b;
if(baris1 < baris2)
{
char temp1[] = "0";
int temp2 = 0;
if(jumlah[baris1][kolom1] > jumlah[baris2][kolom2])
{
temp2 = jumlah[baris1][kolom1];
jumlah[baris1][kolom1] = jumlah[baris2][kolom2];
jumlah[baris2][kolom2] = temp2;
strcpy(temp1, matriks[baris2][kolom2].nama);
strcpy(matriks[baris2][kolom2].nama, matriks[baris1][kolom1].nama);
strcpy(matriks[baris1][kolom1].nama, temp1);
}
}
else if(baris1 > baris2)
{
char temp1[] = "0";
int temp2 = 0;
if(jumlah[baris2][kolom2] > jumlah[baris1][kolom1])
{
temp2 = jumlah[baris2][kolom2];
jumlah[baris2][kolom2] = jumlah[baris1][kolom1];
jumlah[baris1][kolom1] = temp2;
strcpy(temp1, matriks[baris1][kolom1].nama);
strcpy(matriks[baris1][kolom1].nama, matriks[baris2][kolom2].nama);
strcpy(matriks[baris2][kolom2].nama, temp1);
}
}
}
}
}
}
}
}
}
for (int i = 0, a= 0; i < m && a < m; i++, a++)
{
for (int j = 0, b = 0; j < n && b < n; j++, b++)
{
strcpy(matriks_baru[a][b].nama ,matriks[j][i].nama);
}
}
int max_len2[m];
max_len2[0] = 0;
int s = 0;
for (int i = 0; i < m; i++)
{
max_len2[s] = 0;
for (int j = 0; j < n; j++)
{
if(strlen(matriks_baru[i][j].nama) > max_len2[s])
{
max_len2[s] = strlen(matriks_baru[i][j].nama);
}
}
s++;
}
printf("Posisi akhir :\n");
for(int i = 0; i < n; i++)
{
s = 0;
for(int j = 0; j < m; j++)
{
if(j != m-1)
{
printf("%s ", matriks[i][j].nama);
}
else
{
printf("%s", matriks[i][j].nama);
}
if(j != m-1)
{
for(int k = 0; k < (max_len2[s]) - (strlen(matriks[i][j].nama)); k++)
{
printf(" ");
}
}
s++;
}printf("\n");
}
return 0;
}

how to get different array in for loop every time?

#include<stdio.h>
#include<time.h>
int main(void)
{
srand(time(NULL));
int answer;
int treatment = rand() % 4;
printf("###발모제 찾기###\n\n");
int cntShowBottle = 0;
int prevCntShowBottle = 0;
int ptr[4] = { 0,0,0,0 };
int bottle[4] = { 0, 0, 0, 0 };
int isincluded = 0;
for (int i = 1; i <= 3; i++)
{
do {
cntShowBottle = rand() % 2 + 2;
} while (cntShowBottle == prevCntShowBottle);
prevCntShowBottle = cntShowBottle;
printf(" %d 번째 시도 : ", i);
for (int j = 0; j < cntShowBottle; j++)
{
int randBottle = rand() % 4;
if (bottle[randBottle] == 0)
{
bottle[randBottle] = 1;
if (randBottle == treatment)
{
isincluded = 1;
}
}
else
{
j--;
}
}
}
if (bottle[0] == ptr[0] && bottle[1] == ptr[1] && bottle[2] == ptr[2] && bottle[3] == ptr[3])
{
int bottle[4] = { 0,0,0,0 };
for (int j = 0; j < cntShowBottle; j++)
{
int randBottle = rand() % 4;
if (bottle[randBottle] == 0)
{
bottle[randBottle] = 1;
if (randBottle == treatment)
{
isincluded = 1;
}
}
else
{
j--;
}
}
}
else
{
return 0;
}
for (int i = 0; i < 4; i++)
{
ptr[i] = bottle[i];
}
for (int k = 0; k < 4; k++)
{
if (bottle[k] == 1)
printf("%d ", k + 1);
}
printf("번 물약을 머리에 바릅니다.\n\n");
if (isincluded == 1)
{
printf("성공!\n");
}
else
{
printf("실패...\n");
}
printf("\n ...계속 하려면 아무키나 누르세요...");
getchar(0);
printf("\n\n발모제는 몇 번? : ");
scanf_s("%d", &answer);
if (answer == treatment+1)
{
printf("\n 정답! \n");
}
else
{
printf("\n 실패! \n 정답은 %d 였습니다.\n", treatment+1);
}
return 0;
}
in this loop, for (int j = 0; j < cntShowBottle; j++), 'bottle' array will be [0,1,1,1] or [1,1,0,0] etc. In this loop, how to get different array without overlapping(like [0,1,1,0] and again [0,1,1,0])?? I tried comparing each elements, if it takes overlapping array, makes 'bottle' array again. but it didn't run properly. please help..

fprintf in a while loop will only print my data 10 times to a file

ran into a problem regarding fprintf, we want to write a double array into our file for every loop in our main's while loop, however when this loop goes higher than 10 it simply stops fprintf'ing.
The function in question is "store_trash_file". Also the program is controlled by a few defines at top.
Day_max = controls the amount of loops in our while loop
SIZE = with higher SIZE it seems it will fprintf less, and with a smaller it will fprintf more lines.
Ill be putting up the entire code, so u can run it, perhaps keep an eye on the create directories function, as it will make folders and some txts
Any help is much appreciated
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <conio.h>
#include <io.h>
#include <process.h>
#define MARGIN 70
#define MARGIN2 30
#define NAREA 4
#define NSUBAREA 4
#define SIZE 20
#define DAY_MAX 20
#define AREAS_PER_TRUCK 4
#define LOWER1 6
#define LOWER2 8
#define LOWER3 10
#define UPPER1 8
#define UPPER2 10
#define UPPER3 12
typedef struct subarea
{
int co2_cost, time, emptied_subarea_counter, lower_random, upper_random, activity_level;
double average, total_trash_subarea_avg, sensorData[SIZE], sensorDataTotal[SIZE];
} subarea;
typedef struct area
{
subarea *subarea_array[NAREA + 1];
double average, total_trash_area_avg;
int emptied_area_counter;
} area;
void simulated_days(area *area_dynamic, area *area_static);
void average_trash(area *area);
void sensor_data_start(area *area, double start_var);
void compare_trash_to_empty(area *area_array[NAREA + 1], int *area_number_p);
void empty_trash(area *area, int *co2_counter_p, int *time_counter_p);
void store_trash_file(area *area_array[NAREA + 1]);
void create_areas(area *area_array[NAREA + 1]);
void empty_trash_static(area *area, int *co2_counter_static_p, int *time_counter_static_p);
void create_directories();
void make_txt_file(char file_name[30], char file_name2[30]);
int main(void)
{
int co2_counter = 0, time_counter = 0;
int co2_counter_static = 0, time_counter_static = 0;
int day_counter = 0;
int area_number;
int garbage_truck_amount = 0;
double *area_array_avg[DAY_MAX][NAREA];
double *subarea_array_avg[DAY_MAX][NAREA * NSUBAREA];
double *trashcan_array_avg[DAY_MAX][NAREA * NSUBAREA * SIZE];
area *area_array[NAREA + 1];
area *area_array_static[NAREA + 1];
srand(time(NULL));
for (int i = NAREA; i > 0; i -= AREAS_PER_TRUCK)
{
garbage_truck_amount++;
}
printf("Garbage trucks: %d\n", garbage_truck_amount);
create_areas(area_array);
create_areas(area_array_static);
create_directories();
int running = 1;
while (running)
{
for (int i = 1; i < NAREA + 1; i++)
{
simulated_days(area_array[i], area_array_static[i]);
average_trash(area_array[i]);
average_trash(area_array_static[i]);
}
for (int i = 1; i < NAREA + 1; i++)
{
for (int j = 0; j < NSUBAREA; j++)
{
printf("area array: %lf\n", area_array[i]->subarea_array[j]->average);
printf("area array static: %lf\n", area_array_static[i]->subarea_array[j]->average);
}
}
for (int i = 0; i < garbage_truck_amount; i++)
{
compare_trash_to_empty(area_array, &area_number);
empty_trash(area_array[area_number], &co2_counter, &time_counter);
for (int j = 1; j < NAREA + 1; j++)
{
average_trash(area_array[j]);
}
}
if (day_counter > 1 && day_counter % 14 == 0)
{
for (int i = 1; i < NAREA + 1; i++)
{
empty_trash_static(area_array_static[i], &co2_counter_static, &time_counter_static);
average_trash(area_array_static[i]);
}
}
store_trash_file(area_array);
printf("Day: %d\n", day_counter + 1);
for (int i = 0; i < NSUBAREA; i++)
{
for (int j = 1; j < NAREA + 1; j++)
{
printf("%lf\t", area_array[j]->subarea_array[i]->average);
printf("%lf\t", area_array_static[j]->subarea_array[i]->average);
}
printf("\n");
}
printf("\n");
day_counter++;
if(day_counter == DAY_MAX)
{
running = 0;
}
}
}
void simulated_days(area *area_dynamic, area *area_static)
{
for (int i = 0; i < NSUBAREA; i++)
{
for (int j = 0; j < SIZE; j++)
{
int x = ((rand() % (area_dynamic->subarea_array[i]->upper_random - area_dynamic->subarea_array[i]->lower_random + 1)) + area_dynamic->subarea_array[i]->lower_random);
area_dynamic->subarea_array[i]->sensorData[j] += x;
area_dynamic->subarea_array[i]->sensorDataTotal[j] += x;
area_static->subarea_array[i]->sensorData[j] += x;
area_static->subarea_array[i]->sensorDataTotal[j] += x;
}
}
}
void average_trash(area *area)
{
double sum[NSUBAREA];
double sum_total[NSUBAREA];
double area_trash_sum = 0;
double area_trash_sum_total = 0;
for (int i = 0; i < NSUBAREA; i++)
{
sum[i] = 0;
sum_total[i] = 0;
}
for (int i = 0; i < NSUBAREA; i++)
{
for (int j = 0; j < SIZE; j++)
{
sum[i] += area->subarea_array[i]->sensorData[j];
sum_total[i] += area->subarea_array[i]->sensorDataTotal[j];
}
area->subarea_array[i]->average = sum[i] / SIZE;
area->subarea_array[i]->total_trash_subarea_avg = sum_total[i] / SIZE;
}
for (int i = 0; i < NSUBAREA; i++)
{
area_trash_sum += area->subarea_array[i]->average;
area_trash_sum_total += area->subarea_array[i]->total_trash_subarea_avg;
}
area->average = area_trash_sum / NSUBAREA;
area->total_trash_area_avg = area_trash_sum_total / NSUBAREA;
}
void sensor_data_start(area *area, double start_var)
{
for (int i = 0; i < NSUBAREA; i++)
{
for (int j = 0; j < SIZE; j++)
{
area->subarea_array[i]->sensorData[j] = start_var;
area->subarea_array[i]->sensorDataTotal[j] = start_var;
}
}
}
void compare_trash_to_empty(area *area_array[NAREA + 1], int *area_number_p)
{
int highBlock = 0;
for (int i = 1; i < NAREA + 1; i++)
{
if (area_array[i]->average >= MARGIN)
{
if (area_array[i]->average > area_array[highBlock]->average)
{
highBlock = i;
}
}
}
*area_number_p = highBlock;
printf("\nhighblock %d\n", highBlock);
}
void empty_trash(area *area, int *co2_counter_p, int *time_counter_p)
{
for (int i = 0; i < NSUBAREA; i++)
{
if (area->subarea_array[i]->average > MARGIN2)
{
*co2_counter_p += area->subarea_array[i]->co2_cost;
*time_counter_p += area->subarea_array[i]->time;
area->subarea_array[i]->average = 0;
area->subarea_array[i]->emptied_subarea_counter++;
for (int j = 0; j < SIZE; j++)
{
if (area->subarea_array[i]->sensorData[j] <= 100)
{
area->subarea_array[i]->sensorData[j] = 0;
}
else if (area->subarea_array[i]->sensorData[j] > 100)
{
area->subarea_array[i]->sensorData[j] -= 100;
}
}
}
}
area->emptied_area_counter++;
}
void store_trash_file(area *area_array[NAREA + 1])
{
// int xcount = 0;
// int ycount = 0;
// double area_array_avg[NAREA];
// double subarea_array_avg[NAREA * NSUBAREA];
// double trashcan_array_avg[NAREA * NSUBAREA * SIZE];
FILE *output_filepointer;
char *dirname2 = "C:\\Users\\jacob\\Documents\\software\\projekter\\p1\\kode\\intelligenttrash\\data\\area";
char newdirname[150];
char newdirname2[150];
char newdirname3[150];
for (int i = 1; i < NAREA + 1; i++)
{
// area_array_avg[i - 1] = (area_array[i]->total_trash_area_avg);
snprintf(newdirname, 150, "%s%d\\area_average_total.txt", dirname2, i);
output_filepointer = fopen(newdirname, "a");
fprintf(output_filepointer, "%lf ", area_array[i]->total_trash_area_avg);
for (int j = 0; j < NSUBAREA; j++)
{
// subarea_array_avg[j + (ycount * NSUBAREA)] = (area_array[i]->subarea_array[j]->total_trash_subarea_avg);
snprintf(newdirname2, 150, "%s%d%s%d\\subarea_average_total.txt", dirname2, i, "\\subarea", j);
output_filepointer = fopen(newdirname2, "a");
fprintf(output_filepointer, "%lf ", area_array[i]->subarea_array[j]->total_trash_subarea_avg);
for (int z = 0; z < SIZE; z++)
{
// trashcan_array_avg[z + (xcount * SIZE)] = (area_array[i]->subarea_array[j]->sensorDataTotal[z]);
snprintf(newdirname3, 150, "%s%d%s%d\\trashcan_trash.txt", dirname2, i, "\\subarea", j);
output_filepointer = fopen(newdirname3, "a");
fprintf(output_filepointer, "%lf ", area_array[i]->subarea_array[j]->sensorDataTotal[z]);
}
// xcount++;
fprintf(output_filepointer, "\n");
output_filepointer = fopen(newdirname2, "a");
fprintf(output_filepointer, "\n");
}
// ycount++;
output_filepointer = fopen(newdirname, "a");
fprintf(output_filepointer, "\n");
}
fclose(output_filepointer);
}
void create_areas(area *area_array[NAREA + 1])
{
int percentage_added[3][2] = {{LOWER1, UPPER1}, {LOWER2, UPPER2}, {LOWER3, UPPER3}};
int activity_level;
for (int i = 0; i < NAREA + 1; i++)
{
area_array[i] = malloc(sizeof(area));
for (int j = 0; j < NSUBAREA; j++)
{
activity_level = (rand() % 3);
area_array[i]->subarea_array[j] = malloc(sizeof(subarea));
area_array[i]->subarea_array[j]->co2_cost = 50;
area_array[i]->subarea_array[j]->time = 50;
area_array[i]->subarea_array[j]->average = 0;
area_array[i]->subarea_array[j]->emptied_subarea_counter = 0;
area_array[i]->subarea_array[j]->total_trash_subarea_avg = 0;
area_array[i]->subarea_array[j]->lower_random = percentage_added[activity_level][0];
area_array[i]->subarea_array[j]->upper_random = percentage_added[activity_level][1];
area_array[i]->subarea_array[j]->activity_level = activity_level;
}
sensor_data_start(area_array[i], 0);
area_array[i]->average = 0;
area_array[i]->total_trash_area_avg = 0;
area_array[i]->emptied_area_counter = 0;
}
}
void empty_trash_static(area *area, int *co2_counter_static_p, int *time_counter_static_p)
{
for (int i = 0; i < NSUBAREA; i++)
{
*co2_counter_static_p += area->subarea_array[i]->co2_cost;
*time_counter_static_p += area->subarea_array[i]->time;
area->subarea_array[i]->average = 0;
area->subarea_array[i]->emptied_subarea_counter++;
for (int j = 0; j < SIZE; j++)
{
if (area->subarea_array[i]->sensorData[j] <= 100)
{
area->subarea_array[i]->sensorData[j] = 0;
}
else if (area->subarea_array[i]->sensorData[j] > 100)
{
area->subarea_array[i]->sensorData[j] -= 100;
}
}
}
area->emptied_area_counter++;
}
void create_directories()
{
int check;
char *dirname1 = "C:\\Users\\jacob\\Documents\\software\\projekter\\p1\\kode\\intelligenttrash\\data";
char *dirname2 = "C:\\Users\\jacob\\Documents\\software\\projekter\\p1\\kode\\intelligenttrash\\data\\area";
mkdir(dirname1);
for (int i = 1; i < NAREA + 1; i++)
{
char newdirname[100];
char newdirname2[100];
snprintf(newdirname, 100, "%s%d", dirname2, i);
mkdir(newdirname);
for (int j = 1; j < NSUBAREA + 1; j++)
{
snprintf(newdirname2, 100, "%s%d%s%d", dirname2, i, "\\subarea", j);
mkdir(newdirname2);
}
}
}
void make_txt_file(char file_name[30], char file_name2[30])
{
FILE *output_filepointer;
char *dirname2 = "C:\\Users\\jacob\\Documents\\software\\projekter\\p1\\kode\\intelligenttrash\\data\\area";
char newdirname[100];
char newdirname2[100];
for (int i = 1; i < NAREA + 1; i++)
{
if (file_name != "none")
{
snprintf(newdirname, 100, "%s%d\\%s.txt", dirname2, i, file_name);
output_filepointer = fopen(newdirname, "a");
}
for (int j = 1; j < NSUBAREA + 1; j++)
{
if (file_name2 != "none")
{
snprintf(newdirname2, 100, "%s%d%s%d\\%s.txt", dirname2, i, "\\subarea", j, file_name2);
output_filepointer = fopen(newdirname2, "a");
}
}
}
fclose(output_filepointer);
}

How to access an array in a different function

I am writing a program to solve Sudoku puzzles in C I declared the array in main() when I try and access it in a different function the compiler gives the error I've tried using pointers to the array but nothing seems to allow me to access it
error: ‘grid’ undeclared (first use in this function) 121 |
printf("%d ", &grid[i][i]);
How can I access this array from a different function?
Here is the code
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main()
{
int grid[9][9] =
{
{8,0,0,0,0,0,0,0,0},
{0,0,3,6,0,0,0,0,0},
{0,7,0,0,9,0,2,0,0},
{0,5,0,0,0,7,0,0,0},
{0,0,0,0,4,5,7,0,0},
{0,0,0,1,0,0,0,3,0},
{0,0,1,0,0,0,0,6,8},
{0,0,8,5,0,0,0,1,0},
{0,9,0,0,0,0,4,0,0}
};
int LineNum = 9;
int RowAmount = 9;
for (int i = 0; i < LineNum; ++i)
{
for (int i = 0; i < RowAmount; ++i)
{
printf("%d ", &grid[i][i]);
}
printf("\n");
}
}
int find_empty_box(int sudoku)
{
for (int x = 0; x <= 9; ++x)
{
for (int y = 0; y < 9; ++y)
{
if (sudoku[&x][&y] == 0)
{
return x, y;
}
}
return NULL, NULL;
}
}
int Answer_Valid(int sudoku, int guess, int row, int col)
{
int row_values = &sudoku[&row];
for (int i = 0; i < sudoku[&row]; ++i)
{
if (guess == sudoku[&row[&i]])
{
return false;
}
}
int column_values = &sudoku[&col];
for (int t = 0; t <= 9; ++t)
{
for (int n = 0; n < &sudoku[&col]; ++n)
{
if (&guess == &sudoku[&col[&n]])
{
return false;
}
}
}
int row_start = (row / 3) * 3;
int col_start = (col / 3) * 3;
for (int x = 0; x <= row_start && row_start + 3; ++x)
{
for (int y = 0; y < col_start && col_start + 3; ++y)
{
if (sudoku[&x][&y] == guess)
{
return false;
}
return true;
}
}
}
int Solver(sudoku, guess)
{
int row, col = find_empty_box(sudoku);
if (row == NULL)
{
return true;
}
for (int i = 0; i < 1||2||3||4||5||6||7||8||9; ++i)
{
if(Answer_Valid(sudoku, guess, row, col))
{
sudoku[&row][&col] = guess;
if(Solver(sudoku))
{
return true;
}
return false;
}
}
int LineNum = 9;
int RowAmount = 9;
for (int i = 0; i < LineNum; ++i)
{
for (int i = 0; i < RowAmount; ++i)
{
printf("%d ", &grid[i][i]);
}
printf("\n");
}
}

I have a problem dynamic memory allocation

int szar(t_obraz obr)
{
int x = 0, i, j;
int **wsp;
wsp = (int **)malloc(obr.wymy * sizeof(int *));
for (i = 0; i < obr.wymy; i++)
{
wsp[i] = (int *)malloc(obr.wymx * sizeof(int));
}
for (i = 0; i < obr.wymy; i++)
{
for (j = 1; j < 3 * obr.wymx; j += 3)
{
wsp[i][x] = (obr.obraz[i][j - 1] + obr.obraz[i][j] + obr.obraz[i][j + 1]) / 3;
x++;
}
}
for (i = 0; i < obr.wymy; i++)
{
obr.obraz[i] = (int *)realloc(obr.obraz[i], obr.wymx * sizeof(int));
}
for (i = 0; i < obr.wymy; i++)
{
for (j = 0; j < obr.wymx; j++)
{
obr.obraz[i][j] = wsp[i][j];
}
}
for (i = 0; i < obr.wymy; i++)
{
free(wsp[i]);
}
free(wsp);
}
I have segmentation fault(core dumped), when try to compile that. I have array obraz[wymy][3*wymx] in structure t_obraz.
I think you need to reset your x in the outer Loop
for (i=0;i<obr.wymy;i++) {
x = 0; // here
for (j=1;j<3*obr.wymx;j+=3) {
wsp[i][x]=(obr.obraz[i][j-1]+obr.obraz[i][j]+obr.obraz[i][j+1])/3;
x++;
}
}

Resources