C/C++ Coding challenges

DURApix

Well-known member
  • Aug 1, 2010
    1,259
    192
    63
    /dev/null
    Tuck Tik Took

    implementation of the game Tic-tac-toe using C++.
    my own method.using combinations.This method hv some weak points.
    try to improve the code..

    Code:
    /*
    All Rights Reserved.
    Copyright(c) 2010 June 18
    dURA Solutions.
    
    Tuck Tik Took (Version 2.0)
    
    Note :
    Updated Version.Thanx Navodth Bhanuka,Dr.PHP and Pera 4 da ideas
    May be the program looks crazy now.(with da effects of timers)
    Now dURA can Offend and Defend :)
    Consist with most of move lists.
    
    TTT game have atleast 3!x8=3x2x1x8=48 Combinations in total.may be Even more.
    still im trying to program all da moves lists and powerful da coding..
    */
    
    #include<iostream>
    #include <time.h>
    #include <stdlib.h>
    
    using namespace std;
    
    int Check(char Ar[3][3]);
    int checkLoss(char Ar[3][3]);
    int checkAggMove(char Ar[3][3]);
    void Wait(int seconds);
    void Progress();
    void ShowBoard(char Ar[3][3]);
    void ComsMove(char Ar[3][3]);
    void PlayerMove(char Ar[3][3]);
    void RandomMove(char Ar[3][3]);
    void AShowBoard(char Ar[3][3]);
    
    int main()
    {
    Start:
    cout<<"===================================\n";
    cout<<" << Tuck Tik Took >> \n";
    cout<<" [Version 2.0] \n";
    cout<<"-----------------------------------\n";
    cout<<" dURA(R) Solutions(C) 2010 June \n";
    cout<<"===================================\n\n";
    cout<<" dURA-O | Player-X\n\n";
    
    char y;
    char Ar[3][3]={'1','2','3','4','5','6','7','8','9'};
    
    ShowBoard(Ar);
    
    Begin:
    int x=0;
    
    //Players Move Functions
    if (!(Ar[0][0]=='1' || Ar[0][1]=='2' || Ar[0][2]=='3' || Ar[1][0]=='4' || Ar[1][1]=='5' || Ar[1][2]=='6' || Ar[2][0]=='7' || Ar[2][1]=='8' || Ar[2][2]=='9'))
    {cout<<"\nMatch Ends Up in a Draw !!!\n";
    goto Last;}
    
    PlayerMove(Ar);
    cout<<"\n\n----- Players Move -------\n\n";
    ShowBoard(Ar);
    
    x=Check(Ar);
    if(x==1 || x==2)
    goto Last;
    
    if (!(Ar[0][0]=='1' || Ar[0][1]=='2' || Ar[0][2]=='3' || Ar[1][0]=='4' || Ar[1][1]=='5' || Ar[1][2]=='6' || Ar[2][0]=='7' || Ar[2][1]=='8' || Ar[2][2]=='9'))
    {cout<<"\nMatch Ends Up in a Draw !!!\n";
    goto Last;}
    
    Progress();
    
    //Coms Move Functions
    ComsMove(Ar);
    cout<<"------- dURAs Move -------\n\n";
    AShowBoard(Ar);
    x=Check(Ar);
    if(x==1 || x==2)
    goto Last;
    
    goto Begin;
    
    Last:
    cout<<"\n\nGame Over !! \n";
    cout<<"\n\nDo Want to Play Again (y/n) : ";
    cin>>y;
    if(y=='y')
    {system("cls");goto Start;}
    else
    return 0;
    }
    
    int Check(char Ar[3][3])
    {
    int x=0;
    
    if (Ar[0][0]=='X' && Ar[0][1]=='X' && Ar[0][2]=='X') x=1;
    if (Ar[1][0]=='X' && Ar[1][1]=='X' && Ar[1][2]=='X') x=1;
    if (Ar[2][0]=='X' && Ar[2][1]=='X' && Ar[2][2]=='X') x=1;
    if (Ar[0][0]=='X' && Ar[1][0]=='X' && Ar[2][0]=='X') x=1;
    if (Ar[0][1]=='X' && Ar[1][1]=='X' && Ar[2][1]=='X') x=1;
    if (Ar[0][2]=='X' && Ar[1][2]=='X' && Ar[2][2]=='X') x=1;
    if (Ar[0][0]=='X' && Ar[1][1]=='X' && Ar[2][2]=='X') x=1;
    if (Ar[2][0]=='X' && Ar[1][1]=='X' && Ar[0][2]=='X') x=1;
    
    if (Ar[0][0]=='O' && Ar[0][1]=='O' && Ar[0][2]=='O') x=2;
    if (Ar[1][0]=='O' && Ar[1][1]=='O' && Ar[1][2]=='O') x=2;
    if (Ar[2][0]=='O' && Ar[2][1]=='O' && Ar[2][2]=='O') x=2;
    if (Ar[0][0]=='O' && Ar[1][0]=='O' && Ar[2][0]=='O') x=2;
    if (Ar[0][1]=='O' && Ar[1][1]=='O' && Ar[2][1]=='O') x=2;
    if (Ar[0][2]=='O' && Ar[1][2]=='O' && Ar[2][2]=='O') x=2;
    if (Ar[0][0]=='O' && Ar[1][1]=='O' && Ar[2][2]=='O') x=2;
    if (Ar[2][0]=='O' && Ar[1][1]=='O' && Ar[0][2]=='O') x=2;
    
    if(x==1)
    cout<<"\n\nPlayer Wins the Game!!! \n";
    else if(x==2)
    cout<<"\n\ndURA Wins the Game!!! \n";
    
    return x;
    }
    
    void RandomMove(char Ar[3][3])
    {int i,j;
    
    if(Ar[1][1]=='5')
    {Ar[1][1]='O';
    goto last;}
    
    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
    {
    if (Ar[i][j]=='1' || Ar[i][j]=='2' || Ar[i][j]=='3' || Ar[i][j]=='4' || Ar[i][j]=='5' || Ar[i][j]=='6' || Ar[i][j]=='7' || Ar[i][j]=='8' || Ar[i][j]=='9')
    {Ar[i][j]='O';
    goto last;}
    }
    }
    last:
    cout<<"";
    }
    
    void Progress()
    {
    cout<<"\n\dURA is Thinking... ";
    for(int i=0;i<5;i++)
    {cout<<">";
    Wait(1);}
    cout<<"\n\nOK!! Friend";
    Wait(1);
    cout<<"\ndURA got the Point..!!\n";
    Wait(2);
    }
    
    void ComsMove(char Ar[3][3])
    {
    int Ans;
    
    Ans=checkAggMove(Ar);
    if (Ans==1)
    goto skip;
    
    Ans=checkLoss(Ar);
    if (Ans==1)
    goto skip;
    
    RandomMove(Ar);
    
    skip:
    cout<<"\n";
    }
    
    void Wait(int seconds)
    {//BTW I dont know WTF Process :(
    clock_t endwait;
    endwait = clock () + seconds * CLK_TCK ;
    while (clock() < endwait) {}
    }
    
    int checkAggMove(char Ar[3][3])
    {
    int Flag=0;
    
    if (Ar[0][0]=='O' && Ar[1][0]=='O' && Ar[2][0]=='7') {Ar[2][0]='O';Flag=1;goto last;}
    if (Ar[0][0]=='O' && Ar[2][0]=='O' && Ar[1][0]=='4') {Ar[1][0]='O';Flag=1;goto last;}
    if (Ar[1][0]=='O' && Ar[2][0]=='O' && Ar[0][0]=='1') {Ar[2][0]='O';Flag=1;goto last;}
    
    if (Ar[0][1]=='O' && Ar[1][1]=='O' && Ar[2][1]=='8') {Ar[2][1]='O';Flag=1;goto last;}
    if (Ar[0][0]=='O' && Ar[2][1]=='O' && Ar[1][1]=='5') {Ar[1][1]='O';Flag=1;goto last;}
    if (Ar[1][1]=='O' && Ar[2][1]=='O' && Ar[0][1]=='2') {Ar[0][1]='O';Flag=1;goto last;}
    
    if (Ar[0][2]=='O' && Ar[1][2]=='O' && Ar[2][2]=='9') {Ar[2][2]='O';Flag=1;goto last;}
    if (Ar[0][2]=='O' && Ar[2][2]=='O' && Ar[1][2]=='6') {Ar[1][2]='O';Flag=1;goto last;}
    if (Ar[1][2]=='O' && Ar[2][2]=='O' && Ar[0][2]=='3') {Ar[0][2]='O';Flag=1;goto last;}
    
    //Columns
    if (Ar[0][0]=='O' && Ar[0][1]=='O' && Ar[0][2]=='3') {Ar[0][2]='O';Flag=1;goto last;}
    if (Ar[0][0]=='O' && Ar[0][2]=='O' && Ar[0][1]=='2') {Ar[0][1]='O';Flag=1;goto last;}
    if (Ar[0][1]=='O' && Ar[0][2]=='O' && Ar[0][0]=='1') {Ar[0][0]='O';Flag=1;goto last;}
    
    if (Ar[1][0]=='O' && Ar[1][1]=='O' && Ar[1][2]=='6') {Ar[1][2]='O';Flag=1;goto last;}
    if (Ar[1][0]=='O' && Ar[1][2]=='O' && Ar[1][1]=='5') {Ar[1][1]='O';Flag=1;goto last;}
    if (Ar[1][1]=='O' && Ar[1][2]=='O' && Ar[1][0]=='4') {Ar[1][0]='O';Flag=1;goto last;}
    
    if (Ar[2][0]=='O' && Ar[2][1]=='O' && Ar[2][2]=='9') {Ar[2][2]='O';Flag=1;goto last;}
    if (Ar[2][0]=='O' && Ar[2][2]=='O' && Ar[2][1]=='8') {Ar[2][1]='O';Flag=1;goto last;}
    if (Ar[2][1]=='O' && Ar[2][2]=='O' && Ar[2][0]=='7') {Ar[2][0]='O';Flag=1;goto last;}
    
    //Axels
    if (Ar[0][0]=='O' && Ar[1][1]=='O' && Ar[2][2]=='9') {Ar[2][2]='O';Flag=1;goto last;}
    if (Ar[0][0]=='O' && Ar[2][2]=='O' && Ar[1][1]=='5') {Ar[1][1]='O';Flag=1;goto last;}
    if (Ar[1][1]=='O' && Ar[2][2]=='O' && Ar[0][0]=='1') {Ar[0][0]='O';Flag=1;goto last;}
    
    if (Ar[0][2]=='O' && Ar[1][1]=='O' && Ar[2][0]=='7') {Ar[2][0]='O';Flag=1;goto last;}
    if (Ar[0][2]=='O' && Ar[2][0]=='O' && Ar[1][1]=='5') {Ar[1][1]='O';Flag=1;goto last;}
    if (Ar[2][0]=='O' && Ar[1][1]=='O' && Ar[0][2]=='3') {Ar[0][2]='O';Flag=1;goto last;}
    
    last:
    return Flag;
    }
    
    int checkLoss(char Ar[3][3])
    {
    int Flag=0;
    //Duras Combinations Of TTT
    
    if(Ar[1][1]=='5')
    {Ar[1][1]='O';Flag=1;goto last;}
    
    //Rows
    if (Ar[0][0]=='X' && Ar[1][0]=='X' && Ar[2][0]=='7') {Ar[2][0]='O';Flag=1;goto last;}
    if (Ar[0][0]=='X' && Ar[2][0]=='X' && Ar[1][0]=='4') {Ar[1][0]='O';Flag=1;goto last;}
    if (Ar[1][0]=='X' && Ar[2][0]=='X' && Ar[0][0]=='1') {Ar[2][0]='O';Flag=1;goto last;}
    
    if (Ar[0][1]=='X' && Ar[1][1]=='X' && Ar[2][1]=='8') {Ar[2][1]='O';Flag=1;goto last;}
    if (Ar[0][0]=='X' && Ar[2][1]=='X' && Ar[1][1]=='5') {Ar[1][1]='O';Flag=1;goto last;}
    if (Ar[1][1]=='X' && Ar[2][1]=='X' && Ar[0][1]=='2') {Ar[0][1]='O';Flag=1;goto last;}
    
    if (Ar[0][2]=='X' && Ar[1][2]=='X' && Ar[2][2]=='9') {Ar[2][2]='O';Flag=1;goto last;}
    if (Ar[0][2]=='X' && Ar[2][2]=='X' && Ar[1][2]=='6') {Ar[1][2]='O';Flag=1;goto last;}
    if (Ar[1][2]=='X' && Ar[2][2]=='X' && Ar[0][2]=='3') {Ar[0][2]='O';Flag=1;goto last;}
    
    //Columns
    if (Ar[0][0]=='X' && Ar[0][1]=='X' && Ar[0][2]=='3') {Ar[0][2]='O';Flag=1;goto last;}
    if (Ar[0][0]=='X' && Ar[0][2]=='X' && Ar[0][1]=='2') {Ar[0][1]='O';Flag=1;goto last;}
    if (Ar[0][1]=='X' && Ar[0][2]=='X' && Ar[0][0]=='1') {Ar[0][0]='O';Flag=1;goto last;}
    
    if (Ar[1][0]=='X' && Ar[1][1]=='X' && Ar[1][2]=='6') {Ar[1][2]='O';Flag=1;goto last;}
    if (Ar[1][0]=='X' && Ar[1][2]=='X' && Ar[1][1]=='5') {Ar[1][1]='O';Flag=1;goto last;}
    if (Ar[1][1]=='X' && Ar[1][2]=='X' && Ar[1][0]=='4') {Ar[1][0]='O';Flag=1;goto last;}
    
    if (Ar[2][0]=='X' && Ar[2][1]=='X' && Ar[2][2]=='9') {Ar[2][2]='O';Flag=1;goto last;}
    if (Ar[2][0]=='X' && Ar[2][2]=='X' && Ar[2][1]=='8') {Ar[2][1]='O';Flag=1;goto last;}
    if (Ar[2][1]=='X' && Ar[2][2]=='X' && Ar[2][0]=='7') {Ar[2][0]='O';Flag=1;goto last;}
    
    //Axels
    if (Ar[0][0]=='X' && Ar[1][1]=='X' && Ar[2][2]=='9') {Ar[2][2]='O';Flag=1;goto last;}
    if (Ar[0][0]=='X' && Ar[2][2]=='X' && Ar[1][1]=='5') {Ar[1][1]='O';Flag=1;goto last;}
    if (Ar[1][1]=='X' && Ar[2][2]=='X' && Ar[0][0]=='1') {Ar[0][0]='O';Flag=1;goto last;}
    
    if (Ar[0][2]=='X' && Ar[1][1]=='X' && Ar[2][0]=='7') {Ar[2][0]='O';Flag=1;goto last;}
    if (Ar[0][2]=='X' && Ar[2][0]=='X' && Ar[1][1]=='5') {Ar[1][1]='O';Flag=1;goto last;}
    if (Ar[2][0]=='X' && Ar[1][1]=='X' && Ar[0][2]=='3') {Ar[0][2]='O';Flag=1;goto last;}
    
    //checkUpsetMoves
    
    //Salutes 4 Navodth Bhanuka.... :)
    if (Ar[0][0]=='X' && Ar[2][2]=='X' && Ar[2][0]=='7') {Ar[2][0]='O';Flag=1;goto last;}
    if (Ar[0][0]=='X' && Ar[2][2]=='X' && Ar[0][2]=='3') {Ar[0][2]='O';Flag=1;goto last;}
    
    if (Ar[2][0]=='X' && Ar[0][2]=='X' && Ar[0][0]=='1') {Ar[0][0]='O';Flag=1;goto last;}
    if (Ar[2][0]=='X' && Ar[0][2]=='X' && Ar[2][2]=='9') {Ar[2][2]='O';Flag=1;goto last;}
    
    if (Ar[0][0]=='X' && Ar[0][2]=='3') {Ar[0][2]='O';Flag=1;goto last;}
    if (Ar[0][0]=='X' && Ar[2][0]=='7') {Ar[2][0]='O';Flag=1;goto last;}
    if (Ar[2][2]=='X' && Ar[0][2]=='3') {Ar[0][2]='O';Flag=1;goto last;}
    if (Ar[2][2]=='X' && Ar[2][0]=='7') {Ar[2][0]='O';Flag=1;goto last;}
    
    if (Ar[0][2]=='X' && Ar[0][0]=='1') {Ar[0][0]='O';Flag=1;goto last;}
    if (Ar[0][2]=='X' && Ar[2][2]=='9') {Ar[2][2]='O';Flag=1;goto last;}
    if (Ar[2][0]=='X' && Ar[0][0]=='1') {Ar[0][0]='O';Flag=1;goto last;}
    if (Ar[2][0]=='X' && Ar[2][2]=='9') {Ar[2][2]='O';Flag=1;goto last;}
    
    /////////////////////
    
    if (Ar[0][0]=='O' && Ar[2][2]=='9') {Ar[2][2]='O';Flag=1;goto last;}
    if (Ar[0][2]=='O' && Ar[2][0]=='7') {Ar[2][0]='O';Flag=1;goto last;}
    if (Ar[2][0]=='O' && Ar[0][2]=='3') {Ar[0][2]='O';Flag=1;goto last;}
    if (Ar[2][2]=='O' && Ar[0][0]=='1') {Ar[0][0]='O';Flag=1;goto last;}
    
    /*
    00 01 02 | 1 2 3
    10 11 12 | 4 5 6
    20 21 22 | 7 8 9
    */
    
    if (Ar[0][0]=='X' && Ar[2][2]=='X' && Ar[2][1]=='8') {Ar[2][1]='O';Flag=1;goto last;}
    if (Ar[0][0]=='X' && Ar[2][2]=='X' && Ar[0][1]=='2') {Ar[0][1]='O';Flag=1;goto last;}
    if (Ar[0][0]=='X' && Ar[2][2]=='X' && Ar[1][0]=='4') {Ar[1][0]='O';Flag=1;goto last;}
    if (Ar[0][0]=='X' && Ar[2][2]=='X' && Ar[1][2]=='6') {Ar[1][0]='O';Flag=1;goto last;}
    
    if (Ar[2][0]=='O' && Ar[0][2]=='O' && Ar[2][2]=='9') {Ar[2][2]='O';Flag=1;goto last;}
    if (Ar[2][0]=='O' && Ar[0][2]=='O' && Ar[0][0]=='1') {Ar[0][0]='O';Flag=1;goto last;}
    
    // ............
    
    last:
    return Flag;
    }
    
    void AShowBoard(char Ar[3][3])
    {
    cout<<"-------------\n";Wait(1);
    cout<<"| "<<Ar[0][0]<<" | "<<Ar[0][1]<<" | "<<Ar[0][2]<<" |\n";Wait(1);
    cout<<"-------------\n";Wait(1);
    cout<<"| "<<Ar[1][0]<<" | "<<Ar[1][1]<<" | "<<Ar[1][2]<<" |\n";Wait(1);
    cout<<"-------------\n";Wait(1);
    cout<<"| "<<Ar[2][0]<<" | "<<Ar[2][1]<<" | "<<Ar[2][2]<<" |\n";Wait(1);
    cout<<"-------------\n";Wait(1);
    }
    
    void ShowBoard(char Ar[3][3])
    {
    cout<<"-------------\n";
    cout<<"| "<<Ar[0][0]<<" | "<<Ar[0][1]<<" | "<<Ar[0][2]<<" |\n";
    cout<<"-------------\n";
    cout<<"| "<<Ar[1][0]<<" | "<<Ar[1][1]<<" | "<<Ar[1][2]<<" |\n";
    cout<<"-------------\n";
    cout<<"| "<<Ar[2][0]<<" | "<<Ar[2][1]<<" | "<<Ar[2][2]<<" |\n";
    cout<<"-------------\n";
    }
    
    void PlayerMove(char Ar[3][3])
    {
    int r,c;
    int move;
    
    begin:
    cout<<"\nPlayers Turn,\nEnter the Position : ";
    cin>>move;
    
    switch (move)
    {
    case 1 : r=0;c=0;break;
    case 2 : r=0;c=1;break;
    case 3 : r=0;c=2;break;
    case 4 : r=1;c=0;break;
    case 5 : r=1;c=1;break;
    case 6 : r=1;c=2;break;
    case 7 : r=2;c=0;break;
    case 8 : r=2;c=1;break;
    case 9 : r=2;c=2;break;
    default : cout<<"\nIllegal move!!!\n";goto begin;break;
    }
    
    if (Ar[r][c]=='O' || Ar[r][c]=='X')
    {cout<<"Illegal move,This Position is Reserved !!!!\n";
    goto begin;}
    else
    Ar[r][c]='X';
    
    }
     

    hasiwebmail

    Member
    Feb 27, 2007
    501
    52
    0
    Sri Lanka
    Write a C++ program for the following......

    It reads a sequence of positive integers. It stores it
    in an array, then it prompts the user to enter an
    integer, and then rotates clockwise the content of the
    array a corresponding number of positions. The contents
    the array are printed out whenever it changes.


    For example, if the sequence was 1,2,3,4,5 and the number 3 is entered, the array becomes 3,4,5,1,2
    and if the number 1 is now entered, the array becomes 2,3,4,5,1
     

    Enigma_1

    Junior member
  • Feb 5, 2008
    70
    3
    8
    Colombo
    Mata kauru hari me program eke coding eka denawada... Home work karanne ne arawa mewa kiyala baninna nam epa. Code eka post karanna. Vs 2005 walin tibunot hodai.....




    Create an application in VB / VB.NET to display a list of subjects allotted in MBA / MCA program offered by Colombo University. Select the subject from the list whose first letter matches with the character entered in a textbox.

    Mekata database ekak use karanne ne. VB ekema denna tiyanne


    Loku udauwak

    C/C++ eke post karata sorry :(

    Solution eka hoyagathttha da? machan mama num VB.NET gana wadiya danne na. mama karanne C# anb C. ithin machan la kauru hari vb.net danna kenek innawa num help ekak denna.
     

    DURApix

    Well-known member
  • Aug 1, 2010
    1,259
    192
    63
    /dev/null
    Write a C++ program for the following......

    It reads a sequence of positive integers. It stores it
    in an array, then it prompts the user to enter an
    integer, and then rotates clockwise the content of the
    array a corresponding number of positions. The contents
    the array are printed out whenever it changes.


    For example, if the sequence was 1,2,3,4,5 and the number 3 is entered, the array becomes 3,4,5,1,2
    and if the number 1 is now entered, the array becomes 2,3,4,5,1

    its very simple ban..may be i am wrong..did u expect this kind of output or other one?? elaazzzz:yes::yes::yes:

    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
    int i,j=0,c,x,y;
    
    cout<<"Enter Starting Point : ";cin>>x;
    cout<<"Enter Ending Point : ";cin>>y;
    
    for(i=x;i<=y;i++) cout<<i<<" ";cout<<endl;
    cout<<"Enter the Capture Point : ";cin>>c;
    
    for(i=c;i<=y;i++) cout<<i<<" ";
    for(i=x;i<c;i++) cout<<i<<" ";
    
    cout<<endl;
    }
     

    ruwantheekshana

    Well-known member
  • Mar 10, 2008
    5,868
    1,316
    113
    මටත් C ++ කරන්න තියෙනවා engineering වලට.හැබැයි පුදුම අමාරුයි.
     

    ldwkm

    Member
    Dec 30, 2008
    2,811
    45
    0
    LONDON,UK
    http://canyoucrackit.co.uk/

    if you crack this, you have a chance of getting a job at GCHQ
    this is NOT easy guys :) think about it..

    _57042185_image008.jpg


    more info: http://www.bbc.co.uk/news/technology-15968878
     

    rukey184

    Well-known member
  • Aug 14, 2010
    4,420
    680
    113
    on earth
    need help

    :rolleyes::rolleyes::rolleyes::rolleyes::rolleyes:

    machanla mata kiyanawada numbers 2k dammama higest aka anda code aka.and api same nubers dammama higest 1k na kiyala kiyanda ona.

    for eg:- api 5 and and 4 enter kaloth higest aka kiyala penanda ona 5 and if we enter 5 and 5 as the two numbers it should says there is no higest ...
     

    shenat

    Well-known member
  • May 13, 2007
    57,760
    85,932
    113
    ආශ්චර්යමත් රටක
    :rolleyes::rolleyes::rolleyes::rolleyes::rolleyes:

    machanla mata kiyanawada numbers 2k dammama higest aka anda code aka.and api same nubers dammama higest 1k na kiyala kiyanda ona.

    for eg:- api 5 and and 4 enter kaloth higest aka kiyala penanda ona 5 and if we enter 5 and 5 as the two numbers it should says there is no higest ...

    C walin menna..

    #include<stdio.h>
    int main()
    {
    int x,y,z ;
    printf("Enter first number = ");
    scanf("%d",&x);
    printf("\n Enter second number = ");
    scanf("%d",&y);
    z = x-y ;
    if (z>0)
    printf("\n\n maximum number is %d",x);
    else if(z<0)
    printf("\n\n maximum number is %d",y);
    else
    printf("\n\n both numbers are same");
    printf("\n\n\n");
    return 0;
    }
     

    rukey184

    Well-known member
  • Aug 14, 2010
    4,420
    680
    113
    on earth
    C walin menna..

    #include<stdio.h>
    int main()
    {
    int x,y,z ;
    printf("Enter first number = ");
    scanf("%d",&x);
    printf("\n Enter second number = ");
    scanf("%d",&y);
    z = x-y ;
    if (z>0)
    printf("\n\n maximum number is %d",x);
    else if(z<0)
    printf("\n\n maximum number is %d",y);
    else
    printf("\n\n both numbers are same");
    printf("\n\n\n");
    return 0;
    }


    :) so much thanks machan man code karla balannam :)
     

    Singleton

    Member
    Jan 7, 2012
    10
    0
    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... :(


    Nice problems! Here are my solutions in C#:

    Solution to problem 1:
    Code:
            static void Main(string[] args)
            {
                partition(10); // print the partitions of number 10
                Console.ReadKey();
            }
    
            // Print all partitons of a given number
            static void partition(int num)
            {
                for (int i = 1; i <= num; i++)
                {
                    int[] v = new int[i + 1];
                    v[0] = num - i + 1;
                    partitionTo(num, 1, i, 0, v);
                }
            }
    
            // Print all partitons of n of a given number. n is the number of partitons
            static void partitionTo(int n, int from, int to, int sum, int[] v)
            {
                if (from <= to)
                {
                    for (int i = v[from - 1]; i >= 1; i--)
                    {
                        v[from] = i;
                        partitionTo(n, from + 1, to, sum + i, v);
                    }
                }
                else
                {
                    if (sum == n)
                    {
                        for (int i = 1; i <= to; i++)
                            Console.Write(v[i] + " ");
                        Console.WriteLine();
                    }
                }
            }
    Solution to problem 2:
    Code:
            static int NumberOfOnes(int n)
            {
                if (n == 0) return 0;
                if (n % 2 == 0)
                    return NumberOfOnes(n / 2);
                else
                    return NumberOfOnes((n - 1) / 2) + 1;
            }
    BTW are you from University of Moratuwa?