Answer to Question #255626 in C++ for khangphanana

Question #255626

Write a function that replaces the elements of an integer sequence whose value a is the value of b. Write a main program that uses that function and displays the resulting sequence of numbers on the screen. (Example: for the integer sequence 1, 2, 5, 2, 6, 7, 8, 12. Replace the numbers with the value 2 with the number with the value of 9, then we get the result sequence 1, 9, 5, 9, 6, 7, 8, 12)



1
Expert's answer
2021-10-23T14:11:31-0400

Source code



#include <iostream>


using namespace std;


int sequence[]={1, 2, 5, 2, 6, 7, 8, 12};
int n=8;
void replace(int a, int b){
    for(int i=0;i<n;i++){
        if (sequence[i]==a){
            sequence[i]=b;
        }
    }
    cout<<"\nSequence after replacement: ";
    for(int i=0;i<n;i++){
        if(i==(n-1))
            cout<<sequence[i];
        else
            cout<<sequence[i]<<", ";
    }
}
int main()
{
    cout<<"\nSequence before replacement: ";
    for(int i=0;i<n;i++){
        if(i==(n-1))
            cout<<sequence[i];
        else
            cout<<sequence[i]<<", ";
    }
    cout<<endl;
    replace(2,9);


    return 0;
}


Output





Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS