Trouble with mod C [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
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.

Related

how would I write "while( x/y is not a multiple of 4)" [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am relatively new to programming and I want to it to loop in a "do while" loop while the quotient of two variables is not a multiple of 4.
The modulo operator, or % will do exactly as you need. It will return the "remainder" of dividing a number by a given value. Using that logic, if the result is 0, then the specified number was divided evenly by the value.
Therefore:
do
{
// do stuff that changes x and/or y
} while ((x / y) % 4) != 0)
Should accomplish your goals.
You divide your x/y value, and then use % 4 on the result. If the result is zero, then the it was evenly divisible by 4, if it is not zero, there was a remainder and it was not evenly divisible.
As pointed out in a comment below, the do...while syntax first "does", then evaluates, which although not indicated in your question, is unlikely the intended behavior, and what you need is simply a while loop without the do. This first evaluates, and then "does" only if the result was true, otherwise does nothing.
while (y != 0 && (x / y) % 4) != 0)
{
// do stuff that changes x and/or y
}
I'll give you the math. Let Z = X / Y. Then you want Z % 4 to be non-zero.
try like this.You can use z to store result of (x/y)%4.
main(){
int x=0,y=0, z=0;
do{ // do anything
}
while((x/y)%4 != 0);
}

C, decimal system to other base system without loops [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Write a function unsigned int pose (unsigned int val, unsigned int base),
which returns a value val given in decimal notation for each given value val.
Value of val in the system can be construed with the base base. You can
assume that base is always between 2 and 9, and that val is sufficiently small to
on the target system to cause any number range violation.
Examples:
• pose (3,2) = 11 // 310 = 1 · 2 + 1 · 1 = 12
• pose (5,5) = 10 // 510 = 1 · 5 + 0 · 1 = 105
• pose (19,5) = 34 // 1910 = 3 · 5 + 4 · 1 = 345
• pose (5,6) = 5 // 510 = 5 · 1 = 56
• pose (7,7) = 10 // 710 = 1 · 7 + 0 · 1 = 107
• pose (543,9) = 663 // 54310 = 6 · 9
2 + 6 · 9 + 3 · 1 = 6639
Please note the following rules:
• Your output archive should contain exactly one file named "convert.c".
This file defines the function pose () and possibly other calls to be made by pose ()
Functions. Do not specify a main () function, do not include a makefile.
• You must not use loops (keywords for, while, goto).
• You must not use global variables.
• You may not use a library function.
Coderredoc's answer shows a nice solution using recursion. However, the only sensible interpretation of the question is to print val in base:
void pose (unsigned int val, unsigned int base){
if(val) {
pose(val/base,base);
printf("%c", (val%base)+'0');
}
}
int main(void)
{
pose (8,8); printf("\n");
pose (8,2); printf("\n");
pose (19,5); printf("\n");
return 0;
}
Output:
10
1000
34
First few hints or approach on how to solve it:-
Just solve the problem first.
Use loops and local variables and whatever is needed.
Once you solved it. Then look for applying the constraints.
The thing pretty easily indicates that you got to use recursion. Now rest is pretty easy - just decide what you will do in one loop. And transfer it to another recursive call with proper parameters.
unsigned int pose (unsigned int val, unsigned int base){
unsigned int ret = 0;
if(val)
ret=base*pose(val/base,base)+(val%base);
return ret;
}
Dissecting this code will reveal few things:-
I am just doing the necessary step in each step. In loop solution basically a digit is extracted. Here I do the same.And the reduced subproblem is then solved by another call to pose.
If you consider standard solution you will see the digits are extracted in reverse order. You don't want that. Instead of using another extra variable you should just use recursion to hold the result and do the necessary final operation with it. Reverse is done base*pose(val/base,base)+(val%base); using this specifically.
As discussed with Paul, the question is much more logical if we interpret it to simply "print the number in converted base b".
The solution will be similar like this:-The code next shown is of Paul's idea. Previous code is mine.
void pose (unsigned int val, unsigned int base){
if(val) {
pose(val/base,base);
printf("%c", (val%base)+'0');
}
}

Understanding Simple For Loop Code in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm beginner in C programming (just started) and i need help from you to understand the output of this very simple code:
int main()
{
int x=1;
for (;x<=10;x++);
printf("%d\n",x);
return 0;
}
output is:
11
the same output if x value is <=11
and if x value is 12 or more, it prints the exact value of x (ex: if int x=12; the output is 12).
how did the computer understand this code?
So,
int main()
{
int x=1; // line 1
for (;x<=10;x++); // line 2
printf("%d\n",x); // line 3
return 0; // line 4
}
Line 1 initializes x to 1.
Line 2 keeps increasing x by 1 until it reaches 11. The first semicolon indicates "don't do anything before starting the loop", x<=10 indicates keep going until x > 10 (so when x = 11) and x++ means increase x by 1 each time. If x >= 11, this line gets basically skipped because x is already greater than 10.
Line 3 prints out x to the command line (in this case, x = 11 if x started out less than 11 or just x if x started at >= 11 due to the previous line)
Line 4 means the program was successful, exit the program.
for is this:
for(*init-expr*; *test-expr*; *update-expr*)
*body-statement*
Or rather, commonly, it can be decribed like this:
*init-expr*;
while(*test-expr*){
*body-statement*
*update-expr*;
}
and, your for statement is followed by a semicolon, where body-statement is.So, it is a "null statement", just loop and update x, when finishes the loop, just print the x after loop, so, the output is 11.

Palindrome Test [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
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.

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