Question

C++ question I'm trying to write a code that takes in n number of variables between...

C++ question

I'm trying to write a code that takes in n number of variables between 00 and 99. The program should be able to output the 1st and 2nd lowest digits as well as the highest and second highest digits. So if the input is 4 - 50 20 10 70 it should output 10 20 50 70. I think I've gotten the basic code but I can't seem to get the last digit to shift.

#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
int n, m1, m2, m3, m4;
int temp;
int newval;

cin >> n;

for (int i = 1; i <= n; i++)
    {
      cin >> newval;
    
      if(newval <= 50)
      {
      m4 = newval;
      if(newval < m1)
         m2 = m1;
         m1 = newval;
      }
       
      if(newval > 50)
      {
      m4 = newval;
      if(newval > m4)
         m4 = m3;
         m3 = newval;
      }
   }
      cout << m1 << endl;
      cout << m2 << endl;
      cout << m3 << endl;
      cout << m4 << endl;

return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

#include <iostream>
#include <limits> // for numeric_limits
#include <cstdlib> // for exit()

using namespace std;

int main ()
{
// number of variables
int n;

cout << "Enter number of variables(min 2): ";
cin >> n;
// exit if size < 2
if(n < 2) {
exit(1);
}

// first store all the values in an array
int *array = new int[n];

for (int i = 0; i < n; i++)
{
// input array elements
cin >> array[i];
}
// define the four elements
int lowest = numeric_limits<int>::max();
int secondLowest = numeric_limits<int>::max();
int highest = numeric_limits<int>::min();
int secondHighest = numeric_limits<int>::min();

for (int i = 0; i < n; i++) {
// check if array[i] is smaller than the current lowest elements
if(array[i] < lowest) {
// then the lowest becomes the second lowest
secondLowest = lowest;
// assign lowest
lowest = array[i];
}
// check if array[i] is between current lowest and current secondLowest
// update secondLowest
else if(array[i] < secondLowest && array[i] != lowest) {
secondLowest = array[i];
}

if(array[i] > highest) {
secondHighest = highest;
highest = array[i];
}
else if(array[i] > secondHighest && array[i] != highest) {
secondHighest = array[i];
}
}
cout << "Lowest: " << lowest << endl;
// if all elements are not the same
if(secondLowest != numeric_limits<int>::max())
cout << "Second Lowest: " << secondLowest << endl;
// if all elements are not the same
if(secondHighest != numeric_limits<int>::min())
cout << "Second Highest: " << secondHighest << endl;
cout << "Highest: " << highest << endl;

delete array;
return 0;
}

OUTPUT:

Screens:

Add a comment
Know the answer?
Add Answer to:
C++ question I'm trying to write a code that takes in n number of variables between...
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
  • C++ Language I have a class named movie which allows a movie object to take the...

    C++ Language I have a class named movie which allows a movie object to take the name, movie and rating of a movie that the user inputs. Everything works fine but when the user enters a space in the movie's name, the next function that is called loops infinetly. I can't find the source of the problem. Down below are my .cpp and .h file for the program. #include <iostream> #include "movie.h" using namespace std; movie::movie() { movieName = "Not...

  • C++ Create a program that finds the dot product of two vectors. I'm currently trying to...

    C++ Create a program that finds the dot product of two vectors. I'm currently trying to display the dot product by calling the dotProduct member function however I am confused as to how to do this. What would be the proper way to display the dot product? I don't believe my dotProduct member function is set up correctly to access the proper data. Feel free to modify the dotProduct member function to allow for it to work with the other...

  • C++ problem where should I do overflow part? in this code do not write a new...

    C++ problem where should I do overflow part? in this code do not write a new code for me please /////////////////// // this program read two number from the user // and display the sum of the number #include <iostream> #include <string> using namespace std; const int MAX_DIGITS = 10; //10 digits void input_number(char num[MAX_DIGITS]); void output_number(char num[MAX_DIGITS]); void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base); int main() { // declare the array = {'0'} char num1[MAX_DIGITS] ={'0'}; char...

  • Modify this code to display the message "That's hot!" if the temperature entered is greater than...

    Modify this code to display the message "That's hot!" if the temperature entered is greater than 90 degrees Farenheit or equivalent Celsius, or the message "That's cold!" if the temperature entered is less than -10 degrees Farenheit or equivalent Celsius. And to show one in between where it doesn't show any message. #include <iostream> #include <iomanip> using namespace std; // a temperature conversion program int main() { char tempType; double temp, fahren, celsius; cout << "Enter the temperature to be...

  • I am trying to write this code which asks "Write a program that ask the user,...

    I am trying to write this code which asks "Write a program that ask the user, the question: What is a^b? //The program generates two signed integers and gives the user four attempts to get the correct answer //a=- , b = + //a= + , b = - " So far this what I wrote. I am not sure how to do this correctly. #include<iostream> #include<cstdlib> #include<ctime> using namespace std; int main() {    srand(time(0));    int guess,a,ans,b, k;...

  • Write a c++ code into the given code  to find composite numbers from the given random number...

    Write a c++ code into the given code  to find composite numbers from the given random number list. The composite numbers is only counted once if there is a repeated number. I need to use this code and add on a code to find if the numbers generated is a composite function. Please help #include <cmath> #include <cstdlib> #include <ctime> #include <iostream> #include <vector> using namespace std; int main() {    srand(time(NULL)); int size_of_list = 0; // the number of random...

  • what is the output for the following code? explain the steps. /*#include <iostream> using namespace std;...

    what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...

  • The C++ code below will compile but has a variety of runtime issues. Identify a runtime...

    The C++ code below will compile but has a variety of runtime issues. Identify a runtime issue with the program and describe how you might fix the problem. #include <iostream> #include <vector> using namespace std; //------------------------------------------------------------------------------ int main() { vector<double> temps; // temperatures double temp = 0; double sum = 0; double high_temp = 0; double low_temp = 0; while (cin >> temp) // read and put into temps temps.push_back(temp); for (int i = 0; i < temps.size(); ++i) {...

  • Can anyone help me with my C++ assignment on structs, arrays and bubblesort? I can't seem...

    Can anyone help me with my C++ assignment on structs, arrays and bubblesort? I can't seem to get my code to work. The output should have the AVEPPG from highest to lowest (sorted by bubbesort). The output of my code is messed up. Please help me, thanks. Here's the input.txt: Mary 15 10.5 Joseph 32 6.2 Jack 72 8.1 Vince 83 4.2 Elizabeth 41 7.5 The output should be: NAME             UNIFORM#    AVEPPG Mary                   15     10.50 Jack                   72      8.10 Elizabeth              41      7.50 Joseph                 32      6.20 Vince                  83      4.20 ​ My Code: #include <iostream>...

  • I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include...

    I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include <string> #include <iomanip> using namespace std; struct Drink {    string name;    double cost;    int noOfDrinks; }; void displayMenu(Drink drinks[], int n); int main() {    const int size = 5;       Drink drinks[size] = { {"Cola", 0.65, 2},    {"Root Beer", 0.70, 1},    {"Grape Soda", 0.75, 5},    {"Lemon-Lime", 0.85, 20},    {"Water", 0.90, 20} };    cout <<...

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