Search code examples
c++arraysc++11jagged-arrays

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;
    int n;                                       
    std::cout << "Enter the number of elements (n): "; //no of elements in the 
    std::cin >> n;                                     //array

    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 (from the      
             i++;                            // `for loop k` to be precise)
             n++;
        }
        std::cout << array[i] << ", 0";
    }
        
                                  
    
    return 0;
}

But my answer doen't put zeroes exactly after minimums


Solution

  • There are few issues in your code, first of all your min is wrong. I have fixed your code with comments on fixes I have made. Please take a look :

    #include "stdafx.h"
    #include <iostream>
    
    
    int main()
    {
        int min = 10000;
        bool found = 0;
        int n;
        std::cout << "Enter the number of elements (n): "; //no of elements in the 
        std::cin >> n;                                     //array
    
        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) //< instead of >
                min = array[i];
        }
    
        for (int i = 0; i < n; i++) {
    
            if (array[i] == min) 
            {                  
                for (int k = n; k > i; k--)
                {
                    array[k] = array[k - 1];
                }
                array[i + 1] = 0;               
                i++; //increment i here because you don't want to consider 0 that you have just added above.                           
                n++; //since total number of elements in the array has increased by one (because of 0 that we added), we need to increment n
            }
        }
    
        //print the array separately 
        for (int i = 0; i < n; i++)
        {
            std::cout << array[i];
            if (i != n - 1)
            {
                std::cout << ",";
            }
        }
    
        return 0;
    }