Greetings, everyone i have trouble with this exercise in programing C. Write a u
ID: 3838593 • Letter: G
Question
Greetings, everyone i have trouble with this exercise in programing C. Write a user-defined function definition for a function called GetSmallest that takes 2 arguments: an array of integers, and the size of the array. The function should return the smallest value in the array. You should not assume that the variables will contain different values.
my ans was (it was wrong):
int GetSmallest (int a[], int n) {
int c, Smallest, index;
Smallest = a[0];
index = 0;
for (c = 1; c < 9; c++) {
if (a[c] < Smallest) {
index = c;
Smallest= a[c];
}
}
return index;
}
Explanation / Answer
Hi
I have modified the code and highlighted the code changes below. Since we arer looking for small value in array, we dont need to get the index of small value in array. In above method, we are returning small value index in array which is wrong. As per requiremen, we have to return the small value in array.
int GetSmallest (int a[], int n) {
int c, Smallest;
Smallest = a[0];
for (c = 1; c < 9; c++) {
if (a[c] < Smallest) {
Smallest= a[c];
}
}
return Smallest;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.