Question

C++

C. In the file c_final_practice.cpp: (1) Write a function void replace(vector<int>& v, int old, int new) that replaces all oc

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

#include<iostream>
#include <vector>
using namespace std;
// replace the old value with new
void replace(vector<int>& v,int old,int neww)
{
    // iterate the vector
    for(int i=0;i<v.size();i++)
    {
        if(v[i] == old)
            v[i] = neww; // replace the all occurences of old value with new value
    }
}

int main()
{
    //main program start here
    vector<int>v;
    // initialize three vector
    vector<int>v1;
    vector<int>v2;
    vector<int>v3;
    string input; // declare input as string
    cout<<"Enter a list of integers. Type Q to quit."<<endl;
    while(true)
    {
        cin>>input; //take input from user
        if(input == "Q") // if the input is Q then stop taking input
            break;
        v.push_back(atoi(input.c_str())); // Converting string to integer
    }
      
    // take the copy of v in v1,v2,v3
    v1 = v;
    v2 = v;
    v3 = v;
    replace(v1,0,2); // call replace function for replace 0 by 2
    for (int i = 0; i < v1.size(); i++) // display the list of integers after replace
        cout<<v1[i]<<" ";
    cout<<endl;

    replace(v2,0,1); // call replace function for replace 0 by 1
    for (int i = 0; i < v2.size(); i++) // display the list of integers after replace
        cout<<v2[i]<<" ";
    cout<<endl;

   replace(v3,1,0); // call replace function for replace 1 by 0
    for (int i = 0; i < v3.size(); i++) // display the list of integers after replace
        cout<<v3[i]<<" ";
    cout<<endl;

  
}

**********************OUTPUT*****************************
Enter a list of integers. Type Q to quit.
0 1 2 -1 0 -2 1 Q
2 1 2 -1 2 -2 1
1 1 2 -1 1 -2 1
0 0 2 -1 0 -2 0

Add a comment
Know the answer?
Add Answer to:
C++ C. In the file c_final_practice.cpp: (1) Write a function void replace(vector<int>& v, int old, int...
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
  • (3) Problem P6.10. Implement a function void remove.duplicates (vector<int> & v) discussed in Problem 6.10 that...

    (3) Problem P6.10. Implement a function void remove.duplicates (vector<int> & v) discussed in Problem 6.10 that removes duplicates from a vector. Write a program that prompts the user to enter a list of integers and then use the above function to remove duplicates. Then display the modified vector. Implement a loop in which the above actions are repeated until the user requests to quit. . Assume that the lists of integers is nonempty Assume that the the user's input is...

  • C Programming 23 points Write a function for the following specs: type: void parameters: FILE", int[],...

    C Programming 23 points Write a function for the following specs: type: void parameters: FILE", int[], int size • Behavior: Write the text value of each element from the int[] to the file File format: an integer on each line of the file. Le: 4 3 1 5

  • Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int...

    Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int *a2) Write a program addition.c that reads in an array (a1) of numbers, and creates a new array (a2) of numbers such that the first and last numbers of a1 are added and stored as the first number, the second and second-to-last numbers are added and stored as the second number, and so on. You need to check for even and odd length of...

  • in c++ Program 1 Write a program that sorts a vector of names alphabetically using the...

    in c++ Program 1 Write a program that sorts a vector of names alphabetically using the selection sort and then searches the vector for a specific name using binary search. To do that, you need to write three functions I. void selSort (vector string &v: sorts the vector using selection sort 2. void display (const vector <string & v): displays the vector contents . int binSearch (const vector <ing& v, string key): searches the vector for a key returns the...

  • Java Write the function void insertAtTail (int v). Don’t add any class variables to the List...

    Java Write the function void insertAtTail (int v). Don’t add any class variables to the List class. Here are the class definitions of Node and List that implement a linked list. class Node {private Node next; private int key; Node (Node nxt, int keyValue);//constructor Node getNext(); int getKey(); void putNext(Node nxt);} class List {//assume the class does not use a dummy Node private Node head; List ();//constructor boolean exists (int ky);//returns true if v is in the list void insertAtHead(int...

  • Example program #include <string> #include <iostream> #include <cmath> #include <vector> using namespace std; vector<int> factor(int n)...

    Example program #include <string> #include <iostream> #include <cmath> #include <vector> using namespace std; vector<int> factor(int n) {     vector <int> v1;     // Print the number of 2s that divide n     while (n%2 == 0)     {         printf("%d ", 2);         n = n/2;         v1.push_back(2);     }     // n must be odd at this point. So we can skip     // one element (Note i = i +2)     for (int i = 3; i <=...

  • COMPLETE THE BUCKETSORT METHOD public static void bucketSort(int[] array) {        int bucketCount = array.length/2;...

    COMPLETE THE BUCKETSORT METHOD public static void bucketSort(int[] array) {        int bucketCount = array.length/2;        int minIntValue = 0;        int maxIntValue = array.length - 1;        // Create bucket array        List<Integer>[] buckets = new List[bucketCount];        // Associate a list with each index in the bucket array           for(int i = 0; i < bucketCount; i++){            buckets[i] = new LinkedList<>();        }        //...

  • Write a program that replace repeated three characters in a string by the character followed by 3...

    Write a program that replace repeated three characters in a string by the character followed by 3. For example, the string aabccccaaabbbbcc would become aabc3ca3b3cc. When there are more than three repeated characters, the first three characters will be replaced by the character followed by 3. You can assume the string has only lowercase letters (a-z). Your program should include the following function: void replace(char *str, char *replaced); Your program should include the following function: void replace(char *str, char *replaced);...

  • In C, thanks. #include <stdio.h> void set-flag (unsigned int* flag-holder , int flag-position); void unset-flag (unsigned...

    In C, thanks. #include <stdio.h> void set-flag (unsigned int* flag-holder , int flag-position); void unset-flag (unsigned int * flag-holder, int flag-position); int check-flag (unsigned int flag-holder , int flag-position); void display -32_flags (unsigned int flag-holder); int main(int argc, char* argv (1) unsigned int flag -holder = 0; set-flag (& flag-holder, 3); set-flag (& flag-holder, 16); set-flag (& flag-holder, 31); display-32-flags (flag-holder); unset-flag(& flag-holder , 31); unset-flag (& flag-holder, 3); set-flag (& flag-holder , 9); display-32-flags (flag-holder ); return 0; Write...

  • *********C Language******* Write a file final_main.c 4. Inside main(void): Write a loop that oops until the...

    *********C Language******* Write a file final_main.c 4. Inside main(void): Write a loop that oops until the entered number is 0 or negative 5. Ask the user to enter a positive int between 0 and 10 inclusive 6. If the number is < 1, it terminates. 7. Else: call the above four functions 8. Print the result of each function after its call. Q2. Write a program that removes the punctuations letters from a file and display the output to the...

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