C matrix character not appearing printing properly - c

I am trying to fill a char matrix in C and print it in C but I get only weird characters. I am running this programm on windows, hence the system(cls);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void paint(char tab[10][10], int lignes, int colonnes)
{
system("cls");
for (int i = 0; i < lignes; i++)
{
for (int j = 0; j < colonnes; j++)
{
printf("%c", tab[i][j]);
}
printf("\n");
}
}
void create(char tab[10][10], int lignes, int colonnes)
{
for (int i = 0; i < lignes; i++)
{
for (int j = 0; j < colonnes; j++)
{
tab[i][j] = ' ';
if (i == 0 || i == lignes--)
tab[i][j] = 205;
if (j == 0 || j == colonnes--)
tab[i][j] = 186;
if (i == 0 && j == 0)
tab[i][j] = 201;
if (i == 0 && j == colonnes--)
tab[i][j] = 187;
if (i == lignes-- && j == 0)
tab[i][j] = 200;
if (i == lignes-- && j == colonnes--)
tab[i][j] = 188;
if (i == 50 && j == 50)
tab[i][j] = 248;
}
}
}
int main()
{
char tab[10][10];
create(tab, 10, 10);
paint(tab, 10, 10);
char i;
while(scanf(" %c", &i) != 'q')
{
}
}
I tried changing the output type in printf with %d and %s as shown in other answers here, but %d shows random numbers and %s makes the programm crash, I don't know if it is because of a segfault somewhere.I also tried using simple characters to fill my matrix, not their ascii value.
Don't mind the ineffective scanf , I am still trying to figure out keyboard input without using enter on my own for now.

there were a few problems with your code, like colonnes-- changes the value of colonnes while cheking your if statement
I believe you wanted to draw a rectangle
try this (it's your own code, modified a bit)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char tab[10][10];
void paint(int lignes, int colonnes)
{
system("cls");
for (int i = 0; i < lignes; i++)
{
for (int j = 0; j < colonnes; j++)
{
printf("%c", tab[i][j]);
}
printf("\n");
}
}
void create(int lignes, int colonnes)
{
for (int i = 0; i < lignes; i++)
{
for (int j = 0; j < colonnes; j++)
{
tab[i][j] = ' ';
if (i == 0 || i == lignes-1)
tab[i][j] = 205;
if (j == 0 || j == colonnes-1)
tab[i][j] = 186;
if (i == 0 && j == 0)
tab[i][j] = 201;
if (i == 0 && j == colonnes-1)
tab[i][j] = 187;
if (i == lignes-1 && j == 0)
tab[i][j] = 200;
if (i == lignes-1 && j == colonnes-1)
tab[i][j] = 188;
if (i == 50 && j == 50)
tab[i][j] = 248;
}
}
}
int main()
{
create(10, 10);
paint(10, 10);
}

Related

Why does the first column not get printed when I am using gotoxy(x,y)

So I am building a map with borders and filling up with ' * '
Now what I want to do is empty all of the ' * ' and fill them up with blank spaces.
I am not getting the expected output and can't figure out what I am doing wrong, I'd really appreciate if someone could help me.
#include <stdio.h>
#define gotoxy(x,y) printf("\033[%d;%dH", (y), (x))
int height=5;
int width=5;
void fill_blank_spaces()
{
gotoxy(0,0);
for(int i=0;i<height;i++)
{
for(int j=0;j<width;j++)
{ if(i!=0 && j!=0 && i!=height-1 && j!=width-1)
printf(" ");
}
printf("\n");
}
}
I expect the output to be:
X---X
| |
| |
| |
X---X
But the displayed output is:
X---X
*|
*|
*|
X---X
int main()
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if ((i == 0 && j == 0) || (i == 0 && j == width - 1) || (j == 0 && i == height - 1) || (j == width - 1 && i == height-1))
printf("X");
else if ((j == 0) || (j == width - 1))
printf("|");
else if (i == height - 1 || i == 0)
printf("-");
else
printf("*") ;
}
printf("\n");
}
fill_blank_spaces();
}
I am new here so excuse my unconventional description.
The top left corner is at ( 1, 1).
The first space on the following lines should go in
( 2, 2), ( 2, 3) and ( 2, 4)
#include <stdio.h>
#define gotoxy(x,y) printf("\033[%d;%dH", (y), (x))
int height=5;
int width=5;
void fill_blank_spaces()
{
for ( int i = 1; i < height - 1; i++)
{
for ( int j = 1; j < width - 1; j++)
{
gotoxy ( j + 1, i + 1);
printf ( " ");
}
}
printf ( "\n");
printf ( "\n");
}
int main ( void)
{
gotoxy ( 1, 1);
for ( int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if ((i == 0 && j == 0) || (i == 0 && j == width - 1) || (j == 0 && i == height - 1) || (j == width - 1 && i == height-1))
printf("X");
else if ((j == 0) || (j == width - 1))
printf("|");
else if (i == height - 1 || i == 0)
printf("-");
else
printf("*") ;
}
printf("\n");
}
fill_blank_spaces();
}
You don't need fill_blanck_spaces, simply replace * by a space :
int main()
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if ((i == 0 && j == 0) || (i == 0 && j == width - 1) || (j == 0 && i == height - 1) || (j == width - 1 && i == height-1))
printf("X");
else if ((j == 0) || (j == width - 1))
printf("|");
else if (i == height - 1 || i == 0)
printf("-");
else
printf(" ") ; //<=== Here
}
printf("\n");
}
return 0;
}

