简体   繁体   English

函数,数组和动态数组

[英]Function, array and dynamic array

I have done this so far, i donot know where my mistake is or where im going wrong, I have come up with this program 1st time. 到目前为止,我已经做到了这一点,我不知道我的错误在哪里或我在哪里出错,我第一次提出了该程序。 We have to ask from the user amount of elements in an array which i have done using dynamic array. 我们必须向用户询问使用动态数组完成的数组中元素的数量。 Then we have to pass 2 arguments one is the size of an array and the other is array(float type). 然后,我们必须传递2个参数,一个是数组的大小,另一个是array(float type)。

The function should replace the contents of each cell with the sum of the contents of all the cells in the original array from the left end to the cell in question. 该函数应将每个单元格的内容替换为从左端到相关单元格的原始数组中所有单元格的内容之和。 for example if i have array {1,2,3,4,5} function should return {1,3,6,10,15}. 例如,如果我有数组{1,2,3,4,5},则函数应返回{1,3,6,10,15}。 This is my program below please tell me what changes I have to make in my existing code. 这是我下面的程序,请告诉我必须对现有代码进行哪些更改。

#include <iostream>
using namespace std;

float compute(int x, float arr[]){
    float sum=0;

    for(int i=0; i<x;i++){
        sum+=arr[i];
        arr[i]=sum;
    }
    return arr;
}

int main(){
    int x;
    cout<<"How many elements you want"<<endl;
    cin>>x;

    float *p=new float[x];

    for(int i=0; i<x; i++){
        cin>>p[i];
    }
    cout<<compute(x,p);
    return 0;
}

Simple fixes, if you can modify original array 简单修复,如果可以修改原始数组

void compute(int x, float arr[]) 
{ 
    float sum=0;

    for(int i=1; i<x ;i++)
        arr[i] = arr[i] + arr[i-1];

    // return arr; // Not required
}

And then, you can do following 然后,您可以执行以下操作

compute(x,p);
for(int i= 0 ; i< x; ++i)
   std::cout<< p[i] << std::endl;

Also, make sure to release the allocated memory, after processing 另外,请确保在处理后释放分配的内存

delete [] p;

Fix 固定

#include <iostream>
using namespace std;

void compute(int x, float arr[]){
    float sum=0;

    for(int i=0; i<x;i++){
        sum+=arr[i];
        arr[i]=sum;
    }
}

int main(){

    int x;
    cout<<"How many elements you want"<<endl;
    cin>>x;

    float *p=new float[x];

    for(int i=0; i<x; i++){
        cin>>p[i];
    }

    for(int i=0; i<x; i++)
    {
        cout << p[i] << endl;
    }

    compute(x,p);
    cout << "----"<< endl;
    for(int i=0; i<x; i++)
    {
        cout << p[i] << endl;
    }
    delete[] p;
    return 0;
}

Notes 笔记

float compute(int x, float arr[])
{
...
return arr;
}

You declare the return type of compute as a float and then return arr which has the type of float[] . 您将计算的返回类型声明为float ,然后返回具有float[]类型的arr。 These types are not compatible. 这些类型不兼容。


You cannot expect cout to print an array. 您不能期望cout打印一个数组。

 cout<<compute(x,p);

In C and C++ an array is actually passed by the memory address of its first element. 在C和C ++中,数组实际上是通过其第一个元素的内存地址传递的。 There is no way for cout to know how to print an array. cout无法知道如何打印数组。


When you create an array using new , you will always have to use the delete[] operator to free the memory you previously allocated. 当使用new创建数组时,必须始终使用delete[]运算符释放先前分配的内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM