printf() function and displaying % - c

I am trying to execute a program that prints the numerical value when the && operator returns true and when it returns false. The code is as follows:-
#include <stdio.h>
main()
{
int a,b;
scanf("%d%d",&a,&b);
printf("Part I\n");
printf("(a%2 == 0) && (b%2 == 0): %d\n",(a%2 == 0) && (b%2 == 0));
printf("(a%3 == 0) && (b%3 == 0): %d\n",(a%3 == 0) && (b%3 == 0));
printf("(a%5 == 0) && (b%5 == 0): %d\n",(a%5 == 0) && (b%5 == 0));
printf("(a%7 == 0) && (b%7 == 0): %d\n",(a%7 == 0) && (b%7 == 0));
printf("Part II\n");
printf("The AND operator yields: %d\n",(a%2 == 0) && (b%2 == 0));
printf("The AND operator yields: %d\n",(a%3 == 0) && (b%3 == 0));
printf("The AND operator yields: %d\n",(a%5 == 0) && (b%5 == 0));
printf("The AND operator yields: %d\n",(a%7 == 0) && (b%7 == 0));
return 0;
}
The output ( along with my input ) is as follows:-
210
210
Part I
(a%2 == 0) && (b%2 == 0): %d
(a%2 == 0) && (b%2 == 0): %d
(a%2 == 0) && (b%2 == 0): %d
(a%2 == 0) && (b%2 == 0): %d
Part II
The AND operator yields: 1
The AND operator yields: 1
The AND operator yields: 1
The AND operator yields: 1
Why is the first part behaving in such a manner? This is happening even when I replace && by ||. I am using a Borland C++ Compiler 5.5 . Please Help.

Because if you want to actually display a %, then you must escape it in the printf format string with another %. e.g.
printf("(a%%2 == 0) && (b%%2 == 0): %d\n",(a%2 == 0) && (b%2 == 0));
^ ^

I've tested this with http://codepad.org/, which I think uses gcc, and the code worked ok. But you might try to add an extra % before a literal % (i.e, %%) so the compiler knows the % that follows is an actual character. Like this:
printf("Part I\n");
printf("(a%%2 == 0) && (b%%2 == 0): %d\n",(a%2 == 0) && (b%2 == 0));
printf("(a%%3 == 0) && (b%%3 == 0): %d\n",(a%3 == 0) && (b%3 == 0));
printf("(a%%5 == 0) && (b%%5 == 0): %d\n",(a%5 == 0) && (b%5 == 0));
printf("(a%%7 == 0) && (b%%7 == 0): %d\n",(a%7 == 0) && (b%7 == 0));

You are actually using illegal escape sequence character to print % in the first part. Thats why printf is yielding garbage values.
printf("(a%2 == 0) && (b%2 == 0): %d\n",(a%2 == 0) && (b%2 == 0));
^ ^
Here is you are mistaking
It should be like
printf("(a%%2 == 0) && (b%%2 == 0): %d\n",(a%2 == 0) && (b%2 == 0));
You can also read about all format specifiers used in C.

Related

Arduino Exiting Loop To Previous Loop

