1

i've been assigned to write a program in C++ that, given an array of integers, finds the smallest (including its index) and largest number, and their subtraction. To do this, i decided to write three functions for each operation and it works perfectly. The problem is i don't know how to get the index of the smallest number.

I tried solving the problem by creating a variable and putting in inside a for loop and then print it. However, the program always says the index is 0.

Here you can see an example of how the output should be:

int myvector[200] = {40, 250, 9, 50} // Array containing 4 numbers

It should output: 

The smallest number is: 9 at index 2
The largest number is: 250
Their subtraction is: 241

Here you can see my code:

#include <iostream>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

int find_smallest (int myvector[], int smallest, int index){

    smallest = myvector[0];
    index = myvector[0];
    for(int i=0; i<4; i++) {
        if(myvector[i] < smallest) {
            smallest = myvector[i];
            index = i;
        }
    }

    return smallest;
}


int find_largest (int myvector[], int largest) {

    largest = myvector[0];
     for(int i=0; i<4; i++) {
        if(myvector[i] > largest) {
            largest = myvector[i];
        }
    }

    return largest;
}

int sub_minmax (int myvector[], int subtraction, int smallest, int largest){

    subtraction = largest - smallest;
    return subtraction;
}

int main()
{

    int myvector[200], smallest, largest, subtraction, index;

    for(int i=0;i<4; i++) {
        cout<<"Enter the number " <<i+1<<endl;
        cin>>myvector[i];
    }

    smallest = find_smallest(myvector, smallest, index);
    largest = find_largest(myvector, largest);
    subtraction = sub_minmax(myvector, subtraction, smallest, largest);

    cout<<endl;
    cout<<"the smallest number is: "<<smallest<<" at index "<<index<<endl;
    cout<<"the largest number is: "<<largest<<endl;
    cout<<"the substraction is: "<<subtraction<<endl;

    return 0;
}

Any help will be appreciated, thanks

1
  • 1
    Pass index by reference. Commented Mar 19, 2020 at 12:35

2 Answers 2

1

Pass index by reference.

int find_smallest (int myvector[], int smallest, int &index)
Sign up to request clarification or add additional context in comments.

1 Comment

@alex108 You are welcome. Note that I did not explained this reference thing since it would be redundant, there is lot of materials on this just search something like "c++ pass by value vs reference" to see the reason why this work.
1

change this

int find_smallest (int myvector[], int smallest, int index)

to

int find_smallest (int myvector[], int smallest, int &index)  

here index is not local variable it reference to value passed, so any changes in index in function reflect is original value.

if you want to return both you can use struct or class.

2 Comments

Thanks. and Sorry i am a C++ beginner
@alex108 if you got the answer feel free to accept any one of answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.