Function. Language C

Asked

Viewed 42 times

-1

Good evening, I am doing a function exercise, where I type two numbers and choose which operation I want to do, 1 it applies the sum function and 2 the subtraction function, but my algorithm is not working and I would like to know what I am doing wrong =s follows the algorithm

#include <stdio.h>
#include <stdlib.h>

int somanumero(int numero1, int numero2){
    int result;
    result = numero1 +numero2;
    return result; 
}

int subnumero (int numero1, int numero2){
    int result;
    result = numero1 - numero2;
    return result;
}

int main (){
    int n1,n2,resp,result;
    result = 0;
    printf ("\nDigite o primeiro numero: ");
    scanf ("%d", &n1);
    printf ("\nDigite o segunto numero: ");
    scanf ("%d", &n2);
    printf ("\nQual operação gostaria de fazer. Soma [1] Sub [2]");
    scanf ("%d", resp);
    if (resp = 1){
        result = somanumero(n1,n2);
    }
    else {
        result = subnumero (n1,n2);
    }
    printf ("O resultado da equação é: %d", result);
}
  • 1

    Instead of if (resp = 1), has to be if (resp == 1) - the operator = assigns the value, and since 1 is considered "true", it always enters the if. Already the operator == makes the comparison correctly

2 answers

0

It is very likely that you have studied algorithms before going into programming and there we used the receive as an arrow (<-) just remember that in most languages receiving it is a sign of simple equal that is to say if you want to check if x equals y would x==y otherwise only lacked the "&" on the third scan Good luck with your studies

#include <stdio.h>
#include <stdlib.h>

int somanumero(int numero1, int numero2){
    int result;
    result = numero1 +numero2;
    return result; 
}

int subnumero (int numero1, int numero2){
    int result;
    result = numero1 - numero2;
    return result;
}

int main (){
    int n1,n2,resp,result;
    result = 0;
    printf ("\nDigite o primeiro numero: ");
    scanf ("%d", &n1);
    printf ("\nDigite o segunto numero: ");
    scanf ("%d", &n2);
    printf ("\nQual operação gostaria de fazer. Soma [1] Sub [2]");
    scanf ("%d", &resp);
    if (resp == 1){
        result = somanumero(n1,n2);
    }
    else {
        result = subnumero (n1,n2);
    }
    printf ("O resultado da equação é: %d", result);
}

0

I could see that the & in the third scanf (resp) and also an error in the condition of the operation.

I made these adjustments and it reduces the code a little bit, it was like this:

#include <stdio.h>
#include <stdlib.h>

int somanumero(int numero1, int numero2){
    return numero1 + numero2;
}

int subnumero (int numero1, int numero2){
    return numero1 - numero2;
}

int main (){
    int n1,n2,resp,result = 0;
    
    printf ("\nDigite o primeiro numero: ");
    scanf ("%d", &n1);
    
    printf ("\nDigite o segunto numero: ");
    scanf ("%d", &n2);
    
    printf ("\nQual operação gostaria de fazer. Soma [1] Sub [2]");
    scanf ("%d", &resp);
    
    if (resp == 1){
        result = somanumero(n1,n2);
    } else {
        result = subnumero (n1,n2);
    }
    
    printf ("O resultado da equação é: %d", result);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.