Click here to Skip to main content
15,901,205 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questionneed help with unix commands Pin
Member 139744595-Sep-18 13:24
Member 139744595-Sep-18 13:24 
AnswerRe: need help with unix commands Pin
Peter_in_27805-Sep-18 19:49
professionalPeter_in_27805-Sep-18 19:49 
AnswerRe: need help with unix commands Pin
Richard MacCutchan5-Sep-18 21:59
mveRichard MacCutchan5-Sep-18 21:59 
QuestionRe: need help with unix commands Pin
David Crow6-Sep-18 5:45
David Crow6-Sep-18 5:45 
AnswerRe: need help with unix commands Pin
jeron16-Sep-18 6:31
jeron16-Sep-18 6:31 
AnswerRe: need help with unix commands Pin
Richard MacCutchan6-Sep-18 7:04
mveRichard MacCutchan6-Sep-18 7:04 
QuestionUnable to receive custom Ethernet frame Pin
Donnie_Song1-Sep-18 16:45
Donnie_Song1-Sep-18 16:45 
SuggestionRe: Unable to receive custom Ethernet frame Pin
Richard MacCutchan1-Sep-18 21:58
mveRichard MacCutchan1-Sep-18 21:58 
GeneralRe: Unable to receive custom Ethernet frame Pin
Donnie_Song1-Sep-18 22:04
Donnie_Song1-Sep-18 22:04 
GeneralRe: Unable to receive custom Ethernet frame Pin
Richard MacCutchan1-Sep-18 22:43
mveRichard MacCutchan1-Sep-18 22:43 
GeneralRe: Unable to receive custom Ethernet frame Pin
Donnie_Song1-Sep-18 23:06
Donnie_Song1-Sep-18 23:06 
GeneralRe: Unable to receive custom Ethernet frame Pin
Victor Nijegorodov1-Sep-18 23:19
Victor Nijegorodov1-Sep-18 23:19 
GeneralRe: Unable to receive custom Ethernet frame Pin
Richard MacCutchan1-Sep-18 23:25
mveRichard MacCutchan1-Sep-18 23:25 
GeneralRe: Unable to receive custom Ethernet frame Pin
Jochen Arndt2-Sep-18 0:31
professionalJochen Arndt2-Sep-18 0:31 
GeneralRe: Unable to receive custom Ethernet frame Pin
Donnie_Song2-Sep-18 0:59
Donnie_Song2-Sep-18 0:59 
GeneralRe: Unable to receive custom Ethernet frame Pin
Donnie_Song2-Sep-18 3:32
Donnie_Song2-Sep-18 3:32 
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 

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.