samedi 17 novembre 2018

How do I put any value X after all the minimums in an array?

If I enter an array , at first the code finds the minimums then I want to put zeroes after all the minimums . For example given an array = 1,1,3,1,1 As we see 1s are the minimum so the result should be = 1,0,1,0,3,1,0,1,0

CODE

#include <pch.h>
#include <iostream>


int main()
{
int min = 10000;
bool found = 0;
int n;                                       
std::cout << "Enter the number of elements (n): ";
std::cin >> n;

int *array = new int[2 * n];

std::cout << "Enter the elements" << std::endl;



for (int i = 0; i < n; i++) {                      
    std::cin >> array[i];
    if (array[i] > min)
        min = array[i];
}

for (int i = 0; i < n; i++) {                

    if (array[i] == min) {                   // Not very clear about this
        for (int k = n; k > i; k--)          // part of the code, my teacher
             array[k] = array[k - 1];        //explained it to me , but i 
             array[i + 1] = 0;               // didn't understand       
             i--;                            
             n--;
    }
    std::cout << array[i] << ", 0";
}



return 0;
}

But my answer doen't put zeroes exactly after minimums

Aucun commentaire:

Enregistrer un commentaire