C++ Program to Replace Array Elements by Given Value

Write C++ Program to Replace Array Elements by Given Value

// CPP Program to Replace Array Elements by Given Value

#include <iostream>

using namespace std;

int main()
{
	int A[] = {1, 6, 5, 8, 6}, n, k, i, j, flag=0;

	n = sizeof(A) / sizeof(A[0]);

	cout << "Array A :--> ";
	for(i=0; i<n; i++)
        cout << A[i] << " ";


	cout << "\nEnter the Element to replace :--> ";
	cin >> k;


	for(i = 0;i < n; i++)
	{
		if(A[i] == k)
		{
			int r;
			flag = 1;
			cout << "Enter Element Replaced with " << i << " :--> ";
			cin >> r;
			A[i] = r;
		}
	}
	if(flag == 0)
	{
		cout << "\nThe element not found in the array";
	}
	else
	{
	    cout << "\nArray After Replacement :--> ";
		for(j = 0; j < n; j++)
		{
            cout << A[j] << " ";
		}
	}
	return 0;
}

Output:

Array A :--> 1  6  5  8  6
Enter the Element to replace :--> 3

The element not found in the array

Array A :--> 1  6  5  8  6
Enter the Element to replace :--> 6
Enter Element Replaced with 1:--> 11
Enter Element Replaced with 4:--> 22

Array After Replacement :--> 1 11 5 8 22

≪ Previous | Next ≫