Question

I have a bunch of positive integers that need to be sorted by the number of...

I have a bunch of positive integers that need to be sorted by the number of zeros in it. For instance, 0 has 1 zero in it. 3001 has 2. Write a function called "sort_by_zeros" that takes in a vector of ints and returns a vector of ints sorted by their zero count. When two ints have the same count, preserve the original order. Note, you can only write one named function definition (i.e. sort_by_zeros) for this problem and you are not allowed allowed to use any looping or conditional constructs (no while, do-while, goto, for, or if statements).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//C++ program

#include<iostream>
#include<vector>
using namespace std;

int count_zero(int num){
   int count=0;
   while(num){
       if(num%10==0)count++;
       num/=10;
   }
   return count;
  
}

bool Greater(int a , int b){
   return count_zero(a)>count_zero(b);
}

void sort_by_zeros(vector<int>&vec){
   int n = vec.size();
   for(int i=n-1;i>=0;i--){
       for(int j=0;j<i;j++){
           if(Greater(vec[j],vec[j+1])){
               int temp=vec[j];
               vec[j]=vec[j+1];
               vec[j+1]=temp;
           }
       }
   }
}

int main(){
   vector<int >vec;
   vec.push_back(123);
   vec.push_back(0);
   vec.push_back(100001);
   vec.push_back(3001);
   vec.push_back(2001);
  
   for(int i=0;i<vec.size();i++){
       cout<<vec[i]<<" ";
   }
   cout<<"\n\nSorted Vector : \n";
   sort_by_zeros(vec);
   for(int i=0;i<vec.size();i++){
       cout<<vec[i]<<" ";
   }

return 0;
}

//sample output

CUsers\IshuManish\Documents countByZero.exe 1230 100001 3001 2001 Sorted Vector: 123 0 3001 2001 100001 Process exited after

Add a comment
Know the answer?
Add Answer to:
I have a bunch of positive integers that need to be sorted by the number of...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python GPA calculator: Assume the user has a bunch of courses they took, and need to...

    Python GPA calculator: Assume the user has a bunch of courses they took, and need to calculate the overall GPA. We will need to get the grade and number of units for each course. It is not ideal to ask how many courses they took, as they have to manually count their courses. Programming is about automation and not manual work. We will create a textual menu that shows 3 choices. 1. Input class grade and number of units. 2....

  • Need the answers to these RECURSIVE practice problems using Linked Lists in JAVA . Thank you. Complete the Link cl...

    Need the answers to these RECURSIVE practice problems using Linked Lists in JAVA . Thank you. Complete the Link class by writing methods described below. Do not use loops, or create any more methods (other than those specified), class or instance variables. public class Link private Link next; /null if this is the last link private int value; public Link(Link n, int v) nextn valuev; Do not use loops, or create any more methods (other than those specified), class or...

  • GPA calculator: Assume the user has a bunch of courses they took, and need to calculate...

    GPA calculator: Assume the user has a bunch of courses they took, and need to calculate the overall GPA. We will need to get the grade and number of units for each course. It is not ideal to ask how many courses they took, as they have to manually count their courses. Programming is about automation and not manual work. We will create a textual menu that shows 3 choices. 1. Enter course grade and units. 2. Show GPA. 3....

  • (Count positive and negative numbers and compute the average of numbers) Write a program that reads...

    (Count positive and negative numbers and compute the average of numbers) Write a program that reads an unspecified number of integers, determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point number. If you entire input is 0, display 'No numbers are entered except 0.' *So I think my code is looping because the...

  • LANGUAGE = C i. Write a program that takes int numbers from user until user gives...

    LANGUAGE = C i. Write a program that takes int numbers from user until user gives a sentinel value (loop terminating condition). Sort the numbers in ascending order using Insertion sort method. Sorting part should be done in different function named insertionSort(). Your code should count the number of swaps required to sort the numbers. Print the sorted list and total number of swaps that was required to sort those numbers. (4 marks) ii. In this part take another number...

  • I have a dictionary in which has strings for keys and integers for values. I need...

    I have a dictionary in which has strings for keys and integers for values. I need to count the length of each key and place it in one of three categories, small, medium or large. Those are defined by: small: length 0-4, inclusive. medium: length 5-7, inclusive. large: length 8+, inclusive. After they are they have been sorted into their appropriate category, the program needs to check which word of each small, medium, large category had the largest key. Sample...

  • I need help writing this code in C++ Proj11.cpp is provided as well as the randomdata.txt thank you in advance! Objectives: The main objectives of this project is to introduce you to recursion,...

    I need help writing this code in C++ Proj11.cpp is provided as well as the randomdata.txt thank you in advance! Objectives: The main objectives of this project is to introduce you to recursion, and test your ability to work with some STL functionalities. A review of your knowledge on working with templates, dynamic data structures, as well as manipulating dynamic memory, classes, pointers and iostream to all extents, is also included. Description: For the entirety of this project, you will...

  • I only need help with the constructors and add method. Thanks! Background Arbitrary length integers are...

    I only need help with the constructors and add method. Thanks! Background Arbitrary length integers are integers that have no size restriction. Recall that the int type has a range of -2,147,483,648 to 2,147,483,647. The long type runs from -263 to 263 - 1. Sometimes these numbers are not large enough. One example application that may need larger numbers is public-key cryptography where very large integers are used to make decryption hard. A large integer may be represented as a...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT