【问题标题】:Velocity & Momentum - I can't go the distance速度和动量——我走不远
【发布时间】:2014-10-24 19:58:41
【问题描述】:

我目前正在处理一项要求我编译和执行具有以下参数的程序的任务:

编写一个执行以下操作的程序: 计算物体的速度和动量。速度的公式是 V=d/t,动量的公式是 m=质量*速度。你的程序应该包含两个函数:传递值(一个),一个传递指针(一个)。它还应该有一个 for 循环和必要的打印语句,以表格格式打印结果。 · Passing By Values函数是计算物体的速度,这里你给这个函数传递两个参数一个恒定的距离,但是时间是for循环的值:I=1:

double Velocity(double distance,int time); · Pass By Pointers 函数计算物体的动量,您将两个参数传递给该函数:物体的速度和恒定质量:mass=100:

double Momentum(double *Velocity,double *mass);

输出应具有包含时间、速度和动量的表格格式。用户无需输入数值,时间输入范围为1-200。

** 现在这是我的挣扎,我已经尽我所能,但似乎无法正确编译,它只是一直在“按任意按钮继续...”

我真的不明白我做错了什么,只需要帮助来编译和运行,任何帮助都将不胜感激。

#include <iostream>

using std::cout;
using std::endl;
using std::cin;

//Function prototypes (declaring the functions).
double velocity(double distance, int time);
double momentum(double *velocity ,double *mass);

//Main function.
int main()
{
    double PrimaryVelo(0);
    double TotalMomentum(0);
    int t(1);
    for (double d = 1; d <= 10; d++)
    {
        PrimaryVelo = velocity(d, t);
    }  //End for the primary for loop. 
    system("pause"); //Prevents closing of debug automatically.
    return 0;
} //End of the main function.

//Pass-by-value method for velocity.
double velocity(double distance, int time)
{
    return distance / time;
}

//Pass-by-pointers method for momentum.
double momentum(double &velocity ,double &mass)
{
    return (velocity * 205);
}

【问题讨论】:

  • 你说:“它只是一直在“按任意按钮继续...”“嗯,当然。来自system("pause"); 您的程序编译得很好,但该消息是它唯一的副作用。剩下的就是一些你不使用的算术结果,编译器正在优化它。
  • 应该实际编写任何输出的代码在哪里?
  • It should also have a for loop and necessary print statements to print the result in a tabular format. 就像已经指出的那样,您还没有满足这个要求。
  • ::在额头上打自己::我真是个白痴。
  • @remyable 我认为 (double d = 1; d

标签: c++ for-loop pass-by-reference pass-by-value


【解决方案1】:

嗯.... 这是你的代码.....

#include <iostream>

using namespace std;

double velocity(double distance, int time);
double momentum(double velocity ,double mass);

int main()
{
    double Mass=205;
    double Distance=100;
    double Velocity;
    //cout << "Time\t\tVelocity\tMomentum" << endl; //You'll need to reformat this
    for ( int Time = 1 ; Time <= 200 ; Time++ )
    {
        cout << Time << "\t" ;
        cout << velocity ( Distance , Time ) << "\t" ;
        Velocity = velocity ( Distance , Time ) ;
        cout << momentum ( Velocity , Mass ) << endl ;
    }

    // If you can't use conio.h and getch();
    // Use something like this to stop console from closing itself
    int a;  
    cin>>a;  
    return 0;
}
double velocity(double distance, int time)
{
    return distance / time;
}

double momentum(double velocity ,double mass)
{
    return velocity * mass;
}

注意:我给你的代码带有接受由值传递的参数的函数......

希望对你有帮助....

祝你有美好的一天......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    • 2013-01-22
    • 2010-09-05
    • 2011-01-20
    • 2012-12-14
    • 1970-01-01
    • 2015-08-03
    相关资源
    最近更新 更多