Computers and Technology, 05.05.2021 16:10 rousedem28381
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.
Answers: 1
Computers and Technology, 23.06.2019 12:00
What does the level 1 topic in a word outline become in powerpoint? a. first-level bullet item b. slide title c. third-level bullet item d. second-level bullet item
Answers: 1
Computers and Technology, 25.06.2019 02:00
Feedback is one of the basic elements games have in common true false
Answers: 2
Computers and Technology, 25.06.2019 05:00
How many meatballs required for a larty of 25 adults and 6 children
Answers: 1
Computers and Technology, 25.06.2019 10:30
How do you transfer an image onto a computer
Answers: 2
Sort a vector Write a program that gets a list of integers from input, and outputs the integers in a...
Health, 23.02.2021 21:10
Geography, 23.02.2021 21:10
Mathematics, 23.02.2021 21:10
Arts, 23.02.2021 21:10
Mathematics, 23.02.2021 21:10
Mathematics, 23.02.2021 21:10
Biology, 23.02.2021 21:10