Click here to Skip to main content
13,765,990 members

C / C++ / MFC

 
GeneralMessage Closed Pin
5-Sep-18 20:43
memberfarman abdullah5-Sep-18 20:43 
QuestionInvalid operands Pin
meerokh29-Aug-18 5:30
membermeerokh29-Aug-18 5:30 
AnswerRe: Invalid operands Pin
Victor Nijegorodov29-Aug-18 5:39
memberVictor Nijegorodov29-Aug-18 5:39 
GeneralRe: Invalid operands Pin
meerokh29-Aug-18 5:49
membermeerokh29-Aug-18 5:49 
QuestionRe: Invalid operands Pin
David Crow29-Aug-18 10:34
memberDavid Crow29-Aug-18 10:34 
AnswerRe: Invalid operands Pin
Richard MacCutchan29-Aug-18 6:51
protectorRichard MacCutchan29-Aug-18 6:51 
AnswerRe: Invalid operands Pin
CPallini29-Aug-18 21:53
mvpCPallini29-Aug-18 21:53 
QuestionRearrange array in alternating positive & negative items with O(1) extra space, while keeping the order of the elements maintained. Pin
Tarun Jha28-Aug-18 19:33
memberTarun Jha28-Aug-18 19:33 
The idea is to process array from left to right. While processing, find the first out of place element in the remaining unprocessed array. An element is out of place if it is negative and at odd index, or it is positive and at even index. Once we find an out of place element, we find the first element after it with opposite sign. We right rotate the sub-array between these two elements (including these two).

/*  C++ program to rearrange positive and negative integers in alternate
    fashion while keeping the order of positive and negative numbers. */
#include <iostream>
#include <assert.h>
using namespace std;

void printArray(int arr[], int n);

// Utility function to right rotate all elements between [outofplace, cur]
void rightrotate(int arr[], int n, int outofplace, int cur)
{
    char tmp = arr[cur];
    for (int i = cur; i > outofplace; i--)
        arr[i] = arr[i-1];
    arr[outofplace] = tmp;
}

void rearrange(int arr[], int n)
{
    int outofplace = -1;

    for (int index = 0; index < n; index ++)
    {
        if (outofplace >= 0)
        {
            // find the item which must be moved into the out-of-place
            // entry if out-of-place entry is positive and current
            // entry is negative OR if out-of-place entry is negative
            // and current entry is negative then right rotate
            //
            // [...-3, -4, -5, 6...] -->   [...6, -3, -4, -5...]
            //      ^                          ^
            //      |                          |
            //     outofplace      -->      outofplace
            //
            if (((arr[index] >= 0) && (arr[outofplace] < 0))
                || ((arr[index] < 0) && (arr[outofplace] >= 0)))
            {
                rightrotate(arr, n, outofplace, index);
                printArray(arr, n);

                // the new out-of-place entry is now 2 steps ahead
                if (index - outofplace > 2)
                    outofplace = outofplace + 2;
                else
                    outofplace = -1;
            }
        }


        // if no entry has been flagged out-of-place
        if (outofplace == -1)
        {
            // check if current entry is out-of-place
            if (((arr[index] >= 0) && (!(index & 0x01)))      // what does (index & 0x01) means ??
                || ((arr[index] < 0) && (index & 0x01)))
            {
                outofplace = index;
            }
        }
    }
}

// A utility function to print an array 'arr[]' of size 'n'
void printArray(int arr[], int n)
{
    for (int i = 0; i < n; i++)
      cout << arr[i] << " ";
    cout << endl;
}

// Driver program to test abive function
int main()
{
    //int arr[n] = {-5, 3, 4, 5, -6, -2, 8, 9, -1, -4};
    //int arr[] = {-5, -3, -4, -5, -6, 2 , 8, 9, 1 , 4};
    //int arr[] = {5, 3, 4, 2, 1, -2 , -8, -9, -1 , -4};
    //int arr[] = {-5, 3, -4, -7, -1, -2 , -8, -9, 1 , -4};
    int arr[] = {-5, -2, 5, 2, 4, 7, 1, 8, 0, -8};
    int n = sizeof(arr)/sizeof(arr[0]);

    cout << "Given array is \n";
    printArray(arr, n);
    cout << "\n\n";

    rearrange(arr, n);

    cout << "\n\nRearranged array is \n";
    printArray(arr, n);

    return 0;
}


