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, 22.06.2019 15:00
Who is the first president to use social media as part of his campaign strategy
Answers: 1
question
Computers and Technology, 22.06.2019 23:30
To check spelling errors in a document, the word application uses the to determine appropriate spelling. internet built-in dictionary user-defined words other text in the document
Answers: 1
question
Computers and Technology, 23.06.2019 06:30
How do you write an argumentative essay about the importance of free enterprise ?
Answers: 1
question
Computers and Technology, 24.06.2019 17:00
Carlos, an algebra teacher, is creating a series of powerpoint presentations to use during class lectures. after writing, formatting, and stylizing the first presentation, he would like to begin writing the next presentation. he plans to insert all-new content, but he wants to have the same formatting and style as in the first one. what would be the most efficient way for carlos to begin creating the new presentation? going under the file tab and opening the first presentation, deleting all content from each page, and adding new content going under the file tab and clicking on new in the left pane, then choosing new from existing going under the design tab and clicking on themes, then selecting the theme that was used for the first template going under the design tab and opening the template that was created for the first presentation
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, 28.09.2020 14:01
question
Mathematics, 28.09.2020 14:01
question
Mathematics, 28.09.2020 14:01