Array + Function (Void functions and sum)

Task:
1. Build the function f(x) that creates an array according to formula: Ai=(i+1)/(x)*(-1)^i
2. Use the function to build the array B with size 150
3. Display on the screen values of the array elements in reverse mode (from 149 to 0)
4. Calculate the sum of all elements that are less than 0.
5. Display results on the screen.

So the thing is that the part until point 4. I have done and it works. Now, I'm not sure how to assign sum in this case and I tried doing a following thing which is displayed below in the code section... Please have a look.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  #include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;
void create_array(double arr[], int size_arr, double k);
void display_array(double arr[], int size_arr, int a, int b);
void create2_array(double arr[], int size_array, double sum);
void display2_array(double arr[], int size_arr, double sum);
int main()
{
    cout << "9A" << endl;
    const int array_size=150;
    double B[array_size]={0};
    double x;
    double summ;
    cout<<"type in x value: "; cin>>x; cout<<endl;
    create_array(B,array_size,x);
    display_array(B,array_size,149,0);
    create2_array(B,array_size,summ);
    display2_array(B,array_size,summ);

    cout<<endl;
    return 0;
}
void create_array(double arr[], int size_arr, double k)
{
    for(int i=0; i<size_arr; i++)
    {
        arr[i]=((i+1)/(k))*pow(-1,i);
    }
}


void display_array(double arr[], int size_arr, int a, int b)
{
    cout<<endl;
    for(int i=size_arr-1; i>=0; i--)
    {
        cout<<arr[i]<<" ";

    }
}

void create2_array(double arr[], int size_arr, double sum)
{
    for(int i=size_arr-1; i<=0; i--)
    {
        sum=0;
        if(arr[i]<0)
        {
            sum=sum+arr[i];
        }
    }
}

void display2_array(double arr[], int size_arr, double sum)
{

    cout<<endl;
    cout<<endl;
    {
        cout<<"SUM of all elements less than zero: "<<sum;
    }
}


Also, I know something is wrong with the sum as in the logs section of Codeblocks I have a warnign which states that sum may be used uninitilaized.
Last edited on
Do you know what the "scope" of a variable is?
https://www.codesdope.com/cpp-scope-of-variables/

1
2
3
4
5
6
7
8
9
10
11
void create2_array(double arr[], int size_arr, double sum)
{
    for(int i=size_arr-1; i<=0; i--)
    {
        sum=0;
        if(arr[i]<0)
        {
            sum=sum+arr[i];
        }
    }
}

The sum variable here is a variable local to the scope of the create2_array function.
You reset it each loop, and then assign the value of arr[i] to it (if it's less than 0).
But this doesn't affect anything outside of this function itself, because you are only changing a local variable (sum).

In other words, the sum variable in your main function is never set.

One possible remedy is pass sum as a reference into create2_array.

BTW: Your function name for "create2_array" doesn't really make sense. You aren't creating or setting any arrays in that function. You're just (attempting to) calculate the sum of all variables less than 0, it looks like. I would call it something like this:
1
2
3
4
5
6
7
8
9
10
11
12
double sum_negative_values(double arr[], int size_arr)
{
    double sum = 0.0;
    for (int i = 0; i < size_arr; i++)
    {
        if (arr[i] < 0)
        {
            sum += arr[i];
        }
    }
    return sum;
}


Calling code:
double sum = sum_negative_values(B, array_size);
Last edited on
Thank you so much my friend. It works and I will study what you sent me there.

Thanks again,
Best Regards!
Topic archived. No new replies allowed.