Quote:
// check if current entry is out-of-place
if (((arr[index] >= 0) && (!(index & 0x01))) // what does (index & 0x01) means ??
|| ((arr[index] < 0) && (index & 0x01)))
{
outofplace = index;
}

what does the above statement do ?
AnswerRe: Rearrange array in alternating positive & negative items with O(1) extra space, while keeping the order of the elements maintained. Pin
CPallini28-Aug-18 22:52
mvpCPallini28-Aug-18 22:52 
QuestionHow I can make the GUI like as photoshop (dark theme) by using Visual Studio C++ ? Pin
sasia22521-Aug-18 18:38
membersasia22521-Aug-18 18:38 
AnswerRe: How I can make the GUI like as photoshop (dark theme) by using Visual Studio C++ ? Pin
Richard MacCutchan21-Aug-18 22:19
protectorRichard MacCutchan21-Aug-18 22:19 
GeneralRe: How I can make the GUI like as photoshop (dark theme) by using Visual Studio C++ ? Pin
sasia22521-Aug-18 22:28
membersasia22521-Aug-18 22:28 
GeneralRe: How I can make the GUI like as photoshop (dark theme) by using Visual Studio C++ ? Pin
Richard MacCutchan21-Aug-18 23:38
protectorRichard MacCutchan21-Aug-18 23:38 
GeneralRe: How I can make the GUI like as photoshop (dark theme) by using Visual Studio C++ ? Pin
sasia22521-Aug-18 23:44
membersasia22521-Aug-18 23:44 
AnswerRe: How I can make the GUI like as photoshop (dark theme) by using Visual Studio C++ ? Pin
Jochen Arndt21-Aug-18 23:40
mvpJochen Arndt21-Aug-18 23:40 
GeneralRe: How I can make the GUI like as photoshop (dark theme) by using Visual Studio C++ ? Pin
sasia22521-Aug-18 23:43
membersasia22521-Aug-18 23:43 
AnswerRe: How I can make the GUI like as photoshop (dark theme) by using Visual Studio C++ ? Pin
Michael Haephrati28-Aug-18 5:27
mvpMichael Haephrati28-Aug-18 5:27 
QuestionUsing Alternate Memory Heaps Pin
Richard Andrew x6419-Aug-18 11:04
professionalRichard Andrew x6419-Aug-18 11:04 
AnswerRe: Using Alternate Memory Heaps Pin
Richard MacCutchan19-Aug-18 22:24
protectorRichard MacCutchan19-Aug-18 22:24 
AnswerRe: Using Alternate Memory Heaps Pin
Daniel Pfeffer21-Aug-18 6:46
professionalDaniel Pfeffer21-Aug-18 6:46 
GeneralRe: Using Alternate Memory Heaps Pin
Richard Andrew x6421-Aug-18 13:08
professionalRichard Andrew x6421-Aug-18 13:08 
GeneralRe: Using Alternate Memory Heaps Pin
Daniel Pfeffer21-Aug-18 21:21
professionalDaniel Pfeffer21-Aug-18 21:21 
GeneralRe: Using Alternate Memory Heaps Pin
Richard Andrew x6422-Aug-18 1:39
professionalRichard Andrew x6422-Aug-18 1:39 
QuestionIterating over an indefinitely large number of concentric loops Pin
Anthony Appleyard16-Aug-18 2:54
memberAnthony Appleyard16-Aug-18 2:54 
AnswerRe: Iterating over an indefinitely large number of concentric loops Pin
Richard MacCutchan17-Aug-18 0:17
protectorRichard MacCutchan17-Aug-18 0:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Cookies | Terms of Service
Web04-2016 | 2.8.181114.1 | Last Updated 15 Nov 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid