jane
jane

Reputation: 124

Passing value from one function to another C++

I'm writing two functions: one of them is for "filling" array with random values and int the second function I have to use the same array, choose one row and find the min element of that row.

But the problem is that I don't know how to pass values from one function to another.

Here is my code:

#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;

void fillarray(int arr[5][5], int rows, int cols) {
    cout << "Static Array elements = \n\n" << flush;

    for(int i = 0; i < rows; ++i) {
        cout << "Row " << i << "  ";
        for(int j = 0; j < cols; ++j) {
            arr[i][j] = rand() % 10;
            cout << arr[i][j] << " " << flush;
        }
        cout << endl;
    }
    cout << " \n\n";
}

void minarray(int a, void fillarray) { // don't know what to write here

there:
    int min = INT_MAX; // Value of INT_MAX is 2147483648.

    if(a > 4) {
        cout << "Invalid input! " << endl;
        goto there;
    }

    for(int counter = 0; counter < 5; ++counter) {
        if(arr[a][counter] < min) min = arr[a][counter];
    }
    cout << "Minimum element is " << min << endl;
}
int main() {
    int z;

    srand(time(NULL));
    const int rows = 5;
    const int cols = 5;
    int arr[rows][cols];
    fillarray(arr, rows, cols);
    cout << "Enter the number of row: ";
    cin >> z;
    minarray(z, fillarray) 
    system("PAUSE");
}

Upvotes: 1

Views: 887

Answers (4)

Ted Lyngmo
Ted Lyngmo

Reputation: 117583

I'll let the other answers answer your question and concentrate on the code around your goto that you asked about in the comments.

In main you have this:

    cout << "Enter the number of row: ";
    cin >> z;
    minarray(z, fillarray) 

In minarray you have this:

void minarray(int a, void fillarray) { // don't know what to write here

there:
    int min = INT_MAX; // Value of INT_MAX is 2147483648.

    if(a > 4) {
        cout << "Invalid input! " << endl;
        goto there;
    }

First, there's absolutely no reason to use goto. You could do this:

void minarray(int a, void fillarray) { // don't know what to write here
    int min = INT_MAX; // Value of INT_MAX is 2147483648.

    while(a > 4) { // loop for as long as "a > 4"
        cout << "Invalid input! " << endl;
    }

Removing the goto made the bug rather apparent. a will never change inside the loop, so it'll just print Invalid input! forever if you give it invalid input. An alternative would be to validate the input when you actually get the input from the user (in main):

    while(true) { // loop forever
        cout << "Enter the number of row: ";
        if(cin >> z) {                  // check that the user inputs an int 
            if(z<0 || z>4)              // validate the input
                cout << "Invalid input!\n";
            else
                break;                  // we got valid input, break out of the while loop
        } else {                        // user did not input an int
            std::cout << "input failed - aborting\n";
            return 1;                   // return from main to exit the program
        }
    } // if the program reaches this point, it'll ask the user for input again
      // and that will only happen if the user gives it an int that is <0 or >4

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 311068

For starters the function fillarray has redundant parameter cols because this number is known from the declaration of the first parameter int arr[5][5].

Th function can be declared like

void fillarray(int arr[5][5], int rows )

You could supply the parameter cols in case when not the whole array is filled in the function.

You already filled the array by this call

fillarray ( arr, rows, cols );

The function performed its task. So there is no need to reference the function one more time as you are trying

minarray(z, fillarray)

The function minarray can be declared either like

void minarray( const int arr[], size_t n );

and called like

minarray( arr[z], cols );

with a preliminary check that z is less than 5.

Or it can be declared like

void minarray( const int arr[][5], size_t n, size_t row );

and called like

minarray( arr, rows, z );

Pay attention to that there is the standard algorithm std::min_element that allows to find minimum element in an array. And to fill an array with values you can use the standard algorithm std::generate.

And each function should do only one task. For example the function fillarray should silently fill the array with values. To output the array you could write a separate function.

Upvotes: 1

Joseph Larson
Joseph Larson

Reputation: 9068

What you need to do is call fillarray to fill your array. So it would look like

fillarray(arr, rows, cols);

Just like you have so far. Now, you have array arr all filled in. minarray doesn't care how that happened. So don't pass it your filler method. Pass it the array.

minarray(cols, arr[z]);

You don't need to pass the entire array -- just the row in question. You're also passing the width.

And change the definition of minarray:

void minarray(int length, int[] array)

Now, your minarray itself needs changes. First, get rid of the if-check. You don't need to pass a row number now, but you do need the number of columns passed as length.

Then your for loop looks like:

for (int index = 0; index < length; ++index) {
    if (array[index] < min) {
        min = array[index];
    }
}

So, to summarize:

  • Main declares the data and calls your two methods.

  • fillarray populates the array. It is called from main the way you already have.

  • minarray prints the minimum on a single line. It is also called from main, passing in the array, not the method that filled it.

You have one more issue, however. fillarray hardcodes the array size as 5x5, but main uses constants defined. I'd move those contents to the top of the file and use them in both places.

Move to the top, below any #includes:

const int rows = 5;
const int cols = 5;

Define fillarray:

void fillarray(int arr[rows][cols]) {

And when you call it from main:

fillarray(arr);

Upvotes: 1

KalBaratheon
KalBaratheon

Reputation: 82

I'm not sure this even compiles, but i'm guessing you want to pass int arr[x][y] from the fill Array function to the minArray function. To do that you first need to include arr as a parameter of minArray. From there you need to pass it by reference. Then, you can call minArray from fillArray.

Upvotes: 1

Related Questions