So currently I am trying to write code for an arduino car/robot to solve a maze made of black tape on white background. The current sensors I am working with is a wheel encoder(lm393), yellow standard motors, and IR emmiters and detectors in paralell(QTR-8RC).
Currently I am having a problem where I have code to follow the line, and I have code to turn the car around 180 degrees by counting the encoder pulses once it exits the line. The problem is that the car just keeps spinning and that is because it is stuck in the function turnAround();. Is there any way I can go back to the lineFollow(); function once my car is done rotating 180 degrees?
The code used is here:
void turnAround()
{
// NOW THE problem is that the counter is not jumping back to the line follower
while(countRW <= 27 && countLW <= 27) {
printEncoderMesurements();
analogWrite(RWF, 180);
analogWrite(RWB, 0);
analogWrite(LWF, 0);
analogWrite(LWB, 180);
}
resetCounters();
followLine();
}
void followLine() {
if (
((s2 == 0) && (s3 == 1) && (s4 == 1) && (s5 == 0))||
((s2 == 1) && (s3 == 1) && (s4 == 1) && (s5 == 0))||
((s2 == 0) && (s3 == 1) && (s4 == 1) && (s5 == 1))||
((s2 == 0) && (s3 == 0) && (s4 == 1) && (s5 == 1))||
((s2 == 1) && (s3 == 1) && (s4 == 0) && (s5 == 0))||
((s2 == 0) && (s3 == 0) && (s4 == 1) && (s5 == 0))||
((s2 == 0) && (s3 == 1) && (s4 == 0) && (s5 == 0))){
forwards();
}else if(s0 == 1 || s1 == 1) {
slowLeft();
}else if(s7 == 1 || s6 == 1){
slowRight();
}else if((s0 == 0)&&(s1 == 0)&&(s2 == 0) && (s3 == 0) && (s4 == 0) && (s5 == 0) && (s6 == 0) && (s7 == 0)) {
turnAround();
}
}
void loop(){
makeIRReadings();
printEncoderMesurements();
followLine();
delay(50);
}
I have tried every variations of different exit(0) or return 0 but none worked.
Your while loop is checking if countRW and countLW are less than 28, but while its looping in that while loop, you never update countRW or countLW so they will forever be less than 28.
Also, I'm not sure how your sensors are setup, but it seems like you drive off the line before turning around. So then after it turns around, it's still off the line, so it will call turnAround() again. You need a way to "catch" the line after turning around so it could follow it again.

If condition doesn't output properly yet it compiles in C

So it compiles ok, but the output it is not the expected. The issue I think is in the OR operator which does not work properly, or it conflicts with the && operator. But perhaps it's bad written or something. Please let me know. Thanks in advance.
Here's the code:
if((len == 16) && secDigit == (51 | 52 | 53 | 54 | 55)
{
printf("MASTERCARD\n");
exit(0);
}
else if((len == 15) && secDigit == (34 | 37))
{
printf("AMEX\n");
exit(0);
}
else if((len == (16 | 13)) && firstDigit == 4)
{
printf("VISA\n");
exit(0);
}
else
{
printf("INVALID\n");
exit(0);
}
| is the Binary OR operator while what you need is || ,the Logical OR operator.
Syntax:
a|b
(Boolean 1) || (Boolean 2)
Note: Conditional operators evaluate to boolean values
if((len == 16) && (secDigit >= 51 && secDigit<= 55))
{
printf("MASTERCARD\n");
exit(0);
}
else if((len == 15) && (secDigit == 34 || secDigit == 37))
{
printf("AMEX\n");
exit(0);
}
else if((len == 16 || len == 13)) && firstDigit == 4)
{
printf("VISA\n");
exit(0);
}
else
{
printf("INVALID\n");
exit(0);
}

Case 3: if n is multiple of both 3 and 5

