subject

Sort a vector Write a program that gets a list of integers from input, and outputs the integers in ascending order (lowest to highest). The first integer indicates how many numbers are in the list. Assume that the list will always contain less than 20 integers. Ex: If the input is:5 10 4 39 12 2the output is:2 4 10 12 39For coding simplicity, follow every output value by a space, including the last one. Your program must define and call the following function. When the SortVector function is complete, the vector passed in as the parameter should be sorted. void SortVector(vector & myVec) Hint: There are many ways to sort a vector. You are welcome to look up and use any existing algorithm. Some believe the simplest to code is bubble sort: https://en. wikipedia. org/wiki/Bubble_sort. But you are welcome to try others: https://en. wikipedia. org/wiki/Sorting_algorithm. My code is:
#include
#include
using namespace std;
int main() {
int arr[20];
int count = 0, num, swap;
for(int i=0; i < 20; i++) {
arr[i] = 0;
}
for(int i=0; i<20; i++){
cin>>num;
if(num!=0){
arr[i] = num;
count++;
}
}
for(int i=0; i<20; i++) {
for(int j=i+1; j<20; j++) {
if(arr[i] != 0) {
if(arr[i]>arr[j]) {
swap = arr[i];
arr[i] = arr[j];
arr[j] = swap;
}
}
}
}
for(int i=0; i<20; i++) {
if(arr[i] != 0) {
cout< }
}
return 0;
}
I used the given input (5 10 4 39 12 2) and expected the given output (2 4 5 10 12 39) but I am instead getting: 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 4 5 10 12 39. I can't figure out why the 2 is repeating 15 times as it should only be there once.

ansver
Answers: 1

Another question on Computers and Technology

question
Computers and Technology, 21.06.2019 20:20
Wireless communications is likely to be viewed as an essential part of an enterprise network infrastructure when: select one: a. mobile communication is needed b. communication facilities must be installed at low initial cost c. communication must take place in a hostile or difficult terrain that makes wired communication difficult or impossible d. the same information must be broadcast to many locations
Answers: 1
question
Computers and Technology, 22.06.2019 17:00
Annie is creating a corporate report for a company’s annual meeting. in the report, she wants to add the signature of various department heads. which device can annie use to capture signatures to include in the report? a. printer b. monitor c. e-reader d. digitizing tablet
Answers: 1
question
Computers and Technology, 23.06.2019 14:00
How are stop motion special effects in animated films created
Answers: 1
question
Computers and Technology, 23.06.2019 16:30
Which of the following is not an enhancement to the standard wiki to make it more attractive for corporations? encryptionwork spacespermission toolspredictive text
Answers: 2
You know the right answer?
Sort a vector Write a program that gets a list of integers from input, and outputs the integers in a...
Questions
question
Mathematics, 10.08.2021 14:00
question
English, 10.08.2021 14:00
question
English, 10.08.2021 14:00
question
Arts, 10.08.2021 14:00
question
English, 10.08.2021 14:00
question
Mathematics, 10.08.2021 14:00
question
English, 10.08.2021 14:00
Questions on the website: 13722361