Exception Thrown when attempting to compare using pointers

I am trying to make a loop that makes a pointer point to different elements in an array (Sorting in descending order) and I can't seem to get the comparing correctly since I always get an exception thrown. I also have a loop to print all the pointer's elements to test if the loop worked correctly. Never really used pointers before, but I tried to format them the same way I've seen on other websites when studying pointers. This is the part of code I am talking about:
//What I have included
#define _CRT_SECURE_NO_WARNINGS
#define MAX 5
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
//Variables related to the loops
int nums[MAX], *ptrd[MAX];
//Loops in question (Assume nums[MAX] = {1, 2, 3, 4, 5})
for (int i = 0; i < MAX; i++)
{
for (int j = 0; j < MAX; j++)
{
if (*ptrd[i] < nums[j] && nums[j] <= *ptrd[i - 1])
{
if (i > 0)
{
if (ptrd[i] == ptrd[i - 1])
continue;
}
ptrd[i] = &nums[i];
}
}
}
for (int i = 0; i < MAX; i++)
{
printf("\n%d", *ptrd[i]);
}
Current full code so far (note the code in question here is different):
#define _CRT_SECURE_NO_WARNINGS
#define MAX 5
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
//Variables
char input[20];
int nums[MAX], *ptrd[MAX], *ptra[MAX];
bool isValid;
//Methods
bool checkValidity();
void convertChars();
void resetInput();
int main()
{
printf("Please enter 5 numbers (Separate each by spaces): ");
input:
resetInput();
scanf(" %[^\n]%*c", &input);
isValid = checkValidity();
if (isValid == false)
{
printf("Invalid input. Retry: ");
goto input;
}
convertChars();
for (int i = 0; i < MAX; i++)
{
for (int j = 0; j < MAX; j++)
{
if (*ptrd[i] < nums[j])
{
if (i > 0)
{
if (ptrd[i] == ptrd[i - 1])
continue;
if (nums[j] <= *ptrd[i - 1])
ptrd[i] = &nums[i];
}
else
ptrd[i] = &nums[i];
}
}
}
for (int i = 0; i < MAX; i++)
{
printf("\n%d", (*ptrd)[i]);
}
getchar();
return 0;
}
bool checkValidity()
{
bool multNum = false;
int chars = 0;
for (int i = 0; i < 20; i++)
{
if (!isdigit(input[i]))
{
if (input[i] == ' ' && multNum == true || input[i] == NULL && multNum == true)
chars += 1;
if (input[i] != ' ' && input[i] != NULL)
{
printf("\nIncorrect characters\n");
return false;
}
if (input[i] == ' ' && multNum == false)
{
printf("\nToo many spaces at at once\n");
return false;
}
if (input[i] == ' ' && multNum == true || input[i] == NULL && multNum == true)
multNum = false;
}
else if (isdigit(input[i]))
{
multNum = true;
}
}
if (chars != 5)
{
printf("\nIncorrect amount of nums (%d)\n", chars);
return false;
}
else
return true;
}
void convertChars()
{
int placeHolder, nums_ = 0, done = 0;
for (int i = 0; i < 20 && done < 5; i++)
{
if (isdigit(input[i]))
{
placeHolder = input[i] - '0';
nums_ = (nums_ * 10) + placeHolder;
}
else
{
nums[done] = nums_;
nums_ = 0;
done += 1;
}
}
}
void resetInput()
{
for (int i = 0; i < 20; i++)
{
input[i] = NULL;
}
}
You didn't initialize *ptrd[MAX], which results in an undefined behavior. Run your code with gdb, and it crashes with following output:
(gdb) run
Starting program: /home/pringles/Desktop/a.out
Please enter 5 numbers (Separate each by spaces): 3 2 1 5 6
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400719 in main () at test.c:37
37 if (*ptrd[i] < nums[j])
By printing the elements in ptrd, you can see all the elements point to 0x0:
(gdb) print ptrd
$1 = {0x0, 0x0, 0x0, 0x0, 0x0}