I'm not able to find the write line for third case. I'm a beginner in programming world. Much appreciated if someone helped me with this Thanks in advance!
#include <stdio.h>
int main()
{
int n;
scanf("%d",&n);
if(n%3==0) {
printf("\n 1");}
else if(n%5==0){
printf("\n 2");}
else if((n%3==0) && (n%5==0)){
printf("\n 3");}
else{
printf("\n 4");
}
return 0;
}
The 3. test condition, else if ((n % 3 == 0) && (n % 5 == 0)), will never be true, because if n % 3 == 0 the 1. test, if (n % 3 == 0), will catch it before. The same goes for the case if n % 5 == 0 with the 2. test, else if (n % 5 == 0).
You need to place if ((n % 3 == 0) && (n % 5 == 0)) at the start instead to proof both sub-expressions before the testing for each sub-expression:
scanf("%d",&n);
if ((n % 3 == 0) && (n % 5 == 0)) {
printf("\n 1");
}
else if (n % 5 == 0) {
printf("\n 2");
}
else if (n % 3 == 0) {
printf("\n 3");
}
else {
printf("\n 4");
}
First you check if the number can be divided by 3, then you check if it can be divided by 5 and then if it can be divided by both.
If it can be divided by both (in other words it can be divided by 15), then it can be divided by 3, so you'll get out of the first loop and print "\n 1".
Change the order of your test. If you put the longest case first, then if both n%3 and n%5 == 0, it will print. Otherwise, it can be one or the other.
if (n%3 == 0 && n%5 == 0)
{
printf("\n 3"); // You might want this to be printf("3\n"); instead
}
else if... // other cases.
Another way to do it if you really want to keep the order is to test on the single cases that they aren't both true, ie:
if (n%3 == 0 && n%5 != 0)
{
// Only a multiple of 3
}
else if (n%5 == 0 && n%3 != 0)
{
// Only a multiple of 5
}
else if (n%3 == 0 && n%5 == 0)
{
// Multiple of 3 and 5
}
I'm not able to find the write line for third case.
else if((n%3==0) && (n%5==0))
3rd line never true as first if(n%3==0) would have also been true and code would have executed that block.
Simplify with
// v------v 1 for multiple of 3
// v------v 2 for multiple of 5
int m15 = (n%3 == 0)*1 + (n%5 == 0)*2;
if (m15 == 0) m15 = 4;
printf("\n %d", m15);`

Function recursively runs forever if value above 3 is input

As a mandatory preface, I am new to C, and am likely simply missing something extremely obvious. I appreciate any and all time and effort taken to look over my silly problem.
I have a recursive function whose purpose is to print out a large "x" made of smaller x characters, where width is the length of each side of the x. For example, a width of "3" would have the following output:
Shape:
X X
X
X X
Returning.
Where "Returning." prints just before returning to main.
The following function does just this for a width of 1 and 3, but fails to do so with 5, 7, 9, etc.
void Recurse(int left, int right, int flag, int num){
int i;
if(((left && right) == num/2) && (flag == 0)){
for(i=0;i<num;i++){
if (i == (num/2) ){
printf("X");
}
else
printf(" ");
}
printf("\n");
flag = 1;
Recurse(left-1, right+1, flag, num);
}
else if(flag == 0){
for(i=0;i<num;i++){
if((i == left) || (i == right)){
printf("X");
}
else
printf(" ");
}
printf("\n");
Recurse(left+1, right-1, flag, num);
}
else if(flag == 1){
for(i=0;i<num;i++){
if((i == left) || (i == right)){
printf("X");
}
else
printf(" ");
}
printf("\n");
if (((left == 0) && (right == num-1)) && (flag == 1))
printf("\nReturning.\n");
return;
Recurse(left-1, right+1, flag, num);
}
}
The only thing I have in my main function is an initialization of width with some odd int, and a call to the function. I would like to get the code actually... printing correctly prior to cleaning up the logic a bit. Thank you for any help provided.
In
if(((left && right) == num/2) && (flag == 0)){
left && right is a boolean value, probably you wanted
if ((left == num/2) && (right == num/2) && (flag == 0)){

tic tac toe in c, trying to determine who wins

Trying to determine who wins in a game of tic tac toe,I am new to programming. Currently getting user has won after only entering 1 X or O input. Inputs must be entered with 2 int, row and coloumn. Any help is greatly appreciated!
#include <stdio.h>
#include <stdlib.h>
void drawBoard(char board[][3])
{
int rows, columns;
for ( rows = 0 ; rows < 3 ; rows++ )
{
for ( columns = 0 ; columns < 3 ; columns++ )
{
if(board[rows][columns]){
printf( "|%c", board[rows][columns] );
}else{
printf("| ");
}
}
printf("|\n");
}
}
int main()
{
char game[3][3]={{0}};
int totalEntry =0,row,column;
char value;
while(totalEntry<=9){
printf("Please choose x or o: ");
scanf("%c",&value);
getchar();
printf("Enter row number: ");
scanf("%d",&row);
getchar();
printf("Enter Column number: ");
scanf("%d",&column);
getchar();
game[row][column] = value;
drawBoard(game);
if((game[0][0] == game[0][1]) && (game[0][1] == game[0][2]) && game[0][0] != 'x')
if((game[1][0] == game[1][1]) && (game[1][1] == game[1][2]) && game[1][0] != 'x')
if((game[2][0] == game[2][1]) && (game[2][1] == game[2][2]) && game[2][0] != 'x')
if((game[0][0] == game[1][0]) && (game[1][0] == game[2][0]) && game[0][0] != 'x')
if((game[0][1] == game[1][1]) && (game[1][1] == game[2][1]) && game[0][1] != 'x')
if((game[0][2] == game[1][2]) && (game[1][2] == game[2][2]) && game[0][2] != 'x')
if((game[0][0] == game[1][1]) && (game[1][1] == game[2][2]) && game[0][0] != 'x')
if((game[2][0] == game[1][1]) && (game[1][1] == game[0][2]) && game[2][0] != 'x')
printf("User x has won!");
if((game[0][0] == game[0][1]) && (game[0][1] == game[0][2]) && game[0][0] != 'o')
if((game[1][0] == game[1][1]) && (game[1][1] == game[1][2]) && game[1][0] != 'o')
if((game[2][0] == game[2][1]) && (game[2][1] == game[2][2]) && game[2][0] != 'o')
if((game[0][0] == game[1][0]) && (game[1][0] == game[2][0]) && game[0][0] != 'o')
if((game[0][1] == game[1][1]) && (game[1][1] == game[2][1]) && game[0][1] != 'o')
if((game[0][2] == game[1][2]) && (game[1][2] == game[2][2]) && game[0][2] != 'o')
if((game[0][0] == game[1][1]) && (game[1][1] == game[2][2]) && game[0][0] != 'o')
if((game[2][0] == game[1][1]) && (game[1][1] == game[0][2]) && game[2][0] != 'o');
printf("User o has won!");
break;
}
return 0;
}
I would have used something like following, using a loop. Note that since the board is initialized to zero, assigning winner to a board position which still has zero means that a winner isn't found even if the test for all equal passes.
//declare this outside the main while-loop:
char winner=0;
for(i=0;i<3 && !winner;i++) {
if(game[i][0]==game[i][1] && game[i][1]==game[i][2])
winner=game[i][0]; // across
else if(game[0][i]==game[1][i] && game[1][i]==game[2][i])
winner=game[0][i]; // down
}
if( !winner && ( game[0][0]==game[1][1] && game[1][1]==game[2][2] ||
game[0][2]==game[1][1] && game[1][1]==game[2][0] ) )
winner = game[1][1]; // diagonal
if(winner) {
printf( "Winner is %c!\n",winner );
break;
}
IckIckIck.
First, it'll make you life much easier (at least for determining the winner), if you use a one-dimenisional array of 9 elements.
Then, write a function which test one possible win and returns true or false.
bool Test(char board[9], char player, int a, int b, int c)
{
return board[a] == player
&& board[b] == player
&& board[c] == player;
}
Then Test(game, 'x', 0,3,6); -- tests of x wins down left side.
The logic in your conditional is incorrect. Imagine the board looks like this. 0 denotes an empty spot.
0 0 0
0 0 x
0 0 0
The statement if((game[0][0] == game[0][1]) && (game[0][1] == game[0][2]) && game[0][0] != 'x') will evaluate to true because game[0][0] is empty game[0][1] is empty and game[0][2] is empty so they are all equal.
Also since game[0][0] is empty, it is not equal to 'x'.
So changing your != to == will help in that regard.

Resources