Array Subtraction

I am supposed to write a program that has the user enter in numbers (let's assume all the numbers are positive) and gets terminated when 0 is entered or the array exceeds 25 numbers. The program finds the minimum and then subtracts it from the array. However, my function is not subtracting by one. Any help would be appreciated. My subtraction function are lines 70-76
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
66
67
68
69
70
71
72
73
74
75
76
#include<iostream>
using namespace std;
const int ARRAY_SIZE(25);

void read_list(int list[],int & num);
void print_array(const int list[], const int num);
int find_min(const int list[], const int num);
void array_subtract(const int array_min,int list[], const int num);
int main()
{
    int list[ARRAY_SIZE];
    int num(0);
    int array_min(0);
    
    read_list(list, num);
    cout << endl;
    cout << "Before list: (" << num << " numbers):" << endl;
    print_array(list, num);
    cout << endl;
    find_min(list, num);
    cout << "After list (" << num << " numbers):" << endl;
    array_subtract(array_min,list,num);
    print_array(list,num);
}
void read_list(int list[], int & num)
{
    int x(0);
    
    cout << "Enter positive numbers (ints) terminated by a 0: " << endl;
    cin >> x;
    while(x != 0 && num < ARRAY_SIZE)
    {
        list[num]=x;
        num++;
        cin >> x;
    }
}
void print_array(const int list[],const int num)
{
    int k(0);
    for(int i = 0; i < num; i++)
    {
        if(list[i] > 0 && k<num-1)
        {
            cout << list[i] << ", ";
        }
        if(list[i] > 0 and k == num-1)
        {
            cout << list[i] << ".";
        }
        k++;
    }
    cout << endl;
}
int find_min(const int list[], const int num)
{
    int array_min(0);
    array_min=list[0];
    for(int i=1; i < num; i++)
    {
        if (array_min > list[i])
        {
            array_min=list[i];
        }
    }
    cout << "The mininum value = " << array_min << endl;
    cout << endl;
    return(array_min);
}
void array_subtract(const int array_min,int list[], const int num)
{
    for(int i=0; i < num; i++)
    {
       list[i]= (list[i]-array_min);
    }
}
¿do you know about the scope of a variable?
¿where do you put a value in `array_min' that is in `main()'?
I know the scope of variables. Im not sure about your second question
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
    int list[ARRAY_SIZE];
    int num(0);
    int array_min(0); //<---
    
    read_list(list, num);
    cout << endl;
    cout << "Before list: (" << num << " numbers):" << endl;
    print_array(list, num);
    cout << endl;
    find_min(list, num);
    cout << "After list (" << num << " numbers):" << endl;
    array_subtract(array_min,list,num); //<--- 0 is a neutral element for subtraction
    print_array(list,num);
}
You initialize `array_min' to 0, then pass it to `array_subtract()'
Wouldn't array_min be changed when the code reaches find_min(list,num)?
No, ¿why should it?
Because in the function, it returns the value of the minimum
but you ignore the returned value, you don't assign it to any variable, ¿why should modify `array_min' then?
I suppose that makes sense. Should I initialize it like so:

 
int array_min(list[num]);
int array_min = find_min(list, num);
If I do that, then in the display window, it automatically picks a minimum value and doesn't print out my numbers anymore. This is what showed up in my display window when I typed in "1 2 3 4 5 6 0"

The mininum value = 304152179

Enter positive numbers (ints) terminated by a 0: 
1 2 3 4 5 6 0

Before list: (6 numbers):
1, 2, 3, 4, 5, 6.

The mininum value = 1

After list (6 numbers):

Program ended with exit code: 0
The idea is to find the minimum after you have filled your list...
the code is execute in a sequence.

in c++ you can create variables anywhere in the code, it is not necessary for them to be all at the start of the scope
So line 20 would be int array_min = find_min(list, num); and get rid of line 13
I am able to find the minimum, it is subtracting that minimum from each segment in the array that is the problem.
You are not listening.
array_min has garbage, because you fill it with garbage and you if intend to use that garbage you'll obtain garbage. So stop doing that.

If you call `find_min()' but you discard the returned value, then it's useless.
If you call `find_min()' before putting something in the list, ¿what's the point?


Inspect the parameters of `array_subtract()' with a debugger.
Oh alright, I see what you're saying and I was able to successfully run the program with just positive integers. Thank you for your help
Topic archived. No new replies allowed.