Gonbe 32 Newbie Poster
#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>

using namespace std;

/* Just a helper function that prints the contents of the vector, separated by a space */
void print_vector(vector<int> data)
{
    for (auto elem : data)
        cout << elem << ' ';
}


/* Uses the <algorithm> function to generate permutations */
void print_permutations_cheesy(vector<int> data)
{
    /* Permutations are generated in lexicographical order, sort to start with the "smallest" value */
    sort(data.begin(), data.end());

    do /* For every permutation.. */
    {
        /* Print the vector */
        print_vector(data);

        /* End the line */
        cout << endl;
    } 
    /* Calculate the next permutation */
    while (next_permutation(data.begin(), data.end()));
}



/* The function that actually generates the permutations, and releases the "f" function on each one of them */
void permute(vector<int> data, const unsigned begin, const unsigned end, function<void(const vector<int>&)> f)
{
    /* If begin equals end, it means we only look at a single element; there is nothing left to permute */
    if (begin == end)
    {
        f(data);
    }
    else
    {
        /* Go past every element in the range [begin,end] */
        for (unsigned i = begin; i <= end; i++)
        {
            /* Swap the first element with the i-th element */
            swap(data[i], data[begin]);

            /* Recursively permute the rest of the range */
            permute(data, begin + 1, end, f);

            /* Restore the array to it's original (as in: how it was supplied to this function) state */
            swap(data[i], data[begin]);
        }
    }
}

