1D array help


Write your question here.
I need a program to find the min number in an array, where u input the numbers, don't know how to get it working. here is my program so far
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
double array_min(double [], int); 
// main 
int main () 
{ 
double min,i; 
double array1[10]; 
min = array_min(array1,10); 
cout << " Enter in ten numbers " << endl; 
cin >> array1; 
array_min(array1,i)
cout << " The minimum number is "<< min << endl; 

return 0; 
} 
double array_min(double array [], int size) 
{ 
double min; 

for (int i = 0; i < size; i++) 
{ 
if (array[i] < min) 
min = array[i]; 
} 
return min; 
}
Your code has numerous mistakes, I am providing you the correct code rather than pointing out the mistakes (It will save me lots of effort).

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
#include<iostream>
using namespace std;
double arr_Min(double [], int);
// main
int main ()
{
double Min,i;
double arr1[10];
//Min = arr_Min(arr1,10);
cout << " Enter in ten numbers " << endl;
for(int i=0; i<10; i++){
cin >> arr1[i];}
Min=arr_Min(arr1,10);
cout << " The Minimum number is "<< Min << endl;

return 0;
}
double arr_Min(double arr [], int Size)
{
double Min=100000000;//The maximum number you can find

for (int i = 0; i < Size; i++)
{
if (arr[i] < Min)
Min = arr[i];
}
return Min;
}


In your array_min function, you compare array[i] to min, but the problem is that the variable "min" is never initialized to any value, so you are comparing junk.

If your array only has positive numbers, I would make a very large value be the initial value of your min variable. ex: double min = 10000.0;
___________
Second problem is that on line 7, you call array_min on your array1, but your array1 is never initialized with values, so the min value will be junk.

___________
Third problem is that you can't use cin on an array like that (line 9), you have to use a loop and do
1
2
for (int i = 0; i < size; i++)
    cin >> array[i];


___________
Fourth problem is on line 10, you call array_min, but you never store the value anywhere.


So overall:
- before calling array_min, you need to make sure your array actually has non-junk values in it
- initialize the min variable inside your array_min function to some large number.
Last edited on
Topic archived. No new replies allowed.