一年级CS学生试图了解功能?

时间:2018-11-21 01:51:11

标签: function variables visual-c++

我是CS的第一年级学生,试图理解函数,但是我陷入了这个问题,必须在另一个函数中使用一个函数。我必须创建一个程序来检查从0到100的所有数字,并找到除数可整除的所有数字。我只允许拥有三个函数,分别命名为getDivisor,findNumbers和calcSquare。输出应该是找到的每个数字(从0到100)和该数字的平方。我编写了一个程序(如下所示),该程序运行并回答第一个问题是除数,但它仅打开几秒钟,然后在尝试计算除数可除的数时关闭。我不确定到底是我做错了什么,但我想知道,以便我可以从我的错误中学习!请忽略样式,它非常草率,通常在完成程序后回去清理它。

#include <vector>

#include <emscripten.h>
#include <emscripten/bind.h>

class test_class {
    float x;

public:
    test_class(std::vector<float> arr);
    float get_x() const;
    void set_x(float val);
};

test_class::test_class(std::vector<float> arr) {
    x = 0;
    for (size_t i = 0; i < arr.size(); i++) {
        x += arr[i];
    }
    x = x / arr.size();
}

float test_class::get_x() const {
    return x;
}

void test_class::set_x(float val) {
    x = val;
}

EMSCRIPTEN_BINDINGS(my_class) {

    emscripten::register_vector<float>("VectorFloat");

    emscripten::class_<test_class>("test_class")
        .constructor<std::vector<float>>()
        .property("x", &test_class::get_x, &test_class::set_x)
        ;
}

int main() {
    EM_ASM(
        var arr = new Float32Array([1.0, 2.0, 0.5]);
        var vec = new Module.VectorFloat();
        for (var i = 0; i < arr.length; i++) {
            vec.push_back(arr[i]);
        }
        var obj = new Module.test_class(vec);
        console.log('obj.x is ' + obj.x);
    );
}

输出应为(如果用户输入15)。输出应为列表格式,其左侧为数字,右侧为数字的平方,但我不知道如何在此处正确格式化...对不起:

输入除数:15

以下是从0到100的数字除以9以及它们的平方:

#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;

int getDivisor();
void findNumbers(int divisor, int lower, int upper, double &lowerSquared);
double calcSquare(int lower);


int main()
{
    int divisor;
    int lower = 0;
    int upper = 100;
    double lowerSquared;


    divisor = getDivisor();

    cout << "Here are the numbers, from 0 to 100, that are evenly divisble by " 
        << divisor << ", and their squares:\n";
    findNumbers(divisor, lower, upper, lowerSquared);


    system("pause");
    return 0;
}



int getDivisor()
{
    int divisor;

    cout << "Enter a divisor: ";
    cin >> divisor;

    return divisor;
}



void findNumbers(int divisor, int lower, int upper, double &lowerSquared)
{
    while (lower < upper)
    {
        if (((lower / divisor) % 2) == 0)
        {
            lowerSquared = calcSquare(lower);
            cout << setprecision(0) << fixed << setw(4) << lower << setw(8)<< lowerSquared << endl;
            lower++;
        }
        else
        {
            lower++;
        }
    }
}



double calcSquare(int lower)
{
    double lowerSquared;
    lowerSquared = pow(lower, 2);
    return lowerSquared;
}

15 115

30 900

45 2025

60 3600

75 5625

90 8100

感谢您的协助!

2 个答案:

答案 0 :(得分:0)

您遇到任何错误吗?因为在运行您的代码时,我得到了异常。

Floating point exception(core dumped)

发生此异常的原因是,您正在尝试对float进行一些非法操作,例如在if语句中除以0。

要解决此问题,只需将较低的数字分配给1,以便计数从1开始而不是0。

int lower = 1; 

此外,您可能还想检查if语句中的逻辑,因为就目前情况而言,它不会提供您想要的结果。

答案 1 :(得分:0)

/*Description:
This program is homework assignment to practice what I
learned from lecture #7a. It illustrates how to use
functions properly, specifically how to use functions
within other functions. The user is prompted to input
a divisor that once entered goes thru a function to
see if it is evenly divisble by every number from 0-100.*/

#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;

int getDivisor();
void findNumbers(int divisor, int lower, int upper, double &lowerSquared);
double calcSquare(int lower);
//====================== main ===========================
//
//=======================================================
int main()
{
    int divisor;
    int lower = 0;
    int upper = 100;
    double lowerSquared;

    //Gets the divisor and assigns it to this variable.
    divisor = getDivisor();

    cout << "Here are the numbers, from 0 to 100, that are evenly divisble by " 
        << divisor << ", and their squares:\n";
    //Finds the numbers that are divisible by divisor,
    //displays and shows their squares.
    findNumbers(divisor, lower, upper, lowerSquared);

    system("pause");
    return 0;
}


/*===================== getDivisor ==========================
This function gets the divisor from the user so it can
assign it to the divisor variable to use in a later
function to check and see if it is divisible from 0-100.

Input:
    Divisor
Output:
    Divisor being assigned to divisor variable.*/
int getDivisor()
{
    int divisor;

    cout << "Enter a divisor: ";
    cin >> divisor;

    return divisor;
}



/*===================== findNumbers ==========================
This function runs a loop from 0 to 100 to check and see
if the divisor the user inputted is evenly divisble by 
every number from 0 to 100. It also displays the numbers
that are evenly divisble and their squares with the help
of the calcSquare function.

Input:
    There is no user input, other than the divisor from
    the getDivisor function.
Output:
    Numbers between 0 and 100 that are divisible by the
    divisor and their squares.*/
void findNumbers(int divisor, int lower, int upper, double &lowerSquared)
{
    while (lower <= upper)
    {
        if (lower % divisor == 0)
        {
            lowerSquared = calcSquare(lower);
            cout << setprecision(0) << fixed << setw(4) << lower << setw(8) <<
                lowerSquared << endl;
            lower++;
        }
        else
        {
            lower++;
        }
    }
}



/*===================== calcSquare ==========================
This function squares the number from 0 to 100 (whatever
number that might be in the loop) that is divisible by the
user entered divisor, so that it may assign it to the 
lowersquared variable in the findNumbers function to be 
used in the output.

Input:
    Number from 0 to 100 that is divisible by user entered
    divisor
Output:
    Number from 0 to 100 squared.*/
double calcSquare(int lower)
{
    double lowerSquared;
    lowerSquared = pow(lower, 2);
    return lowerSquared;
}

//==========================================================
/*OUTPUT:
Enter a divisor: 15
Here are the numbers, from 0 to 100, that are evenly divisble by 15, and their
squares:
   0       0
  15     225
  30     900
  45    2025
  60    3600
  75    5625
  90    8100
Press any key to continue . . .
*/
//==========================================================