Palindrome Test [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Is there anything wrong with this palindrome test?
It was written in C language.
#include<stdio.h>
main()
{
int a, b, c, d, e, div1, div2;
printf("Enter a 5 digits number. (Please put space every time you type each digit.) \n");
scanf("%d %d %d %d %d", &a,&b,&c,&d,&e);
div1= a%e;
div2= b%d;
if(div1==0 && div2==0){
printf("The number %d%d%d%d%d is a palindrome.", a, b, c, d, e);
}
else if(div1!=0 || div2!=0){
printf("The number %d%d%d%d%d is not a palindrome.", a, b, c, d, e);
}
return 0;
}

Yes
Typing 6 4 3 2 3 would make the program output that its a palindrome..
Rather then
div1= a%e; // a%e is 0 even when a = 6 and e = 3
div2= b%d; // b%d is 0 even when b = 4 and d = 2
if(div1==0 && div2==0)
a%e is 0 even when a = 6 and e = 3
b%d is 0 even when b = 4 and d = 2
You need to do the check
if(a == e && b == d)
As a side note. This is not the proper way to check for palindrome.
Check out this link for the proper way.

Yes, there is.
For example, If the input is
1 0 1 0 1
This program will invoke undefined behavoir because the second operand of % operator becomes zero, and typically will crash.
Another example: this program will consider 5 4 3 2 1 as a palindrome.
Why on the earth are there weird division? Why couldn't they be simple
div1= a==e;
div2= b==d;
? (the name divn is now not good)
There are also some minor wrong point such as using main() instead of int main(void) and not checking if the scanf() successfully read 5 numbers.

Two problems:
The test for palindromicity is a == e && b == d.
The behaviour of n % 0 is undefined. So zero d or e will give you trouble.
Luckily (2) goes away when refactoring your code to 1.

Related

Problem when I print a result of integer operation with chars in C [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I state that I'm a newbie of C programming. I have to solve this "easy" C exercise.
The program has in input two chars. These chars must be a numeric value between 0 and 9. I have to sum the chars and print the result. I have to repeat this until the first inserted char isn't -1. This is my implementation:
#include <stdio.h>
#define ASCII_ZERO 48
int main() {
char a, b;
while (1) {
scanf("%c %c", &a, &b);
int x = (int)a - ASCII_ZERO;
int y = (int)b - ASCII_ZERO;
if (x == -1) break;
int z = x + y;
char ris = z;
printf("ris: %d\n", ris);
}
exit(0);
}
When I run this code, I got this:
1 2
ris: 3
3 4
ris: -35
ris: -12
Why I get "ris" two times when I insert 3 and 4?
This works
...
char a, b, c;
while (1) {
scanf("%c %c%c", &a, &b, &c);
...
printf("ris: %d\n", ris);
}
return 0;
}
I will not comment on code. But what is happening is that a is reading '\n' when the loop runs again : which you have pressed. As you are reading characters, '\n' and white spaces are also read. I just read \n into a dummy variable c which is not used.
All this said, I suggest that you use fgets

getting wrong answer on a uva online judge problem(problem tag 10056) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Here is the problem link. I don't know why uva is showing wrong answer. For comparsion, I downloaded a solution and tried manually for many test cases. My code gives the same answer. Where is the problem?
#include<stdio.h>
#include<math.h>
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int x, n, a;
double b, sum1 = 0, sum3 = 0;
scanf("%d %lf %d", &n, &b, &a);
if (b > 0) {
x = a - 1;
double re = (pow(1 - b, x) * b) / (1 - ((pow(1 - b, x + n) * b) / (pow(1 - b, x) * b)));
printf("%.4lf\n", re);
}
else
{
printf("0.0000\n");
}
}
}
Try to return 0 at the end. There is a convention that if a program has no errors, it returns 0 at the end. Some verification softwares check if your code has run successfully by looking at it's return value.
info:
pow( doubleValue, 0 ) = 1
pow( doubleValue, 1 ) = doubleValue
the expression:
pow(1-probableWin,x )
where x is 1-1 = 0 (first player per your code)
pow(1-probableWin,x )
where x is 1 results in 1-probableWin (my idea for first player)
multiplying the above by:
*probableWin
where x is 0 results in 0 per the OPs code
*probableWin
where x is 1 results in (1-probableWin) * probableWin which is much better.
Suggest using a debugger and walking through the code, to determine where things are going wrong.
BTW: strongly suggest that long calculation of re be broken into (say) 4 or 5 statements so you can see the result of each expression.

