Question

12.37 Chapter 5 : Put Smallest First Read a list of 10 integers into a vector...

12.37 Chapter 5 : Put Smallest First

  • Read a list of 10 integers into a vector
  • Swap the smallest value alphabetically into the first position
  • Print list

C++

Homework Answers

Answer #1
#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<int> v;
    int n;
    cout << "Enter 10 integers: ";
    for (int i = 0; i < 10; ++i) {
        cin >> n;
        v.push_back(n);
    }
    int minIndex = 0;
    for (int i = 0; i < v.size(); ++i) {
        if (v[i] < v[minIndex])
            minIndex = i;
    }
    int temp = v[0];
    v[0] = v[minIndex];
    v[minIndex] = temp;
    for (int i = 0; i < v.size(); ++i) {
        cout << v[i] << " ";
    }
    cout << endl;
    return 0;
}

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
In Coral,write a program that reads a list of 10 integers and outputs those Integers in...
In Coral,write a program that reads a list of 10 integers and outputs those Integers in Reverse. For coding Simplicity, follow each output integer by a space, including the last one. Then, output a new line. Example if the input is 2 4 6 8 10 12 14 16 18 20, the output is: 20 18 16 14 12 10 8 6 4 2 to achieve above first read the integers into an array. Then output the array in reverse
3. Create vector V of integers randomly distributed on [-10 20] .display the vector. a) Use...
3. Create vector V of integers randomly distributed on [-10 20] .display the vector. a) Use for-end loop to examine each element of the vector. If element is positive double its value ; if element is negative triple its value. Display the result. b) without using for loop find number of elements in V that are greater than 1 c) without using for loop find all of elements in V that are greater or equal to -7 and are less...
Chapter 18 Lab – Design and code your own LIFO Stack using single liked list to...
Chapter 18 Lab – Design and code your own LIFO Stack using single liked list to hold a list of integers. Use the C++ “struct” structure to create your SLL nodes. Node position numbering should start with one. Your program should include the main program to test and demonstrate one function for each Stack operations including: InitStack – Initialize LIFO Stack PushStack – Add an item to top of Stack at PrintStack – Print out the Stack items PopStack –...
USE ONLY C LANGUAGE The program should read inputs that look like this: 10 11 56...
USE ONLY C LANGUAGE The program should read inputs that look like this: 10 11 56 92 10 5 42 7 1 33 99 The program should start by reading one integer, which indicates how many numbers there are in the random sequence. The program should then read a sequence of random integers. If fewer numbers are provided than specified (by the first integer on the first line), the program should print the following error message (replace XXX with the...
C++ 19.37 LAB: Adjust list by normalizing - functions When analyzing data sets, such as data...
C++ 19.37 LAB: Adjust list by normalizing - functions When analyzing data sets, such as data for human heights or for human weights, a common step is to adjust the data. This can be done by normalizing to values between 0 and 1, or throwing away outliers. For this program, adjust the values by subtracting the smallest value from all the values. The input begins with an integer indicating the number of integers that follow. Assume that the list will...
1. Build a double linked list with 5 in the first node following the instructions: Insert...
1. Build a double linked list with 5 in the first node following the instructions: Insert a node with 3 at the top of the list Insert a node with 10 at the bottom of the list Insert a node with 7 between nodes with 5 and 10 2. Start deleting nodes from the list in the following order; Delete the node with 7 Delete the node with 3 Delete the node with 10 3. Print the resulting list forward...
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...
1. What will the following python code display? num = list(range(5)) print(num) 2. Answer the following...
1. What will the following python code display? num = list(range(5)) print(num) 2. Answer the following questions using the list below. You can record your answers in the essay textbox. data = [5,3,7] A. Write code to replace the value in the 0 position with the number 8. B. Add the value 10 to the end of the list. C. Insert the value 22 after position 1 in the list. D. Remove the value at position 2. E. Sort the...
c++ 19.36 LAB: Output values below an amount - functions Write a program that first gets...
c++ 19.36 LAB: Output values below an amount - functions Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Then, get the last value from the input, and output all integers less than or equal to that value. Ex: If the input is: 5 50 60 140 200 75 100 the output is: 50 60 75 The 5 indicates that there are five integers...
List 5 desirable characteristics of antimicrobial drugs (chapter 10). For example, we would want a drug...
List 5 desirable characteristics of antimicrobial drugs (chapter 10). For example, we would want a drug that is selectively toxic and affects the microbe, without harming the host. Other that selective toxicity, think of 5 other desirable characteristic and list them here for participation 9.