C++ program using visual c++. Functions, call and ref statements with for loops, output two separate tabless?
-
Need a little help please: i have been trying to complete this for a couple days but i get more and more confused everytime i try. When an object is falling because of gravity, the following formula can be used to determine the distance the object falls in a specific time period. D = ½ gt^2 The variables in the formula are as follows: d is the distance in meters, g is 9.8, and t is the amount of time in second that the object has been falling. Write a function named fallingDistance that accepts an object’s falling time (in seconds) as an argument. The function should return the distance, in meters, that the object has fallen during that time interval. Write a program that demonstrates the function by calling it in a loop that passes the values 1 though 10 as arguments, and displays the return value. *Additional to this problem my prof wants two functions that calculate the falling distance. function 1 passes arguments by value function 2 passes argument by reference outputs should be in form of a table whats confusing is the prof established that in the main it should resemeble like this: main() { for(--------------) call (by value) output for(---------------------) call (by reference) output my code so far call by ref works fine but call by value only works when i put double d as global but then it only comes back as 0's in the output. its not reading the equation. -------------------------- # include <iostream> # include <cmath> # include <string> using namespace std; void fallingDistance2(double &); double fallingDistance1(double); const double g = 9.8; int t; double d; int main() { cout<<"calculated by passby values.\n"; cout<<"Time \t\t Distance\n"; cout<<"-------------------\n"; for (t=1;t<=10;t++) { fallingDistance1(d); cout<<t<<"\t\t"<<d<<endl; } cout<<"calculated by reference values.\n"; cout<<"Time \t\t Distance\n"; cout<<"-------------------\n"; for (t=1;t<=10;t++) { fallingDistance2(d); cout<<t<<"\t\t"<<d<<endl; } return 0; } double fallingDistance1(double d) { d=0.5*9.8*t*t; return d; } void fallingDistance2(double &refd) { refd=0.5*9.8*t*t; }
-
Answer:
Both functions are wrong because you are supposed to pass the time and return the distance which you do neither. for pass by value you need to do this: double fallingDistance(int time){ return(0.5 * 9.8 * time * time); } and when you call it do this: d = fallingDistance(t); I'll let you figure out the other function you need.
e at Yahoo! Answers Visit the source
Other answers
Both functions you write should accept the time that the object has been falling as an argument. You shouldn't depend on a global variable for that. For the pass by reference version, have it take two arguments: the time, and the variable that the result will be stored in.
Justin
Related Q & A:
- How Can I use .net dll in C program?Best solution by Stack Overflow
- How to run an executable from within a c program?Best solution by Stack Overflow
- Is there some kind of API for MSN protocol (for using in C# asp.net application?Best solution by Stack Overflow
- How do I wire up two separate amps in my car?Best solution by Yahoo! Answers
- Can anyone help me with this C++ program?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.