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
  • 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...

  • 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...

  • C++ ONLY THANKS! USE VISUAL STUDIO COMPILER ONLY! Part 1: Exercise 1: Problem description Write a...

    C++ ONLY THANKS! USE VISUAL STUDIO COMPILER ONLY! Part 1: Exercise 1: Problem description Write a three function program (your main function and two other functions). The main function of the program should create an array that can store 10 integers. The main function will then pass the array to a second function that gets the integers from the user and stores them in the array. Then your main will call a third function that displays the elements in the...

  • 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...

  • (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...

  • C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the...

    C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the user for ten (10) grades, and store the data in an array.  Compute the average of all the grades.  Print the original ten grades and the average. a.       Declare an integer array with the name of “grades” of size 10 in the main function. b.      Create a function called “getGrades” that prompts the User for the grades and puts them in an integer array.                                                                i.      The function will receive...

  • Program must be wriiten in c++ Write a function, removeAt, that takes three parameters: an array...

    Program must be wriiten in c++ Write a function, removeAt, that takes three parameters: an array of integers, the number of elements in the array, and an integer (say, index). The function should delete the array element indicated by index. If index is out of range or the array is empty, output an appropriate message. (Note that after deleting the element, the number of elements in the array is reduced by 1.) Assume that the array is unsorted.

  • Please send this C++ code with ( // ) Comments 1. In main, you have the...

    Please send this C++ code with ( // ) Comments 1. In main, you have the following array declared: int arr[5] = {1, 5, 3, 2} a. What are the elements in the array? Create a function called displayArr that accepts the array and the size of the array, and displays every element in the array. b. Create a function called findAverage that accepts the array and the size of the array as arguments, and returns the average of all...

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
ADVERTISEMENT