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 06:00
Write a function that draws a pool ball. this function should take as parameters, the color, the number that should go on the pool ball, and the location of the center of the pool ball. the radius of the pool balls should be pool_ball_radius, and the font of the number should be pool_ball_font. the text of the pool ball font should be white. drawpoolball(color.orange, 5, 100, 100); drawpoolball(color.green, 6, 50, 200); drawpoolball(color.red, 3, 150, 350); drawpoolball(color.blue, 2, 250, 140); to center the numbers on the pool ball, you should use the getwidth() and getheight() methods. you are allowed to call these methods on your text object, such as txt.
Answers: 3
question
Computers and Technology, 22.06.2019 22:00
Discuss the ways in which electronic information associated with payments is addressed in terms of security. include encryption, secure sockets layers, and secure electronic transactions in your discussion. are there any other ways that consumers and businesses can keep their payment information secure in an electronic commerce environment? do you feel that your information is safe when conducting electronic business? why or why not?
Answers: 1
question
Computers and Technology, 24.06.2019 08:30
Intellectual property rights are exclusive rights that protect both the created and the creation. ipr offers exclusively what benefits to the person or people covered by it
Answers: 3
question
Computers and Technology, 24.06.2019 10:30
You're programming an infinite loop. what must you include in your code to prevent crashes? in roblox
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
Questions on the website: 13722363