Problem using pointers

I have a small issue with my code. My instructor provided the main and we are to write the functions. I have most of my code functional, but he has thrown me for a bit of loop in one section. I don't understand what he has done with the table_min function. I don't understand how that line of code is supposed to work. The original instructions stated:

7. Write table_min a function to find the smallest value in an array.
int table_min (array , size);

If I take my code out, it will calculate the average, but when I put it into what he has, I get very long, long lines of cryptic errors. I appreciate any help or tips provided. Again, Thanks in advance for your time!!!

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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <cstdlib>

using namespace std;

//**********Function Prototypes**********
void table_fill(int *, int, int);
void table_print(int *, int);
void table_add1(int *, int);
void table_print_rev(int *, int);
void table_min(int *, int);
//********End Function Prototypes********

int main()
{
    const int Max = 20;
    int ary[Max];
    int bry[Max];
    int *p, *s, *tp;

    p = &ary[0];
    s = &bry[0];

    table_fill( p, Max, 20 );
    cout << endl << "Fill array with 20s " << endl;
    table_print( p, Max );  cout << endl << endl;

    cout << endl << "Add 1 to first 10 elements " << endl;
    table_add1( p, 10);
    table_print( p, Max );  cout << endl << endl;

    tp = s;   // save pointer
    for (int i=1; i<= Max; i++ )
    {
	table_fill( s, 1, i );
	s++;
    }
    cout << endl << "Fill array with 1 to 20 " << endl;
    s = tp;   // restore pointer
    table_print( s, Max );  cout << endl << endl;

    cout << endl << "Print reverse order " << endl;
    table_print_rev( s, Max );  cout << endl << endl;

    table_fill( p, Max, 0 );
    cout << endl << "Zero out array " << endl;
    table_print( p, Max );  cout << endl << endl;

    s = tp;        // What's happening here?
    for ( int i=Max; i>=0; i--)
    {
	   table_add1( s, i );
    }

    cout << endl << "Fill array with ???? " << endl;
    table_print( s, Max );  cout << endl << endl;

    cout << endl << "Print reverse order " << endl;
    table_print_rev( s, Max );  cout << endl << endl;

    for (int i=0; i<Max/2; i++)
    {
	    tp = p+10+i;
	    table_fill( tp, 1, i*2 );
	    tp = p+10-i;
	    table_fill( tp, 1, i*3 );
    }
    cout << endl << "Fill array with <-> " << endl;
    tp = p + 1;
    table_print( p, Max );  cout << endl << endl;

//*******Issues Begin Here********
//I don't understand how this works.
    cout << endl << "Min for previous table " << table_min( p, 12 );

    return 0;
}

//**********Function Definitions**********
void  table_fill(int *ptr, int M, int val)
{
    //Fill the Array
    for(int i = 0; i < M; i++)
    {
        *ptr = val;
        ptr++;
    }
}

void table_print(int *ptr, int M)
{
    //Print the Table
    for(int i = 0; i < M; i++)
    {
        cout << *ptr << " ";
        ptr++;
    }
}

void table_add1(int *ptr, int val)
{
    //Add 1 to the first 10 elements of the array
    for(int i = 0; i < val; i++)
    {
        *ptr = *ptr + 1;
        ptr++;
    }
}

void table_print_rev(int *ptr, int M)
{
    //Print Array in Reverse
    for(int i = 0; i < 20; i++)
    {
        cout << *(ptr + 19) << " ";
        ptr--;
    }
}

void table_min(int *ptr, int val)
{

    int smallest = *ptr;

    for(int i = 0; i < val; i++)
    {
        if(*ptr <= smallest)
        {
            smallest = *ptr;
        }
        ptr++;
    }
}
//********End Function Definitions********



The statement

cout << endl << "Min for previous table " << table_min( p, 12 );

is incorrect because your function table_min does not return a value. Its return type is defined as void

It is better to define your function as follows

1
2
3
4
5
6
7
8
9
10
11
int table_min( int *a, int n )
{
    int min = a[0];

    for( int i = 1; i < n; i++ )
    {
        if ( a[i] < min ) min = a[i];
    }

    return ( min );
}
Last edited on
Thanks so much for your help. I made the corrections and it works perfectly.
Topic archived. No new replies allowed.