Chess Game with C ,alignment issue - c

First of all, i don't know if the word alignment is the correct one to describe what i want help for so that's why i provide images from the console application.
I want to know if there is a way to make the zeros be in order . I am providing to you my code .
#include <stdio.h>
#include <stdlib.h>
int emptynes(int table1[16],int table2[16],int zero[32])
{
int i,k=1;
for(i=0;i<16;i++)
{
table1[i]=k;
k++;
}
k=17;
for(i=0;i<16;i++)
{
table2[i]=k;
k++;
}
for(i=0;i<32;i++)
zero[i]=0;
return table1,table2,zero;
}
int firstloop(int table1[16])
{
int i;
for(i=0;i<16;i++)
{
if(i==8)
printf("\n\n");
printf("%d ",table1[i]);
if(i<9)
printf(" ");
else
printf(" ");
}
}
int zeroloop(int zero[16])
{
int i;
for(i=0;i<32;i++)
{
if(i%8==0)
printf("\n\n");
printf("%d ",zero[i]);
}
}
int secondloop(int table2[16])
{
int i;
for(i=0;i<16;i++)
{
if(i==8)
printf("\n\n");
printf("%d ",table2[i]);
if(i<9)
printf("");
}
}
int twomoves(int x,int table1[16],int zero[32])
{
int i,p=0,l=0;
for(i=0;i<16;i++)
{
if(x==table1[i])
{
p=i;
break;
}
}
if(i>8)
l=table1[i]-8;
if(zero[l]==0&&zero[l+8]==0)
{
zero[l+7]=table1[i];
table1[i]=0;
}
return zero,table1;
}
int main()
{
printf("\n\n\n");
int table1[16],table2[16],zero[32];
int i,x,y,p;
emptynes(table1,table2,zero);
firstloop(table1);
zeroloop(zero);
printf("\n\n");
secondloop(table2);
printf("\n\n\n");
printf("Player 1: Pawn & Moves -> ");
scanf("%d %d",&x,&y);
twomoves(x,table1,zero); // do it as loop
firstloop(table1);
zeroloop(zero);
printf("\n\n");
secondloop(table2);
return 0;
}

Use a width specifier:
// Use (at least) 2 chars.
printf("%2d ", table1[i]);
// Use (at least) 2 chars. The minus means left adjustment.
printf("%-2d ", table1[i]);
// Use (at least) 2 chars, zero-padded.
printf("%02d ", table1[i]);
Output:
7
7
07

Related

a matrix as an argument of a function in c not working( making a game grid)

I'm new to c and I want to insert this code to generate a game grid, when I run it without making it inside a function it works, and when I declare it in void function, grille('0', matrice[36][36]) causes problems I don't know why!
Here is the main function of the program:
#include "GO_c1.c"
#include "switch_grille.c"
#include <time.h>
#include <string.h>
int main()
{
char matrice[36][36];
grille('0',matrice[36][36]);
return 0;
}
Here is the code of the grid in a function:
#include "Go_1.h"
#define dimension 9
#define spaceleft 2
void grille(char t,char mat[][36])
{
int a=0;
int b=1;
const int ligne_colonne=25;
printf(" "); /*2 spaces*/
for(int j=0;j<dimension;j++)
{
printf("%c", 'A'+a);
a++;
for (int k=0; k<(2*spaceleft)-1;k++)
{
printf(" "); /*5 spaces 5= 2*spaceleft-1 */
}
}
printf("\n");
for (int i=0;i<ligne_colonne;i++) /* ligne_colonne in this case= 25*/
{
if (i%spaceleft==0)
{
printf("%d ",b);
b++;
}
else
printf(" ");
for (int j=0; j<ligne_colonne;j++)
{
if ((i%spaceleft==0)&&(j%spaceleft==0))
{
mat[i][j]= t;
printf("%c ",mat[i][j]));
}
else if ((i%spaceleft==0)&&(j%spaceleft!=0))
printf("* ");
else if ((i%spaceleft!=0)&&(j%spaceleft!=0))
printf(" ");
else if ((i%spaceleft!=0)&&(j%spaceleft==0))
printf("* ");
}
printf("\n");
}
return;
}
The code of the grid in a void function
#include <stdio.h>
#include <stdlib.h>
#define dimension 9
#define spaceleft 2
int main()
{
char matrice[36][36];
int a=0, b=1;
const int ligne_colonne = (spaceleft-1)*(dimension-1)+ dimension;
printf(" "); /*2 espaces*/
for(int j=0;j<dimension;j++)
{
printf("%c", 'A'+a);
a++;
for (int k=0; k<(2*spaceleft-1);k++)
{
printf(" "); /*5 espaces 5= 2*spaceleft-1 */
}
}
printf("\n");
for (int i=0;i<ligne_colonne;i++) /* ligne_colonne dans ce cas = 25*/
{
if (i%spaceleft==0)
{
printf("%d ",b);
b++;
}
else
printf(" ");
for (int j=0; j<ligne_colonne;j++)
{
if ((i%spaceleft==0)&&(j%spaceleft==0))
{
matrice[i][j]='0';
printf("%c ",matrice[i][j]);
}
else if ((i%spaceleft==0)&&(j%spaceleft!=0))
printf("* ");
else if ((i%spaceleft!=0)&&(j%spaceleft!=0))
printf(" ");
else if ((i%spaceleft!=0)&&(j%spaceleft==0))
printf("* ");
}
printf("\n");
}
return 0;
}
I tried a matrix form [][] and then a pointer, but none of the solutions worked when setting the grid code inside a function.

