arrays and functions -

Write your question here.


Doing a programming exercise and I would like to take
the below code block and change it to a function.

1
2
3
4
5
6
7
    // Print Small Index
    int index, smindx = 0;
    for (index = 0; index < MAXINDEX; index ++) {
        if (alpha[index] < alpha[smindx]) {
            smindx = index;
        }
    }




Then to output to cout the function like this.


 
    cout << "Smallest index in alpha is: " << alpha[smindx] << " at postion " << smindx << endl;


EDIT: one more question.

The output of the program looks like

1
2
3
4
5
6
0        1  1.41421  1.73205        2  2.23607  2.44949  2.64575  2.82843        3  
3.16228  3.31662   3.4641  3.60555  3.74166  3.87298        4  4.12311  4.24264   4.3589  
4.47214  4.58258  4.69042  4.79583        0       75       78       81       84       87  
     90       93       96       99      102      105      108      111      114      117  
    120      123      126      129      132      135      138      141      144      147  
Smallest index in alpha is: 0 at postion 0


where I would like it to look like this

1
2
3
4
5
6
7
8
9
0        1         1.41421 1.73205  2        2.23607  2.44949  2.64575  2.82843   3  
3.16228  3.31662   3.4641  3.60555  3.74166  3.87298  4        4.12311  4.24264   4.3589  
4.47214  4.58258   4.69042 4.79583  0        75       78       81       84        87  
90       93        96      99       102      105      108      111      114       117  
120      123       126     129      132      135      138      141      144       147  

Smallest index in alpha is: 0 at postion 0



is there a way to justify this way? What would be the best way to do this?
using - below is the code block responsible for the "table"

1
2
3
4
5
6
7
8
9
 // Print Table
    int k = 1;
    for (int j = 0; j < MAXINDEX; j++ ,k++) {
        cout << alpha[j] << "  " << setw(7);
        if (k == 10) {
            cout << endl;
            k = 0;
        }
    }






Full code that I have is below.
Included function prototype. (not sure if that is correct)


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
#include <iostream>
#include <math.h>       /* sqrt */
#include <iomanip>      // std::setw

int const MAXINDEX = 50;

using namespace std;

void smallestIndex(const double array[], int listSize);


double alpha[MAXINDEX];


int main(int argc, const char * argv[])
{
    

    // Giving lower half of array values
    for (int i = 0; i < 24; i++) {
        alpha[i] = sqrt(i);
    }
    
    // Giving upper half of array values
    for (int i = 25; i < MAXINDEX; i++) {
        alpha[i] = i * 3;
    }

    // Print Table
    int k = 1;
    for (int j = 0; j < MAXINDEX; j++ ,k++) {
        cout << alpha[j] << "  " << setw(7);
        if (k == 10) {
            cout << endl;
            k = 0;
        }
    }

    // Print Small Index
    int index, smindx = 0;
    for (index = 0; index < MAXINDEX; index ++) {
        if (alpha[index] < alpha[smindx]) {
            smindx = index;
        }
    }
    
    cout << "Smallest index in alpha is: " << alpha[smindx] << " at postion " << smindx << endl;
    
    return 0;
}


Thanks for you help


Last edited on
1
2
3
4
5
6
7
8
9
10
template<class T, int U>
int smallest_index(T (&array)[U])
{
    int result = 0;

    for (int i = 1; i < sizeof(array) / sizeof(T); i++)
        result = array[i] < array[result] ? i : result;

    return result;
}
Last edited on
closed account (28poGNh0)
// Try to avoid as much as possible to use global variables, observe the new location of alpha, that would help you to understand function more ,try to do the same with MAXINDEX
//You should know that you left the element alpha[24] empty/or taking a random value

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
# include <iostream>
# include <math.h>       /* sqrt */
# include <iomanip>      // std::setw
using namespace std;

int const MAXINDEX = 50;

void smallestIndex(const double array[], int listSize);

void function(double array[MAXINDEX])
{
    // Print Small Index
    int index, smindx = 0;
    for (index = 0; index < MAXINDEX; index ++)
    {
        if (array[index] < array[smindx])
            smindx = index;
    }

    cout << "Smallest index in alpha is: " << array[smindx] << " at postion " << smindx << endl;
}

int main(int argc, const char * argv[])
{
    double alpha[MAXINDEX];

    // Giving lower half of array values
    for (int i = 0; i < 24; i++) {
        alpha[i] = sqrt(i);
    }

    // Giving upper half of array values
    for (int i = 25; i < MAXINDEX; i++) {
        alpha[i] = i * 3;
    }
    
    // Print Table
    int k = 1;
    for (int j = 0; j < MAXINDEX; j++ ,k++)
    {
        cout << alpha[j] << "  " << setw(7);
        if (k == 10)
        {
            cout << endl;
            k = 0;
        }
    }

    function(alpha);

    return 0;
}
Hi Josue

Can you walk me through what you are doing here

specifically this line?

result = array[i] < array[result] ? i : result;

can you break it down, I understand peices of this, but you start to loose me at the
? i : result;
result = array[i] < array[result] ? i : result;

is the exact same as

1
2
3
4
5
6
7
8
if (array[i] < array[result])
{
    result = i;
}
else
{
    result = result;
}

?: is called the ternary operator.

http://www.cplusplus.com/forum/articles/14631/
Last edited on
@Techno01

Thank you for pointing that out alpha[24] was not included in my for loops.

Yes. Your right about the global variables. I forgot to put it back into main, after I placed it there when troubleshooting adding a function.

Thank you for

1
2
3
4
5
6
7
8
9
10
void function(double array[MAXINDEX])
{
    // Print Small Index
    int index, smindx = 0;
    for (index = 0; index < MAXINDEX; index ++)
    {
        if (array[index] < array[smindx])
            smindx = index;
    }


A question I have is the book I am going over tends to create array functions like with the formal parameters like my original function prototype.

e.g
 
void smallestIndex(const double array[], int listSize);


where it appears that you are only using one formal parameter
to me what your doing seems to make sense.

Are you aware of the style difference and why one may work better than another?

His function takes one parameter because the size of the array is defined globally, thus passing it again would be redundant.

In my version, the size of the array is determined at run-time using advanced ideas.

The book's version is beginner-friendly; there's no trivial way to determine the size of a passed array since this decays to a pointer, so the function requires it.

By the way, to print the array, try this:

1
2
3
4
5
6
7
8
9
10
11
12
// Print Table
cout << setprecision(7);
int k = 1;
for (int j = 0; j < MAXINDEX; j++ ,k++)
{
    cout << setw(8) << alpha[j] << "  ";
    if (k == 10)
    {
        cout << endl;
        k = 0;
    }
}
@Josue Molina Thank you input here. That makes more sense to me about the function prototype. also thank you for your feedback about the printing, I see what I was doing wrong there.

Now that I re-read this

 
 result = array[i] < array[result] ? i : result;


This is a pretty fantastic assignment statement.
Final code with all of your help..
still open for feedback but otherwise solved.

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
#include <iostream>
#include <math.h> /* sqrt */
#include <iomanip> // std::setw

int const MAXINDEX = 50;

using namespace std;

int smallestIndex(const double array[]);

int main(int argc, const char *argv[])
{
    double alpha[MAXINDEX];


    // Giving lower half of array values
    for (int i = 0; i < 25; i++) {
        alpha[i] = sqrt(i);
    }

    // Giving upper half of array values
    for (int i = 25; i < MAXINDEX; i++) {
        alpha[i] = i * 3;
    }

    // Print Table
    cout << setprecision(7);
    int k = 1;
    for (int j = 0; j < MAXINDEX; j++, k++) {
        cout << setw(8) << alpha[j] << " ";
        if (k == 10) {
            cout << endl;
            k = 0;
        }
    }


    int smindx = smallestIndex(&alpha[MAXINDEX]);

    cout << "Smallest index in alpha is: " << alpha[smindx] << " at postion " << smindx << endl;

    return 0;
}

// Smallest index
int smallestIndex(const double alpha[MAXINDEX])
{
    int index, smindx = 0;
    for (index = 0; index < MAXINDEX; index++) {
        if (alpha[index] < alpha[smindx]) {
            smindx = index;
        }
    }
    
    return smindx;
    
}
Last edited on
Topic archived. No new replies allowed.