Write this as a single expression help?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • td0g03
    New Member
    • Jan 2007
    • 64

    Write this as a single expression help?

    Assume that x is a variable that has been declared as an int and been given a value. Assume that twice is a function that receives a single integer paramter and returns twice its value. (So if you pass 7 to twice it will return 14. Thus the expression twice(7) has the value 14.

    Write an expression whose value is eight times that of x without using the standard C arithmetic operators (*,+, etc.). Instead, use calls to twice to accomplish this.

    In this exercise you must write this as a single expression-- you must not write any statements. Also, you may only use the twice() function-- no other functions or operators.

    I tried a few codes example as
    Code:
    twice(twice x,twice x)
    But I can't get it to work. I'm only suppose to write a small code and not a actual whole code. If someone can give me some tips or some sort that would be great. Thanks in advance.
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Originally posted by td0g03
    Assume that x is a variable that has been declared as an int and been given a value. Assume that twice is a function that receives a single integer paramter and returns twice its value. (So if you pass 7 to twice it will return 14. Thus the expression twice(7) has the value 14.

    Write an expression whose value is eight times that of x without using the standard C arithmetic operators (*,+, etc.). Instead, use calls to twice to accomplish this.

    In this exercise you must write this as a single expression-- you must not write any statements. Also, you may only use the twice() function-- no other functions or operators.

    I tried a few codes example as
    Code:
    twice(twice x,twice x)
    But I can't get it to work. I'm only suppose to write a small code and not a actual whole code. If someone can give me some tips or some sort that would be great. Thanks in advance.
    Your teacher has said that twice returns two times its value, both taking and giving one parameter. So you've got the right idea with your code, except it's only going to take one parameter(in the parentheses), instead of trying to pass two to it. Does that help?

    Comment

    • td0g03
      New Member
      • Jan 2007
      • 64

      #3
      Originally posted by sicarie
      Your teacher has said that twice returns two times its value, both taking and giving one parameter. So you've got the right idea with your code, except it's only going to take one parameter(in the parentheses), instead of trying to pass two to it. Does that help?
      Yeah, sorta, but I'm still kinda confused. I understand that I only pass one which would be x believe. How do I pass something to the function? Prehap you can give me an example of something else so it won't seem like I'm asking for the homework answer. Thanks in again!

      Edit: It alway askes to do 8 times the value of x. I think it would be something like you pass back and fore. Storing the value then passing it again?

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Originally posted by td0g03
        Yeah, sorta, but I'm still kinda confused. I understand that I only pass one which would be x believe. How do I pass something to the function? Prehap you can give me an example of something else so it won't seem like I'm asking for the homework answer. Thanks in again!

        Edit: It alway askes to do 8 times the value of x. I think it would be something like you pass back and fore. Storing the value then passing it again?
        Well, let's start at the beginning. You have a function that will return two times the value passed to it. So, x*2 is equivalent to passing x to twice.

        This seems to be a good, quick overview on functions. I'm betting if you read through that, you will be able to figure out how to pass x to twice. Then you just need to do that eight times.

        Comment

        • Manjiri
          New Member
          • Nov 2006
          • 40

          #5
          Originally posted by td0g03
          Assume that x is a variable that has been declared as an int and been given a value. Assume that twice is a function that receives a single integer paramter and returns twice its value. (So if you pass 7 to twice it will return 14. Thus the expression twice(7) has the value 14.

          Write an expression whose value is eight times that of x without using the standard C arithmetic operators (*,+, etc.). Instead, use calls to twice to accomplish this.

          In this exercise you must write this as a single expression-- you must not write any statements. Also, you may only use the twice() function-- no other functions or operators.

          I tried a few codes example as
          Code:
          twice(twice x,twice x)
          But I can't get it to work. I'm only suppose to write a small code and not a actual whole code. If someone can give me some tips or some sort that would be great. Thanks in advance.

          hello friend here i have the solution...
          Just see is it required one...?#include <stdio.h>

          Code:
          int c;
          int twice(int);
          int main()
          {
          long int num;
          printf("Enter the number\n");
          scanf("%ld",&num);
          c=num;
          num=twice(twice(twice(twice(twice(twice(twice(num)))))));
          printf("Result : %ld\n",num);
          return 0;
          }
          
          int twice(int a)
          {
          
          return(a*c);
          }

          Comment

          • td0g03
            New Member
            • Jan 2007
            • 64

            #6
            Thank for the help! Both of you. I was able to read the website and the code to realize that I have to do twice(twice(twi ce((x)))). Took me 10 hours to figure this out. Thanks again guys!

            Comment

            Working...