Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns the index of the first occurance of the smallest value in the array. Your function must be able to process all the elements in the array. Create a function prototype and function definition (after the main function). Your main function should declare a 100 element integer array. Prompt the user for the number of integers to enter and then prompt the user for each value using a loop. You need to verify the that the count is between 1 and 100 (inclusive). Your main function will call your function with the array entered by the user and print the results as shown below. Sample Output: How many values: 0 Array count out of range! How many values: 6 Enter value [0]:44 Enter value [1]:23 Enter value [2]:77 Enter value [3]:3 Enter value [4]:99 Enter value [5]:3 Smallest value = 3, was found at index [3]
We can create this program in different ways. Here, I am creating a global variable called index. Because we change this value anywhere from the program. Also I am declaring the function prototype after the main() as the question suggested.
If you are agree with the code, kindly upvote. Otherwise comment what's wrong in the code.
Here is the code
#include <stdio.h>
int index;
/* index is a global integer variable we can make chages to this
variable
anywhere in the program*/
void main()
{
int a[100],i,n,min;
/* array a is declared with 100 memory
locations, i variable
is used for loop iteration min is used to store
smallest value*/
printf(" Enter Number of Elements in Array
:\n");
scanf("%d",&n);
/*Reading n value for number of array elements
you want to read now*/
/* array size is 100, so you should have to
check if the entered size is less than 100 and greater
than 0*/
if(n>0 && n<=100)
{
/* if n is in the
correct range, enter the numbers*/
/* Read Elements in an array */
for(i=0 ; i < n ; i++)
{
// loop for entering each
value
printf("Enter
value[%d]:",i);
/* This is to print like
enter value[0]*/
scanf("%d",&a[i]);
/*reading each element
in array*/
}//end of loop
min=smallest(a,n);
/* calling smallest() method, which pass
array name and size n*/
printf("Smallest value=%d was found at
index [%d]",min,index);
/*print the smallest value and its index*/
}
/* the else part is works only the entered n
value is not in the correct range*/
else
printf("\n Array count
out of range..!!");
getch();
}//end of main
int smallest(int a[],int);
/* function prototype which is an integer returning finction
it passes two arguments an array and an integer variable*/
/* The question state that both function prptotype and definition
should be after the main method,
so it is declared here. If you need you can declare it before the
main()*/
int smallest(int a[],int n)
{
//min for smallest value*/
int min,i;
/* First, we assume that the first value in the array is the
minimum value,
for that we just assign value ina[0] to min
variable*/
min = a[0];
/*also we have to keep track the smallest value occuring index,
here it is 0*/
index=0;
for(i = 0 ; i < n ; i++)
{
/*iterating through the array,
comparing each array element with
current min value, if it is less
than current min value then set it as the new min value*/
if ( a[i] <
min)
{
min = a[i];
/* setting new minimum is the array value, if it is less than
current minimum*/
index=i;
/* also keep tracking the i position to index only when the minimum
changes */
}//end of if
}//end of for
return min;//returning min value
}
Screenshot for code
screenshot for output
Get Answers For Free
Most questions answered within 1 hours.