473,413 Members | 2,051 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Write this as a single expression help?

64
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
Expand|Select|Wrap|Line Numbers
  1. 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.
Feb 13 '07 #1
5 14428
sicarie
4,677 Expert Mod 4TB
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
Expand|Select|Wrap|Line Numbers
  1. 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?
Feb 13 '07 #2
td0g03
64
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?
Feb 13 '07 #3
sicarie
4,677 Expert Mod 4TB
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.
Feb 13 '07 #4
Manjiri
40
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
Expand|Select|Wrap|Line Numbers
  1. 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>

Expand|Select|Wrap|Line Numbers
  1. int c;
  2. int twice(int);
  3. int main()
  4. {
  5. long int num;
  6. printf("Enter the number\n");
  7. scanf("%ld",&num);
  8. c=num;
  9. num=twice(twice(twice(twice(twice(twice(twice(num)))))));
  10. printf("Result : %ld\n",num);
  11. return 0;
  12. }
  13.  
  14. int twice(int a)
  15. {
  16.  
  17. return(a*c);
  18. }
Feb 13 '07 #5
td0g03
64
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(twice((x)))). Took me 10 hours to figure this out. Thanks again guys!
Feb 13 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: sinister | last post by:
The examples in the online manual all seem to use double quotes, e.g. at http://us3.php.net/preg_replace Why? (The behavior is different with single quotes, and presumably simpler to...
2
by: ajay | last post by:
How to write a single update sql statement to update different set of attributes of a table. In other words what will be the value of indicator variables to ignore updates of some attributes in a...
4
by: Greg | last post by:
I keep getting an error when I have a tick mark in a text value that I am searching for in my XPath Query. Example: <Authors> <Author LastName="O'Donnel"> <Author LastName="Smith">...
15
by: Anton Gavrilov | last post by:
Hi all, I seek your advice on where to start if I want to write a compiler for a toy C-like language I invented (or, rather, am in the process of inventing). Yes, yes, I know I'm crazy and the...
15
by: silverburgh.meryl | last post by:
Hi, I am trying to write a transform_until template. It is bascially doing what transform is doing except if UnaryOperation return NULL, it will break out from the loop. Here is the code, but...
3
by: TD | last post by:
I have an unbound form that adds and updates records to one table. I use an INSERT sql statment to add a new record and a UPDATE statement to update a record. I created a function named C2F to...
74
by: lovecreatesbeauty | last post by:
My small function works, but I have some questions. And I want to listen to you on How it is implemented? 1. The function does not check if parameter x is larger or smaller than parameter y. ...
10
by: sandraz444 | last post by:
I have an expression in the query under my form to autofill the date under a certain condition but it wont write to the underlying table?? The date shows in the form but not the table. Does anyone...
3
by: cool17 | last post by:
printf("\nEnter the number of terms:\n"); scanf("%i",&j); printf("\nPlease enter expression info as follow:\n"); { double value; double power; double times; int pos; int isX;
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.