How to extend my multiple loop conditions?

My code
#include <stdlib.h>
#include <stdio.h>
int main()
{
int i=0;
int j=0;
size_t count=0;
float numbers[20][100];
float velocity[21][101];
char *line = NULL;
FILE *myFile;
myFile = fopen("vel.txt", "r");
if (myFile == NULL)
{
printf("Error Reading File\n");
exit (0);
}
while(i < 20 && getline(&line, &count, myFile)!=-1) {
int len = 0, pos = 0;
j = 0;
while(j < 100 && 1 == sscanf(line + pos, "%f%n", &numbers[i][j++], &len))
pos += len;
i++;
}
free(line);
fclose(myFile);
i=1;
for( j = 0; j < 101; j++ )
{
if( j == 1 )
{
velocity[i][j]=numbers[i][j];
}
else if ( j == 101 )
{
velocity[i][j]=numbers[i][j];
}
else
{
velocity[i][j]=(numbers[i][j-1]+numbers[i][j])/2;
}
}
for (j=0 ; j<101 ; j++) {
printf("\n%f", velocity[i][j]);
}
}
I need to calculate velocities for 21,101 two dimensional mesh.If i==1 ,that is my code above and works fine.The sam conditions apply if i==21.But for all other values (2 to 20) calculations are different.How should I change
if( i== from 2 to 20 &&j == 1 )
{
do something
}
else if (i== from to to 20 && j == 101 )
{
do something 2
}
else(means i goes from 2,20 j goes from 2,100)
{
do something 3
}
Do you want something like this: if(i >= 2 %% i <= 20)? Means: 2 <= i <= 20 or if i is greater or the same as 2 and i is lower or the same as 20 it is true.
If your example:
if(i >= 2 && i <= 20 && j == 1)
{
//do something
}
else if(i >= 2 && i <= 20 && j == 101)
{
//do something 2
}
else if(i >= 2 && i <= 20 && j >= 2 && j <= 100) //means i goes from 2,20 j goes from 2,100
{
//do something 3
}
or is there anything I missed?

C Filling Chess board with T and H, somehow there are gaps and only 7 rows instead of 8

