C/C++ Coding challenges

madurax86

Member
Jun 29, 2006
4,385
88
0
macho C++ wala print karanne "cout<<" kiyala. "System.out.println" kiyala nemei babo.......
scan karanne "cin>>" kiyala baby........
ube try eka hodai..Good...
But ara errors tika hadaganna hode.....
TRY AGAIN!!!!!!!!!!

Do you think it'll compile with a C++ compiler after fixing those two errors ?
 
Last edited:

maxXxa

Member
Apr 14, 2010
341
5
0
shaa maru thread ekakne .. menna magenuth prashnayak...

1. A partition of a positive integer n is a sequence of positive integers that sum to n. Implement the algorithm (in C) to print all non-increasing partitions of n.
[FONT=&quot] eg. If n=4
4[/FONT]

[FONT=&quot] 3 1[/FONT]
[FONT=&quot] 2 2[/FONT]
[FONT=&quot] 2 1 1[/FONT]
[FONT=&quot] 1 1 1 1[/FONT]

2. Implement (in C) an algorithm that computes the number of 1's in the binary representation of a non-negative integer n . For example if n = 15 your program should return 4 since the binary representation of 15 is 1111. The program should be a recursive one, based on the following definition of f(n) , the number of 1's in the binary representation of n :
f(n) = f((n-1)/2) + 1 if n is odd
= f(n/2) if n is even
ayyalatanam meka gemak nathuwa athi.. eth mamanam erila inne agak mulak hoyaganna baruwa... :(
 

DURApix

Well-known member
  • Aug 1, 2010
    1,259
    192
    63
    /dev/null
    shaa maru thread ekakne .. menna magenuth prashnayak...

    ayyalatanam meka gemak nathuwa athi.. eth mamanam erila inne agak mulak hoyaganna baruwa... :(

    Code:
    #include<iostream>
    using namespace std;
    
    int main(){
    
    int x;
    Begin:
    cout<<"\n\nEnter a No : ";
    cin>>x;
    
    cout<<endl<<x<<endl;
    
    int i,j,k,m;
    
    for(i=x;i>0;i--){
        for(j=1;j<x;j++){
    
            if((i+j)==x)
            {
            cout<<i<<" "<<j<<endl;
    
            for(k=2;k<j;k++){
            for(m=j;m>0;m--){
            if((k+m)==j)
            cout<<i<<" "<<k<<" "<<m<<endl;
            }
            }
    
            if(j>1){
            cout<<i<<" ";
            for(k=0;k<j;k++) cout<<"1 ";
            cout<<endl; }
    
            }
    
        }
    }
    
    goto Begin;
    
    }



    Onna machan 1veni gaana nam hari..:D:D:yes::yes:

    second ekatath try ekak dennam...elazzzz :yes::yes::yes:
     

    maxXxa

    Member
    Apr 14, 2010
    341
    5
    0
    Code:
    #include<iostream>
    using namespace std;
    
    int main(){
    
    int x;
    Begin:
    cout<<"\n\nEnter a No : ";
    cin>>x;
    
    cout<<endl<<x<<endl;
    
    int i,j,k,m;
    
    for(i=x;i>0;i--){
        for(j=1;j<x;j++){
    
            if((i+j)==x)
            {
            cout<<i<<" "<<j<<endl;
    
            for(k=2;k<j;k++){
            for(m=j;m>0;m--){
            if((k+m)==j)
            cout<<i<<" "<<k<<" "<<m<<endl;
            }
            }
    
            if(j>1){
            cout<<i<<" ";
            for(k=0;k<j;k++) cout<<"1 ";
            cout<<endl; }
    
            }
    
        }
    }
    
    goto Begin;
    
    }

    Onna machan 1veni gaana nam hari..:D:D:yes::yes:

    second ekatath try ekak dennam...elazzzz :yes::yes::yes:
    maru ayye... oyata godak pin mekata help ekak denawata .. :)
     

    cyber22118

    Member
    Jan 30, 2010
    3,569
    99
    0
    Not Far From U
    ok.. write a program to input a line of text ( with spaces ) and print it on the screen with reverse order.. ( reverse character)

    this is pretty easy .. but since this thread was closed i thought i should post a challenge,,

    lets see who does this..
     

    madurax86

    Member
    Jun 29, 2006
    4,385
    88
    0
    Code:
    #include<iostream>
    using namespace std;
    
    int main(){
    
    int x;
    Begin:
    cout<<"\n\nEnter a No : ";
    cin>>x;
    
    cout<<endl<<x<<endl;
    
    int i,j,k,m;
    
    for(i=x;i>0;i--){
        for(j=1;j<x;j++){
    
            if((i+j)==x)
            {
            cout<<i<<" "<<j<<endl;
    
            for(k=2;k<j;k++){
            for(m=j;m>0;m--){
            if((k+m)==j)
            cout<<i<<" "<<k<<" "<<m<<endl;
            }
            }
    
            if(j>1){
            cout<<i<<" ";
            for(k=0;k<j;k++) cout<<"1 ";
            cout<<endl; }
    
            }
    
        }
    }
    
    goto Begin;
    
    }



    Onna machan 1veni gaana nam hari..:D:D:yes::yes:

    second ekatath try ekak dennam...elazzzz :yes::yes::yes:

    Very good. I appreciate your way of getting the partitions. But there are repetitions, for example, 1 2 5 and 5 2 1 both add up to 8. When I tried it all gave repeating outputs, this is a rather hard one to make an algorithm for(definitely not easy!), see http://en.wikipedia.org/wiki/Partition_(number_theory) for the mathematical aspects of number partitioning. I tried but got repeating results so I didn't continue on it :dull:
     

    madurax86

    Member
    Jun 29, 2006
    4,385
    88
    0
    YAY! I finally did it! This doesn't repeat the partitions, it uses a bit of mathematics theory for this. But hey, it gets the job done!.
    Read this,
    http://mathworld.wolfram.com/Partition.html

    My algorithm uses the equation given in that page(1.x1+2.x2+3.x3 .....n.xn=n) to find the partitions, the program finds roots of that equation, that's all!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef unsigned int uint;
    typedef struct anode{
        uint num;
        uint level;
        struct anode *parent;
    }node;
    
    uint n=0;
    
    node* alloc()
    {
        node *new = malloc(sizeof(node));
        new->num = 0;
        new->level = 0;
        new->parent = NULL;
        return new;
    }
    void backtrace(node *a)
    {
        uint i;
        static uint count=0;
        count++;
        printf("%d. ",count);
        while (a->parent!=NULL){
            for (i=0;i<a->num;i++)
                printf("%d ", a->level);
            a = a->parent;
        }
        printf("\n");
    }
    
    void sum(uint total, uint level, node *owner)
    {
        uint i;
        uint tmp = 0;
        node *this;
    
        if ( level > n ) return;
        for (i=0;i<=n;i++){
        
            tmp = total + level*i;
            this = alloc();
            this->parent = owner;
            this->num = i;
            this->level = level;
    
            if (tmp >= n){
                if (tmp == n) backtrace(this);
                return;
            }else{
                sum(tmp, level+1, this);
            }
        }
    
    }
    
    int main()
    {
        node *root = alloc();
        printf("Enter a number : ");
        scanf("%d",&n);
        
        sum(0, 1, root);
        return 0;
    }
     

    DURApix

    Well-known member
  • Aug 1, 2010
    1,259
    192
    63
    /dev/null
    Very good. I appreciate your way of getting the partitions. But there are repetitions, for example, 1 2 5 and 5 2 1 both add up to 8. When I tried it all gave repeating outputs, this is a rather hard one to make an algorithm for(definitely not easy!), see http://en.wikipedia.org/wiki/Partition_(number_theory) for the mathematical aspects of number partitioning. I tried but got repeating results so I didn't continue on it :dull:


    yes ayya..i tried to fix dat repetition problem but i didnt got an idea do fix dat..anyway now its nice to go through in your C program all the bugs are fixed and working properly.
    nice program ayya.....elazzzz
    11.gif
    :):):)
     

    madurax86

    Member
    Jun 29, 2006
    4,385
    88
    0
    yes ayya..i tried to fix dat repetition problem but i didnt got an idea do fix dat..anyway now its nice to go through in your C program all the bugs are fixed and working properly.
    nice program ayya.....elazzzz
    11.gif
    :):):)

    :) thanks. It's not actually a bug we know that it can output duplicates but I didn't find a way to filter them out, filtering is not a good method since when the result set gets big it becomes very slow. This one uses recursion to get results and the linked list to store them :D..hell it wasn't easy! Good to have these kinda problems!
     

    cyber22118

    Member
    Jan 30, 2010
    3,569
    99
    0
    Not Far From U
    :) thanks. It's not actually a bug we know that it can output duplicates but I didn't find a way to filter them out, filtering is not a good method since when the result set gets big it becomes very slow. This one uses recursion to get results and the linked list to store them :D..hell it wasn't easy! Good to have these kinda problems!

    really impressive.. where did u learn C++ ?
     

    DURApix

    Well-known member
  • Aug 1, 2010
    1,259
    192
    63
    /dev/null
    ok.. write a program to input a line of text ( with spaces ) and print it on the screen with reverse order.. ( reverse character)

    this is pretty easy .. but since this thread was closed i thought i should post a challenge,,

    lets see who does this..


    machan oya wadee JAVA walin nam gemak na...:lol::):):lol:
    oni nam C walinut hadannam...elazzz:lol::lol::cool::cool::cool::):):)
    11.gif


    Code:
    import java.util.Scanner;
    
    class Print{
    public static void main(String[]args){
    
    Scanner in=new Scanner(System.in);
    
    System.out.print("Enter the Text : ");
    String x=in.nextLine();
    int l=x.length();
    for(int i=l-1;i>=0;i--){
    System.out.print(x.charAt(i)+" ");
    }
    
    }
    }
     

    madurax86

    Member
    Jun 29, 2006
    4,385
    88
    0
    machan oya wadee JAVA walin nam gemak na...:lol::):):lol:
    oni nam C walinut hadannam...elazzz:lol::lol::cool::cool::cool::):):)
    11.gif


    Code:
    import java.util.Scanner;
    
    class Print{
    public static void main(String[]args){
    
    Scanner in=new Scanner(System.in);
    
    System.out.print("Enter the Text : ");
    String x=in.nextLine();
    int l=x.length();
    for(int i=l-1;i>=0;i--){
    System.out.print(x.charAt(i)+" ");
    }
    
    }
    }

    and in Python,
    Code:
    print raw_input("Enter text\n")[::-1]

    oh I love one liners :P