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.
Related
i'm trying to make a slot machine type thing and i wanted to assign the randomly generated numbers to certain symbols like 1 = cherry, 2 = bell and so on so i could print out the results in symbol form at the end.
i tried putting the symbols as strings in an array and assigning the numbers to the elements in the array in each slot functions but it didn't work out... is there a way to do this?
here's the code i've written so far, minus the array attempts. any suggestions would be helpful! :D
EDIT: here's an example of what i've tried doing on one of the slots but it keeps saying i need a cast to assign the integer from a pointer (i've tried searching online but idk how to do this)
char * slotOne(int randOne, const char *symbols[])
{
randOne = rand() % 4 + 1;
if (randOne = 1)
{
randOne = *symbols;
}
if (randOne = 2)
{
randOne = *(symbols+1);
}
if (randOne = 3)
{
randOne = *(symbols+2);
}
else
{
randOne = *(symbols+3);
}
return randOne;
}
this is the part of my main function where i've tried declaring the string array:
int main()
{
int x, one, two, three;
const char *symbols[4] = {"bell", "orange", "cherry", "horseshoe"};
srand(time(NULL));
one = slotOne(x);
two = slotTwo(x);
three = slotThree(x);
printf("%s - %s - %s\n", one, two, three);
//...
}
not sure if %s or %c is the right type too...
At least these problems:
Code is assigning = when it should compare ==.
// if (randOne = 1)
if (randOne == 1)
The last if () { ... } else { ... } will cause one of the 2 blocks to execute. OP wants an if () { ... } else if () { ... } else { ... } tree.
// Problem code
if (randOne = 3) {
randOne = *(symbols+2);
} else {
randOne = *(symbols+3);
}
Suggest
if (randOne == 1) {
randOne = *symbols;
} else if (randOne == 2) {
randOne = *(symbols+1);
} else if (randOne == 3) {
randOne = *(symbols+2);
} else {
randOne = *(symbols+3);
}
Also research switch.
switch (randOne) {
case 1:
randOne = *symbols;
break;
case 2:
randOne = *(symbols+1);
break;
case 3:
randOne = *(symbols+2);
break;
default:
randOne = *(symbols+3);
break;
}
Or consider a coded solution:
randOne = *(symbols+(randOne-1));
But code needs to return a pointer to a string not an int and has no need to pass in randOne as a parameter.
const char * slotOne(const char *symbols[]) {
int randOne = rand() % 4;
return symbols[randOne];
}
Calling code also needs to adjust to receive a const char *, not an int.
// int one;
// one = slotOne(x);
const char *one = slotOne(symbols);
int ValidFirstName(char *firstname) {
while (*firstname != ' ') {
if (((*firstname>='a')&&(*firstname<='z'))||((*firstname>='A')&&(*firstname<='Z'))){
firstname++;
} else {
return 0;
}
}
return 1;
}
It is always returning a 0. The input is a pointer to a string like "sam johnson:".
Your code looks right, Try this main function:
main()
{
char* name = "sam johnson:";
printf("%d", ValidFirstName(name));
}
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.
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.
I'm using a structure array to save data about specific person, and have 2 function which write out data. 3rd function (Wypisz) uses pointer to use specified one. The problem is when i try to compile it, error occurs in this function in both 'if clauses':
error: expected expression before 'struct'
struct Osoba
{ char imie[MAX], nazwisko[MAX];
int rokurodzenia[N];
};
void WypiszWLinii(struct Osoba osoba[])
{ int i;
for(i=0;i<N;i++)
{
printf("%c %c, %d\n", osoba[i].imie, osoba[i].nazwisko, osoba[i].rokurodzenia);
}
}
void WypiszJedenPoDrugim(struct Osoba osoba[])
{
int i;
for(i=0;i<N;i++)
{
printf("%c\n%c\n%d\n", osoba[i].imie, osoba[i].nazwisko, osoba[i].rokurodzenia);
}
}
void Wypisz(void (*wskfun)(struct Osoba), int i)
{
if(i=1)
{
wskfun=WypiszJedenPoDrugim(struct Osoba osoba[]);
}
else if(i=0)
{
wskfun=WypiszWLinii(struct Osoboa osoba[]);
}
else
{
printf("Wybrano zla opcje w menu\n");
return -1;
}
}
N and MAX is predefined variable with library inclusions
the problem you have in the Wypisz function implementation is the assignement of pointer to a function to a wrong value.
Instead to :
wskfun=WypiszJedenPoDrugim(struct Osoba osoba[]);
or
wskfun=WypiszWLinii(struct Osoba osoba[]);
you have to get the pointer to that function and assing it to the variable:
wskfun = &WypiszJedenPoDrugim;
or
wskfun = &WypiszWLinii;
After that you could use the pointer to the function like:
wskfun("pointer to struct array");
Said that, to solve your problem the function must be changed like:
void Wypisz(void(*wskfun)(struct Osoba[]), int i)
{
if (i = 1)
{
wskfun = &WypiszJedenPoDrugim;
}
else if (i = 0)
{
wskfun = &WypiszWLinii;
}
else
{
printf("Wybrano zla opcje w menu\n");
return -1;
}
}