omitting last comma when printing a list

I want to show prime numbers between two numbers like this 2,5,7,11
but it shows like this 2,5,7,11, there is an extra ",".
#include <stdio.h>
int main()
{
int n1,n2,f,i,j;
scanf("%d %d", &n1, &n2);
for(i=n1; i<n2; ++i)
{
f=0;
for(j=2; j<=i/2; ++j)
{
if(i%j==0)
{
f=1;
break;
}
}
if(f==0)
if(i!=1)
printf("%d,",i);
}
return 0;
}
Answering the question but not accounting for the point raised by #Bathsheeba:
#include <stdio.h>
int main()
{
int n1,n2,f,i,j;
scanf("%d %d", &n1, &n2);
int itemCount = 0; // NEW
for(i=n1; i<n2; ++i)
{
f=0;
for(j=2; j<=i/2; ++j)
{
if(i%j==0)
{
f=1;
break;
}
}
if (f == 0 && i != 1) // TESTS COMBINED
{
if (itemCount++ > 0) putchar(','); // HERE
printf("%d",i); // COMMA REMOVED
}
}
printf("\n"); // newline at the end
return 0;
}
The only way to really remove a comma after you've added it is if you're building a buffer at runtime - which is a reasonable approach - so here we have to only generate a comma when we need it, before all but the first item.

My code is not running properly not printing all the rows it should

#include <stdio.h>
int main(void) {
int i,j,n,m;
printf("enter the numbr of rows : ");
scanf("%d",&n);
m=(2*n)+1;
printf("%d, %d",n,m);
for(i=0;i<=m;i++)
{
for(j=0;j<m;j++)
{
if(i==n)
{
printf("*");
}
else
{
printf(" ");
}
}
for(j=0;j<i;j++)
{
if((i+j)>=m)
break;
printf("*");
}
printf("\n");
}
return 0;
}
My code is not running properly not printing all the rows it should. It is the code to print a full arrow like
*
**
********
**
*
its just an example.
Your problem is:
m=(2*n)+1;
It needs to be
m=(2*n);
see: Online gcc compiler example draw arrow
https://onlinegdb.com/rk9U4nUFf
It got fixed!!! i was running the loop one time less so it was running but not printing anything as the last loop was skipping a loop which it should not. Above is the correct code.
#include <stdio.h>
int main(void) {
int i,j,n,m;
printf("enter the numbr of rows : ");
scanf("%d",&n);
m=(2*n);
printf("%d, %d",n,m);
for(i=0;i<=m;i++)
{
for(j=0;j<m;j++)
{
if(i==n)
{
printf("*");
}
else
{
printf(" ");
}
}
for(j=0;j<i;j++)
{
if((i+j)>=m)
break;
printf("*");
}
printf("\n");
}
return 0;
}
Source Link: https://www.onlinegdb.com/H1gogfIHYz

C doesn't open functions

