If statement ignoring condition
#include <stdio.h>
#include<math.h>
int binary_to_decimal(int n){
    int ostatok, i=0, pom, decimal=0;
    pom=n;
    while(pom)
    {
        ostatok=pom%10;
        decimal+=ostatok*pow(2,i);
        pom/=10;
        i++;
    }
    return decimal;
}
int main()
{
    int m, n, i, max=0, number;
    scanf(" %d %d", &n, &m);
    int array[n];
    for(i=0; i<n ;i++){
        scanf(" %d", &number);
        binary_to_decimal(number);
        if(number>m) // this if statement doesn't seem to be doing it's job
            array[i]=number;
        if(array[i]>max)
            max=array[i];
    }
    for(i=0; i<n ;i++)
    {
        printf(" %d %dn", array[i], binary_to_decimal(array[i]));
    }
    printf("Max %d %d", max,  binary_to_decimal(max));
    return 0;
}
The program is: you enter two int values, m and n. m is used as a value for comparison, while n is the number of binary number the user is inputting in the loop. If the inputted number is greater than the number m print out  the values of the inputted number in binary then in decimal. At the end, print out the number with the biggest value in binary and decimal.
My problem is the if where i am comparing m and the inputted number and i can't seem to find the problem. 
c if-statement
|
show 4 more comments
#include <stdio.h>
#include<math.h>
int binary_to_decimal(int n){
    int ostatok, i=0, pom, decimal=0;
    pom=n;
    while(pom)
    {
        ostatok=pom%10;
        decimal+=ostatok*pow(2,i);
        pom/=10;
        i++;
    }
    return decimal;
}
int main()
{
    int m, n, i, max=0, number;
    scanf(" %d %d", &n, &m);
    int array[n];
    for(i=0; i<n ;i++){
        scanf(" %d", &number);
        binary_to_decimal(number);
        if(number>m) // this if statement doesn't seem to be doing it's job
            array[i]=number;
        if(array[i]>max)
            max=array[i];
    }
    for(i=0; i<n ;i++)
    {
        printf(" %d %dn", array[i], binary_to_decimal(array[i]));
    }
    printf("Max %d %d", max,  binary_to_decimal(max));
    return 0;
}
The program is: you enter two int values, m and n. m is used as a value for comparison, while n is the number of binary number the user is inputting in the loop. If the inputted number is greater than the number m print out  the values of the inputted number in binary then in decimal. At the end, print out the number with the biggest value in binary and decimal.
My problem is the if where i am comparing m and the inputted number and i can't seem to find the problem. 
c if-statement
 
 
 2
 
 
 
 
 
 - binary_to_decimal(number);this has no effect at all.
 
 – Osiris
 Nov 20 '18 at 16:37
 
 
 
 
 
 
 
 
 
 
 Add- printf("%d > %d?n", number, m);above- if(number>m)?
 
 – Fiddling Bits
 Nov 20 '18 at 16:37
 
 
 
 
 
 
 
 1
 
 
 
 
 
 My guess is that you need to write- number = binary_to_decimal(number);otherwise you compare the binary representation to the decimal one.
 
 – Osiris
 Nov 20 '18 at 16:39
 
 
 
 
 
 1
 
 
 
 
 
 - array[i]only gets a value if- number>m. That's conditional. But it is used unconditionally. Results are undefined. It's not an answer to your question, but is an additional problem.
 
 – donjuedo
 Nov 20 '18 at 16:45
 
 
 
 
 
 
 
 3
 
 
 
 
 
 @Spyware If you get- some random numberthen maybe your- binary_to_decimalfunction is broken.
 
 – Osiris
 Nov 20 '18 at 16:51
 
 
 
|
show 4 more comments
#include <stdio.h>
#include<math.h>
int binary_to_decimal(int n){
    int ostatok, i=0, pom, decimal=0;
    pom=n;
    while(pom)
    {
        ostatok=pom%10;
        decimal+=ostatok*pow(2,i);
        pom/=10;
        i++;
    }
    return decimal;
}
int main()
{
    int m, n, i, max=0, number;
    scanf(" %d %d", &n, &m);
    int array[n];
    for(i=0; i<n ;i++){
        scanf(" %d", &number);
        binary_to_decimal(number);
        if(number>m) // this if statement doesn't seem to be doing it's job
            array[i]=number;
        if(array[i]>max)
            max=array[i];
    }
    for(i=0; i<n ;i++)
    {
        printf(" %d %dn", array[i], binary_to_decimal(array[i]));
    }
    printf("Max %d %d", max,  binary_to_decimal(max));
    return 0;
}
The program is: you enter two int values, m and n. m is used as a value for comparison, while n is the number of binary number the user is inputting in the loop. If the inputted number is greater than the number m print out  the values of the inputted number in binary then in decimal. At the end, print out the number with the biggest value in binary and decimal.
My problem is the if where i am comparing m and the inputted number and i can't seem to find the problem. 
c if-statement
#include <stdio.h>
#include<math.h>
int binary_to_decimal(int n){
    int ostatok, i=0, pom, decimal=0;
    pom=n;
    while(pom)
    {
        ostatok=pom%10;
        decimal+=ostatok*pow(2,i);
        pom/=10;
        i++;
    }
    return decimal;
}
int main()
{
    int m, n, i, max=0, number;
    scanf(" %d %d", &n, &m);
    int array[n];
    for(i=0; i<n ;i++){
        scanf(" %d", &number);
        binary_to_decimal(number);
        if(number>m) // this if statement doesn't seem to be doing it's job
            array[i]=number;
        if(array[i]>max)
            max=array[i];
    }
    for(i=0; i<n ;i++)
    {
        printf(" %d %dn", array[i], binary_to_decimal(array[i]));
    }
    printf("Max %d %d", max,  binary_to_decimal(max));
    return 0;
}
The program is: you enter two int values, m and n. m is used as a value for comparison, while n is the number of binary number the user is inputting in the loop. If the inputted number is greater than the number m print out  the values of the inputted number in binary then in decimal. At the end, print out the number with the biggest value in binary and decimal.
My problem is the if where i am comparing m and the inputted number and i can't seem to find the problem. 
c if-statement
c if-statement
edited Nov 20 '18 at 17:01
dbush
99.8k13104138
99.8k13104138
asked Nov 20 '18 at 16:35
PauseUnpausePauseUnpause
6418
6418
 
 
 2
 
 
 
 
 
 - binary_to_decimal(number);this has no effect at all.
 
 – Osiris
 Nov 20 '18 at 16:37
 
 
 
 
 
 
 
 
 
 
 Add- printf("%d > %d?n", number, m);above- if(number>m)?
 
 – Fiddling Bits
 Nov 20 '18 at 16:37
 
 
 
 
 
 
 
 1
 
 
 
 
 
 My guess is that you need to write- number = binary_to_decimal(number);otherwise you compare the binary representation to the decimal one.
 
 – Osiris
 Nov 20 '18 at 16:39
 
 
 
 
 
 1
 
 
 
 
 
 - array[i]only gets a value if- number>m. That's conditional. But it is used unconditionally. Results are undefined. It's not an answer to your question, but is an additional problem.
 
 – donjuedo
 Nov 20 '18 at 16:45
 
 
 
 
 
 
 
 3
 
 
 
 
 
 @Spyware If you get- some random numberthen maybe your- binary_to_decimalfunction is broken.
 
 – Osiris
 Nov 20 '18 at 16:51
 
 
 
|
show 4 more comments
 
 
 2
 
 
 
 
 
 - binary_to_decimal(number);this has no effect at all.
 
 – Osiris
 Nov 20 '18 at 16:37
 
 
 
 
 
 
 
 
 
 
 Add- printf("%d > %d?n", number, m);above- if(number>m)?
 
 – Fiddling Bits
 Nov 20 '18 at 16:37
 
 
 
 
 
 
 
 1
 
 
 
 
 
 My guess is that you need to write- number = binary_to_decimal(number);otherwise you compare the binary representation to the decimal one.
 
 – Osiris
 Nov 20 '18 at 16:39
 
 
 
 
 
 1
 
 
 
 
 
 - array[i]only gets a value if- number>m. That's conditional. But it is used unconditionally. Results are undefined. It's not an answer to your question, but is an additional problem.
 
 – donjuedo
 Nov 20 '18 at 16:45
 
 
 
 
 
 
 
 3
 
 
 
 
 
 @Spyware If you get- some random numberthen maybe your- binary_to_decimalfunction is broken.
 
 – Osiris
 Nov 20 '18 at 16:51
 
 
 
2
2
binary_to_decimal(number); this has no effect at all.– Osiris
Nov 20 '18 at 16:37
binary_to_decimal(number); this has no effect at all.– Osiris
Nov 20 '18 at 16:37
Add
printf("%d > %d?n", number, m); above if(number>m)?– Fiddling Bits
Nov 20 '18 at 16:37
Add
printf("%d > %d?n", number, m); above if(number>m)?– Fiddling Bits
Nov 20 '18 at 16:37
1
1
My guess is that you need to write
number = binary_to_decimal(number); otherwise you compare the binary representation to the decimal one.– Osiris
Nov 20 '18 at 16:39
My guess is that you need to write
number = binary_to_decimal(number); otherwise you compare the binary representation to the decimal one.– Osiris
Nov 20 '18 at 16:39
1
1
array[i] only gets a value if number>m.  That's conditional.  But it is used unconditionally.  Results are undefined.  It's not an answer to your question, but is an additional problem.– donjuedo
Nov 20 '18 at 16:45
array[i] only gets a value if number>m.  That's conditional.  But it is used unconditionally.  Results are undefined.  It's not an answer to your question, but is an additional problem.– donjuedo
Nov 20 '18 at 16:45
3
3
@Spyware If you get
some random number then maybe your binary_to_decimal function is broken.– Osiris
Nov 20 '18 at 16:51
@Spyware If you get
some random number then maybe your binary_to_decimal function is broken.– Osiris
Nov 20 '18 at 16:51
|
show 4 more comments
                                1 Answer
                            1
                        
active
oldest
votes
The call binary_to_decimal(number) does not change your number.
Try something like:
number = binary_to_decimal(number);
 
 
 3
 
 
 
 
 
 C does not have references
 
 – Gerhardh
 Nov 20 '18 at 16:52
 
 
 
 
 
 1
 
 
 
 
 
 - int &nis C++, not C.
 
 – Barmar
 Nov 20 '18 at 16:53
 
 
 
 
 
 
 
 
 
 
 
 
 And your function never changes- n.
 
 – Barmar
 Nov 20 '18 at 16:54
 
 
 
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397523%2fif-statement-ignoring-condition%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
                                1 Answer
                            1
                        
active
oldest
votes
                                1 Answer
                            1
                        
active
oldest
votes
active
oldest
votes
active
oldest
votes
The call binary_to_decimal(number) does not change your number.
Try something like:
number = binary_to_decimal(number);
 
 
 3
 
 
 
 
 
 C does not have references
 
 – Gerhardh
 Nov 20 '18 at 16:52
 
 
 
 
 
 1
 
 
 
 
 
 - int &nis C++, not C.
 
 – Barmar
 Nov 20 '18 at 16:53
 
 
 
 
 
 
 
 
 
 
 
 
 And your function never changes- n.
 
 – Barmar
 Nov 20 '18 at 16:54
 
 
 
add a comment |
The call binary_to_decimal(number) does not change your number.
Try something like:
number = binary_to_decimal(number);
 
 
 3
 
 
 
 
 
 C does not have references
 
 – Gerhardh
 Nov 20 '18 at 16:52
 
 
 
 
 
 1
 
 
 
 
 
 - int &nis C++, not C.
 
 – Barmar
 Nov 20 '18 at 16:53
 
 
 
 
 
 
 
 
 
 
 
 
 And your function never changes- n.
 
 – Barmar
 Nov 20 '18 at 16:54
 
 
 
add a comment |
The call binary_to_decimal(number) does not change your number.
Try something like:
number = binary_to_decimal(number);
The call binary_to_decimal(number) does not change your number.
Try something like:
number = binary_to_decimal(number);
edited Nov 20 '18 at 16:54
answered Nov 20 '18 at 16:45


D. DianaD. Diana
314
314
 
 
 3
 
 
 
 
 
 C does not have references
 
 – Gerhardh
 Nov 20 '18 at 16:52
 
 
 
 
 
 1
 
 
 
 
 
 - int &nis C++, not C.
 
 – Barmar
 Nov 20 '18 at 16:53
 
 
 
 
 
 
 
 
 
 
 
 
 And your function never changes- n.
 
 – Barmar
 Nov 20 '18 at 16:54
 
 
 
add a comment |
 
 
 3
 
 
 
 
 
 C does not have references
 
 – Gerhardh
 Nov 20 '18 at 16:52
 
 
 
 
 
 1
 
 
 
 
 
 - int &nis C++, not C.
 
 – Barmar
 Nov 20 '18 at 16:53
 
 
 
 
 
 
 
 
 
 
 
 
 And your function never changes- n.
 
 – Barmar
 Nov 20 '18 at 16:54
 
 
 
3
3
C does not have references
– Gerhardh
Nov 20 '18 at 16:52
C does not have references
– Gerhardh
Nov 20 '18 at 16:52
1
1
int &n is C++, not C.– Barmar
Nov 20 '18 at 16:53
int &n is C++, not C.– Barmar
Nov 20 '18 at 16:53
And your function never changes
n.– Barmar
Nov 20 '18 at 16:54
And your function never changes
n.– Barmar
Nov 20 '18 at 16:54
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397523%2fif-statement-ignoring-condition%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
binary_to_decimal(number);this has no effect at all.– Osiris
Nov 20 '18 at 16:37
Add
printf("%d > %d?n", number, m);aboveif(number>m)?– Fiddling Bits
Nov 20 '18 at 16:37
1
My guess is that you need to write
number = binary_to_decimal(number);otherwise you compare the binary representation to the decimal one.– Osiris
Nov 20 '18 at 16:39
1
array[i]only gets a value ifnumber>m. That's conditional. But it is used unconditionally. Results are undefined. It's not an answer to your question, but is an additional problem.– donjuedo
Nov 20 '18 at 16:45
3
@Spyware If you get
some random numberthen maybe yourbinary_to_decimalfunction is broken.– Osiris
Nov 20 '18 at 16:51