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
likoan [24]
3 years ago
15

Complete the given C program (prob1.cpp) to read an array of integers, print the array, and find the 2nd largest element in the

array. A main function in prob1.cpp is given to read in the array. You are to write two functions, printArray() and getElement(), which are called from the main function. printArray() outputs integers with a space in between numbers and a newline at the end. getElement() returns an integer that is the second largest element in the array. You may assume that the integers are unique.
Computers and Technology
1 answer:
labwork [276]3 years ago
5 0

Answer:

#include <stdio.h>

int getElement(int arr[], int size) {

   

   int largest2, largest = 0;

   for(int i=0; i<size; i++)

   {

       if(arr[i] > arr[largest])

       {

           largest = i;

       }

   }

   if(largest != 0)

       largest2 = 0;

   else

       largest2 = size - 1;

   for(int i=0; i<size && i != largest ;i++)

   {

       if(arr[i] > arr[largest2])

           largest2 = i;

   }

   

   return arr[largest2];

}

printArray(int arr[], int size) {

   

   printf("The array is: ");

   for(int i=0; i<size; i++)

   {

       printf("%d ", arr[i]);

   }

   printf("\n");

}

int main()

{

 

   int arr[5] = {30, 20, 5, 10, 24};

   printArray(arr, 5);

   printf("Second largest number is: %d", getElement(arr, 5));

   return 0;

}

Explanation:

Since you did not provide any code, I wrote it from the scratch. I also added the main function for you to check the result.

<em>printArray</em> function basically prints the element of the array

<em>getElement</em> function:

- Declare the two variables to hold the value for largest and second largest

- Inside the first loop, check if there is any number that is greater than largest, if yes, assign it to largest

- Inside the second for loop, check if there is any number that is greater than largest2, if yes, assign it to largest2 (Be aware that the largest we found in the first loop is excluded)

- Return the second largest

* Notice that we were able to apply this solution since the numbers are unique.

You might be interested in
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binar
mylen [45]

Answer:

The program to this question can be described as follows:

Program:

num= int(input('Enter a number: ')) #input value by user

st = '' #defining string variable

while num > 0: #define loop to calculte values binary number

   val= num % 2 # holding remainder (0 or 1) in val variable

   st =st+str(val) # store value in str variable

   num = num//2 #calculte quotient value

print("binary number is: ",st)

Output:

Enter a number: 5

binary number is:  101

Explanation:

Program description as follows:

Firstly the "num" variable is declared, for user input, then an "st" variable is declared, to calculates user input value binary number.

In the next step, a while loop is declared, inside the loop, another variable "Val" is declared, that holds value remainders, which is added on the "st" variable.

Outside the loop, the print method is used, that prints st variable holds value.

3 0
4 years ago
Características que debe tener un módulo o kit tecnológico para aprender​
12345 [234]

Answer:

Multi herramientas

Explanation:

Un kit tecnologico debe contener todas las herramientas necesarias para el trabajo que lo necesites, por ejemplo destornillador magnetico con varias puntas, herramientas para desmontar y por supuesto tener buena claridad para trabajar y alcohol isoprofilico de al menos 90+ para limpiar electronicos.

5 0
3 years ago
Multiple boolean expressions can be combined by using a logical operator to create ________ expressions.
Licemer1 [7]
<span>Boolean expressions can be combined by using a logical operator to create compound expressions. A boolean expression is an expression that takes into account and evaluates specific data, which is in the form of true and/or false. A compound expression is merely a statement of expressions.</span>
8 0
3 years ago
22 POINTS IF YOU GET THIS RIGHTTTT<br><br> KACHOW!! Is a catchphrase used by?
dexar [7]
Lightning McQueen in the Disney movie, Cars

KACHOW
5 0
3 years ago
Read 2 more answers
What is transformer and its function?
Anna007 [38]

Answer:

A transformer is an electrical device which, by the principles of electromagnetic induction, transfers electrical energy from one electric circuit to another, without changing the frequency. The energy transfer usually takes place with a change of voltage and current.

The major function of a transformer is to step up or step down the voltage in an Alternate Current (AC) circuit.

4 0
3 years ago
Other questions:
  • An important advantage of using GUI data-entry objects is that you often can control what users enter by limiting their options?
    6·1 answer
  • Which of the following statements most accurately describes the difference between aptitudes and skills?
    8·1 answer
  • Discuss two advantages and two disadvantages of agile methods.
    6·1 answer
  • what type of machine is a ramp for wheelchairs? compound machine, mechanical, machine complex machine, simple machine
    15·2 answers
  • Diverting an attacker from accessing critical systems, collecting information about the attacker's activity and encouraging the
    8·1 answer
  • Crees que es necesario que el estado Peruano, proponga nuevamente el aislamiento desde este 22 de diciembre al 02 de enero del 2
    6·1 answer
  • The range of a finite nonempty set of $n$ real numbers $S$ is defined as the difference between the largest and smallest element
    15·1 answer
  • Can someone tell me how to get rid of the the orange with blue and orange on the status bar
    14·2 answers
  • ¿Qué aspectos de impacto medioambiental e impacto social tienen relación con la poca disminución de los residuos? Es para hoy.
    13·1 answer
  • Which shortcut keys can be used to duplicate a slide?
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!