Question

Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns...

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]

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C Programming: Write a function that takes in an array of integers and an integer containing...
C Programming: Write a function that takes in an array of integers and an integer containing the count of elements, then have it returns the sum of all the even values inside the array.
c++ please 1. Write and test the function maximum that is passed an array of n...
c++ please 1. Write and test the function maximum that is passed an array of n pointers to integers and returns the maximum value among the n integers. The function must use the traveling pointer notation to traverse the array. you have to ask the user for the size of the array and ask the user to input the values in the main. The function has the following prototype. int maximum ( int *p [ ], int n);
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
COMPLETE IN C++ Declare and initialize a global constant named SIZE with the value 50. Write...
COMPLETE IN C++ Declare and initialize a global constant named SIZE with the value 50. Write a void function called count that accepts two parameters-- a C-string called str representing a C-string passed to the function and an array of integers called alphabets to store the count of the letters of the C-string. Note that the alphabets array stores 26 counters – one for each letter of the alphabet. Use the same counter for lowercase and uppercase characters. The first...
Questions: 1. (5 marks) Create a VB.NET Console Application that defines a function Smallest and calls...
Questions: 1. Create a VB.NET Console Application that defines a function Smallest and calls this function from the main program. The function Smallest takes three parameters, all of the Integer data type, and returns the value of the smallest among the three parameters. The main program should (1) Prompt a message (using Console.WriteLine) to ask the user to input three integers. (2) Call the built-in function Console.ReadLine() three times to get the user’s input. (3) Convert the user’s input from...
Write a function that accepts an int array and the array’s size as arguments. The function...
Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N...
C CODE PLZ! All instructions are in sections of code #include <stdio.h> /* TODO: Define 3...
C CODE PLZ! All instructions are in sections of code #include <stdio.h> /* TODO: Define 3 functions input, gcd and lcm in such a way that the main function below compiles correctly and has the correct behavior. The input function prompts the user to enter a non-negative integer. If the user enters a negative integer, the function prints a "sorry" statement and prompts the user again. It keeps on prompting until the user enters a non-negative number. The input function...
Write a function that accepts an int array and the array’s size as arguments. The function...
Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N (that is not more than 50) from standard input and then reads N integers from a file named data into an...
C++ Write a function that takes in 3 arguments: a sorted array, size of the array,...
C++ Write a function that takes in 3 arguments: a sorted array, size of the array, and an integer number. It should return the position where the integer value is found. In case the number does not exist in that array it should return the index where it should have been if it were present in this sorted array. Use pointer notation of arrays for this question. c++ code
Write a program in c++ to Convert an array of inches to an array of centimeters....
Write a program in c++ to Convert an array of inches to an array of centimeters. The program should contain a function called inchesTOcm with three parameters (inches array that contains the values in inches, cm array to save the result in, and an integer variable that defines the length of the array). In the main function: 1. Define an array (inches) of length 3. 2. Initialize the array by asking the user to input the values of its elements....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT