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 9 years ago.
Improve this question
I would like to compare a table i have and compare it with a given string number.
I have the table code ready and i guess it's not a problem to scan a string of 4 digits.
The table is initialize[1296][4] and I want it to compare to a given string[4].
I'm new to programming and i'm having a bit of difficulty. I'm actually building a Bulls and Cows game and I need to compare each digit of my table row to the given string of each column.
I hope i was clear with my question because it could get confusing or i'm just not explaining it right. I tried doing it but i can't seem to make it work.
Here's my table. If someone could help i would really appreciate it. Thanks.
void initialize(int poss[1296][4])
{
int i=0;
int j, k=0;
int m;
while (i<=5)
{
for (j=0; j<216 ; j++)
{
poss[k][0]=i;
k++;
}
i++;
}
k=0;
i=0;
j=0;
while (k<1296)
{
for (m=0; m<6; m++)
{
for (j=0; j<6; j++)
{
for (i=0; i<36 ; i++)
{
poss[k][1]=j;
k++;
}
}
}
}
k=0;
i=0;
j=0;
m=0;
while (k<1296)
{
for (j=0; j<6; j++)
{
for (i=0; i<6; i++)
{
poss[k][2]=j;
k++;
}
}
}
k=0;
i=0;
j=0;
m=0;
while (k<1296)
{
for (i=0; i<6; i++)
{
poss[k][3]=i;
k++;
}
}
}
Make sure to reduce the number of rows they can't be shown all at once on console.
Another think you have to read the definition of the bulls and cows (http://en.wikipedia.org/wiki/Bulls_and_cows). Your implimentation is not right.
Link to your code http://ideone.com/uTZf7R.
EDIT:
#include <stdio.h>
#include <string.h>
void initialize(int poss[1296][4]);
int main()
{
int table[1296][4];
char str[5];
char tmp[5];
int i, j, k;
int bull = 0;
int cow = 0;
initialize(table);
printf("Enter 4 digits: ");
gets(str);
for (i=0; i<100; i++){
strcpy(tmp, str);
for (j=0; j<4; j++){
for (k=0; k<4; k++){
if (table[i][j]==tmp[k]-'0' && j==k){
tmp[k] = -1;
bull++;
break;
}
else if (table[i][j]==tmp[k]-'0' && j!=k){
tmp[k] = -1;
cow++;
break;
}
}
}
printf ("Number: %d%d%d%d, Input: %s\n",table[i][0], table[i][1], table[i][2], table[i][3], str);
printf ("%d bulls and %d cows\n\n", bull, cow);
bull = 0;
cow = 0;
}
}
void initialize(int poss[1296][4])
{
int i=0;
int j, k=0;
int m;
while (i<=5)
{
for (j=0; j<216 ; j++)
{
poss[k][0]=i;
k++;
}
i++;
}
k=0;
i=0;
j=0;
while (k<1296)
{
for (m=0; m<6; m++)
{
for (j=0; j<6; j++)
{
for (i=0; i<36 ; i++)
{
poss[k][1]=j;
k++;
}
}
}
}
k=0;
i=0;
j=0;
m=0;
while (k<1296)
{
for (j=0; j<6; j++)
{
for (i=0; i<6; i++)
{
poss[k][2]=j;
k++;
}
}
}
k=0;
i=0;
j=0;
m=0;
while (k<1296)
{
for (i=0; i<6; i++)
{
poss[k][3]=i;
k++;
}
}
}
Change the variable names the way they serve you.
Note that string[j] - '0' is to get string[j] as an integer.
Related
Assume the matrices to be of the form,
The required output on stdout using C language should be
My code:
#include<stdio.h>
int main(void)
{
int size, i=0, j=0;
printf("Enter the size of the array:");
scanf("%d",&size);
int A[size][size], B[size][size], C[size][size];
//Entering the values in A[][]
printf("enter the elements of Array A:\n");
for(int i=0; i<size; i++)
for(int j=0; j<size; j++)
scanf("%d",&A[i][j]);
//Entering the values in B[][]
printf("enter the elements of Array B:\n");
for(int i=0; i<size; i++)
for(int j=0; j<size; j++)
scanf("%d",&B[i][j]);
// Calculating C[][]=A[][]+B[][]
for(int i=0; i<size; i++)
for(int j=0; j<size; j++)
C[i][j]=A[i][j]+B[i][j];
i=0;
j=0;
while(i<size)
{
if( i==size/2)
printf("%*c%*c\n\n",6*size+1,'+',13*(size-1),'=');
while(j<size)
printf("%d\t",A[i][j++]);
j=0;
while(j<size)
printf("%d\t",B[i][j++]);
j=0;
while(j<size)
printf("%d\t",C[i][j++]);
j=0;
printf("\n\n");
i++;
}
}
But this code only gives the desired output when size=2.
But I need a solution which works for all values of size entered by the user.
you use a tab (8 columns) to go to the next column, so replace
printf("%*c%*c\n\n",6*size+1,'+',13*(size-1),'=');
by
printf("%*c%*c\n\n",8*size-4,'+',8*size,'=');
example :
of course that supposes all numbers need up to 7 columns
If you want to always have 1 line between each line with numbers modify the while to have :
while(i<size)
{
if( i==size/2)
printf("%*c%*c\n",8*size-4,'+',8*size,'=');
else
putchar('\n');
while(j<size)
printf("%d\t",A[i][j++]);
j=0;
while(j<size)
printf("%d\t",B[i][j++]);
j=0;
while(j<size)
printf("%d\t",C[i][j++]);
j=0;
putchar('\n');
i++;
}
producing :
Out of that I encourage you to check scanf always return 1 else you do not detect an invalid input
This code will solve my question,
int size_odd_or_even=size%2;
int check=1;
while(i<size)
{
if( size_odd_or_even==0 && i==size/2 && check==1)
{
printf("\n%*c%*c\n",8*size-4,'+',8*size,'=');
check=0;
continue;
}
if( size_odd_or_even==1 && i== (int)(size/2) )
{
while( j<size-1)
printf("%d\t",A[i][j++]);
printf("%d + ",A[i][j]);
}
else
{
while( j<size)
printf("%d\t",A[i][j++]);
}
j=0;
if( size_odd_or_even==1 && i== (int)(size/2))
{
while( j<size-1)
printf("%d\t",B[i][j++]);
printf("%d = ",B[i][j]);
}
else
{
while(j<size)
printf("%d\t",B[i][j++]);
}
j=0;
while(j<size)
printf("%d\t",Sum[i][j++]);
j=0;
if( size_odd_or_even==0 && !(i==size/2-1) )
printf("\n\n");
else if(size_odd_or_even==1)
printf("\n\n");
i++;
}
This code gives the outputs for size=4 and size=5 respectively as,
If this code can be optimised then please do tell.
This question already has answers here:
correct way to return two dimensional array from a function c
(7 answers)
Closed 4 years ago.
I tried to separate this code using two int.
#include <stdio.h>
#include <conio.h>
int main(){
int a[3][3],b[3][3],c[3][3],i,d,e;
puts("multiplica 2 matrices de 3x3");
puts("Matriz A");
for (int i=0; i<3; i++){
for (int d=0; d<3; d++){
printf("introdusca el dato %d,%d\t",i+1,d+1);
scanf ("%d",&a[i][d]);
}
}
puts("Matriz B");
for (int i=0; i<3; i++){
for (int d=0; d<3; d++){
printf("introdusca el dato %d,%d\t",i+1,d+1);
scanf ("%d",&b[i][d]);
}
}
for (int i=0; i<3; i++){
for (int d=0; d<3; d++){
c[i][d]=a[i][d]*b[i][d];
}
}
for (int i=0; i<3; i++){
printf("| ");
for (int d=0; d<3; d++){
printf("%4.2d",c[i][d]);
}
printf("|");
printf("\n");
}
getch();
return 0;
}
With void it worked very well but I had troubles when I tried using int giving me random numbers instead of the real multiplication expected by the last code.
#include <stdio.h>
#include <conio.h>
int mult(int x[3][3], int y[3][3]){
int c[3][3];
for (int k=0; k<3; k++){
for (int l=0; l<3; l++){
c[k][l]=x[k][l]*y[k][l];
}
}
return c[3][3];
}
int main (){
int a[3][3],b[3][3],i,j;
puts("multiplica 2 matrices de 3x3");
puts("Matriz A");
for (int i=0; i<3; i++){
for (int j=0; j<3; j++){
printf("introdusca el dato %d,%d\t",i+1,j+1);
scanf ("%d",&a[i][j]);
}
}
puts("Matriz B");
for (int i=0; i<3; i++){
for (int j=0; j<3; j++){
printf("introdusca el dato %d,%d\t",i+1,j+1);
scanf ("%d",&b[i][j]);
}
}
for (int i=0; i<3; i++){
printf("|");
for (int j=0; j<3; j++){
printf("%4.2d",mult(a,b));
}
printf(" |");
printf("\n");
}
getch();
return 0;
}
I guess the way I put the printf("%4.2d",mult(a,b)); inside the for loop doesn't assign the data on int mult or doesn't return "c" correctly.
Ok if it is Hadamard calculations were OK. You will need to malloc your matrix before sending back the pointer.
int **mult(int x[3][3], int y[3][3])
{
if (!(c = malloc(sizeof(int*) * 3)))
return (0);
for (int k = 0; k < 3; k++)
for (int l = 0; l < 3; l++)
c[k][l] = x[k][l] * y[k][l];
return c;
}
At the moment I am creating a program where the user is asked to place the queens for the 8 Queens Problem. Right now I have nearly created the program, but I'm stuck on how to make the program check diagonally.
This is the (unfinished)code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int check_r_c(int**chess,int*p,int N,int M)
{
int times=0;
for(int i=0; i<N; i++)
{
times=0;
for(int j=0; j<M; j++)
{
if(chess[i][j] == 1)
times++;
}
if( times != 1)
{
*p=1;
return 1;
}
}
for(int j=0; j<M; j++)
{
times=0;
for(int i=0; i<N; i++)
{
if(chess[i][j] == 1)
times++;
}
if( times != 1)
{
*p=1;
return 1;
}
}
*p=0;
return 0;
}
int main()
{
int N,M;
printf("Give the number of rows: \n");
scanf("%d",&N);
printf("Give the number of columns: \n");
scanf("%d",&M);
int**chess = malloc(N*sizeof(int));
int i,j,ch;
int*ptr;
ptr=&ch;
if(chess==NULL)
{
printf("\nMemory cannot be allocated\n");
return 1;
}
for(i=0; i<N; i++)
{
if((chess[i] = malloc(M*sizeof(int)))==NULL)
{
printf("\nMemory cannot be allocated\n");
return 1;
}
}
for(int i=0; i<N; i++)
for(int j=0; j<M; j++)
chess[i][j]= 0;
for(int k=0; k<N; k++)
{
printf("Give the position of the %d queen\n",k+1);
scanf("%d",&i);
scanf("%d",&j);
if(chess[i][j] == 1)
{
printf("You cant put 2 queens in the same place!!!\n" );
return 0;
}
chess[i][j] = 1;
}
check_r_c(chess,ptr,N,M);
if(ch == 0)
printf("Solution is correct!");
else
printf("Solution is incorrect!");
for(int i=0; i<N; i++)
free(chess[i]);
free(chess);
}
The logic for checking diagonal for a queen placed is (p,q) is to check for all the places located at (p+i,q+i) for all i, where both p+i and q+i are within the board. For negative side, use (p-i,q-i).
I'm very new to this and to C programming. It's my first year and things are not very clear for me. Hopefully I can get better very soon.
I have a code here where i input a number and I get results based on the table I have below.
I would like to know: If i have a given string (in the code it's: test[7]="2 B 1 C")
How can I compare the result i have to this string and see if it's the same to print=good?
it's very hard to explain so please let me know if i'm not clear with my question. You can test the code to see how it works.
#include <stdio.h>
#include <string.h>
void initialize(int poss[1296][4]);
int main()
{
int table[1296][4];
char str[5];
char tmp[5];
int i, j, k;
int bull = 0;
int cow = 0;
char test[7]={"2 B 0 C"};
initialize(table);
printf("Enter 4 digits: ");
scanf("%s", str);
for (i=0; i<1296; i++) // building this table
{
strcpy(tmp, str); // copying string
for (j=0; j<4; j++)
{
for (k=0; k<4; k++)
{
if (table[i][j]==tmp[k]-'0' && j==k) // gets the string as an integer
{
tmp[k] = -1;
bull++;
break;
}
else if (table[i][j]==tmp[k]-'0' && j!=k)
{
tmp[k] = -1;
cow++;
break;
}
}
}
printf ("%d B %d C\n\n", bull, cow);
bull = 0;
cow = 0;
}
}
//------------------------------------TABLE---------------------------------//
void initialize(int poss[1296][4])
{
int i=0;
int j, k=0;
int m;
while (i<=5)
{
for (j=0; j<216 ; j++)
{
poss[k][0]=i;
k++;
}
i++;
}
k=0;
i=0;
j=0;
while (k<1296)
{
for (m=0; m<6; m++)
{
for (j=0; j<6; j++)
{
for (i=0; i<36 ; i++)
{
poss[k][1]=j;
k++;
}
}
}
}
k=0;
i=0;
j=0;
m=0;
while (k<1296)
{
for (j=0; j<6; j++)
{
for (i=0; i<6; i++)
{
poss[k][2]=j;
k++;
}
}
}
k=0;
i=0;
j=0;
m=0;
while (k<1296)
{
for (i=0; i<6; i++)
{
poss[k][3]=i;
k++;
}
}
}
You can use sprintf to generate the result into a string instead of printing it, then use strcmp to compare that string to what you expect it to be. You can also then use printf to print the string you produced with sprintf. (When you sprintf, don't include the newlines since those aren't in your test string; only output those with printf).
What's not clear from your question, however, is that you only have one test string but print 1296 lines ... if those lines aren't all the same then you need an array of 1296 test results ... or a clearer question.
sample
#include <stdio.h>
#include <string.h>
int main(){
char test[8]={"2 B 0 C"};
char result[8];
int bull = 0;
int cow = 0;
bull = 2;//set by program
cow = 0;
sprintf(result, "%d B %d C", bull, cow);
if(strcmp(result, test)==0){
printf("match!\n");
} else {
printf("not match!\n");
}
return 0;
}
I'm creating a sudoku solver in C and having trouble obtaining user input. The code that I've written doesn't input the data into the game board, but if I change Game_Buffer[counter] to Game_Buffer[i] it inputs the data but only 9 characters. I am aware of why. I just wanted to see if their were problems in other areas.
My primary question is: Why is the method I'm using not placing the user input data into the game board array?
#include <stdio.h>
#include <string.h>
#define CELL 81
int main()
{
// Banner
printf("\t\t\t\tSudoku Solver\n");
printf("\t\t\t***************************\n");
//initialize variables
char Game_Board[9][9];
int i,j;
char Game_Buffer[CELL];
int counter = 0;
printf("Please enter the numbers of the board * denotes a blank space\n");
fgets(Game_Buffer,CELL,stdin);
for(i=0;i<strlen(Game_Buffer);i++)
printf("%c", Game_Buffer[i]);
while(counter < 81)
{
for(i=0; i<9; i++)
for(j=0; j<9; j++)
Game_Board [i][j] = Game_Buffer [counter];
counter++;
}
printf("%d\n", counter);
printf("\t\t\t\t The Board\n");
for( i=0; i<9; i++)
for( j=0; j<9; j++)
{
if( j % 3 == 0)
printf("|");
printf("%c", Game_Board[i][j]);
if(j==8)
printf("|\n");
}
return 0;
}
You probably should use brackets at first place.
for(i=0; i<9; i++)
{
for(j=0; j<9; j++)
{
Game_Board [i][j] = Game_Buffer [counter];
counter++;
}
}
Add all missing brackets and check if your issue still exists.
The counter++ executes after the loop. I have idented the code to show what I mean..
for(i=0; i<9; i++)
for(j=0; j<9; j++)
Game_Board [i][j] = Game_Buffer [counter];
counter++;
You are updating all cells with the same value.