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
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....
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...
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...
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 spim program and execute it on mars. Your program reads two integer values x and...
Write spim program and execute it on mars. Your program reads two integer values x and y. Write a function called sum that gets x and y passed as parameters and return the sum of odd values between x and y inclusive. In addition write another function called even that gets x and y as parameters and returns the count of all even numbers between x and y inclusive. Also in main print the quotient and remainder of diving y...
Please answer the following C question: There is a documented prototype for a function called get_a_line...
Please answer the following C question: There is a documented prototype for a function called get_a_line in the code below. Write a definition for get_a_line—the only function called from that definition should be fgetc. #include <stdio.h> #include <string.h> #define BUFFER_ARRAY_SIZE 10 int get_a_line(char *s, int size, FILE *stream); // Does what fgets does, using repeated calls to fgetc, but // provides a more useful return value than fgets does. // // REQUIRES // size > 1. // s points to...
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to...
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to N. The function calculateSum has one parameter N of type integer and returns an integer which represents the sum from -1 to N, inclusive. Write another function calculateAverage that calculates an average. This function will have two parameters: the sum and the number of items. It returns the average (of type float). The main function should be responsible for all inputs and outputs. Your...
Write a program containing a function, reverseDigit, that takes an integer as a parameter and returns...
Write a program containing a function, reverseDigit, that takes an integer as a parameter and returns the number with its digits reversed, then printout the return result. For example, the value of reverseDigit(12345) is 54321; the value of reverseDigit(5600) is 65; the value of reverseDigit(7008) is 8007; and the value of reverseDigit(-532) is -235.    Modify the following program to make a correct output. /* // Name: Your Name // ID: Your ID // Purpose Statement: ~~~ */ #include <iostream>...
Function Example: Write a Python function that receives two integer arguments and writes out their sum...
Function Example: Write a Python function that receives two integer arguments and writes out their sum and their product. Assume no global variables. def writer(n1, n2): sum = n1 + n2 product = n1*n2 print("For the numbers", n1, "and", n2) print("the sum is", sum) print("and the product is", product) ... 1) Create a PYHW2 document that will contain your algorithms in flowchart and pseudocode form along with your screen shots of the running program. 2) Create the algorithm in both...
Write the functions which are called in the main function given. YOU MAY ASSUME THAT THE...
Write the functions which are called in the main function given. YOU MAY ASSUME THAT THE USER ENTERS A VALID INTEGER. printDouble should accept one value and should print the value of that number times 2. printEndMessage should not accept any values and should print a message indicating that the program has finished. def main(): num1 = int(input("Please enter your number ")) printDouble(num1) printEndMessage() main()