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

  • 5.35 LAB: Sort a vector Write a program that gets a list of integers from input,...

    5.35 LAB: 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 2 the output is: 2 4 10 12 39 For coding simplicity, follow every output value by a space, including the last...

  • 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);...

  • Problem 1. Write a Python function times_i_at_odd(L) that takes as arguments a list L and returns...

    Problem 1. Write a Python function times_i_at_odd(L) that takes as arguments a list L and returns a list consisting of the elements of L multiplied by the index number of the element at odd positions. (Use list comprehensions) >>> times_i_at_odd([1,2,3,4,5,6,7,8,9,10]) [2, 12, 30, 56, 90] Problem 2. Write a recursive function sum_cols(grid, n) that takes a list of lists of integers grid and integer n and returns the sum of column n in grid. For example, the call sum_cols([[1,2,3,4], [10,20,30,40],...

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