Help with Arrays and Functions

I'm writing a program that will read an array from the user, and then it will output the array back to the user, find the minimum value, subtract the minimum value from each value in the array and then output it back to the user. This is what I have so far but when I go to compile the code I keep receiving the error:

/tmp/ccZ5LkFc.o: In function `main':
array.cpp:(.text+0x2a): undefined reference to `read_list(int const*, int&, int)'
collect2: ld returned 1 exit status

I at this point am not very good with functions or arrays so please excuse my awful coding, and sorry if everything I've done is blatantly wrong. I've just kind of hit a wall right here and don't know if some things, or everything is all wrong. Thank you in advance for any help.

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
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <iomanip>

using namespace std;

void read_list(const int a[], int & x, const int length);

void print_array(const int a[], const int x);

int find_min(const int a[], const int x);

void array_subtract(int num, int a[], int x);




int main()
{

  const int SIZE(25);

  int x(0);
  int array[SIZE];
  int z;

  read_list(array, x, SIZE);

  print_array(array, x);

  find_min(array, x);

  array_subtract(z, array, x);

  print_array(array, x);

}

void read_list(int a[], int & x, int length)
{
  int array[25], l;
  
  for(int x = 1; x <= l-1 && x != 0; x++)
  {
  cout << "Enter positive numbers (ints) terminated by 0 " ;
  cin >> array[x];
  }

}

void print_array(const int a[], const int x)
{
  int array[x];
  for(int y = 0; y<=x; y++)
  {
	cout << array[y] << ", ";
	if(y==x)
	{
	  cout << ".";
	}
  }
}

int find_min(const int a[], const int x)
{
  int y;
  int array[x];
  
  int z = array[y];

  for(int y = 0; y<=x; y++)
  {
	if(array[y] < z)
	{
	  z = array[y];
	}
  }
  return z;
}

void array_subtract(int num, int a[], int x)
{
  int numb;
  int array[x];

  for(int y = 0; y<= x; y++)
  {
	array[y]= array[y] - numb;
  }
}

Last edited on
declaration:
void read_list(const int a[], int & x, const int length); 
definition:
void read_list(int a[], int & x, int length) 

Find the difference.
#include <iostream>
#include <iomanip>

using namespace std;

void read_list(int *, int );

void print_array( int *, int );

int find_min( int *, int );

void array_subtract(int , int *, int );




int main()
{

int SIZE(25);


int array[SIZE];
int z;
int n;

cout<<"\n\t Enter the size of Array:";
cin>>n;

read_list(array, n);
cout<<endl;

print_array(array, n);
cout<<endl;

z = find_min(array, n);
cout<<endl;

array_subtract(z, array, n);
cout<<endl;

print_array(array, n);
cout<<endl;

}

void read_list(int *ptr, int n)
{


for(int x = 0; x < n; x++)
{
cout << "Enter positive numbers (ints) terminated by 0 " ;
cin >> *(ptr+x);
}

}

void print_array(int *ptr, int n)
{
cout<<"\n\t the elements of array are:";
for(int y = 0; y<n; y++)
{
cout<<"\t"<<*(ptr+y);
}
}

int find_min( int *ptr, int n)
{
int y = 0;
int z = *(ptr+y);
for(y = 1; y<n; y++)
{
if(*(ptr+y) < z)
{
z = *(ptr+y);
}
}
return z;
}

void array_subtract(int z, int *ptr, int n)
{


for(int y = 0; y < n ; y++)
{
*(ptr+y)= *(ptr+y) - z;
}
}
So I went ahead and took care of all of my mistakes in regards to declaring prototypes and functions, and currently have this program:

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <iomanip>

using namespace std;

void read_list(const int a[], int & x, const int length);

void print_array(const int a[], const int x);

int find_min(const int a[], const int x);

void array_subtract(int num, int a[], int x);




int main()
{

  const int SIZE(25);

  int x(1);
  int array[SIZE];
  int z;

  read_list(array, x, SIZE);

  print_array(array, x);

  find_min(array, x);

  array_subtract(z, array, x);

  print_array(array, x);

  return(0);

}

void read_list(const int a[], int & x, const int length)
{
  int array[x], l;


  cout << "Enter positive numbers (ints) terminated by 0 " ;
  cin >> array[x];  
  while( x != 0 && x -1 < l)
  {
  x++;
  cin >> array[x];
  
  }

}

void print_array(const int a[], const int x)
{
  int array[x];
  for(int y = 0; y<=x; y++)
  {
	cout << array[y] << ", ";
	if(y==x)
	{
	  cout << ".";
	}
  }
}

int find_min(const int a[], const int x)
{
  int y;
  int array[x];
  
  int z = array[y];

  for(int y = 0; y<=x; y++)
  {
	if(array[y] < z)
	{
	  z = array[y];
	}
  }
  return z;
}

void array_subtract(int num, int a[], int x)
{
  int numb;
  int array[x];

  for(int y = 0; y<= x; y++)
  {
	array[y]= array[y] - numb;
  }
}
  
 


When I run the program, I am getting this as the output


Enter positive numbers (ints) terminated by 0 2
4196176, 0, .4196176, 0, .


Like I said, I really am obviously not fully understanding arrays and functions here, up to this point in my class my programs have just kind of been working and I'm trying to figure out what I'm doing wrong so I can stop making these stupid mistakes.
IN all of your functions, you do not use a argument at all. You create new local array, do some actions with it and leave function destroying this array. Additionally you are discarding result of find_min() when you call it in main().
Topic archived. No new replies allowed.