Click here to Skip to main content
16,019,427 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Unable to receive custom Ethernet frame Pin
Jochen Arndt2-Sep-18 7:58
professionalJochen Arndt2-Sep-18 7:58 
QuestionInvalid operands Pin
meerokh29-Aug-18 4:30
meerokh29-Aug-18 4:30 
AnswerRe: Invalid operands Pin
Victor Nijegorodov29-Aug-18 4:39
Victor Nijegorodov29-Aug-18 4:39 
GeneralRe: Invalid operands Pin
meerokh29-Aug-18 4:49
meerokh29-Aug-18 4:49 
QuestionRe: Invalid operands Pin
David Crow29-Aug-18 9:34
David Crow29-Aug-18 9:34 
AnswerRe: Invalid operands Pin
Richard MacCutchan29-Aug-18 5:51
mveRichard MacCutchan29-Aug-18 5:51 
AnswerRe: Invalid operands Pin
CPallini29-Aug-18 20:53
mveCPallini29-Aug-18 20: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 18:33
Tarun Jha28-Aug-18 18: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 21:52
mveCPallini28-Aug-18 21:52 
QuestionHow I can make the GUI like as photoshop (dark theme) by using Visual Studio C++ ? Pin
sasia22521-Aug-18 17:38
sasia22521-Aug-18 17:38 
AnswerRe: How I can make the GUI like as photoshop (dark theme) by using Visual Studio C++ ? Pin
Richard MacCutchan21-Aug-18 21:19
mveRichard MacCutchan21-Aug-18 21:19 
GeneralRe: How I can make the GUI like as photoshop (dark theme) by using Visual Studio C++ ? Pin
sasia22521-Aug-18 21:28
sasia22521-Aug-18 21:28 
GeneralRe: How I can make the GUI like as photoshop (dark theme) by using Visual Studio C++ ? Pin
Richard MacCutchan21-Aug-18 22:38
mveRichard MacCutchan21-Aug-18 22:38 
GeneralRe: How I can make the GUI like as photoshop (dark theme) by using Visual Studio C++ ? Pin
sasia22521-Aug-18 22:44
sasia22521-Aug-18 22:44 
AnswerRe: How I can make the GUI like as photoshop (dark theme) by using Visual Studio C++ ? Pin
Jochen Arndt21-Aug-18 22:40
professionalJochen Arndt21-Aug-18 22:40 
GeneralRe: How I can make the GUI like as photoshop (dark theme) by using Visual Studio C++ ? Pin
sasia22521-Aug-18 22:43
sasia22521-Aug-18 22:43 
AnswerRe: How I can make the GUI like as photoshop (dark theme) by using Visual Studio C++ ? Pin
Michael Haephrati28-Aug-18 4:27
professionalMichael Haephrati28-Aug-18 4:27 
QuestionUsing Alternate Memory Heaps Pin
Richard Andrew x6419-Aug-18 10:04
professionalRichard Andrew x6419-Aug-18 10:04 
AnswerRe: Using Alternate Memory Heaps Pin
Richard MacCutchan19-Aug-18 21:24
mveRichard MacCutchan19-Aug-18 21:24 
AnswerRe: Using Alternate Memory Heaps Pin
Daniel Pfeffer21-Aug-18 5:46
professionalDaniel Pfeffer21-Aug-18 5:46 
GeneralRe: Using Alternate Memory Heaps Pin
Richard Andrew x6421-Aug-18 12:08
professionalRichard Andrew x6421-Aug-18 12:08 
GeneralRe: Using Alternate Memory Heaps Pin
Daniel Pfeffer21-Aug-18 20:21
professionalDaniel Pfeffer21-Aug-18 20:21 
GeneralRe: Using Alternate Memory Heaps Pin
Richard Andrew x6422-Aug-18 0:39
professionalRichard Andrew x6422-Aug-18 0:39 
QuestionIterating over an indefinitely large number of concentric loops Pin
Anthony Appleyard16-Aug-18 1:54
Anthony Appleyard16-Aug-18 1:54 
AnswerRe: Iterating over an indefinitely large number of concentric loops Pin
Richard MacCutchan16-Aug-18 23:17
mveRichard MacCutchan16-Aug-18 23: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.