Return array function HELP!

Ok so I have homework that I am working on, but I cannot figure out what is wrong. I made a function that takes in an array, and returns the smallest number. However it is sending the address instead of the value. I get really confused when messing with pointers, I'm just looking for a little advice that will get me unstuck. Here is my code:
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

template <class T>	
	void print_data(T arrayname[]) 	
	{	
		int *smallest = NULL;
	 cout << setiosflags(ios::left) << "Printing Array: ";
	 for(int i = 0; i < 10; i++)
	 {
		 cout << arrayname[i] << " ";
	 }

	 cout << endl;
	 
	 cout << setiosflags(ios::left) << "Smallest Value: " << resetiosflags << setiosflags(ios::right) << get_smallest(arrayname,10)//THIS IS THE ERROR, it shows the address instead of the value!!;



 	}

int get_smallest(int arrayname[],int size)
{
	int smallest = *(arrayname);
	cout << smallest << endl;
	for(int i =0; i < size; i++)
	{
		if (*(arrayname + i) < smallest)
		{
			smallest = *(arrayname + i);
		}

	}
	cout << smallest << endl;//This actually displays the Value
	return smallest;
}

Unsure what the problem was, but I removed the ios flags (never used them) and I get the lowest and highest values printed.....no address. Here is the code I compiled:
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
#include <iostream>
using namespace std;

template <class T>
	void print_data(T arrayname[])
	{
		int *smallest = NULL;
        cout << "Printing Array: ";
        for(int i = 0; i < 10; i++)
        {
            cout << arrayname[i] << " ";
        }
        cout << endl;
        cout << "Smallest Value: " <<  get_smallest(arrayname,10);
    }

int get_smallest(int arrayname[],int size)
{
	int smallest = *(arrayname);
	cout << smallest << endl;
	for(int i=0; i < size; i++)
	{
		if (*(arrayname + i) < smallest)
		{
			smallest = *(arrayname + i);
		}

	}
	cout << smallest << endl;
	return smallest;
}
int main()
{
   int name[12] = {90,11,15,33,56,24,22,12,1,3,7,9};
   get_smallest(name, 12);
}
Hi thanks for the reply, but if you look at my code, when i call getsmallest function there is a cout << smallest; that displays the number, I put in my comment that that one does display the value, but when you call the print_data function it should call the get_smallest function but it returns a address instead of a value. This particular line is the main point where I'm stuck.
 
 cout << "Smallest Value: " <<  get_smallest(arrayname,10)//This call returns a address and I don't know why...; 


Thanks in advance!
in the code you posted, you never called print_data, so post your actual code...
Ok sorry, I didn't think it would help to post everything but here it is, I hope it won't confuse you more because I have unfinished functions.

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
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <iomanip>
using namespace std;

const int size = 10;

int get_smallest(int ,int );
double get_smallest(double ,int );

template <class T>	
	void get_array_data(T arrayname[], int size) 	
	{	
		 for(int i = 0; i < size;i++)
		{
			cin >> arrayname[i];
		}
 	}

	template <class T>	
	T iterate_sum(T arrayname[], int size = 10) 	
	{	
	
 	}

	template <class T>	
	T recur_sum(T arrayname[], int size = 10) 	
	{	
	
 	}

	template <class T>	
	void print_data(T arrayname[]) 	
	{	
		int *smallest = NULL;
	 cout << setiosflags(ios::left) << "Printing Array: ";
	 for(int i = 0; i < 10; i++)
	 {
		 cout << arrayname[i] << " ";
	 }

	 cout << endl;
	 
	 cout << setiosflags(ios::left) << "Smallest Value: " << resetiosflags << setiosflags(ios::right) << get_smallest(arrayname,10); //RETURNS ADDRESS HERE!! I NEED VALUE!!!!



 	}

	

int main()
{
	
	//declare and initialize arrays 
 int i_array[size] = {0}; 
 double d_array[size] = {0.0}; 
 
 //prompt the user to enter data, read data and print results 
 cout<<"Enter 10 integer values on a line, each separated by a space\n"; 

 
 get_array_data(i_array,size); 
 print_data(i_array); 
 cout<<endl; 
 cout<<"\nEnter 10 double values on a line, each separated by a space\n"; 
 get_array_data(d_array,size); 
 print_data(d_array); 




	system("PAUSE");
	return 0;
}

int get_smallest(int arrayname[],int size)
{
	int smallest = *(arrayname);
	cout << smallest << endl;
	for(int i =0; i < size; i++)
	{
		if (*(arrayname + i) < smallest)
		{
			smallest = *(arrayname + i);
		}

	}
	cout << smallest << endl;
	return smallest;
}

double get_smallest(double arrayname[],int size)
{
	double smallest = *(arrayname);
	for(int i =0; i < size; i++)
	{
		if (*(arrayname + i) < smallest)
		{
			*(arrayname + i) = smallest;
		}

	}

	return smallest;

	
}


I still have not found a solution, I tried changing around the arrays to pointers , and see if i can deference it but I still cannot get it to display the value.
You are going to cuss that it was this simple, but the problem was the prototype at the beginning of the program. You made it call function(int, int) when you are passing an array, so it should read function(int[], int) and function(double[], int). Here is the code less the extra print statements. Compile and relax.....lol

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
#include <iostream>
#include <iomanip>

using namespace std;

const int size = 10;

int get_smallest(int[],int);
double get_smallest(double[],int);

    template <class T>
	void get_array_data(T arrayname[], int size)
	{
		 for(int i = 0; i < size;i++)
		{
			cin >> arrayname[i];
		}
 	}

	template <class T>
	T iterate_sum(T arrayname[], int size = 10)
	{

 	}

	template <class T>
	T recur_sum(T arrayname[], int size = 10)
	{

 	}

	template <class T>
	void print_data(T arrayname[])
	{
        cout << setiosflags(ios::left) << "Printing Array: ";
        for(int i = 0; i < 10; i++)
        {
            cout << arrayname[i] << " ";
        }

        cout << endl;

        cout << get_smallest(arrayname, 10); //RETURNS ADDRESS HERE!! I NEED VALUE!!!!
}

int main()
{

	//declare and initialize arrays
    int i_array[size] = {0};
    double d_array[size] = {0.0};

    //prompt the user to enter data, read data and print results
    cout<<"Enter 10 integer values on a line, each separated by a space\n";


    get_array_data(i_array,size);
    print_data(i_array);

    cout<<endl;
    cout<<"\nEnter 10 double values on a line, each separated by a space\n";
    get_array_data(d_array,size);
    print_data(d_array);
	return 0;
}

int get_smallest(int arrayname[],int x)
{
	int smallest = *(arrayname);

	for(int i =0; i < x; i++)
	{
		if (*(arrayname + i) < smallest)
		{
			smallest = *(arrayname + i);
		}
	}

	return smallest;
}

double get_smallest(double arrayname[],int x)
{
	double smallest = *(arrayname);
	for(int i =0; i < x; i++)
	{
		if (*(arrayname + i) < smallest)
		{
			*(arrayname + i) = smallest;
		}
	}

	return smallest;
}
Last edited on
Topic archived. No new replies allowed.