Reputation: 11
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.
Upvotes: 0
Views: 282
Reputation: 76498
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;
}
Upvotes: 2
Reputation: 21576
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;
}
Upvotes: 1
Reputation: 8215
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.
Upvotes: 3