I had my previous question closed as it was too broad, so I decided to write code in parts and only ask something if I had a specific problem.
My code so far sees what parts of chess board are filled with knights (in code 'H') and fill the parts they dominate with 'T'. The goal is to fill all parts of the chess board with 'H' and 'T' and to have not dominated blocks left at '0'. Then the program tests if all the fields are either 'T' or 'H', if yes returns 1, if no 0. Then it prints out the chess board.
However it only prints out 7 rows (still 8 columns though), and even though I filled the board with 12 knights that are in position to dominate the entire board, somehow there are still 0s left.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
int filling(char array[7][7])
{
int i, j;
for (i = 0;i < 8;i++)
{
for (j = 0; j < 8;j++)
{
if(array[i][j] == 'H')
{
if(i-1>-1 && j+2<8) array[i-1][j+2]='T';
if(i+1<8 && j+2<8) array[i+1][j+2]='T';
if(i-2>-1 && j+1<8) array[i-2][j+1]='T';
if(i+2<8 && j+1<8) array[i+2][j+1]='T';
if(i-2>-1 && j-1>-1) array[i-2][j-1]='T';
if(i+2<8 && j-1>-1) array[i+2][j-1]='T';
if(i-1>-1 && j-2>-1) array[i-1][j-2]='T';
if(i+1<8 && j-2>-1) array[i+1][j-2]='T';
}
}
}
}
int checking(char array[7][7])
{
int i, j;
for(i = 0;i < 8;i++)
{
for(j = 0;j < 8;j++)
{
if(array[i][j] != 'H' && array[i][j] != 'T')
return 0;
else return 1;
}
}
}
int main()
{
int i, j;
char board[7][7];
for(i = 0; i < 8; i++)
{
for(j = 0; j < 8; j++)
board[i][j] = '0';
}
board[2][1] = 'H';
board[2][2] = 'H';
board[3][2] = 'H';
board[5][2] = 'H';
board[6][2] = 'H';
board[5][3] = 'H';
board[2][4] = 'H';
board[1][5] = 'H';
board[2][5] = 'H';
board[4][5] = 'H';
board[5][5] = 'H';
board[5][6] = 'H';
filling(board);
if(checking(board) == 1) printf ("Works");
else printf ("Doesnt work");
for(i = 0; i < 8; i++)
{
printf("\n");
for(j = 0; j < 8; j++)
printf("%c ", board[i][j]);
}
return 0;
}
What do you think is the problem here? Am I doing something worng?
Thank you for your answers.
EDIT: Made a mistake not making sure that horses dont get changed into 'T's. Here is the fixed code:
#include <stdio.h>
#include <stdlib.h>
int pildymas(char array[7][7])
{
int i, j;
for (i = 0;i < 8;i++)
{
for (j = 0; j < 8;j++)
{
if(array[i][j] == 'H')
{
if(i-1>-1 && j+2<8 && array[i-1][j+2]!='H') array[i-1][j+2]='T';
if(i+1<8 && j+2<8 && array[i+1][j+2]!='H') array[i+1][j+2]='T';
if(i-2>-1 && j+1<8 && array[i-2][j+1]!='H') array[i-2][j+1]='T';
if(i+2<8 && j+1<8 && array[i+2][j+1]!='H') array[i+2][j+1]='T';
if(i-2>-1 && j-1>-1 && array[i-2][j-1]!='H') array[i-2][j-1]='T';
if(i+2<8 && j-1>-1 && array[i+2][j-1]!='H') array[i+2][j-1]='T';
if(i-1>-1 && j-2>-1 && array[i-1][j-2]!='H') array[i-1][j-2]='T';
if(i+1<8 && j-2>-1 && array[i+1][j-2]!='H') array[i+1][j-2]='T';
}
}
}
}
int tikrinimas(char array[7][7])
{
int i, j;
for(i = 0;i < 8;i++)
{
for(j = 0;j < 8;j++)
{
if(array[i][j] != 'H' && array[i][j] != 'T')
return 0;
else return 1;
}
}
}
int main()
{
int i, j;
char board[7][7];
for(i = 0; i < 8; i++)
{
for(j = 0; j < 8; j++)
board[i][j] = '0';
}
board[2][1] = 'H';
board[2][2] = 'H';
board[3][2] = 'H';
board[5][2] = 'H';
board[6][2] = 'H';
board[5][3] = 'H';
board[2][4] = 'H';
board[1][5] = 'H';
board[2][5] = 'H';
board[4][5] = 'H';
board[5][5] = 'H';
board[5][6] = 'H';
for(i = 0; i < 8; i++)
{
printf("\n");
for(j = 0; j < 8; j++)
printf("%c ", board[i][j]);
}
pildymas(board);
if(tikrinimas(board) == 1) printf ("Veikia");
else printf ("neveikia");
for(i = 0; i < 8; i++)
{
printf("\n");
for(j = 0; j < 8; j++)
printf("%c ", board[i][j]);
}
return 0;
}
I notice people say that my array doesnt have enough size. I was under impression that an array A[2] will have A[0] A[1] and A[2] spots, is this wrong?
Thank you for your help, I am pretty sure that I will be back tonight though :)
EXTRA QUESTION
I have it all working now with filling and testing, but I need to find a way to fill the board with all the possible combinations of 12 horses, and the only way I have in mind is creating a for cycle that is 12 levels deep, meaning it would have to do 64^12 cycles. Is there any other way I could try every possible positioning of 12 horses on a 8x8 board? My plan is to take a position, then test if it fits, if so save it, and then try another one until all the positions are done. As far as I know there are only 1\2 combinations of 12 horses to dominate all fields on the board.
I have no idea what type of dominance you are attempting to create on the board, but to get your indexes within the correct bounds for a 7x7 array, you will need something similar to the following:
#include <stdio.h>
enum { DIM = 7 };
void filling (char (*array)[DIM])
{
int i, j;
for (i = 0; i < DIM; i++)
for (j = 0; j < DIM; j++)
if (array[i][j] != 'H')
array[i][j] = 'T';
}
int checking (char (*array)[DIM])
{
int i, j;
for (i = 0; i < DIM; i++)
for (j = 0; j < DIM; j++)
if (array[i][j] != 'H' && array[i][j] != 'T')
return 0;
return 1;
}
int main (void) {
int i, j;
char board[DIM][DIM] = {{0}};
board[2][1] = 'H';
board[2][2] = 'H';
board[3][2] = 'H';
board[5][2] = 'H';
board[6][2] = 'H';
board[5][3] = 'H';
board[2][4] = 'H';
board[1][5] = 'H';
board[2][5] = 'H';
board[4][5] = 'H';
board[5][5] = 'H';
board[5][6] = 'H';
filling (board);
if (checking (board) == 1)
printf ("Works");
else
printf ("Doesnt work");
for (i = 0; i < DIM; i++) {
printf ("\n");
for (j = 0; j < DIM; j++)
printf ("%c ", board[i][j]);
}
putchar ('\n');
return 0;
}
Example Use/Output
$ ./bin/chess
Works
T T T T T T T
T T T T T H T
T H H T H H T
T T H T T T T
T T T T T H T
T T H H T H H
T T H T T T T
Or the beauty in properly eliminating all magic numbers from your code and using proper constants is changing 1 number is all that is needed to create an 8x8. e.g.
enum { DIM = 8 };
And now the output is:
$ ./bin/chess
Works
T T T T T T T T
T T T T T H T T
T H H T H H T T
T T H T T T T T
T T T T T H T T
T T H H T H H T
T T H T T T T T
T T T T T T T T
Code with everything fixed and working.
#include <stdio.h>
#include <stdlib.h>
int pildymas(char array[8][8])
{
int i, j;
for (i = 0;i < 8;i++)
{
for (j = 0; j < 8;j++)
{
if(array[i][j] == 'H')
{
if(i-1>-1 && j+2<8 && array[i-1][j+2]!='H') array[i-1][j+2]='T';
if(i+1<8 && j+2<8 && array[i+1][j+2]!='H') array[i+1][j+2]='T';
if(i-2>-1 && j+1<8 && array[i-2][j+1]!='H') array[i-2][j+1]='T';
if(i+2<8 && j+1<8 && array[i+2][j+1]!='H') array[i+2][j+1]='T';
if(i-2>-1 && j-1>-1 && array[i-2][j-1]!='H') array[i-2][j-1]='T';
if(i+2<8 && j-1>-1 && array[i+2][j-1]!='H') array[i+2][j-1]='T';
if(i-1>-1 && j-2>-1 && array[i-1][j-2]!='H') array[i-1][j-2]='T';
if(i+1<8 && j-2>-1 && array[i+1][j-2]!='H') array[i+1][j-2]='T';
}
}
}
}
int tikrinimas(char array[8][8])
{
int i, j;
for(i = 0;i < 8;i++)
{
for(j = 0;j < 8;j++)
{
if(array[i][j] != 'H' && array[i][j] != 'T')
return 0;
}
}
return 1;
}
int main()
{
int i, j;
char board[8][8];
for(i = 0; i < 8; i++)
{
for(j = 0; j < 8; j++)
board[i][j] = '0';
}
board[2][1] = 'H';
board[2][2] = 'H';
board[3][2] = 'H';
board[5][2] = 'H';
board[6][2] = 'H';
board[5][3] = 'H';
board[2][4] = 'H';
board[1][5] = 'H';
board[2][5] = 'H';
board[4][5] = 'H';
board[5][5] = 'H';
board[5][6] = 'H';
for(i = 0; i < 8; i++)
{
printf("\n");
for(j = 0; j < 8; j++)
printf("%c ", board[i][j]);
}
pildymas(board);
if(tikrinimas(board) == 1) printf (" \n Veikia");
else printf ("\n neveikia");
for(i = 0; i < 8; i++)
{
printf("\n");
for(j = 0; j < 8; j++)
printf("%c ", board[i][j]);
}
return 0;
}
Solved my problem. Here are the things I had to change:
1) Increase array size by 1 in both dimensions.
2) Make sure my 'H's dont get overwritten by 'T's.
3) In checking function I made a mistake of including return 1 in a cycle, which therefore would print out that it works as long as the first value was working. I left return 0 in cycle, because as long as there is one spot empty, it doesnt work, but I moved return 1 after the cycle, as if only there are no empty spaces it should count as a success.

