0

I am trying to create a program to find the minimum integer in an array of integers. This is my code:

#include<iostream>

using namespace std;

int findMinimum(int array);

int findMinimum(int array){
  int arraySize = sizeof(array)/sizeof(int);
  int minimum = array[0];
  for (int i = 0; i < arraySize; i++){
    if (arraySize[i] < minimum){
      minimum = arraySize[i];
    }
  }
  return minimum;
}

int main(){
  int array[7] = {17,2,10,291,28,10,11};
  int minimum = findMinimum(array);
  cout << "The minimum of the array is: " << minimum;
}

I am getting this error:

    /Users/Danny/Desktop/C++/Practice/arrays.cpp:9:22: error: subscripted value is not an array, pointer, or vector
  int minimum = array[0];
                ~~~~~^~
/Users/Danny/Desktop/C++/Practice/arrays.cpp:11:18: error: subscripted value is not an array, pointer, or vector
    if (arraySize[i] < minimum){
        ~~~~~~~~~^~
/Users/Danny/Desktop/C++/Practice/arrays.cpp:12:26: error: subscripted value is not an array, pointer, or vector
      minimum = arraySize[i];
                ~~~~~~~~~^~
/Users/Danny/Desktop/C++/Practice/arrays.cpp:20:17: error: no matching function for call to 'findMinimum'
  int minimum = findMinimum(array);
                ^~~~~~~~~~~
/Users/Danny/Desktop/C++/Practice/arrays.cpp:7:5: note: candidate function not viable: no known conversion from 'int [7]' to 'int' for 1st argument
int findMinimum(int array){

How do I fix these errors? Thank you.

3
  • 3
    Well, array in findMinimum(), despite the name, isn't an array... it's just an int. Commented Feb 21, 2016 at 21:41
  • @Barry but when I create an integer array, its "int arrayName[2] = {1,2};". What type do I call for then? Commented Feb 21, 2016 at 21:44
  • The [2] part is part of the type as well - arrayName isn't an int, it's an int[2]. Commented Feb 21, 2016 at 21:51

3 Answers 3

3

The function head should be:

int findMinimum(int* array)

However, for an int*, this won't work:

int arraySize = sizeof(array)/sizeof(int);

Therefore you should also pass the size to the function:

int findMinimum(int* array, int size)

You should also consider to use std::vector instead of the array.

Sign up to request clarification or add additional context in comments.

3 Comments

He could also simply pass int array[7] to findMinimum, in case he doesn't want to bother with pointers.
Almost what I was about to write, nice! One note I'd like to add is that while passing a pointer and the size is quite natural, keeping to how the STL does things would mean passing a pointer to the begin and one past the end of the array. Oh, and the pointer should probably have type int const *.
@Fang Though the sizeof approach won't work with that, too.
2

This may be cheating:

#include <algorithm>
#include <iostream>
#include <iterator>

int main() {
    int array[7] = { 17, 2, 10, 291, 28, 10, 11 };
    int min = *std::min_element(std::begin(array), std::end(array));
    std::cout << "The minimum of the array is: " << min << '\n';
    return 0;
}

1 Comment

Need a * there for std::min_element's return value.
1

What you passed in this line

int minimum = findMinimum(array);

is actually a pointer to an int array... actually, the pointer to the first element. So, you want to change your function signature to

int findMinimum(int* array)


In the modified function int findMinimum(int* array), the line below will be wrong

int arraySize = sizeof(array)/sizeof(int);

because, array is already a decomposed pointer here... so you want to change the function again to

int findMinimum(int* array, int size)

Your complete program will be:

#include<iostream>

using namespace std;

int findMinimum(int* array, int arraySize);

int findMinimum(int* array, int arraySize){
  int minimum = array[0];
  for (int i = 0; i < arraySize; i++){
    if (arraySize[i] < minimum){
      minimum = arraySize[i];
    }
  }
  return minimum;
}

int main(){
  int array[7] = {17,2,10,291,28,10,11};
  int minimum = findMinimum(array, sizeof(array)/sizeof(int));
  cout << "The minimum of the array is: " << minimum;
}

Comments

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.