/* Generate the permutations ourselves, this is just a wrapper function …
Gonbe 32 Newbie Poster

I understand the concern and I've had discussions about it before. I (still) think that people don't really learn anything if you have to force them to do it. Sure, I think it's stupid to just copy paste code from the internet and I'm sure that'll come back to bite them in the ass, but that's not my problem. I like doing these kind of small simple problems just as a distraction between working on more complex things.. But I'll try to meet somewhere in the middle :<

And can you code that in Lisp, Fortran, ADA, Go, Rust, Scheme, Python, Perl, and Brain####, for me?

Don't challenge me :P

Gonbe 32 Newbie Poster

what is the code to display "*" as pyramid?

#include <stdio.h>

static void print_n(const char symbol, unsigned int n)
{
    while (n --> 0)
        printf("%c", symbol);
}

void print_pyramid(const unsigned int height)
{
    int       i           = 0,
              width       = 0;
    const int WIDTH_TOTAL = (2 * height) - 1;

    for (i = 1; i <= height; i++)
    {
        width = (2 * i) - 1;

        print_n(' ' , (WIDTH_TOTAL - width) / 2);
        print_n('*' , width);
        print_n('\n', 1);
    }
}

int main(void)
{
    print_pyramid(10);
    return 0;
}
iamthwee commented: -1 for being an idiot -3
Gonbe 32 Newbie Poster

-edit- no idea what you wanted "re" to return so it returns "nothing"..

#include<iostream>

using namespace std;

int re(int a)
{
    cout << a << endl;

    if (a > 0)
    {
        re(a - 1);
    }
}

int main()
{
     int a;

     cout<<"enter number\n";
     cin>>a;

     re(a);

     return 0;
}
Gonbe 32 Newbie Poster

Managed to get rid of the compiler errors. Compiles without warnings too.

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
    string name;

    cout << "What is the person's name?";
    cin >> name;
    cout << "Interesting! Thanks for letting me know. Don't forget to visit our cinema!\n";

    return 0;
}

You're welcome.

Gonbe 32 Newbie Poster

A cheesy way would be to brute force, you'd end up with something like this:

#include <stdbool.h>
#include <stdio.h>
#include <math.h>

// The treshold mentioned in the exercise.
#define TRESHOLD (0.0001)

// Test number.
#define N (0.123456789)

int  max   (const int l, const int r);
bool solve (const double n, int* const a, int* const b, int* const c, int* const d);

int max(const int l, const int r)
{
    return (l > r) ? l : r;
}

bool solve (const double n, int* const a, int* const b, int* const c, int* const d)
{
    // Iterate over the possible 'a' values.
    for ((*a) = 24; (*a) <= 100; (*a)++)
    {
        // The check for 24 <= b <= 100 is implicit due to a's bounds.
        // Should make it explicit if you want the ranges as variables.
        for ((*b) = max(73 - (*a), 24); (*b) <= 121 - (*a); (*b)++)
        {
            // iterate over the possible 'c' values.
            for ((*c) = 24; (*c) <= 100; (*c)++)
            {
                // Same as for 'b' here.
                for ((*d) = max(90 - (*c), 24); (*d) <= 134 - (*c); (*d)++)
                {
                    // Found an answer. Should add a a treshold for accuracy due to floating points.
                    if (fabs(n - ((double)((*a) * (*c)) / ((*b) * (*d)))) <= TRESHOLD)
                    {
                        // Found the answer.
                        return true;
                    }
                }
            }
        }
    }

    return false;
}


int main()
{
    int a, b, c, d;

    // Will get rounded.
    printf("Trying to solve the equation …
deceptikon commented: Cheesy or not, giving away the answer is unhelpful -3
Gonbe 32 Newbie Poster

Something I made for a similar question in "C". Checks lines of text as opposed to words.

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#include <ctype.h>

bool IsPalindrome (const char *string, const int size)
{
    assert (size >= 0);

    // A word consisting of one letter or less is palindrome.
    if (size <= 1)
    {
        return true;
    }
    // non-letters may be skipped.
    else if (!isalpha(string[0]))
    {
        return IsPalindrome(string + 1, size - 1);
    }
    // the same goes for the end
    else if (!isalpha(string[size - 1]))
    {
        return IsPalindrome(string, size - 1);
    }
    // The first and final character are both letters. These must be equal.
    else
    {
        return (tolower(string[0]) == tolower(string[size - 1])) && IsPalindrome(string + 1, size - 2);
    }
}

int main(void)
{
    const char* tests[] = {
        "Bush saw Sununu swash sub.",
        "Do, O God, no evil deed! Live on! Do good!",
        "This is a random text",
        "Flee to me, remote elf.",
        "No, I told Ed \"lotion.\"",
        "Never gonna give you up, never gonna let you down, Never gonna run around and desert you."
    };

    const int testsCount = sizeof(tests) / sizeof(char*);
    int i = 0;

    for (i = 0; i < testsCount; i++)
    {
        if (IsPalindrome(tests[i], strlen(tests[i])))
        {
            printf("\"%s\" is a palindrome!\n", tests[i]);
        }
    }

    return 0;
}
PrimePackster commented: Again! No spoon feeding on DANIWEB +0
Gonbe 32 Newbie Poster

Example of b

#include <stdio.h>

int Highest (const int l, const int r)
{
    return ((l > r) ? l : r);
}

int Lowest (const int l, const int r)
{
    return ((l < r) ? l : r);
}

void FindMinMax (const int A[], const int n, int* const min, int* const max)
{
    int curMin = 0, curMax = 0;

    if (n == 1)
    {
        (*min) = A[0];
        (*max) = A[0];
    }
    else if (n > 1)
    {
        curMin = A[0];
        curMax = A[0];

        FindMinMax(A + 1, n - 1, min, max);

        (*min) = Lowest ((*min), curMin);
        (*max) = Highest((*max), curMax);
    }
}

int main(void)
{
    int example[] = { 23, 488, 1, 2930, 99, 102 };
    int low, high;

    FindMinMax(example, sizeof(example) / sizeof(int), &low, &high);
    printf("lowest: %d, highest: %d\n", low, high);

    return 0;
}

Found "a" too boring so didn't bother doing that.. Could maybe help if you start on it so I only have to finish/correct it.

deceptikon commented: The OP's question falls under our homework rule. -2
nitin1 commented: not even single effort seen , yet you give the code instead of advices +0
Gonbe 32 Newbie Poster

1) you are expecting us to remember one post you made elsewhere out of hundreds we read daily

You've made the reference to a previous post in another thread yourself. In that case, yes. I do expect you to remember it.

2) sarcasm requires knowledge of the one making sarcastic remarks. All I understand is from what you posted above.

Knowledge about recent events also aid and you were present in those.

3) on a forum, a smily generally hints at sarcasm. No smily, you must be simply daft.

As someone who dislikes smileys I can tell you there's more than that. I don't see writers using smileys in their books (thanks god) and they seem to get their intent across just fine.

The combination of the first two lines ending with '!' and the statement following it which you even quotes should be way obvious. If you're incapable of detecting it then I consider it your issue. But sure, I can omit sarcasm from my posts from now on. Posting a sneer trying to ridicule a post while you are in fact the one not understanding the post at all has the opposite desired effect for you, though. I guess I'll view it as karma.

4) whether you agree or not, you are not to post working answers. Period.

The community rules don't seem to mention this. I'll determine on a per-post basis how I could help a user best, thanks.

WaltP commented: If you have a problem with the way things are supposed to be done, keep it out of the responses and make your points in the Feedback Forum. Stop cluttering up other people's threads with your rants. -3
Gonbe 32 Newbie Poster

The more you backtrack like this (you've done it twice that I know of today) maybe you should give it some thought before posting... Maybe??? ;-)

Oh look, one of the clowns from the other thread. Nice contribution to this thread as well bud. I assume you're still upset about the discussion in the other thread? Maybe you should reply to the questions asked in threads instead of contributing absolutely nothing and merely attempt to de-rail the thread by attempting to flame someone who is actually trying to help the OP... Maybe??? ;-).

Besides, I can't care less about your opinion so you're just wasting your time. I'm honored you're tracking my posts though. I feel bad for having upset you so easily. Oh wait, I don't.

exactly, my thought was just like this, but my prob was that should i use a 3D Dp here ? because for n,k and one for 1 or 0 ?

Yeah that's what I would do. The 3rd dimension having a fixed size of 2. (so in that sense you could also view it as 2 2-dimensional arrays)

-edit-

To elaborate on the "should" part of your question: you'll want to track 2 different types of subsets. There's not 1 generic sub-problem once going into the recursion but 2. (the n,k combination could be identical but the context can be different; The 'outer problem' ended in either a 1 or 0. So this context provides a slightly different problem …

Gonbe 32 Newbie Poster

Really, if that was the case why don't we all just post up the answers to everyones' homework?

You tell me. Just because 'everyone' (which is not even true, but okay) does something, it doesn't mean it's logical or the best thing to do. (A controversial example would be religion if you'd ask me)

In fact, why don't teachers just bypass giving homework altogether and give out questions with model solutions?

If you've ever gone to school you'd know that partially you do get provided examples of good practices. Generally these are subsets of bigger problems that have to be solved later but they can sometimes be a larger example too as these introduce additional complications.

Your statement is very short-sighted as it implies teacher ONLY give homework, which is ofcourse not true.

If you really expect someone to buy your BS at least give plausible reasoning, not some BS you just pulled out of your a##.

Right back at you. Everything you said so far was completely biased.

Again, you clearly miss the purpose of setting homework. To you, it's more instructional to just hand out solutions. Hands up anyone who agrees with this? Come on now, don't be shy.

Again, you want to try to get people behind you to back you up. Should I interpret this as an obvious sign of you being insecure about your own stance? Provided that I seem to have provoked you I guess I …

Gonbe 32 Newbie Poster

^^ "The OP gets a freebie answer and passes his course". Sure he gets an answer. What he does with it is his choice though. If he is indeed one of the people that hands this one in without trying to understand it himself, do you really think your supposedly brilliant 'nudges in the right direction' would have learned him anything? It wouldn't. He would do the absolute bare minimum and not really attempt to learn. Sure, he might have to look some things up and will likely paste some snippets but if he's truly looking just to get rid of the assignment it's all in vain anyway.

I assume that people who ask things actually have the intention to understand it. An example code will serve as a good point to start in my opinion and could serve as a base of other question.

Oh and please refrain from calling people you do not know 'class A idiots', you wouldn't want to be seen as one yourself I presume. And I am already contributing to open-source projects; One activity doesn't rule out the other.

Have a good day, bright light.

iamthwee commented: I am Iamthwee and I disagree with this post. -3
Gonbe 32 Newbie Poster

This assignment contains easy to identify subtasks which can be expressed in their own functions. Following this idea I would suggest something like the following:

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>

// The maximum length of a string.
#define STRING_MAX (40)

// Function declarations/prototypes.
void reverse_substring (char str[], int start, int end);
void reverse_words     (char str[], const int length);

// Reverse the substring within 'str' starting at index 'start' and ending at index 'end'.
void reverse_substring (char str[], int start, int end)
{
    // Little check to ensure correct parameters.
    // Not checking if end < strlen(str) so str doesn't have to be NULL-terminated.
    assert (str != NULL);

    char temp = 0;

    while (start < end)
    {
        // Store the character that's on the left side.
        temp = str[start];

        // Overwrite the character that's on the left side with the one that's on
        // the right side. And move the left index one to the right afterwards.
        str[start++] = str[end];

        // Overwrite the character that's on the right side with
        // the backup of the one that used to be left.
        str[end--] = temp;
    }
}

// Reverse the contents of words in a given string.
void reverse_words (char str[], const int length)
{
    // Not checking for string length so it doesn't have to be NULL-terminated.
    assert (str != NULL);

    int i           = 0,    // Counter to iterate over the string.
        start_found = 0,    // Flag that shows if we're looking for the beginning or the …
iamthwee commented: Yes you deserve this +0
happygeek commented: some more well deserved neg rep from me... -2
Gonbe 32 Newbie Poster

While this doesn't even deserve an answer as you don't even ask a proper question (You just dump your code and state "it doesn't work!") you could try this.

I'm not going to explain anything about it I guess as you didn't ask me to. It's also a naive implementation that could be optimized a lot but I'm not going to waste time on someone who doesn't even put in an effort to post his thread.

#include <stdio.h>
#include <math.h>

int get_digit_count(unsigned int number);

// Obtain the amount of digits a number consists of.
int get_digit_count(unsigned int number)
{
    int digits = 0;

    do
    {
        // Cut off the last digit
        number /= 10;

        // Add this digit to the total number of digits.
        digits++;
    }
    // Do this until there are no more digits left.
    while (number != 0);

    return digits;
}


// Entry point of the application. Naive implementation 
// of an algorithm to find armstrong numbers. 
int main(void) 
{
    int i                 = 0,    // Integer for iterating.
        limit             = 0,    // The limit of the search.
        digits            = 0,    // The amount of digits for the current number.
        powered_digit_sum = 0,    // The sum of the digits pow'd by the number of digits
        current_number    = 0;    // Current number looked at.

    printf("Enter the limit of the search (inclusive): ");
    scanf ("%d", &limit);

    printf("Calculating armstrong numbers from 0-%d (inclusive)..\n", limit);

    // Go through all numbers from 1 till 'limit' (inclusive)
    for(i = 0; i <= limit; i++) …
WaltP commented: Then why did you bather doing his moework for him in the first place? -3