testcase not running in c program

iam new to c program and facing difficulty in debugging programs.In the below code test case 2 is not running.I have found that the error is in reading interger n in the second test case.someone please hep me with this issue.Also please recommend me with some tools that can be ued for debugging c programs using terminal.Thanks for help
#include <stdio.h>
#include <stdlib.h>
int read(){
int r = 0;
char c = getchar_unlocked();
while(c >= '0' && c <= '9'){
r = r*10 + c - 48 ;
c = getchar_unlocked();
}
return r;
}
void main(){
int t = 0;
t = read();
int rr = 0;
for(rr = 0;rr < t;rr++){
int i,n = 0;
n = read();
int *p = (int *)calloc(n,sizeof(int));
for(i = 0;i < n;++i){
*(p+i) = getchar_unlocked() - 48;
}
int no,nz = 0;
for(i = 0;i < n;++i){
if(*(p+i) == 0){nz += 1;}
if(*(p+i) == 1){no += 1;}
}
int k = 0;
if(((no)%2 == 0) && ((nz)%2) == 0){
k = -1;
}
if(((no)%2 == 0) && ((nz)%2) == 1){
k = 0;
}
if(((no)%2 == 1) && ((nz)%2) == 0){
k = 1;
}
if(((no)%2 == 1) && ((nz)%2) == 1){
k = 1;
}
int result = 0;printf("%d\n",5556);
if(k == 1){
for(i = 0;i < n;++i){
if(*(p+i) == 1){
result = i+1 ;
break;
}
}
}
if(k == 0){
for(i = 0;i < n;++i){
if(*(p+i) == 0){
result = i+1 ;
break;
}
}
}
printf("%d\n",result);
}
}
Your strategy to read an integer is flawed. You don't have the logic to skip whitespaces. I would change the function name to read_int and change its implementation to
int read(){
int n;
if ( scanf("%d", &n) != 1 )
{
// Deal with the error
}
return n;
}
Also, change
*(p+i) = getchar_unlocked() - 48;
to
*(p+i) = read_int();
or a more intuitive version:
p[i] = read_int();
With those changes, I am able to read and process the numbers. But I still get the wrong output. I'll let you figure the logic error in your code.
Additional Comments
main is expected to return an int. If your compiler didn't complain about that, it's time to up the warning level. I use -Wall by default.
When you are in the process of debugging your code, it's always good to test the code that reads the input to make sure that there is no error in reading the input.
Here's what I did to your code:
#include <stdio.h>
#include <stdlib.h>
int read_int(){
int n;
if ( scanf("%d", &n) != 1 )
{
// Deal with the error.
}
return n;
}
int main(){
int t = 0;
int rr = 0;
t = read_int();
printf("t = %d\n", t);
for(rr = 0;rr < t;rr++){
int i,n = 0;
n = read_int();
printf("n = %d\n", n);
int *p = (int *)calloc(n,sizeof(int));
for(i = 0;i < n;++i){
p[i] = read_int();
printf("p[%d] = %d\n", i, p[i]);
}
int no,nz = 0;
for(i = 0;i < n;++i){
if(*(p+i) == 0){nz += 1;}
if(*(p+i) == 1){no += 1;}
}
int k = 0;
if(((no)%2 == 0) && ((nz)%2) == 0){
k = -1;
}
if(((no)%2 == 0) && ((nz)%2) == 1){
k = 0;
}
if(((no)%2 == 1) && ((nz)%2) == 0){
k = 1;
}
if(((no)%2 == 1) && ((nz)%2) == 1){
k = 1;
}
int result = 0;
// printf("%d\n",5556);
if(k == 1){
for(i = 0;i < n;++i){
if(*(p+i) == 1){
result = i+1 ;
break;
}
}
}
if(k == 0){
for(i = 0;i < n;++i){
if(*(p+i) == 0){
result = i+1 ;
break;
}
}
}
printf("%d\n",result);
}
return 0;
}

Resources