Im writing a program where it will compare the elements in 2 separate 2d array and check for elements that overlap each other. It then prints out an array where elements in both the 2d array are present but the elements in the second 2d array will replace the elements of the first array when the elements overlap each other. How do I overlap these two array into 1 array?
void tile1(char pat1[13][13]) //to arrange the pattern
{
int r,c;
for(r=0;r<13;r++)
{
for(c=0;c<13;c++)
{
if(r==0 || r==12) // top and bottom border
{
pat1[r][c] = '-';
}
else if(r>0 && r<6 && c>=0 && c<6)
{
pat1[r][c] = '^';
}
else
{
pat1[r][c] = ' ';
}
}
}
}
void tile3(char pat3[13][13]) //to arrange the pattern
{
int r,c;
for(r=0;r<13;r++)
{
for(c=0;c<13;c++)
{
if(r==0 || r==12)
{
pat3[r][c] = '-';
}
else if(r==c || r+1==c || r-1==c)
{
pat3[r][c] = 'X';
}
else
{
pat3[r][c] = ' ';
}
}
}
}
This is my codes for the 'overlap':
void overlap(char pat1[13][13], char pat2[13][13])
{
int r,c;
for(r=0;r<13;r++)
{
printf("|");
for(c=0;c<13;c++)
{
if(pat1[r][c]!=' ' || pat2[r][c]!=' ')
{
pat1[r][c] == pat2[r][c];
}
else
{
pat1[r][c] == pat1[r][c];
}
printf(" %c ", pat1[r][c]);
}
printf("|\n");
}
}
When I run the program, it prints an empty 2d array? Which part of the code should i modify, im pretty sure its the overlap function right?
The following test is incorrect:
if (pat1[r][c]!=' ' || pat2[r][c]!=' ') {
pat1[r][c] == pat2[r][c];
}
Consider the case where pat1 has a non-space and pat2 has a space. You want to leave pat1 alone, but instead this test will succeed, and it will be replaced with the space from pat2.
You can change it to:
if (pat1[r][c] == ' ') {
pat1[r][c] == pat2[r][c];
}
That will replace the pat1 entry with whatever is in pat2 (space or otherwise), if the pat1 entry is a space.
You can delete the else clause as well, since it has no effect.
Related
Given a n × n checkerboard. Each cell of the board has either character 'x', or character 'o'. Is it true that each cell of the board has even number of adjacent cells with 'o'? Two cells of the board are adjacent if they share a side.
Input
The first line contains an integer n (1 ≤ n ≤ 100). Then n lines follow containing the description of the checkerboard. Each of them contains n characters (either 'x' or 'o') without spaces.
Output
Print "YES" or "NO" (without the quotes) depending on the answer to the problem.
I have been pondering quite a while as to what I have done wrong, can someone please help me point out the error in my logic in my code.
#include<stdio.h>
int main () {
int n,flag=0;
scanf("%d",&n);
char arr[n][n];
int arr_counter[n][n];
for (int x=0;x<n;x++) {
for (int y=0;y<n;y++) {
arr_counter[x][y] = 0;
}
}
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
scanf("%c",&arr[i][j]);
}
}
// corners
// top left
if(arr[1][0]=='o') {
arr_counter[0][0] += 1;
}
if(arr[0][1] == 'o') {
arr_counter[0][0] += 1;
}
// top right
if(arr[0][n-2]=='o') {
arr_counter[0][n-1] += 1;
}
if(arr[1][n-1] == 'o') {
arr_counter[0][n-1] += 1;
}
// bottom left
if(arr[n-2][0]=='o') {
arr_counter[n-1][0] += 1;
}
if(arr[n-1][1] == 'o') {
arr_counter[n-1][0] += 1;
}
// bottom right
if(arr[n-2][n-1]=='o') {
arr_counter[n-1][n-1] += 1;
}
if(arr[n-1][n-2] == 'o') {
arr_counter[n-1][n-1] += 1;
}
// edges
for (int a=1;a<n;a++) {
if(arr[0][a+1] == 'o') {
arr_counter[0][a] += 1;
}
if(arr[0][a-1] == 'o') {
arr_counter[0][a] += 1;
}
if(arr[1][a] == 'o') {
arr_counter[0][a] += 1;
}
}
for (int b=1;b<n;b++) {
if(arr[b-1][0] == 'o') {
arr_counter[b][0] += 1;
}
if(arr[b+1][0] == 'o') {
arr_counter[b][0] += 1;
}
if(arr[b][1] == 'o') {
arr_counter[b][0] += 1;
}
}
for (int c=1;c<n;c++) {
if(arr[c-1][n-1] == 'o') {
arr_counter[c][n-1] += 1;
}
if(arr[c+1][n-1] == 'o') {
arr_counter[c][n-1] += 1;
}
if(arr[c][n-2] == 'o') {
arr_counter[c][n-1] += 1;
}
}
for (int d=1;d<n;d++) {
if(arr[n-1][d+1] == 'o') {
arr_counter[n-1][d] += 1;
}
if(arr[n-1][d-1] == 'o') {
arr_counter[n-1][d] += 1;
}
if(arr[n-2][d] == 'o') {
arr_counter[n-1][d] += 1;
}
}
//middle
for (int s=1;s<n-1;s++) {
for (int t=1;t<n-1;t++) {
if(arr[s+1][t] == 'o') {
arr_counter[s][t] += 1;
}
if(arr[s-1][t] == 'o') {
arr_counter[s][t] += 1;
}
if(arr[s][t+1] == 'o') {
arr_counter[s][t] += 1;
}
if(arr[s][t-1] == 'o') {
arr_counter[s][t] += 1;
}
}
}
for (int k=0;k<n;k++) {
for (int l=0;l<n;l++) {
if (arr_counter[k][l]%2 != 0) {
flag = 1;
}
}
}
if (flag == 0) {
printf("YES");
} else if (flag == 1) {
printf("NO");
}
}
/*
Test Case 1
input
3
xxo
xox
oxx
output
YES
Test Case 2
input
4
xxxo
xoxo
oxox
xxxx
output
NO
*/
Here I am converting an infix expression to a prefix expression.
For some test cases my result is perfect.
But for certain test cases I get an output which is correct according to some sites but wrong according to other sites. So I am in a dilemma as to whether my code is correct or not.
For example, if infix expression is:
A+B*C+D
then output according to my code is:
+A+*BCD
But some site mentions the output as:
++A*BCD
for the same input.
#include<stdio.h>
#include<string.h>
int main()
{
int top=-1,k=0,i,l,top2=-1,j,a,t,m,t1;
char s[100] ,s2[100],n, main[100];
char q[100],p[100];
printf("\nenter string :");
gets(main);
for(i=strlen(main)-1;i>=0;i--)
{
if(main[i]=='(')
{
q[k]=')';
}
else if(main[i]==')')
{
q[k]='(';
}
else
{
q[k]=main[i];
}
q[k+1]='\0';
k++;
}
k=0;
strcat(q,")");
s[++top]='(';
for(i=0;i<strlen(q);i++)
{
if(q[i]>='a' && q[i]<='z' || q[i]>='A' && q[i]<='Z' || q[i]>='0' && q[i]<='9')
{
p[k]=q[i];
p[k+1]='\0';
k++;
}
else if (q[i]=='(')
{
s[++top]=q[i];
}
else if (q[i]==')')
{
for(j=top;j>=0;j--)
{
if(s[j]!='(')
{
s[top--];
p[k]=s2[top2];
p[k+1]='\0';
s2[top2--];
k++;
}
else
break;
}
s[top--];
}
else if(q[i]=='+' || q[i]=='-' || q[i]=='*' || q[i]=='/' || q[i]=='^' || q[i]=='%')
{
if(q[i]=='+' || q[i]=='-')
{
n='1';
}
else if(q[i]=='*' || q[i]=='/' || q[i]=='%')
{
n='2';
}
else if (q[i]=='^')
{
n='3';
}
t=n-48;
if(top2!=-1)
{
for(j=top;j>=0;j--)
{
t1=s[j]-48;
if(t<=t1 )
{
s[top--];
p[k]=s2[top2];
p[k+1]='\0';
k++;
s2[top2--];
}
else if(s[j]=='(')
break;
}
}
s[++top]=n;
s2[++top2]=q[i];
}
}
k=0;
for(i=strlen(p)-1;i>=0;i--)
{
main[k]=p[i];
main[k+1]='\0';
k++;
}
printf("\nFINAL ANSWER :\n");
printf("\n%s",main);
}
I'm using function to assign the patterns of my 2D arrays. All the other arrays are displaying as it should except for my 'ans' array (the last row's dash is missing). Im guessing the problem is because of the 'pat2match' function (merges all the patterns). Because if i don's assign 'ans' to that 'function' and print an empty pattern array, all the dashes shows.
The function that merges the patterns:
char pat2match(char mypattern[13][13], char pat1[13][13], char pat2[13][13],
char pat3[13][13], char pat4[13][13], char pat5[13][13])
{
int r,c;
overlap(mypattern,pat5);
overlap(mypattern,pat2);
overlap(mypattern,pat4);
overlap(mypattern,pat3);
return overlap(mypattern,pat1);
}
Assigning the 'ans':
mytile(ans);
for(r=0;r<13;r++)
{
for(c=0;c<13;c++)
{
ans[r][c] = pat2match(ans,pattern1,pattern2,pattern3,pattern4,pattern5);
}
}
The overlap function:
char overlap(char pat1[13][13], char pat2[13][13])
{
int r,c;
for(r=0;r<13;r++)
{
//printf("|");
for(c=0;c<13;c++)
{
if(pat1[r][c] == ' ' || pat2[r][c] != ' ')
{
pat1[r][c] = pat2[r][c];
}
//printf(" %c ", pat1[r][c]);
}
//printf("|\n");
}
return pat1[r][c];
}
the empty pattern (the dash is like its border) :
void mytile(char pat[13][13])
{
int r,c;
for(r=0;r<13;r++)
{
for(c=0;c<13;c++)
{
if(r==0 || r==12)
{
pat[r][c] = '-';
}
else
{
pat[r][c] = ' ';
}
}
}
}
The return value from overlap is undefined behavior:
return pat1[r][c];
When this return statement is executed, both r and c are 13. Neither can be greater than 12. It is indexing past the bounds of the array, which is undefined behavior.
I have 5 strings. I need to compere all five at once.
char set_password1[5] = "1111";
char set_password2[5] = "2222";
char set_password3[5] = "3333";
char set_password4[5] = "4444";
char set_password5[5] = "5555";
if(!strcmp(Entered_Password,set_password1))
{
}
If any of these passwords match with Enter _Password i need to do something. so do i have to write five if statements like this
if(!strcmp(Entered_Password,set_password1))
{
}
if(!strcmp(Entered_Password,set_password2))
{
}
if(!strcmp(Entered_Password,set_password3))
{
}
if(!strcmp(Entered_Password,set_password4))
{
}
if(!strcmp(Entered_Password,set_password5))
{
}
or is there any other way.
I already tried this way, but it didn't work.
if(!strcmp(Entered_Password, (set_password1||set_password2||set_password3||set_password4||set_password5))
{
}
Alternatively, you may use a 2D array.
char password[5][5] = {"1111", "22222", "3333", "44444", "55555"};
int match = 0;
for(int i = 0; i < 5; i++)
{
if(strcmp(password[i], user_password) == 0)
{
match = 1;
break;
}
}
if(match == 1)
{
//do your action
}
Replace line
if(!strcmp(Entered_Password, (set_password1||set_password2||set_password3||set_password4||set_password5))
with
if (!(strcmp(Entered_Password, set_password1) && strcmp(Entered_Password,set_password2) && (strcmp(Entered_Password,set_password3) && strcmp(Entered_Password,set_password4) && strcmp(Entered_Password,set_password5)))
You have to compare each variable seperately with the initial string.
Code for reversing a string word by word is as follow -
#include<stdio.h>
int main()
{
int i=0;
char str[]="you are selected";
char *q,*r,*s;
q=str;
while(*q!=NULL)
{
q++;
}
q--;
s=q;
while(q!=str) //i tried for q!=str-1 but its not working
{
if(*q==' ')
{
r=q+1;
while(r!=s+1)
{
printf("%c",*r);
r++;
}
printf(" ",*r);
s=q-1;
}
q--;
}
}
but the problem is that above code is not printing first word since in condition part of the while loop i am checking address of the start of string not equal to address hold by q. Since for the first word condition becomes false. please modify the condition so that it gives desired output.
Modify the while loop to be a do.. while loop
#ashrafi iqbal
do
{
if(*q==' ')
{
r=q+1;
while(r!=s+1)
{
printf("%c",*r);
r++;
}
printf(" ",*r);
s=q-1;
}
q--;
}while(q!=str);
___EDIT__
modify if condition as
if ((*q==' ') || (*q == str[strlen(str) - 1))
Try this:
while(1)
{
if(q==str)
{
//Logic to print first word
break;
}
if(*q==' ')
{
r=q+1;
while(r!=s+1)
{
printf("%c",*r);
r++;
}
printf(" ",*r);
s=q-1;
}
q--;
}
Add the following lines after the for loop. This will take care of the word not preceded by a space (i.e. the first word)
#include<stdio.h>
int main()
{
int i=0;
char str[]="you are selected";
char *q,*r,*s;
q=str;
while(*q!=NULL)
{
q++;
}
q--;
s=q;
while(q!=str) //i tried for q!=str-1 but its not working
{
if(*q==' ')
{
r=q+1;
while(r!=s+1)
{
printf("%c",*r);
r++;
}
printf(" ",*r);
s=q-1;
}
q--;
}
r = q;
while (*r != ' ' && *r != NULL)
{
printf("%c", *r);
r++;
}
}
Modify your while loop to:
while(1)
{
if(*q==' ' || q == str)
{
if(q == str)
r = q;
else
r=q+1;
while(r!=s+1)
{
printf("%c",*r);
r++;
}
printf(" ",*r);
if(q == str)
break;
s=q-1;
}
q--;
}
Also,
str-1 is asking for a segfault.
Checking if *q is NULL is bad style. Use strlen instead.