Trouble with mod C [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I just started doing codeforces problems, I started with problem 4-A (Watermelon), in which given an int x number of kilos the program will print "YES" if when you split the watermelon in two halves, each one has an even number of kilos, if not "NO".
My problem here is that I get "YES" when the input is 5, and it should be "NO"
Here is my code:
#include <stdio.h>
int main (int x) {
if((x/2) % 2 == 0) {
printf("YES");
}else {
printf("NO");
}
return 0;
}
The problem has nothing to do with mod, but with parameter passing.
The typical prototype for main is
int main(int argument_count, char *arg_list[], ...);
Calling my_exe 5 will set the first parameter x (argument_count) as 2, and the next unused parameter
arg_list[0] = "my_exe", // the first (0th) argument is the executable name
arg_list[1] = "5" // rest of parameters as arrays of chars
Calling my_exe without parameters sets x = 1 giving YES
Also if the argument x is a character '5' with the decimal value of 53 (0x35), the result is YES. This would be rather unconventional behavior, but definitely possible within some undisclosed IDE or coding framework.
Since the argument is an int then the division the remainder will be left. That is: 5 / 2 will result in a 2. Then that 2 mod 2 will give you a zero. What you should dp is to just put x % 2 inside the 'if' statement. So, with that 5 % 2 will give you a '1' which is thenn different to zero
if((x/2) % 2 == 0)
This condition is why you get yes for 5 as 5/2 is 2(as it is int) and 2%2 is 0.
just this will work -
if(x%2==0)
Since you are performing operation with integers, (5/2)=2, Hence you are getting "YES" as the output.Try this codeint main (int x) {
if(x % 4 == 0) {
printf("YES");
}else {
printf("NO");
}
return 0;
} If the condition is true, it will mean that the halves are even as well.

C - sscanf() %d picking up zero and causing destination &curritem.stock to be NULL [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
sscanf() is great for the problem i'm facing, but it's doing something unexpected.
tmp is a line read in from a file, i need to parse out parts of the line into separate pieces of data. curritem is a struct with int dollar, int cent, cat[], int stock, and int history[].
The odd issue i'm having is that when %d picks up a double zero (00), it appropriately inserts integer zero into destination, but upon confronting a single zero (0), the integer is not added appropriately to the destination; for example, while collecting data pertaining to history[], all is well, adding 1, 4, 8, 2, 13, etc. to the array, but a single zero cuts everything off there, as if NULL terminated at that point, which i presume is related.
The code:
sscanf(tmp, "%d.%d%s %d %d %d", &curritem.dollar, &curritem.cent,
curritem.cat, &curritem.stock, &curritem.history[0],
&curritem.history[1]);
I have left out some indices of curritem.history[], as it clutters the necessary information.
Example of what i should get, if all integers were added correctly:
curritem.history[0...11] = 5 2 6 1 0 11 9 0 15 0 7 10
What actually results as of right now:
curritem.history[0...11] = 5 2 6 1
Something dies when sscanf() sees a single '0' via %d and adds that to &struct.intarray[n]
Any ideas?
Try not to recommend an entirely different solution to my general program function, sscanf() is working wonders in every other aspect.
Thank you for your time!
EDIT:
A snippet of exact input:
WL162343Fleece-Lined Double L Jeans 49.95PANTS 3 8 1 0 1 0 7 1 5 2 10 2 6
All information is acquired successfully until after the category- in this case, 'PANTS'.
With this example, my int array called history should hold the values
3 8 1 0 1 0 7 1 5 2 10 2 6
But upon printing every item in the array:
int n = 0;
for (n = 0; curritem.history[n]; n++) {
printf("%i ", curritem.history[n]);
}
The following output results:
3 8 1
EDIT:
The 'for' loop used for printing is incapable of distinguishing between an integer '0' and a null-termination of the array. Annoying.
What do you think that curritem.history[n] will return in the for's condition when it contains zero?

Determine if a number is a whole number? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to make a program that will divide a number by 2 only if that number is divisible by 2. I made it so that if the result of the number divided by 2 is a float, then divide it by two, like this:
int p;
printf("Percentage please: ");
scanf("%d", &p);
while (((p/2) != // a floating point value)
{
p = p/2;
}
But the problem is that I'm not sure if there is a function in the C standard libraries like isint() or iswholenumber(). Is there any function or any way I could implement something similar?
Any help would be appreciated.
You are looking for the modulo operation, that returns the rest of the division, so:
if( n % 2 == 1) // the number is not divisible by 2
if( n % 2 == 0) // divisible by 2
When you divide two ints the result is always an int (edit: truncated):
1/2 --> 0
2/2 --> 1
3/2 --> 1
So the logic p/2 is not a float does not make sense. Instead, as others have suggested, you want to use the modulo operator which returns the remainder of the division:
if( n % 2 ) // not divisible by 2
{
}
else // divisible by 2
{
}
Note: Since all integers that do not evaluate to 0 are equivalent to true you do not need to check n % 2 != 0.
you could ask the user for a string, then use int.TryParse
int x;
if (int.TryParse(inputString, out x))
{
// input is an integer.
}

Resources