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)
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
Comments
Leave a comment