So, my C doesn't want to open my functions. I don't know where to look further as I don't have any idea what I did wrong in this one. It works fine with another exercise.
Here's what I've written:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int hello();
int sum(int);
int k, i, getal1, getal2;
int goobye(void);
int main(void)
{
int hello();
for (i = 0; i < 5; i++)
{
printf("Give 2 numbers <100 : ");
scanf("%d%*c%d%*c", &getal1, &getal2);
int sum(int k);
}
int goodbye(void);
}
int hello()
{
printf("Welcome, this program will ask you to solve 1 sum");
return 0;
}
int sum(int k)
{
int som, som2;
som = getal1 + getal2;
printf("What is the sum of %d %d? %d", getal1, getal2);
scanf("%d", &som2);
if (som == som2)
{
printf("According to you, the sum of %d and %d equals %d. That is correct", getal1, getal2, som2);
}
else
{
printf("According to you, the sum of %d and %d equals %d. That is not correct", getal1, getal2, som2);
}
return 0;
}
int goobye(void)
{
printf("Thanks for your cooperation.");
return 0;
}
Thanks in advance!
in main function you should call a function without type
int main(void)
{
hello();
for (i = 0; i < 5; i++)
{
printf("Give 2 numbers <100 : ");
scanf("%d%*c%d%*c", &getal1, &getal2);
sum(k);
}
goodbye();
}
Edit the main function like this
Calls are not perform because you don't call the functions, you just redeclare them. This is probably a copy/paste error, but in the end, the calls don't happen.
I modified you code hunk and added comments
int main(void)
{
int hello(); //here is a forward declaration of hello()
hello(); // calls hello
for (i = 0; i < 5; i++)
{
printf("Give 2 numbers <100 : ");
scanf("%d%*c%d%*c", &getal1, &getal2);
int sum(int k); // forward declaration of sum()
sum(k); //call sum on k
}
int goodbye(void); // forward declaration of goodbye
goodbye(); //call goodbye
//careful your original code has a typo; you wrote goobye()
}
Firstly you need to read about the syntax while calling functions.
Also why you assign a certain return type to a certain function i.e. the difference between int hello(); and void hello();.
Also you have placed return 0 statement at multiple places which would end your program before you actually want it to!
#include <stdio.h>
void hello();
int sum(int);
int k, i, getal1, getal2;
void goobye(void);
int main(void)
{
hello(); //no return type mention when calling a function
for (i = 0; i < 5; i++)
{
printf("Give 2 numbers <100 : ");
scanf("%d%*c%d%*c", &getal1, &getal2);
sum(getal1, getal2);//you wish to calculate the sum of these two numbers i suppose
}
goodbye();
return 0;
}
void hello()
{
printf("Welcome, this program will ask you to solve 1 sum");
// return 0 here will end your program
//return 0;
}
int sum(int getal1, int getal2)
{
int som, som2;
som = getal1 + getal2;
printf("What is the sum of %d %d? %d", getal1, getal2);
scanf("%d", &som2);
if (som == som2)
{
printf("According to you, the sum of %d and %d equals %d. That is correct", getal1, getal2, som2);
}
else
{
printf("According to you, the sum of %d and %d equals %d. That is not correct", getal1, getal2, som2);
}//again the same issue with return 0 as above
//return 0;
}
void goobye(void)
{
printf("Thanks for your cooperation.");//again the same issue with return 0 as above
//return 0;
}

creation of 2D double array from a text file

I want to write a program to create a 2D double array from a text file. The contents and format of my file is
0,0.23645,8.457
4.125,7.102,8.102
1.036,0.547,3.2298,
Same number of row and same number of column. Each number is differentiated by one ','. Each line is differentiated by one'\n' character and at the end one comma.
#include <stdio.h>
#include<conio.h>
void main()
{
FILE *myfile
float a,data[10][10];
char ch;
int i,j;
clrscr();
myfile=fopen("E:\\input.txt", "r");
for(i = 0; i<3; i++)
{
for (j = 0 ; j<3; j++)
{
fscanf(myfile,"%f %c",&a,&ch);
printf("%f ",a);
data[i][j]=a;
}
printf("\n");
}
fclose(myfile);
printf("\n");
for(i=0;i<3;i++)
{ for(j=0;j<3;j++)
{ printf("%f ",data[i][j]);
}
printf("\n");
}
getch();
}
It is working, but not for every time. Sometimes it is giving garbage values, zeros or NAN. I don't know why the result of same code gives sometimes correct or sometimes wrong values?
In this code, I have made my number of row/column constant. But if it is unknown then how do I do? So, I have written another code.
#include <stdio.h>
#include<conio.h>
void main()
{
FILE *myfile;
float a,data[10][10];
char ch;
int i,j,line=0,target=0;
int f=getc(myfile);
clrscr();
myfile=fopen("E:\\input.txt", "r");
while (f!= EOF)
{ if(f=='\n')
line++;
f=getc(myfile);
}
//printf("%d",line);
target=++line;
//printf("%d",target);
for(i = 0; i<target; i++)
{
for (j = 0 ; j<target; j++)
{
fscanf(myfile,"%f %c",&a,&ch);
printf("%f ",a);
data[i][j]=a;
}
printf("\n");
}
fclose(myfile);
printf("\n");
for(i=0;i<target;i++)
{ for(j=0;j<target;j++)
{ printf("%f ",data[i][j]);
}
printf("\n");
}
getch();
}
But it is not at all working, everytime is giving wrong values.
Kindly help
It's done with the following code.
#include <stdio.h>
#include<conio.h>
void main()
{
FILE *myfile;
float a,data[10][10];
char ch;
int i=0,j=0,target=-1;
clrscr();
myfile=fopen("E:\\input.txt", "r");
while(1)
{
while(1)
{ fscanf(myfile,"%f %c",&a,&ch);
data[i][j]=a;
j++;
if(ch=='0')
{ target=j;
break;
}
else if(j==target)
break;
}
j=0;
i++;
if(i==target)
break;
}
fclose(myfile);
for(i=0;i<target;i++)
{ for(j=0;j<target;j++)
{ printf("%f ",data[i][j]);
}
printf("\n");
}
getch();
}
removed the EOF, rather using while loop.

Resources