1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
professor190 [17]
3 years ago
5

Write a function that takes an array of integers and its size as parameters and prints the two largest values in the array. In t

his question, the largest value and the second largest value cannot be the same, even if the largest value occurs multiple times.
Computers and Technology
1 answer:
ivolga24 [154]3 years ago
3 0

Answer:

Following are the program in c++ language  

#include <iostream> // header file

using namespace std; // namespace

void largest(int a[], int size); // function declaration

int main() // main method

{

int arr[30],n,count1=0; // variable declaration

cout<<" enter the size elements in array:";

cin>>n; // read the input by user

cout<<"enter array elements:";

for(int k=0;k<n;k++) // iterating over the loop

cin>>arr[k]; // read the elements in the array

largest(arr,n); // calling function largest

return 0;

}

void largest(int a[], int n1) // function definition

{

int i, maximum, second,count=0; // variable declaration

if(n1<2) // checkiing the condition

{

cout<<"invalid input"; // print the message

return;

}

else

{

maximum =INT8_MIN; // store the minimum value of an object

second=INT8_MIN; //store the minimum value of an object

for(i = 0;i<n1; i ++) // iterating over the loop

{

if (a[i]>maximum) // comparing the condition

{

second = maximum; // store the maximum value in the second variable

maximum = a[i];

count++; // increment the count

}

else if (a[i] > second && a[i] != maximum)

{

second = a[i];

count++;

}

}

}

if(count<2)

{

cout<<"Maximum value:"<<maximum; // display the maximum value

cout<<" all the value are equal";

}

else

{

cout<<"Maximum value:"<<maximum; // display the maximum value

cout<<"Second largest:"<<second;// display the  second maximum value

}

}

Output:

enter the size elements in array:4

enter array elements:5

47

58

8

Maximum value:58 Second largest:47

Explanation:

Following are the description of program

  • In this program, we create a function largest which calculated the largest and the second largest element in the array.
  • Firstly check the size of elements in the if block. If the size is less then 2 then an invalid messages will be printed on console otherwise the control moves to the else block.
  • In the else block it stores the minimum value of an object in a maximum and second variable by using INT8_MIN function. After that iterating the loop and find the largest and second-largest element in the array .
  • In this loop, we used the if-else block and find the largest and second-largest element.
  • Finally, print the largest and second-largest elements in the array
You might be interested in
Who is the father of computer​
r-ruslan [8.4K]

charles babbage is the father of computer's

3 0
3 years ago
Read 2 more answers
What is the advantages and disadvantages of hardware devices and software devices ?
yulyashka [42]

Answer:

Advantages of hardware:

Physical existence

Multitasking

Speedy

Disadvantages:

Costly as different equipment cost differently

Time consuming

requires space

Privacy issues

Explanation:

Advantages of Software:

Less costly

Improved privacy controls

Requires no space

Disadvantages:

Does not have physical existence

hacking and cyber attacks may steal important information

Regular updates required

3 0
2 years ago
The U.S. consumes lots of energy. Which fuel provides the most energy?
Nadya [2.5K]

Answer:

<em>Natural</em><em> </em><em>Gas</em>

Explanation:

..............

7 0
2 years ago
Read 2 more answers
Java IntelliJ
andrew11 [14]

Answer:

Your solution is ready.

Explanation:

Visit: gotit-pro.com/design-a-gui-interface-to-accept-user-input-and-add-the-data-to-a-cell-phone

And get the complete working code for this assignment.

Feel free to reach out to me for fastest, top-notch and impeccable homework and exams help including Pearson Labs etc.

Thanks and Best Regards: Your Friendly Study Co-Pilot

3 0
3 years ago
HELP ME PLEASE
MAXImum [283]

Explanation:

Threats

Computer Security Threats are possible dangers that can affect the smooth functioning of your PC. These may be a small piece of adware or a harmful Trojan malware. In the present age, computer security threats are constantly increasing as the world is going digital. computer security threats

Types of Computer Security Threats

There are several types of computer security threats such as Trojans, Virus, Adware, Malware, Rootkit, hackers and much more. Check some of the most harmful types of computer Security Threats.

COMPUTER VIRUS

A Computer Virus is a malicious program, which replicates itself and infects the files and programs of your PC and can make them non-functional.

COMPUTER WORMS

A self-replicating computer program that spreads malicious codes, computer worms make use of the network to send copies of the original codes to other PCS. It can also go to the extent of sending transferring documents utilizing the email of the user.

SCAREWARE

Scareware is a malware that tricks victims to buy software by displaying fake virus alerts. A scareware infected PC may get pop-ups of fake malware threats and to get rid of those, users are prompted to purchase a fake anti-malware software.

KEYLOGGER

Also known as a keystroke logger, Keyloggers can track the real-time activity of a user on his computer. Keylogger runs in the background and records all keystrokes made by a user and passes the information to the hacker with the motive to steal password and banking details.

ROOTKIT

A rootkit is considered extremely dangerous as they appear to be legitimate files and deceives the computer user. Rootkit masks viruses and worms and makes them appear as necessary files. These are very difficult to remove and only an antivirus with the anti-rootkit feature can remove a rootkit.

Tips for Best Computer Security

For best computer security, you must follow certain guidelines, which are also called computer best practices. 1. Use the best antivirus software, which not only provides protection to your PC but also internet protection and guards against cyber threats. 2. Do not download untrusted email attachments and these may carry harmful malware. 3. Never download software from unreliable sites as they may come with a virus that may infect your system as soon as you install the software.

4 0
2 years ago
Other questions:
  • The term composite would be used to describe an image that was altered by the Crop tool.
    15·1 answer
  • convert the following c code to mips. assume the address of base array is associated with $s0, n is associated with $s1, positio
    14·1 answer
  • A user can easily moved to the end of document by pressing what key combination?
    8·2 answers
  • When creating a scene in Blender, you should change Blender Render to ______ to create the best lighting option?
    15·1 answer
  • What is a gutter margin?
    5·2 answers
  • Peter has recently bought a media player and a digital camera he wants to buy a memory card and then use devices which memory do
    11·2 answers
  • For C++ ONLY please,
    14·1 answer
  • Qr code is more developed than barcode​
    11·1 answer
  • Explain the concept of conditional formatting​
    9·1 answer
  • What is algorithm and how does it works​
    11·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!