A Thread for Programmers.!

hasiwebmail

Member
Feb 27, 2007
501
52
0
Sri Lanka


Make a combination program in C++ which will take a string and value of r as input and will generate all possible combinations

*where r is r of formula ncr
 

hasiwebmail

Member
Feb 27, 2007
501
52
0
Sri Lanka
Thanks for submitting your Question
:)

For the first one......

eg1:
Enter String: 123 <- user enters
and it prints,
123
321
213
132
231
312

//@hasi
#include "stdio.h"
#include "string.h"
void posrot(char [],int);
void permutate(char [],int);
int main(void)
{
int n=0,m=0;
char as[30];
printf("Enter a string : ");
gets(as);
n=strlen(as);
permutate(as,n);
return 0;
}

void posrot(char as[],int pos)
{
int i=0;
char temp;
temp=as[0];
while(i< pos)
{
as=as[i+1];
i++;
}
as=temp;
}

void permutate(char as[],int n)
{
int l=0;
for(l=1;l<=n;l++)
{
if(n!=2)
{
permutate(as,n-1);
posrot(as,n-1);
}
else
{
for(l=1;l<=2;l++)
{
posrot(as,1);
puts(as);
}
}
}
}
 
Last edited:

Core

Member
Jan 23, 2010
3,120
195
0
Milky Way/Local Cluster/Local Sol/Earth
Question

Write a C++ program to convert any infix expression to its post-fix form....
Eg-

if user enters a+b
it should print ab+


Answer
Code:
/*\
Write a C++ program to convert any infix expression to its post-fix form....
Eg-

if user enters a+b
it should print ab+
\*/

#include <iostream>
#include <iomanip>

using std::cin;
using std::endl;
using std::cout;
using std::setw;

int main()
{
    int hold=0;
    const int max=100;

    char symbol('+');
    char string[max] = {0};
    char repeat('n');

    char stringExp[max] = {0};
    char stringSymbol[max] = {0};
    int j=0;
    int i=0;
    int k=0;

    cout << "Type any Expression to proceed" << endl;
    cin.getline(string,max,'\n');

    cout << endl;

    cout << "Type the symbol" << endl;
    cin >> symbol;
    
    while(string[i] != '\0') //till it reaches the null character('\0')
    {
        if(string[i] == symbol)
        {
            stringSymbol[j] = string[i];
            j++;
        }else
        {
            stringExp[k] = string[i];
            k++;
        }
        i++;
    }

    cout << endl;
    cout << "Final Out Put " << stringExp << stringSymbol ;
    cout << endl;

    cin >> hold;
    return 0;
}

Thx GT :)
VV
 
Last edited:

GTRZ

Administrator
Staff member
  • Apr 27, 2006
    19,480
    10,860
    113
    Guys help him. Its not his homework. He is trying to create a programming discussion thread. Join the work if you know programming or wanna learn.
     

    Core

    Member
    Jan 23, 2010
    3,120
    195
    0
    Milky Way/Local Cluster/Local Sol/Earth
    Question 2

    Write a small program which asks the user to type 20 number of integers and writes the larger and smaller value,finally add them all and print out the sum.
    also let user to exit from the program when types any character.
    -example Y or N
     

    amilabanuka

    Well-known member
  • Sep 30, 2006
    7,296
    891
    113
    Thama math hoyanooo....
    here is some algorithmic problem. indeed this was given as a part of assignment time back. thought of sharing with you.

    (a). Give a non-recursive algorithm to search a key k in a binary-search-tree (BST) with root x.

    either you can implement this or provide a pseudo code.
     

    hasiwebmail

    Member
    Feb 27, 2007
    501
    52
    0
    Sri Lanka
    here is some algorithmic problem. indeed this was given as a part of assignment time back. thought of sharing with you.

    (a). Give a non-recursive algorithm to search a key k in a binary-search-tree (BST) with root x.

    either you can implement this or provide a pseudo code.


    //Program to illustrate binary search
    #include<iostream.h>
    #include<conio.h>

    class binarysearch()
    {

    void main()
    {
    int a[100],i,loc,mid,beg,end,n,flag=0,item;
    clrscr();
    cout<<"How many elements ";
    cin>>n;
    cout<<"Enter the element of the array"<<endl;
    for(i=0;i<=n-1;i++)
    {
    cin>>a;
    }
    cout<<"Enter the element to be searching ";
    cin>>item;
    loc=0;
    beg=0;
    end=n-1;
    while((beg<=end)&&(item!=a[mid]))
    {
    mid=((beg+end)/2);
    if(item==a[mid])
    {
    cout<<"search is successfull"<<endl;
    loc=mid;
    cout<<"Position of the item "<<loc+1;
    flag=flag+1;
    }
    if(item<a[mid])
    end=mid-1;
    else
    beg=mid+1;
    }
    if(flag==0)
    {
    cout<<"search is not successfull";
    }
    getch();
    }

    /*The output of the above program is:
    How many elements 6
    Enter the element of the array
    11
    22
    33
    44
    55
    66
    Enter the element to be searching 44
    search is successfull
    Position of the item 4 */

    using borland TC 3.0v
    verified
     

    hasiwebmail

    Member
    Feb 27, 2007
    501
    52
    0
    Sri Lanka
    Question




    Answer
    Code:
    /*\
    Write a C++ program to convert any infix expression to its post-fix form....
    Eg-
    
    if user enters a+b
    it should print ab+
    \*/
    
    #include <iostream>
    #include <iomanip>
    
    using std::cin;
    using std::endl;
    using std::cout;
    using std::setw;
    
    int main()
    {
        int hold=0;
        const int max=100;
    
        char symbol('+');
        char string[max] = {0};
        char repeat('n');
    
        char stringExp[max] = {0};
        char stringSymbol[max] = {0};
        int j=0;
        int i=0;
        int k=0;
    
        cout << "Type any Expression to proceed" << endl;
        cin.getline(string,max,'\n');
    
        cout << endl;
    
        cout << "Type the symbol" << endl;
        cin >> symbol;
        
        while(string[i] != '\0') //till it reaches the null character('\0')
        {
            if(string[i] == symbol)
            {
                stringSymbol[j] = string[i];
                j++;
            }else
            {
                stringExp[k] = string[i];
                k++;
            }
            i++;
        }
    
        cout << endl;
        cout << "Final Out Put " << stringExp << stringSymbol ;
        cout << endl;
    
        cin >> hold;
        return 0;
    }

    Thx GT :)
    VV

    well well.... It should check any operator with infix expression. Not only (+) operator. Nice try,
     

    hasiwebmail

    Member
    Feb 27, 2007
    501
    52
    0
    Sri Lanka
    dude + is the default operator
    but you can insert any operator in run time mode.

    I checked ur program, It is asking only the letters first and then the operator, It's not what i expected, program should able to get the whole expression at once. Check ur program with + - / *
    and i realized
    / and * (division and mul) not working currently in ur program

    input should be the expression

    Ex-
    a+b

    not like
    ab

    operator
    +

    keep trying...........
     
    Last edited:

    Core

    Member
    Jan 23, 2010
    3,120
    195
    0
    Milky Way/Local Cluster/Local Sol/Earth
    Try with expression more than one operator....

    :rolleyes:


    Code:
    /*\
    Write a C++ program to convert any infix expression to its post-fix form....
    Eg-
    
    if user enters a+b
    it should print ab+
    \*/
    
    #include <iostream>
    #include <iomanip>
    
    using std::cin;
    using std::endl;
    using std::cout;
    using std::setw;
    
    int main()
    {
        int hold=0;
        const int max=100;
    
        char string[max] = {0};
        char repeat('n');
    
        char stringExp[max] = {0};
        char stringSymbol[max] = {0};
        int j=0;
        int i=0;
        int k=0;
    
        cout << setw(2) << "Type any Expression to proceed" << endl;
        cin.getline(string,max,'\n');
    
        cout << endl;
    
        while(string[i] != '\0') //till it reaches the null character
        {
            if('9' >= string[i] && '0' <= string[i]) 
            {
                stringExp[k] = string[i];
                k++;
            }else 
            {
                stringSymbol[j] = string[i];
                j++;
            }
            i++;
        }
    
        cout << endl;
        cout << "Final Out Put " << stringExp << stringSymbol ;
        cout << endl;
    
        cin >> hold;
        return 0;
    }
     
    Last edited:

    hasiwebmail

    Member
    Feb 27, 2007
    501
    52
    0
    Sri Lanka


    Still not correct......... :no:

    input
    a/b

    output
    a/b

    should be ab/

    evidence......

    89398087.jpg
     
    Last edited:

    Core

    Member
    Jan 23, 2010
    3,120
    195
    0
    Milky Way/Local Cluster/Local Sol/Earth
    Still not correct......... :no:

    input
    a/b

    output
    a/b

    should be ab/

    evidence......

    89398087.jpg

    Sorry it was developed for numbers only
    here is final code


    Code:
    /*\
    Write a C++ program to convert any infix expression to its post-fix form....
    Eg-
    
    if user enters a+b
    it should print ab+
    \*/
    
    #include <iostream>
    #include <iomanip>
    
    using std::cin;
    using std::endl;
    using std::cout;
    using std::setw;
    
    int main()
    {
        int hold=0;
        const int max=100;
    
        char string[max] = {0};
        char repeat('n');
    
        char stringExp[max] = {0};
        char stringSymbol[max] = {0};
        int j=0;
        int i=0;
        int k=0;
    
        cout << "Type any Expression to proceed" << endl;
        cin.getline(string,max,'\n');
    
        cout << endl;
    
        while(string[i] != '\0') //till it reach the null character
        {
            if(('9' >= string[i] && '0' <= string[i]) || ('a'<= string[i] && 'z' >= string[i]) || ('A'<= string[i] && 'Z' >= string[i]) ) 
            {
                stringExp[k] = string[i];
                k++;
            }else 
            {
                stringSymbol[j] = string[i];
                j++;
            }
            i++;
        }
    
        cout << endl;
        cout << "Final Out Put " << stringExp << stringSymbol ;
        cout << endl;
    
        cin >> hold;
        return 0;
    }

    Untitled.jpg
     
    Last edited: