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 4 years ago.
Improve this question
The game works perfectly, but I want to add a timer that measures and shows at the end of the game, the time the player's game lasted.
The code is a classic game of minesweeper where the user gives the coordinates of the point he wants to discover from the board, the game is made in c, not in C#.
The game consists of clearing all the squares of a two-dimensional arrangement that do not hide a mine. Some squares will have a random mine while others will not. The user is asked to position a box in the two-dimensional arrangement, if he does not find a mine he will continue the game until he wins, otherwise he will finish the game and the user will lose.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define FLUSH fflush(stdin)
void difficulty( void );
void beginner( void );
void intermediate( void );
void expert( void );
void minefield_generator( void );
void print_minefield( void );
void guess( void );
void boom( void );
void print_final_minefield( void );
void win( void );
void play_again( void );
void game_over( void );
int x, y;
int M, N;
float diff;
int total_mines = 0;
int mines = 0;
int minefield[30][30]; //This 2-D array contains all of the mines, numbers and blank spaces
int blank_minefield[30][30]; //This contains the minefield full of '|-|' characters
int final_minefield[30][30];
int main()
{
printf("\t\tWelcome to Minesweeper\n");
difficulty();
return 0;
}
void difficulty( void ) //Function for choosing the difficulty level
{
diff = 0;
while( (diff != 1) && (diff != 2) && (diff != 3) && (diff != 4))
{
printf("\t\tChoose a difficulty level(1-3) or 4 for a custom game:");
scanf("%f", &diff);
FLUSH;
if( (diff != 1) && (diff != 2) && (diff != 3) && (diff != 4))
{
printf("\t\tPlease enter either 1, 2, 3 or 4\n");
}
}
if( diff == 1 ) //If, else if and else statements that each go to the respective difficulty
{
beginner();
}
else if( diff == 2 )
{
intermediate();
}
else if( diff == 3 )
{
expert();
}
else if( diff == 4)
{
custom();
}
}
void beginner( void ) //Gives the minefield the 'beginner' grid and mines
{
M = 9;
N = 9;
total_mines = 10;
minefield_generator();
guess();
}
void intermediate( void ) //Gives the minefield the 'intermediate' grid and mines
{
M = 16;
N = 16;
total_mines = 40;
minefield_generator();
guess();
}
void expert( void ) //Gives the minefield the 'expert' grid size and mines
{
M = 16;
N = 30;
total_mines = 99;
minefield_generator();
guess();
}
void custom( void )
{
M = 0;
N = 0;
total_mines = 0;
printf("\t\tPlease enter the size of the dimensions you want\n");
printf("\t\tFirst value:\n");
scanf("%d", &M);
printf("\t\tSecond value:\n");
scanf("%d", &N);
printf("\t\tNumber of mines you want to assign to the board:\n");
scanf("%d", &total_mines);
minefield_generator();
guess();
}
void minefield_generator( void ) //Function that generates the minefield
{
int i = 0, j = 0;
srand( time( NULL ) ); //Starts the random no. generator
while( j < N ) //Nested loop for making the blank minefield and final minefield
{
while( i < M)
{
minefield[i][j] = '-';
blank_minefield[i][j] = minefield[i][j];
final_minefield[i][j] = minefield[i][j];
i++;
}
i = 0;
j++;
}
mines = 0;
while( mines < total_mines ) //Randomly generates the mines into the minefield
{
i = rand()%(M);
j = rand()%(N);
if( minefield[i][j] != '*') //If statement that checks if there is a mine already there and doesn't place a mine if there is
{
minefield[i][j] = '*';
final_minefield[i][j] = minefield[i][j];
mines++;
}
}
i = 0;
j = 0;
while( j < N ) //While loop that generates the numbers for any adjacent mines
{
while( i < M)
{
if( minefield[i][j] != '*')
{
minefield[i][j] = 0;
}
if((minefield[i-1][j-1] == '*') && (minefield[i][j] != '*'))
{
minefield[i][j]++;
}
if((minefield[i-1][j] == '*') && (minefield[i][j] != '*'))
{
minefield[i][j]++;
}
if((minefield[i][j-1] == '*') && (minefield[i][j] != '*'))
{
minefield[i][j]++;
}
if((minefield[i-1][j+1] == '*') && (minefield[i][j] != '*'))
{
minefield[i][j]++;
}
if((minefield[i+1][j-1] == '*') && (minefield[i][j] != '*'))
{
minefield[i][j]++;
}
if((minefield[i+1][j] == '*') && (minefield[i][j] != '*'))
{
minefield[i][j]++;
}
if((minefield[i][j+1] == '*') && (minefield[i][j] != '*'))
{
minefield[i][j]++;
}
if((minefield[i+1][j+1] == '*') && (minefield[i][j] != '*'))
{
minefield[i][j]++;
}
i++;
}
i = 0;
j++;
}
i = 0;
j = 0;
}
void print_minefield(void) // This function prints the minefield
{
int i = 0, j = 0, z = 0;
while( z < M ) // This while loop prints out the line of co-ordinates along the x axis of the minefield
{
if( z == 0 )
{
printf("\t");
}
printf("|%d|\t", z);
z++;
}
printf("\n\n");
while( j < N ) // Loop that prints out each character in the minefield
{
printf("|%d|\t", j);
while( i < M)
{
if( blank_minefield[i][j] == '-')
{
printf("|%c|\t", blank_minefield[i][j]);
}
else if( minefield[i][j] == 0 ) // This changes any spaces with values of zero to the character 'B'
{
blank_minefield[i][j] = 'B';
printf("|%c|\t", blank_minefield[i][j]);
}
else
{
printf("|%d|\t", blank_minefield[i][j]);
}
i++;
}
printf("\n");
i = 0;
j++;
}
}
void guess( void )
{
int q = 0, i=0, j=0, match=0;
print_minefield();
while( j < N ) // While loop for testing whether or not the user has cleared the minefield
{
while( i < M )
{
if(minefield[i][j] == blank_minefield[i][j])
{
match++;
}
i++;
}
i = 0;
j++;
}
if( match == (( M * N ) - total_mines)) // If the user has cleared the minefield, the win() function is run
{
win();
}
printf("\nEnter the x value, then a space, then the y value:");
scanf("%d %d", &x, &y); // Reading in the co-ordinates for the guess
FLUSH;
if( (x >= M) || (x < 0) || (y < 0) || (y >= N) )
{
printf("\nPlease enter a value inside the grid\n");
guess();
}
if( minefield[x][y] == '*' ) // Runs the boom() function if the user selects a mine
{
boom();
}
if( blank_minefield[x][y] != '-' )
{
printf("\nPlease enter a value that has not already been entered\n");
guess();
}
else // Checks if the adjacent spaces are blank, then changes the values in the blank_minefield array. Because they are changed, they will now print out in the print_minefield function
{
blank_minefield[x][y] = minefield[x][y];
if( minefield[x][y] == 0 )
{
if( minefield[x-1][y-1] == 0 )
{
blank_minefield[x-1][y] = minefield[x-1][y];
}
if( minefield[x-1][y] == 0 )
{
blank_minefield[x-1][y] = minefield[x-1][y];
}
if( minefield[x][y-1] == 0 )
{
blank_minefield[x][y-1] = minefield[x][y-1];
}
if( minefield[x-1][y+1] == 0 )
{
blank_minefield[x-1][y+1] = minefield[x-1][y+1];
}
if( minefield[x+1][y-1] == 0 )
{
blank_minefield[x+1][y-1] = minefield[x+1][y-1];
}
if( minefield[x+1][y] == 0 )
{
blank_minefield[x+1][y] = minefield[x+1][y];
}
if( minefield[x][y+1] == 0 )
{
blank_minefield[x][y+1] = minefield[x][y+1];
}
if( minefield[x+1][y+1] == 0 )
{
blank_minefield[x+1][y+1] = minefield[x+1][y+1];
}
}
guess();
}
}
void boom( void ) // Runs the print_final_minefield function, then the play_again function
{
print_final_minefield();
printf("\n\t\tYou hit a mine at %d,%d\n\t\tYOU LOSE!!!!", x, y);
play_again();
}
void print_final_minefield( void ) // Prints the minefield, showing where all of the mines are placed
{
int i = 0, j = 0, z = 0;
while( z < M )
{
if( z == 0 )
{
printf("\t");
}
printf("|%d|\t", z);
z++;
}
printf("\n\n");
while( j < N )
{
printf("|%d|\t", j);
while( i < M)
{
printf("|%c|\t", final_minefield[i][j]);
i++;
}
printf("\n");
i = 0;
j++;
}
}
void win( void ) // Runs the play_again function
{
printf("\n\n\n\t\t\tYOU WIN!!!!!\n\n\n");
play_again();
}
void play_again( void ) // Gives the user the option to play again
{
char option[2];
printf("\n\t\tWould you like to play again(Y/N)?:");
scanf("%c", &option[0]);
FLUSH;
if((option[0] == 'Y') || (option[0] == 'y')) // Restarts the program from after the welcome message
{
difficulty();
}
else if( (option[0] == 'N') || (option[0] == 'n'))
{
game_over();
}
else
{
printf("Please enter either Y or N");
play_again();
}
}
void game_over( void ) // Ends the program
{
printf("\n\n\t\tGame Over");
exit(1);
}
Use the function clock() to catch the moment’s clock, and then use the macro CLOCKS_PER_SEC to calculate how many second has gone by. Like this:
int timeInSeconds;
clock_t start = clock(), end,total;
...
end = clock();
total = end - start;
timeInSeconds = total/CLOCKS_PER_SEC;
Related
I'm trying to run this code on Visual Studio 2022, but it gives an error:
C1083: Cannot open include file: 'unistd.h': No such file or directory.
As I understood that the code was written for the UNIX system, but I would like to run it on Windows.
Snake game
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <conio.h>
#include<time.h>
#include<ctype.h>
#include <time.h>
#include <windows.h>
#include <process.h>
#include <unistd.h>
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
int length;
int bend_no;
int len;
char key;
void record();
void load();
int life;
void Delay(long double);
void Move();
void Food();
int Score();
void Print();
void gotoxy(int x, int y);
void GotoXY(int x, int y);
void Bend();
void Boarder();
void Down();
void Left();
void Up();
void Right();
void ExitGame();
int Scoreonly();
struct coordinate {
int x;
int y;
int direction;
};
typedef struct coordinate coordinate;
coordinate head, bend[500], food, body[30];
int main()
{
char key;
Print();
system("cls");
load();
length = 5;
head.x = 25;
head.y = 20;
head.direction = RIGHT;
Boarder();
Food(); //to generate food coordinates initially
life = 3; //number of extra lives
bend[0] = head;
Move(); //initialing initial bend coordinate
return 0;
}
void Move()
{
int a, i;
do {
Food();
fflush(stdin);
len = 0;
for (i = 0; i < 30; i++)
{
body[i].x = 0;
body[i].y = 0;
if (i == length)
break;
}
Delay(length);
Boarder();
if (head.direction == RIGHT)
Right();
else if (head.direction == LEFT)
Left();
else if (head.direction == DOWN)
Down();
else if (head.direction == UP)
Up();
ExitGame();
} while (!kbhit());
a = getch();
if (a == 27)
{
system("cls");
exit(0);
}
key = getch();
if ((key == RIGHT && head.direction != LEFT && head.direction != RIGHT) || (key == LEFT && head.direction != RIGHT && head.direction != LEFT) || (key == UP && head.direction != DOWN && head.direction != UP) || (key == DOWN && head.direction != UP && head.direction != DOWN))
{
bend_no++;
bend[bend_no] = head;
head.direction = key;
if (key == UP)
head.y--;
if (key == DOWN)
head.y++;
if (key == RIGHT)
head.x++;
if (key == LEFT)
head.x--;
Move();
}
else if (key == 27)
{
system("cls");
exit(0);
}
else
{
printf("\a");
Move();
}
}
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void GotoXY(int x, int y)
{
HANDLE a;
COORD b;
fflush(stdout);
b.X = x;
b.Y = y;
a = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(a, b);
}
void sleep(unsigned int mseconds)
{
clock_t goal = mseconds + clock();
while (goal > clock());
}
void load() {
int row, col, r, c, q;
gotoxy(36, 14);
printf("loading...");
gotoxy(30, 15);
for (r = 1; r <= 20; r++) {
sleep(200);//to display the character slowly
printf("%c", 177);
}
getch();
}
void Down()
{
int i;
for (i = 0; i <= (head.y - bend[bend_no].y) && len < length; i++)
{
GotoXY(head.x, head.y - i);
{
if (len == 0)
printf("v");
else
printf("*");
}
body[len].x = head.x;
body[len].y = head.y - i;
len++;
}
Bend();
if (!kbhit())
head.y++;
}
void Delay(long double k)
{
Score();
long double i;
for (i = 0; i <= (10000000); i++);
}
void ExitGame()
{
int i, check = 0;
for (i = 4; i < length; i++) //starts with 4 because it needs minimum 4 element to touch its own body
{
if (body[0].x == body[i].x && body[0].y == body[i].y)
{
check++; //check's value increases as the coordinates of head is equal to any other body coordinate
}
if (i == length || check != 0)
break;
}
if (head.x <= 10 || head.x >= 70 || head.y <= 10 || head.y >= 30 || check != 0)
{
life--;
if (life >= 0)
{
head.x = 25;
head.y = 20;
bend_no = 0;
head.direction = RIGHT;
Move();
}
else
{
system("cls");
printf("All lives completed\nBetter Luck Next Time!!!\nPress any key to quit the game\n");
record();
exit(0);
}
}
}
void Food()
{
if (head.x == food.x && head.y == food.y)
{
length++;
time_t a;
a = time(0);
srand(a);
food.x = rand() % 70;
if (food.x <= 10)
food.x += 11;
food.y = rand() % 30;
if (food.y <= 10)
food.y += 11;
}
else if (food.x == 0)/*to create food for the first time coz global variable are initialized with 0*/
{
food.x = rand() % 70;
if (food.x <= 10)
food.x += 11;
food.y = rand() % 30;
if (food.y <= 10)
food.y += 11;
}
}
void Left()
{
int i;
for (i = 0; i <= (bend[bend_no].x - head.x) && len < length; i++)
{
GotoXY((head.x + i), head.y);
{
if (len == 0)
printf("<");
else
printf("*");
}
body[len].x = head.x + i;
body[len].y = head.y;
len++;
}
Bend();
if (!kbhit())
head.x--;
}
void Right()
{
int i;
for (i = 0; i <= (head.x - bend[bend_no].x) && len < length; i++)
{
//GotoXY((head.x-i),head.y);
body[len].x = head.x - i;
body[len].y = head.y;
GotoXY(body[len].x, body[len].y);
{
if (len == 0)
printf(">");
else
printf("*");
}
/*body[len].x=head.x-i;
body[len].y=head.y;*/
len++;
}
Bend();
if (!kbhit())
head.x++;
}
void Bend()
{
int i, j, diff;
for (i = bend_no; i >= 0 && len < length; i--)
{
if (bend[i].x == bend[i - 1].x)
{
diff = bend[i].y - bend[i - 1].y;
if (diff < 0)
for (j = 1; j <= (-diff); j++)
{
body[len].x = bend[i].x;
body[len].y = bend[i].y + j;
GotoXY(body[len].x, body[len].y);
printf("*");
len++;
if (len == length)
break;
}
else if (diff > 0)
for (j = 1; j <= diff; j++)
{
/*GotoXY(bend[i].x,(bend[i].y-j));
printf("*");*/
body[len].x = bend[i].x;
body[len].y = bend[i].y - j;
GotoXY(body[len].x, body[len].y);
printf("*");
len++;
if (len == length)
break;
}
}
else if (bend[i].y == bend[i - 1].y)
{
diff = bend[i].x - bend[i - 1].x;
if (diff < 0)
for (j = 1; j <= (-diff) && len < length; j++)
{
/*GotoXY((bend[i].x+j),bend[i].y);
printf("*");*/
body[len].x = bend[i].x + j;
body[len].y = bend[i].y;
GotoXY(body[len].x, body[len].y);
printf("*");
len++;
if (len == length)
break;
}
else if (diff > 0)
for (j = 1; j <= diff && len < length; j++)
{
/*GotoXY((bend[i].x-j),bend[i].y);
printf("*");*/
body[len].x = bend[i].x - j;
body[len].y = bend[i].y;
GotoXY(body[len].x, body[len].y);
printf("*");
len++;
if (len == length)
break;
}
}
}
}
void Boarder()
{
system("cls");
int i;
GotoXY(food.x, food.y); /*displaying food*/
printf("F");
for (i = 10; i < 71; i++)
{
GotoXY(i, 10);
printf("!");
GotoXY(i, 30);
printf("!");
}
for (i = 10; i < 31; i++)
{
GotoXY(10, i);
printf("!");
GotoXY(70, i);
printf("!");
}
}
void Print()
{
//GotoXY(10,12);
printf("\tWelcome to the mini Snake game.(press any key to continue)\n");
getch();
system("cls");
printf("\tGame instructions:\n");
printf("\n-> Use arrow keys to move the snake.\n\n-> You will be provided foods at the several coordinates of the screen which you have to eat. Everytime you eat a food the length of the snake will be increased by 1 element and thus the score.\n\n-> Here you are provided with three lives. Your life will decrease as you hit the wall or snake's body.\n\n-> YOu can pause the game in its middle by pressing any key. To continue the paused game press any other key once again\n\n-> If you want to exit press esc. \n");
printf("\n\nPress any key to play game...");
if (getch() == 27)
exit(0);
}
void record() {
char plname[20], nplname[20], cha, c;
int i, j, px;
FILE* info;
info = fopen("record.txt", "a+");
getch();
system("cls");
printf("Enter your name\n");
scanf("%[^\n]", plname);
//************************
for (j = 0; plname[j] != '\0'; j++) { //to convert the first letter after space to capital
nplname[0] = toupper(plname[0]);
if (plname[j - 1] == ' ') {
nplname[j] = toupper(plname[j]);
nplname[j - 1] = plname[j - 1];
}
else nplname[j] = plname[j];
}
nplname[j] = '\0';
//*****************************
//sdfprintf(info,"\t\t\tPlayers List\n");
fprintf(info, "Player Name :%s\n", nplname);
//for date and time
time_t mytime;
mytime = time(NULL);
fprintf(info, "Played Date:%s", ctime(&mytime));
//**************************
fprintf(info, "Score:%d\n", px = Scoreonly());//call score to display score
//fprintf(info,"\nLevel:%d\n",10);//call level to display level
for (i = 0; i <= 50; i++)
fprintf(info, "%c", '_');
fprintf(info, "\n");
fclose(info);
printf("wanna see past records press 'y'\n");
cha = getch();
system("cls");
if (cha == 'y') {
info = fopen("record.txt", "r");
do {
putchar(c = getc(info));
} while (c != EOF);
}
fclose(info);
}
int Score()
{
int score;
GotoXY(20, 8);
score = length - 5;
printf("SCORE : %d", (length - 5));
score = length - 5;
GotoXY(50, 8);
printf("Life : %d", life);
return score;
}
int Scoreonly()
{
int score = Score();
system("cls");
return score;
}
void Up()
{
int i;
for (i = 0; i <= (bend[bend_no].y - head.y) && len < length; i++)
{
GotoXY(head.x, head.y + i);
{
if (len == 0)
printf("^");
else
printf("*");
}
body[len].x = head.x;
body[len].y = head.y + i;
len++;
}
Bend();
if (!kbhit())
head.y--;
}
I tried to find information on how to fix the code, but I did not succeed.
Hi guys I have a question
How do I pass a Multi-dimensional Array from a Function to the main function and from there we want to pass it to another function.
float array2[][3] = get_sizes(numbers);
The compiler says that the string above is an error because getsizes is an invalid initaliser.
Can somebody tell us what we are doing wrong?
Thank you in advance.
EDIT
int main()
{
float anzahl = get_number();
if(feof(stdin))
{
return 0;
}
int anzahl2 = anzahl;
float array2[][3] = get_sizes(anzahl);
if (feof(stdin))
{
return 0;
}
triangulation(array2, anzahl2);
return 0;
}
int get_number(void)
{
printf("Please enter the number of triangles to check: ");
int anzahl = 0;
char check = 0;
int ret = 0;
while(!(ret == 2 && check == '\n'))
{
ret = scanf("%d%c", &anzahl, &check);
if (feof(stdin))
{
return 0;
}
if (check != '\n')
{
printf("[ERR] Invalid number of triangles.\n");
printf("Please enter the number of triangles to check: ");
my_flush();
}
if((check == '\n' && anzahl > UCHAR_MAX) || (check == '\n' && anzahl <= 0))
{
printf("[ERR] Invalid number of triangles.\n");
printf("Please enter the number of triangles to check: ");
ret = 3;
}
}
return anzahl;
void my_flush(void)
{
while(getchar() != '\n')
{
}
}
int vgl(const void *a, const void *b)
{
int aa, bb;
aa = *(int *)a;
bb = *(int *)b;
return (aa - bb);
float get_sizes(int anzahl)
{
float array [3] = {0,0,0};
float array2[100][3];
while(anzahl>0)
{
char check = 0;
int ret = 0;
while(!(ret == 2 && check == '\n'))
{
printf("Please enter the first number of the triplet: ");
ret = scanf("%f%c",&array[0], &check);
if (feof(stdin))
{
return 0;
}
if (check != '\n')
{
printf("[ERR] Invalid number of triangles.\n");
my_flush();
}
if((check == '\n' && array[0] <= 0.0)|| (check == '\n' && array[0] >=
FLT_MAX))
{
printf("[ERR] Invalid number of triangles.\n");
ret = 3;
}
}
check = 0;
ret = 0;
while(!(ret == 2 && check == '\n'))
{
printf("Please enter the second number of the triplet: ");
ret = scanf("%f%c",&array[1], &check);
if (feof(stdin))
{
return 0;
}
if (check != '\n')
{
printf("[ERR] Invalid number of triangles.\n");
my_flush();
}
if((check == '\n' && array[0] <= 0.0)|| (check == '\n' && array[0] >=
FLT_MAX))
{
printf("[ERR] Invalid number of triangles.\n");
ret = 3;
}
}
check = 0;
ret = 0;
while(!(ret == 2 && check == '\n'))
{
printf("Please enter the third number of the triplet: ");
ret = scanf("%f%c",&array[2], &check);
if (feof(stdin))
{
return 0;
}
if (check != '\n')
{
printf("[ERR] Invalid number of triangles.\n");
my_flush();
}
if((check == '\n' && array[0] <= 0.0)|| (check == '\n' && array[0] >=
FLT_MAX))
{
printf("[ERR] Invalid number of triangles.\n");
ret = 3;
}
}
qsort(array, 3, sizeof(array[3]), vgl);
array2[anzahl][0]=array[0];
array2[anzahl][1]=array[1];
array2[anzahl][2]=array[2];
anzahl--;
}
return array2;
float triangulation(float array2[100][3], int anzahl2)
{
int number = 1;
while(anzahl2>0)
{
if(array2[anzahl2][0] == 1 && array2[anzahl2][1] == 1 && array2[anzahl2]
[2] == 2)
{
printf("Tripelt %d (a=%f, b=%f, c=%f)is NO triangle.\n", number,
array2[anzahl2][0], array2[anzahl2][1], array2[anzahl2][2]);
}
else
{
printf("Tripelt %d (a=%f, b=%f, c=%f)is a triangle.\n", number,
array2[anzahl2][0], array2[anzahl2][1], array2[anzahl2][2]);
if(array2[anzahl2][0] == array2[anzahl2][1] && array2[anzahl2][0] ==
array2[anzahl2][2] && array2[anzahl2][1]== array2[anzahl2][2])
{
printf("It is an equilateral triangle.\n");
printf("It is a isosceles triangle.\n");
}
else if(array2[anzahl2][0] == array2[anzahl2][1] || array2[anzahl2][0] ==
array2[anzahl2][2] || array2[anzahl2][1]== array2[anzahl2][2])
{
printf("It is a isosceles triangle.\n");
}
else if(((array2[anzahl2][0])*(array2[anzahl2][0])) + ((array2[anzahl2]
[1])*(array2[anzahl2][1])) == ((array2[anzahl2][2])*(array2[anzahl2][2])))
{
printf("It is a right triangle.\n");
}
}
anzahl2--;
number++;
}
}
This is the whole code i'ts long
and we aren't allowed to use global variables
Here is a demonstrative program that shows how in general it can be done.
#include <stdio.h>
#include <stdlib.h>
#define N 3
float ( * get_sizes( size_t n ) )[N]
{
float ( *a )[N] = malloc( n * sizeof( float[N] ) );
for ( size_t i = 0; i < n; i++ )
{
for ( size_t j = 0; j < N; j++ )
{
a[i][j] = i * N + j;
}
}
return a;
}
void display( float( *a )[N], size_t n )
{
for ( size_t i = 0; i < n; i++ )
{
for ( size_t j = 0; j < N; j++ )
{
printf( "%4.1f ", a[i][j] );
}
putchar( '\n' );
}
}
int main(void)
{
size_t n = 4;
float ( *a )[N] = get_sizes( 4 );
display( a, n );
free( a );
return 0;
}
The program output is
0.0 1.0 2.0
3.0 4.0 5.0
6.0 7.0 8.0
9.0 10.0 11.0
That is you need to allocate the array dynamically.
This simple code indicates you how to send a bi-dimensional array to a function. The function func1 retrieves the value in float a pointed by int x and int y. The same should be done also for more than two dimensions.
#include <stdio.h>
float func1(float *,int x, int y,int mx, int my);
float func1(float * a,int x, int y,int mx, int my)
{
if (x>=mx)
return 0;
if (y>=my)
return 0;
return a[x*my+y];
}
int main(void)
{
#define _MX 4
#define _MY 3
int x, y;
float a[_MY][_MX] = {
{0.0,0.1,0.2,0.3},
{1.0,1.1,1.2,1.3},
{2.0,2.1,2.2,2.3}
};
for(x=0;x<_MX;x++) {
for(y=0;y<_MY;y++){
// With cast
printf("x =%2d y =%2d => v = %f\n",x,y,func1((float *)a,x,y,_MX,_MY));
}
}
puts("---------------------------------");
for(x=0;x<_MX;x++) {
for(y=0;y<_MY;y++){
// Without cast
printf("x =%2d y =%2d => v = %f\n",x,y,func1(a[0],x,y,_MX,_MY));
}
}
return 0;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I had a feel to solve sudoku using C programming in early October, and soon I came to know its no easy task. Currently, I did write a code to do all the functioning but there seems to be an error somewhere which is preventing the desired result to get printed or even get evaluated.
I use the algorithm provided here as a reference to develop my code,
This is the code which I have written and correcting the error would be greatly appreciated
#include <stdio.h>
int row(int i,int j,int a[9][9]);
int column(int i,int j,int a[9][9]); //function declaration's
int grid(int i,int j,int a[9][9]);
int main()
{
int a[9][9];
int i,j,x;
for (i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
a[i][j]=0;/*for making all the array values = 0*/
/*So that the stored garbage values will be removed*/
}
}
/*Entering known elements*/
printf("Enter the elements of known elements leaving the unknown as 0\n");
for (i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
scanf("%d",&a[i][j]);
}
}
//If a given grid is '0', lets assign a value 1 and if 1 exists in the same row or column or in ints correspond 3x3 box, increment the value upto 9
i=0,j=0,x=1;
incr:
if ( a[i][j] == 0)
{
a[i][j] = x;
if( row(i,j,a) && column(i,j,a) && grid(i,j,a) )
{
a[i][j] = x;
}
else
{
x++;
if ( x>9)
x = 1;
}
}
else
{
while(i < 9)
{
i++;
goto incr;
}
if (i == 9)
{
i =1;
j++;
goto incr;
}
if (j == 9)
{
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
}
}
}
int row(int i,int j,int a[9][9])
{
int m;
for(m=0;m<9;m++)
{
if( m != j)
{
if(a[i][j] == a[i][m])
{
return 0;
}
}
}
return 1;
}
int column(int i,int j, int a[9][9])
{
int m;
printf("%d%d",i,j);
for(m=0;m<9;m++)
{
if ( m != i)
{
if(a[i][j] == a[m][j])
{
return 0;
}
}
}
return 1;
}
int grid(int i,int j, int a[9][9])
{
int m,n;
if( i>=0 && i<=2 && j>=0 && j<=2) /* grid 1*/
{
for(m=0;m<=2;m++)
{
for(n=0;n<=2;n++)
{
if(i != m && j != n)
{
if(a[i][j] == a[m][n])
{
return 0;
}
}
}
}
return 1;
}
if( i>=0 && i<=2 && j>=3 && j<=5) /* grid 2*/
{
for(m=0;m<=2;m++)
{
for(n=3;n<=5;n++)
{
if(i != m && j != n)
{
if(a[i][j] == a[m][n])
{
return 0;
}
}
}
}
return 1;
}
if( i>=0 && i<=2 && j>=6 && j<=8) /* grid 3*/
{
for(m=0;m<=2;m++)
{
for(n=6;n<=8;n++)
{
if(i != m && j != n)
{
if(a[i][j] == a[m][n])
{
return 0;
}
}
}
}
return 1;
}
if( i>=3 && i<=5 && j>=0 && j<=2) /* grid 4*/
{
for(m=3;m<=5;m++)
{
for(n=0;n<=2;n++)
{
if(i != m && j != n)
{
if(a[i][j] == a[m][n])
{
return 0;
}
}
}
}
return 1;
}
if( i>=3 && i<=5 && j>=3 && j<=5) /* grid 5*/
{
for(m=3;m<=5;m++)
{
for(n=3;n<=5;n++)
{
if(i != m && j != n)
{
if(a[i][j] == a[m][n])
{
return 0;
}
}
}
}
return 1;
}
if( i>=3 && i<=5 && j>=6 && j<=8) /* grid 6*/
{
for(m=3;m<=5;m++)
{
for(n=6;n<=8;n++)
{
if(i != m && j != n)
{
if(a[i][j] == a[m][n])
{
return 0;
}
}
}
}
return 1;
}
if( i>=6 && i<=8 && j>=0 && j<=2) /* grid 7*/
{
for(m=6;m<=8;m++)
{
for(n=0;n<=2;n++)
{
if(i != m && j != n)
{
if(a[i][j] == a[m][n])
{
return 0;
}
}
}
}
return 1;
}
if( i>=6 && i<=8 && j>=3 && j<=5) /* grid 8*/
{
for(m=6;m<=8;m++)
{
for(n=3;n<=5;n++)
{
if(i != m && j != n)
{
if(a[i][j] == a[m][n])
{
return 0;
}
}
}
}
return 1;
}
if( i>=6 && i<=8 && j>=6 && j<=8) /* grid 9*/
{
for(m=6;m<=8;m++)
{
for(n=6;n<=8;n++)
{
if(i != m && j != n)
{
if(a[i][j] == a[m][n])
{
return 0;
}
}
}
}
return 1;
}
return 0;
}
Your program exits after inserting a value in the matrix. After setting the value of a position in Line 41: a[i][j] = x;, you need to increment your i or j accordingly and then call goto incr;.
PS.: Learn to use simple printf() statements, they are great debuggers.
I am trying to build a program that could add up binary numbers, of arbitrary length, separated by a whitespace. I don't want to be limited by a maximum number length, therefore I'm doing everything without built-in number types, only chars. I read the numbers from a file. I perform everything on stack.
I can't loop the part where I read and add numbers. My guess is that it's because I can't find a way to distinguish new line from EOF. The program works this way: I read first number and save it into first stack, then I read second number and save in second stack. Then I add it up, save into third stack and transfer it to first stack. I read next number, save it into second stack, add up and so on. So I basically want to loop the second reading and summing. Here is the source code part:
while( !isspace(c=(char)getc(ifp))) {
if( c == '0' || c == '1'){
if(!push(&a , c)){
clearStack(&a);
printf("alloc error");
return 0;
}
}else{
clearStack(&a);
printf("error1");
return 0;
}
}
if( a.size == 0 ){
printf("error2");
return 0;
}
while (!feof(ifp)) {
while( !isspace(c=(char)getc(ifp))) {
if( c == '0' || c == '1'){
if(!push(&b , c)){
clearStack(&a);
clearStack(&b);
printf("alloc error");
return 0;
}
}
else{
//clearStack(&a);
//clearStack(&b);
printf("error3");
//return 0;
}
}
if( b.size == 0 ){
if (a.size == 0){
printf("error4");
}
}
topA = a.size;
topB = b.size;
carry = 0;
while( topA > 0 || topB > 0) {
if ( ( topA > 0 ) ){
topA--;
if(a.data[topA] == '1'){
x = 1;
}else{
x = 0;
}
}else{
x = 0;
}
if ( ( topB > 0 ) ){
topB--;
if(b.data[topB] == '1'){
y = 1;
}else{
y = 0;
}
}else{
y = 0;
}
sum = x + y + carry;
if (sum == 2 || sum == 3){
carry = 1;
}else{
carry = 0;
}
if (sum == 1 || sum == 3) {
c = '1';
}else{
c = '0';
}
if(!push(&output , c)){
clearStack(&a);
clearStack(&b);
clearStack(&output);
printf("alloc error");
return 0;
}
}
if (carry == 1) {
if(!push(&output , '1')){
clearStack(&a);
clearStack(&b);
clearStack(&output);
printf("alloc error");
return 0;
}
}
clearStack(&a);
for (i=0; i < output.size; i++){
push(&a , output.data[i]);
}
clearStack(&b);
clearStack(&output);
} /*end of EOF while*/
For some reason, although it reads multiple times, it reads wrong and works only for 2 numbers. The file looks like this: "1 1", but when its "1 1 1" it will just add first 2 numbers. I don't know where am I making a mistake. Below I attach the whole source code. I invoke the program in console like this: ./program file. I know that the case is poorly described but can't do it in a better manner, I will add comments if anything is needed.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct stack {
char *data;
int size;
};
int push(struct stack * s, char c){
char * newData = (char *)realloc( s->data , (s->size + 1) * sizeof(char));
if( newData ){
s->data = newData;
s->data[s->size]=c;
s->size++;
return 1;
}else{
return 0;
}
}
/* not needed */
char pop(struct stack * s){
if(s->size > 0){
char result = s->data[s->size-1];
char * newData = (char *)realloc( s->data , s->size - 1);
if( newData ){
s->data = newData;
s->size--;
return result;
}else{
return 0;
}
}
else{
return 0;
}
}
struct stack newStack(){
struct stack s;
s.data = NULL;
s.size = 0;
return s;
}
void clearStack(struct stack * s){
free(s->data);
s->data = NULL;
s->size = 0;
}
int main (int argc, char const * argv[])
{
struct stack a = newStack();
struct stack b = newStack();
int topA, topB, x , y, carry, sum,i , start;
struct stack output = newStack();
char c;
FILE *ifp;
if(argc!=2){printf("Usage: ./a.out input_file."); return(1);}
if(!(ifp = fopen(argv[1], "r"))){printf("Unable to open input file!"); return(2);}
while( !isspace(c=(char)getc(ifp))) {
if( c == '0' || c == '1'){
if(!push(&a , c)){
clearStack(&a);
printf("alloc error");
return 0;
}
}else{
clearStack(&a);
printf("error1");
return 0;
}
}
if( a.size == 0 ){
printf("error2");
return 0;
}
while (!feof(ifp)) {
while( !isspace(c=(char)getc(ifp))) {
if( c == '0' || c == '1'){
if(!push(&b , c)){
clearStack(&a);
clearStack(&b);
printf("alloc error");
return 0;
}
}
else if (c == '\0' || c == ' ' || c == '\n'){
}
else{
//clearStack(&a);
//clearStack(&b);
printf("error3");
//return 0;
}
}
if( b.size == 0 ){
if (a.size == 0){
printf("error4");
//return 0;
}
}
topA = a.size;
topB = b.size;
carry = 0;
while( topA > 0 || topB > 0) {
if ( ( topA > 0 ) ){
topA--;
if(a.data[topA] == '1'){
x = 1;
}else{
x = 0;
}
}else{
x = 0;
}
if ( ( topB > 0 ) ){
topB--;
if(b.data[topB] == '1'){
y = 1;
}else{
y = 0;
}
}else{
y = 0;
}
sum = x + y + carry;
if (sum == 2 || sum == 3){
carry = 1;
}else{
carry = 0;
}
if (sum == 1 || sum == 3) {
c = '1';
}else{
c = '0';
}
if(!push(&output , c)){
clearStack(&a);
clearStack(&b);
clearStack(&output);
printf("alloc error");
return 0;
}
}
if (carry == 1) {
if(!push(&output , '1')){
clearStack(&a);
clearStack(&b);
clearStack(&output);
printf("alloc error");
return 0;
}
}
clearStack(&a);
for (i=0; i < output.size; i++){
push(&a , output.data[i]);
}
clearStack(&b);
clearStack(&output);
} /*end of EOF while*/
for (i=a.size - 1; i >= 0; i--){
if(a.data[i] == '1' || start){
start=1;
printf("%c", a.data[i]);
}
}
printf("\n");
clearStack(&a);
//clearStack(&b);
//clearStack(&output);
return 0;
}
So, essentially I have two files:
File 1:
//
// main.c
// frederickterry
//
// Created by Rick Terry on 1/15/15.
// Copyright (c) 2015 Rick Terry. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int size (char *g) {
int ofs = 0;
while (*(g+ofs) != '\0') {
++ofs;
}
return ofs;
}
int parse(char *g) {
// Setup
char binaryConnective;
int negated = 0;
// Looking for propositions
int fmlaLength = size(g);
if(fmlaLength == 0) {
return 1;
}
if(fmlaLength == 1) {
if(g[0] == 'p') {
return 1;
} else if (g[0] == 'q') {
return 1;
} else if (g[0] == 'r') {
return 1;
} else {
return 0;
}
}
// Now looking for negated preposition
if(fmlaLength == 2) {
char temp[100];
strcpy(temp, g);
if(g[0] == '-') {
negated = 1;
int negatedprop = parse(g+1);
if(negatedprop == 1) {
return 2;
}
}
}
// Checking if Binary Formula
char arrayleft[50];
char arrayright[50];
char *left = "";
char *right = "";
int numLeft = 0;
int numRight = 0;
int bclocation = 0;
int binarypresent = 0;
if(fmlaLength != 1 && fmlaLength != 2) {
if(g[0] == '-') {
int negatedBinary = parse(g+1);
if(negatedBinary == 1 || negatedBinary == 2 || negatedBinary == 3) {
return 2;
} else {
return 0;
}
}
int i = 0;
int l = 0;
int p = strlen(g);
for(l = 0; l < strlen(g)/2; l++) {
if(g[l] == '(' && g[p-l-1] == ')') {
i++;
}
}
for(int q = i; q < strlen(g); q++) {
if(g[q] == '(') {
numLeft++;
} else if(g[q] == ')') {
numRight++;
}
arrayleft[q] = g[q];
//printf("%c", arrayleft[i]);
//printf("%s", left);
if((numRight == numLeft) && (g[q+1] == 'v' || g[q+1] == '>' || g[q+1] == '^')) {
arrayleft[q+1] = '\0';
bclocation = q+1;
binaryConnective = g[q+1];
binarypresent = 1;
// printf("The binary connecive is: %c\n", binaryConnective);
break;
}
}
if(binarypresent == 0) {
return 0;
}
int j = 0;
for(int i = bclocation+1; i < strlen(g)-1; i++) {
arrayright[j] = g[i];
j++;
}
arrayright[j] = '\0';
left = &arrayleft[1];
right = &arrayright[0];
//printf("Printed a second time, fmla 1 is: %s", left);
int parseleft = parse(left);
// printf("Parse left result: %d\n", parseleft);
if(parseleft == 0) {
return 0;
}
int parseright = parse(right);
if(parseright == 0) {
return 0;
}
// printf("Parse right result: %d\n", parseleft);
if(negated == 1) {
return 2;
} else {
return 3;
}
}
return 0;
}
int type(char *g) {
if(parse(g) == 1 ||parse(g) == 2 || parse(g) == 3) {
if(parse(g) == 1) {
return 1;
}
/* Literals, Positive and Negative */
if(parse(g) == 2 && size(g) == 2) {
return 1;
}
/* Double Negations */
if(g[0] == '-' && g[1] == '-') {
return 4;
}
/* Alpha & Beta Formulas */
char binaryConnective;
int numLeft = 0;
int numRight = 0;
int bclocation = 0;
int binarypresent = 0;
int i = 0;
if(g[0] == '(') {
i++;
}
if(g[0] == '-') {
i++;
if(g[1] == '(') {
i++;
}
}
for(i; i < strlen(g); ++i) {
if(g[i] == '(') {
numLeft++;
} else if(g[i] == ')') {
numRight++;
}
if(numRight == numLeft) {
if(g[i+1] == 'v' || g[i+1] == '>' || g[i+1] == '^') {
bclocation = i+1;
binaryConnective = g[i+1];
binarypresent = 1;
break;
}
}
}
/* Connective established */
if(binaryConnective == '^') {
if(g[0] == '-') {
return 3;
} else {
return 2;
}
} else if(binaryConnective == '>') {
if(g[0] == '-') {
return 2;
} else {
return 3;
}
} else if (binaryConnective == 'v') {
if(g[0] == '-') {
return 2;
} else {
return 3;
}
}
}
return 0;
}
char bin(char *g) {
char binaryConnective;
char arrayLeft[50];
int numLeft = 0;
int numRight = 0;
int bclocation = 0;
int i = 0;
if(g[0] == '(') {
i++;
}
if(g[0] == '-') {
i++;
if(g[1] == '(') {
i++;
}
}
for(i; i < strlen(g); ++i) {
if(g[i] == '(') {
numLeft++;
} else if(g[i] == ')') {
numRight++;
}
int j = 0;
arrayLeft[j++] = g[i];
if(numRight == numLeft) {
if(g[i+1] == 'v' || g[i+1] == '>' || g[i+1] == '^') {
arrayLeft[i+1] = '\0';
bclocation = i+1;
binaryConnective = g[i+1];
return binaryConnective;
}
}
}
return binaryConnective;
}
char *partone(char *g) {
char binaryConnective;
char arrayLeft[50];
char arrayRight[50];
int numLeft = 0;
int numRight = 0;
int bclocation = 0;
int i = 0;
if(g[0] == '(') {
i++;
}
if(g[0] == '-') {
i++;
if(g[1] == '(') {
i++;
}
}
int j = 0;
for(i; i < strlen(g); ++i) {
if(g[i] == '(') {
numLeft++;
} else if(g[i] == ')') {
numRight++;
}
arrayLeft[j] = g[i];
if(numRight == numLeft) {
if(g[i+1] == 'v' || g[i+1] == '>' || g[i+1] == '^') {
arrayLeft[j+1] = '\0';
bclocation = i+1;
binaryConnective = g[i+1];
break;
}
}
j++;
}
int m = 0;
for(int k = bclocation+1; k < strlen(g)-1; k++) {
arrayRight[m] = g[k];
m++;
}
arrayRight[m] = '\0';
char* leftSide = &arrayLeft[0];
// printf("%s\n", leftSide);
// printf("%s\n", rightSide);
int k = 0;
k++;
return leftSide;
}
char *parttwo(char *g) {
char binaryConnective;
char arrayLeft[50];
char arrayRight[50];
int numLeft = 0;
int numRight = 0;
int bclocation = 0;
int i = 0;
if(g[0] == '(') {
i++;
}
if(g[0] == '-') {
i++;
if(g[1] == '(') {
i++;
}
}
int j = 0;
for(i; i < strlen(g); ++i) {
if(g[i] == '(') {
numLeft++;
} else if(g[i] == ')') {
numRight++;
}
arrayLeft[j] = g[i];
if(numRight == numLeft) {
if(g[i+1] == 'v' || g[i+1] == '>' || g[i+1] == '^') {
arrayLeft[j+1] = '\0';
bclocation = i+1;
binaryConnective = g[i+1];
break;
}
}
j++;
}
int m = 0;
int n = size(g) - 1;
if(g[strlen(g)-1] != ')') {
n++;
}
for(int k = bclocation+1; k < n; k++) {
arrayRight[m] = g[k];
m++;
}
arrayRight[m] = '\0';
char* leftSide = &arrayLeft[0];
char* rightSide = &arrayRight[0];
// printf("%s\n", leftSide);
// printf("%s\n", rightSide);
return rightSide;
}
char *firstexp(char *g) {
char* left = partone(g);
char leftArray[50];
int i = 0;
for(i; i < strlen(left); i++) {
leftArray[i] = left[i];
}
leftArray[i] = '\0';
char binConnective = bin(g);
int typeG = type(g);
if(typeG == 2) {
if(binConnective == '^') {
return &leftArray;
} else if(binConnective == '>') {
return &leftArray;
}
} else if(typeG == 3) {
if(binConnective == 'v')
return &leftArray;
}
char temp[50];
for(int i = 0; i < strlen(leftArray); i++) {
temp[i+1] = leftArray[i];
}
temp[0] = '-';
char* lefttwo = &temp[0];
if(typeG == 2) {
if(binConnective == 'v') {
return lefttwo;
}
} else if(typeG == 3) {
if(binConnective == '>' || binConnective == '^') {
return lefttwo;
}
}
return "Hello";
}
char *secondexp(char *g) {
// char binaryConnective = bin(g);
// char* right = parttwo(g);
// char rightArray[50];
// int i = 0;
// for(i; i< strlen(right); i++) {
// rightArray[i+1] = right[i];
// }
// rightArray[i] = '\0';
// int typeG = type(g);
// if(type(g) == 2) {
// if(binaryConnective == '^') {
// return &rightArray;
// }
// } else if(type(g) == 3) {
// if(binaryConnective == 'v' || binaryConnective == '>') {
// return &rightArray;
// }
// }
return "Hello";
}
typedef struct tableau tableau;
\
\
struct tableau {
char *root;
tableau *left;
tableau *right;
tableau *parent;
int closedbranch;
};
int closed(tableau *t) {
return 0;
}
void complete(tableau *t) {
}
/*int main(int argc, const char * argv[])
{
printf("Hello, World!\n");
printf("%d \n", parse("p^q"));
printf("%d \n", type("p^q"));
printf("%c \n", bin("p^q"));
printf("%s\n", partone("p^q"));
printf("%s\n", parttwo("p^q"));
printf("%s\n", firstexp("p^q"));
printf("Simulation complete");
return 0;
}*/
File 2:
#include <stdio.h>
#include <string.h> /* for all the new-fangled string functions */
#include <stdlib.h> /* malloc, free, rand */
#include "yourfile.h"
int Fsize = 50;
int main()
{ /*input a string and check if its a propositional formula */
char *name = malloc(Fsize);
printf("Enter a formula:");
scanf("%s", name);
int p=parse(name);
switch(p)
{case(0): printf("not a formula");break;
case(1): printf("a proposition");break;
case(2): printf("a negated formula");break;
case(3): printf("a binary formula");break;
default: printf("what the f***!");
}
printf("\n");
if (p==3)
{
printf("the first part is %s and the second part is %s", partone(name), parttwo(name));
printf(" the binary connective is %c \n", bin(name));
}
int t =type(name);
switch(t)
{case(0):printf("I told you, not a formula");break;
case(1): printf("A literal");break;
case(2): printf("An alpha formula, ");break;
case(3): printf("A beta formula, ");break;
case(4): printf("Double negation");break;
default: printf("SOmewthing's wrong");
}
if(t==2) printf("first expansion fmla is %s, second expansion fmla is %s\n", firstexp(name), secondexp(name));
if(t==3) printf("first expansion fmla is %s, second expansion fmla is %s\n", firstexp(name), secondexp(name));
tableau tab;
tab.root = name;
tab.left=0;
tab.parent=0;
tab.right=0;
tab.closedbranch=0;
complete(&tab);/*expand the root node then recursively expand any child nodes */
if (closed(&tab)) printf("%s is not satisfiable", name);
else printf("%s is satisfiable", name);
return(0);
}
If you look at the first file, you'll see a method called * firstexp(char * g).
This method runs perfectly, but only if another method called * secondexp(char * g) is commented out.
If * secondexp(char * g) is commented out, then *firstexp runs like this:
Enter a formula:((pvq)>-p)
a binary formula
the first part is (pvq) and the second part is -p the binary connective is >
A beta formula, first expansion fmla is -(pvq), second expansion fmla is Hello
((pvq)>-p) is satisfiableProgram ended with exit code: 0
otherwise, if *secondexp is not commented out, it runs like this:
Enter a formula:((pvq)>-p)
a binary formula
the first part is (pvq) and the second part is -p the binary connective is >
A beta formula, first expansion fmla is \240L, second expansion fmla is (-
((pvq)>-p) is satisfiable. Program ended with exit code: 0
As you can see, the outputs are completely different despite the same input. Can someone explain what's going on here?
In the commented-out parts of secondexp and in parttwo, you return the address of a local variable, which you shouldn't do.
You seem to fill a lot of ad-hoc sized auxiliary arrays. These have the problem that they might overflow for larger expressions and also that you cannot return them unless you allocate them on the heap with malloc, which also means that you have to free them later.
At first glance, the strings you want to return are substrings or slices of the expression string. That means that the data for these strings is already there.
You could (safely) return pointers into that string. That is what, for example strchr and strstr do. If you are willing to modify the original string, you could also place null terminators '\0' after substrings. That's what strtok does, and it has the disadvantage that you lose the information at that place: If you string is a*b and you modify it to a\0b, you will not know which operator there was.
Another method is to create a struct that stores a slice as pointer into the string and a length:
struct slice {
const char *p;
int length;
};
You can then safely return slices of the original string without needing to worry about additional memory.
You can also use the standard functions in most cases, if you stick to the strn variants. When you print a slice, you can do so by specifying a field width in printf formats:
printf("Second part: '%.*s'\n", s->length, s->p);
In your parttwo() function you return the address of a local variable
return rightSide;
where rightSide is a pointer to a local variable.
It appears that your compiler gave you a warning about this which you solved by making a pointer to the local variabe arrayRight, that may confuse the compiler but the result will be the same, the data in arrayRight will no longer exist after the function returns.
You are doing the same all over your code, and even worse, in the secondexp() function you return a the address of a local variable taking it's address, you are not only returning the address to a local variabel, but also with a type that is not compatible with the return type of the function.
This is one of many probable issues that your code may have, but you need to start fixing that to continue with other possible problems.
Note: enable extra warnings when compiler and listen to them, don't try to fool the compiler unless you know exactly what you're doing.