小编Ani*_*sal的帖子

动态内存分配错误的结果

我试图通过该程序找出拉格朗日的插值.我用数组解决了它,但是当使用动态内存分配时,程序给了我垃圾结果.

#include<stdio.h>
#include<conio.h>
#define SIZE 100

int main()
{
    float *x,*y;
    float value = 0,ask,temp;
    int i,j,n;
    printf("Enter size");
    scanf("%d",&n);
    x = (float*)malloc(n*sizeof(float));
    y = (float*)malloc(n*sizeof(float));
    for(i = 0; i < n;i++)
    {
            printf("x[%d]: ",i);
            scanf("%f",(x+i));
            printf("y[%d]: ",i);
            scanf("%f",(y+i));
    }
    printf("Enter value to find");
    scanf("%f",&ask); //cin >> ask;
    for(i = 0; i < n;i++)
    {
        temp = 1;
        for(j = 0; j < n; j++)
        {
            if(i != j)
            {
                temp = temp * (ask-(*(x+i))/(*(x+i)-*(x+j)));
            }
        }
        value = value …
Run Code Online (Sandbox Code Playgroud)

c numerical-methods

0
推荐指数
1
解决办法
54
查看次数

为什么价值不会增加1?

我正在尝试使用朋友进行一元运算符重载.然而,由于任何原因,价值没有增加1?

#include<iostream>
#include<iomanip>

using namespace std;

class Sample{
private:
    double x;
public:
    Sample()
    {
        /*No comment*/
    }
    ~Sample()
    {
        /*No comment*/
    }
    void setX(double recieveX)
    {
        x = recieveX;
    }
    double getX()
    {
        return x;
    }

friend void operator++(Sample s1);
};

void operator++(Sample s1)
{
    s1.x++;
}

int main()
{
    class Sample s1;
    s1.setX(30);
    ++s1;
    cout << s1.getX();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

答案应该是31,没有朋友也能工作.

c++

-1
推荐指数
1
解决办法
65
查看次数

标签 统计

c ×1

c++ ×1

numerical-methods ×1