Thread: beginners question

  1. #16
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    What are the two arithmetic operations? Can the input values, 0 or 1, be incorperated into the arithmetic function?

  2. #17
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    As laserlight pointed out, it is not at a beginner level -> But I'm sure that the OP can appreciate that it can be done.

    However, if you are only adding two numbers, you could use arrays to input the two different numbers and the input to determine the offset from the start of the array.

    Code:
    int main(int argc, const char *argv[])
    {
        int user_in=0;
        int arr_offset, answer;
        int lookup_arr[6] = {0, 0, 1, 2, 3, 4};
    
    
        printf("Enter 1 for 1 + 2, or \nEnter 2 for 3 + 4\n");
        scanf("%d", &user_in);
    
    
        arr_offset = 2 * user_in * ((unsigned int)user_in <= 2);
        answer = lookup_arr[arr_offset] +  lookup_arr[arr_offset+1];
    
    
        printf("Output: %d + %d = %d", lookup_arr[arr_offset], lookup_arr[arr_offset+1], answer);
    
    
        return EXIT_SUCCESS;
    }
    You couldn't alert the user to invalid inputs without using a conditional operator though...
    Last edited by Click_here; 01-13-2013 at 11:24 PM.
    Fact - Beethoven wrote his first symphony in C

  3. #18
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If its just arithmetic operations then no arrays or function pointers are needed.
    Just perfrom both calculations in one large expression, multiplying one part by the user_input, and the other part by (1-user_input).
    Although in all liklihood, it can be simplified even further from there.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #19
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by facebook1978200 View Post
    The user gets prompted for a choice and will enter in a 0 or 1....entering a 0 will do an arithmetic operation and give an output and entering a 1 will do a different arithmetic operation giving a different output...the catch is no if, if else, switch, while, do while, or anything like that can be used.
    Code:
    double addition(double a, double b) { return a + b; }
    double subtraction(double a, double b) { return a - b; }
    
    int main(void)
    {
        int choice;
        printf("Enter 0 for addition or 1 for subtraction: ");
        scanf("%d", &choice);
    
        double a, b;
        printf("Enter two numbers a b: ");
        scanf("%lf %lf", &a, &b);
        
        double result = 0.0;
        result += !choice * addition(a, b);
        result += choice * subtraction(a, b);
        printf("Result is %g\n", result);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginners question on inline C
    By retrodans in forum C Programming
    Replies: 11
    Last Post: 03-30-2011, 06:36 AM
  2. C beginners question
    By incipientmind in forum C Programming
    Replies: 5
    Last Post: 02-25-2008, 02:06 PM
  3. beginners question
    By c++prog in forum C++ Programming
    Replies: 4
    Last Post: 02-01-2008, 04:41 PM
  4. Beginners question
    By jamesstuart in forum C Programming
    Replies: 9
    Last Post: 09-24-2007, 03:24 PM
  5. Beginners OOP question
    By Skusey in forum C++ Programming
    Replies: 2
    Last Post: 11-06-2006, 06:10 AM