Question
daily21. Computing 1. C programming
Please provide comments and write pre-condition and post-condition for the function.
Description Create a project called Daily21. Add a source file called daily21.c into the project. In this exercise, you will practice working with an array of integers. In the main function, you first need to create an integer array with 10 elements in t. Use a loop to read 10 integers from the user and store the values to the array (in the order of input). For example (in the screenshot below) the first element in the array stores 23, the next element stores value of 55, then the element stores the value of 87 and so on. You may assume that the user will only enter integers, but not characters, etc. Once the array is initialized, call a function to get the index number of the largest value in the array. For example, the largest element in the array is 98 (as shown below), so the return value of the function should be 5. This function should take the array you created in the main function as a parameter, the number of valid elements in the array, and return the index of the maximum value in the array. The array should not be changed after the function is called. Write the pre-condition and post-condition for the function (2 points, 1 point each). Print the result in the main program after calling the function as below CWindowslsystem321cmd.exe nter 10 integer values... Value 1: 23 Value 2: 55 Value 3: 87 alue 4: -12 Jalue 5: alue 6: 98 Value 7: 34 Value 8: 28 alue 9:-39 alue 10: 20 The index for the maxinun is 5 Press any key to continue .
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

//Function declaration
int getMaxValIndex(int arr[]);
int main( )
{

//declaring and initializing an array  
int arr[10];

//Declaring variable
int i;

printf("Enter 10 Integer values...\n");

//Displaying the array
for(i=0;i<10;i++)
{
printf("Value %d :",i+1);
scanf("%d",&arr[i]);
}

//Calling the function which returns the max element index value of an array
int maxValIndex=getMaxValIndex(arr);
printf("The index for the maximum is %d",maxValIndex);

return 0;
}

/* Function implementation which finds the maximum
* element index value of an array
*/
int getMaxValIndex(int arr[])
{
   //Declaring variable max and initialized with array first element
   int max=arr[0];
   int i,index=0;
  
   //This loop will finds the maximum elements index value.
   for(i=0;i<10;i++)
   {
       //Checking whether the array element is greater tham max value
       if(arr[i]>max)
       {
           max=arr[i];
           index=i;
       }

   }
return index;  
}  

__________________________________

Output:

CAProgram Files (x86)\Dev-Cpp\MinGW641bin\MaxindexValue.exe Enter 10 Integer values.. . Ualue 1 :23 Ualue 2 :55 Ualue 3 :87 U

Add a comment
Know the answer?
Add Answer to:
daily21. Computing 1. C programming Please provide comments and write pre-condition and post-condition for the function....
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Code please read comments and add the code to required lines....LINE 1 to LINE 5...

    Java Code please read comments and add the code to required lines....LINE 1 to LINE 5 ********************************************************************** * Program Summary: This program demonstrates these basic Java concepts: * - Creating an array based on user input * - Accessing and displaying elements of the array * * The program should declare an array to hold 10 integers. * The program should then ask the user for an integer. * The program should populate the array by assigning the user-input integer...

  • in c++ please Assignment Instructions: MAIN FUNCTION: Ask the user how many students were surveyed.   Call...

    in c++ please Assignment Instructions: MAIN FUNCTION: Ask the user how many students were surveyed.   Call a function called makeArray to define an array of integers with the number of elements equal to the number of students surveyed. Call a function called getStudentData to allow the user to enter the number of hours each student spent watching Netflix into the array. Call a function called getAverage to calculate and display the average of the hours entered. Call a function called...

  • Write a C program to do the following: 1. Write a C function named find that...

    Write a C program to do the following: 1. Write a C function named find that receives a one-dimensional array of type character named arr and its size of type integer. After that, the function must select a random index from the array. The function must find if the character stored in the randomly selected index comes before all the characters to its left alphabetically. Finally, the function prints to the screen the element if the element meets the condition...

  • Write a C++ function binsearch that carries out the binary search algorithm on a sorted array...

    Write a C++ function binsearch that carries out the binary search algorithm on a sorted array of integers. Your function takes as parameters an array of integers (sorted in increasing order), the size of the array, and a target integer to search for. The function returns the index of the target in the array, and returns -1 if the target is not in the array. The file main1.txt on the webpage contains code that generates a specific array of integers...

  • Write a C++ function, lastLargestIndex that takes as parameters an int array and its size and...

    Write a C++ function, lastLargestIndex that takes as parameters an int array and its size and returns the index of the "last occurrence" of the largest element in the array. Include another function to print the array. Also, write a program to test your function. [HINTS) Create an array of size 15 in the main function and initialize it with the values shown in the below sample output. Pass the array and its size to function lastLargestindex; function lastLargestindex returns...

  • In C++ 1. Write a program that allows a user to enter 10 integer values from...

    In C++ 1. Write a program that allows a user to enter 10 integer values from the keyboard. The values should be stored in an array. 2. The program should then ask the user for a number to search for in the array: The program should use a binary search to determine if the number exists in a list of values that are stored in an array (from Step #1). If the user enters a number that is in the...

  • Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...

    Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. To test your function, write a main that prompts a user for a list of 15 integers and outputs the index and value of the first occurrence of the smallest value. The program should print out Enter 15 integers: The position of the first occurrence of the smallest element in...

  • Need to write a MIPS assembly program that finds the minimum and maximum and sum of...

    Need to write a MIPS assembly program that finds the minimum and maximum and sum of a stored array. It also finds the locations of the minimum and maximum. The interaction between the main program and the function is solely through the stack. The function stores $ra immediately after being called and restores $ra before returning to main. The main program reserves a static C like array (index starts from 0) of 10 elements and initializes it. The maximum should...

  • In C++: Write a C++ function binsearch that carries out the binary search algorithm on a...

    In C++: Write a C++ function binsearch that carries out the binary search algorithm on a sorted array of integers. Your function takes as parameters an array of integers (sorted in increasing order), the size of the array, and a target integer to search for. The function returns the index of the target in the array, and returns -1 if the target is not in the array. Using the code below, add your binsearch function to this C++ program. (Do...

  • (C++) Write a function, remove, that takes three parameters: an array of integers, the number of...

    (C++) Write a function, remove, that takes three parameters: an array of integers, the number of elements in the array, and an integer (say, removeItem). The function should find and delete the first occurrence of removeItem in the array. (Note that after deleting the element, the number of elements in the array is reduced by 1.) Assume that the array is unsorted. Also, write a program to test the function. Your program should prompt the user to enter 10 digits...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT