0

I have done this so far, i donot know where my mistake is or where i m 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).

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}. 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;
}
5
  • 1
    Any idea what float compute(int x, float arr[]) returns ? Commented Dec 14, 2013 at 13:56
  • 2
    This code is leaky, you're never deallocating the dynamically allocated array, it would be a lot safer if you just used std::vector<float> instad. Commented Dec 14, 2013 at 13:57
  • It should return another array for example the input I have taken from the user is {1,2,3,4,5}, function should return {1,3,6,10,15}.it is simply adding the previous one. Commented Dec 14, 2013 at 13:59
  • FYI, there is a standard library algorithm for this called std::partial_sum Commented Dec 14, 2013 at 14:00
  • want to do this without library. Commented Dec 14, 2013 at 14:01

2 Answers 2

1

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;

Sign up to request clarification or add additional context in comments.

3 Comments

What about the safer std::vector<float> based version? Say one that doesn't mutate function arguments...
Please answer my this question.... Are arrays always passed by reference when using function???
@user3100177 Here the array is decaying to pointer while passing it as argument, and the function modifies it, you should learn to use std::vector and std::partial_sum as others suggested, this answer is more or less C style approach
0

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[]. These types are not compatible.


You cannot expect cout to print an array.

 cout<<compute(x,p);

In C and C++ an array is actually passed by the memory address of its first element. There is no way for cout to know how to print an array.


When you create an array using new, you will always have to use the delete[] operator to free the memory you previously allocated.

2 Comments

Thanks a lot, I have clearly understood, Thanks very much................... . Tell me one thing just if we use delete operator, can we again fill the array and do whatever we want??
No, after the delete, you should not touch the memory referenced by the pointer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.