Answer to Question #157529 in C++ for Talha

Question #157529
Read an integer array of 10 positive elements, and a value to search, count how many times that value occurs in the array if it occurs exactly twice then replace its second occurrence with -1. Display the indexes where that value occurred and the modified array afterwards.
1
Expert's answer
2021-01-22T07:19:57-0500
#include <iostream>
using namespace std;
int main () {
    int array[10];
    cout << "enter elements of array: \n";
    for (int i = 0; i < 10; i++) {
        cout << "array[" << i << "] = ";
        cin >> array[i];
    }
    cout << "The array: ";
    for (int i = 0; i < 10; i++) {
        cout << array[i] << " ";
    }
    int occurance = 0, search, index;
    cout << "\nEnter searched element: "; cin >> search;
    for (int i = 0; i < 10; i++) {
        if (search == array[i]) {
            occurance++;
        }
    }
    if (occurance == 2) {
        for (int i = 9; i >= 0; i--) {
            if (array[i] == search) {
                array[i] = -1;
                index = i;
                break;
            }
        }
        cout << "The index of searched element in a second time is " << index << endl;
        cout << "modified array: ";
        for (int i = 0; i < 10; i++) {
            cout << array[i] << " ";
        }
    }
    else {
        cout << "The number of occurance of the " << search << " is " << occurance << endl;
    }
} 

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