Question

Write a function that takes an array of integers and its size as parameters and prints...

Write a function that takes an array of integers and its size as parameters and prints the two largest values in the array. In this question, the largest value and the second largest value cannot be the same, even if the largest value occurs multiple times. In the first sample, the two largest values are 9 and 8 (even though the value 9 appears twice). In the second sample, all the values are equal and the program recognizes this by printing a special message.

Homework Answers

Answer #1

CODE::::

#include <stdio.h>
int main()
{
int size = 10;
int array[size];
int firstMax , secondMax;
int i;
printf("Array data:\n");
for(i=0;i<size;i++) {
scanf("%d",&array[i]);
}
firstMax = array[0] , secondMax = array[1];

for(i=0;i<size;i++) {
if (array[i] > firstMax) {
if(firstMax!=array[i]) {
secondMax = firstMax;
}
firstMax = array[i];
} else if (array[i] > secondMax) {
if(firstMax!=array[i]) {
secondMax = array[i];
}
}
}
printf("Maximum value: %d\n", firstMax);
if(firstMax == secondMax) {
printf("All the values are equal\n");
} else {
printf("Second largest value: %d\n", secondMax);
}
return 0;
}

OUTPUT::::

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 c++ function which takes two parameters: an array of ints and an int size...
Write a c++ function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void printSome(const int array[], int size);
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.
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...
• First, create a function called addNumber, which has a formal parameter for an array of...
• First, create a function called addNumber, which has a formal parameter for an array of integers and increase the value of each array element by a random integer number between 1 to 10. o Add any other formal parameters that are needed. • Second, create another function called printReverse that prints this array in reverse order. • Then, you need to write a C++ program to test the use of these two functions.
(Write in C++) Write a program that reads in two numbers and, if the input is...
(Write in C++) Write a program that reads in two numbers and, if the input is valid, outputs 2 times the product of the integers that lie between the two values (including the values themselves). If either number is not an integer, or if the first number is not less than the second number, just output an error message. The sample runs below should give the idea. User inputs are in bold. Important Notes: Your program should use a loop...
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 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...
8) Write Python code for a function called occurances that takes two arguments str1 and str2,...
8) Write Python code for a function called occurances that takes two arguments str1 and str2, assumed to be strings, and returns a dictionary where the keys are the characters in str2. For each character key, the value associated with that key must be either the string ‘‘none’’, ‘‘once’’, or ‘‘more than once’’, depending on how many times that character occurs in str1. In other words, the function roughly keeps track of how many times each character in str1 occurs...
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...
Write a java program that creates an integer array with 50 random values, prompts the user...
Write a java program that creates an integer array with 50 random values, prompts the user to enter the index of an element in the array between 0 and 49, then displays the corresponding element value. If the specified index is out of bounds, display an error message (e.g. “Out of Bounds”) and ask the user to enter another index. Use a while loop that will keep prompting the user until a valid input is received. To handle invalid inputs,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT