Thread for Programming and Realeted Topics

madurax86

Member
Jun 29, 2006
4,385
88
0
Machan i pasted this code on a windows application of dev C++ .And it compiled successfully.But when i hit the RUN button command prompt appeared and said press any key to continue and after pressing a key it closed..........

I erased all the codings which comes automatcially when we create a windows application ....May be that is the case !!!! So Please help me on this !!!

මුලින් ම අලුත් ප්‍රොජෙක්ට් එකක් Open කරන්න Console mode එක තෝරගන්න
ඊට පස්සෙ ඔය code එක paste කරන්න(මුලින් එතන තියෙන කෝඩ් එක අයින් කරන්න)
ඊට පස්සෙ F9 ගහන්න.
 

rclakmal

Active member
  • May 8, 2008
    698
    183
    43
    In a galaxy far far away
    මුලින් ම අලුත් ප්‍රොජෙක්ට් එකක් Open කරන්න Console mode එක තෝරගන්න
    ඊට පස්සෙ ඔය code එක paste කරන්න(මුලින් එතන තියෙන කෝඩ් එක අයින් කරන්න)
    ඊට පස්සෙ F9 ගහන්න.

    Thanks machan !It worked!!!!!!
     

    rclakmal

    Active member
  • May 8, 2008
    698
    183
    43
    In a galaxy far far away
    Hey no one seem to be interested in programming stuff !!!!!! Please post ur problems here .........i mean then we can develop solutions using a language we know !!!!! Its really fun guys !!!!!! cmon !!!!!!!
     

    rclakmal

    Active member
  • May 8, 2008
    698
    183
    43
    In a galaxy far far away
    i saw a question on "YAHOO ANSWERS" and and here is my solution for all of u !!!!!!

    Question was ---
    Code:
    [B][SIZE=2]How to do this java programme?[/SIZE][/B]
    
                       [SIZE=2]Q3. Write a program to draw a diamond sign in to the screen with different heights entered by the user. See following example. 
    
    Enter the height of the diagonal: 7 
         *
        * *
       * * *
      * * * *
       * * *
        * *
         *
    [URL]http://answers.yahoo.com/question/index?qid=20090925215708AAkjRKl[/URL]


    and here is my answer
    Code:
    package diamond;
    import java.util.Scanner;
    
    /**
     *
     * @author lakmal
     */
    class Diamond {
    
        /**
         * @param args the command line arguments
         */
        static int height;
        static char state;
        Diamond(int height){
            for (int i=1;i<=height;i++){
                if (i<=(height/2)){
                for (int j=1;j<=i;j++){
                    System.out.print(" *");
                   
                }
              }
                else{
                    for(int j=1;j<=(height+1-i);j++){
                        System.out.print(" *");
                       
    
                    }
                }
                System.out.println();
            }
        }
    
        public static void main(String[] args) {
            // TODO code application logic here
            Scanner input=new Scanner(System.in);
            do{
    
            do{
            System.out.println("Enter your height");
             height=input.nextInt();
            Diamond d1=new Diamond(height);
            } while(height%2!=0);
            System.out.println("Enter a odd number!!");
            System.out.println("Do you want to continue?(y/n)");
            state=input.next().charAt(0);
    
    
    
        } while(state=='y');
    
      }
    }

    Happy coding !!!!!
    [/SIZE]
     

    madurax86

    Member
    Jun 29, 2006
    4,385
    88
    0
    i saw a question on "YAHOO ANSWERS" and and here is my solution for all of u !!!!!!

    Question was ---
    Code:
    [B][SIZE=2]How to do this java programme?[/SIZE][/B]
    
                       [SIZE=2]Q3. Write a program to draw a diamond sign in to the screen with different heights entered by the user. See following example. 
    
    Enter the height of the diagonal: 7 
         *
        * *
       * * *
      * * * *
       * * *
        * *
         *
    [URL]http://answers.yahoo.com/question/index?qid=20090925215708AAkjRKl[/URL]


    and here is my answer
    Code:
    package diamond;
    import java.util.Scanner;
    
    /**
     *
     * @author lakmal
     */
    class Diamond {
    
        /**
         * @param args the command line arguments
         */
        static int height;
        static char state;
        Diamond(int height){
            for (int i=1;i<=height;i++){
                if (i<=(height/2)){
                for (int j=1;j<=i;j++){
                    System.out.print(" *");
                   
                }
              }
                else{
                    for(int j=1;j<=(height+1-i);j++){
                        System.out.print(" *");
                       
    
                    }
                }
                System.out.println();
            }
        }
    
        public static void main(String[] args) {
            // TODO code application logic here
            Scanner input=new Scanner(System.in);
            do{
    
            do{
            System.out.println("Enter your height");
             height=input.nextInt();
            Diamond d1=new Diamond(height);
            } while(height%2!=0);
            System.out.println("Enter a odd number!!");
            System.out.println("Do you want to continue?(y/n)");
            state=input.next().charAt(0);
    
    
    
        } while(state=='y');
    
      }
    }

    Happy coding !!!!!
    [/SIZE]
    ඔහොම කරන්න බෑ නේ...

    In the Console you cant output text like dialog boxes
    they are fixed-width if you want a diamond shape you should try either a text box or if you want to use the console
    you should use
    Code:
       *
     ***
    *****
     ***
       *
    that kind of a format to type diamonds not 1,2,3,4,3,2,1
    it should be 1,3,5,7,5,3,1 -- that would be the only output that prints a proper diamond in console
     
    Last edited:

    madurax86

    Member
    Jun 29, 2006
    4,385
    88
    0
    Onna apith awa ehenam :P

    ප්‍රශ්න ඉදිරිපත් කරන්න :P
    නැත්නම් ඔයා හොයාගත්ත algorithms, routines මේකෙ පෝස්ට් කරන්න(හොයාගත්ත කියන්නෙ කලින් අහලවත් නැති වෙන්න ඕන නෑ ඔයා අලුත් ක්‍රමයක් හොයාගත්තා නම් ඒක දාන්න)
     

    rclakmal

    Active member
  • May 8, 2008
    698
    183
    43
    In a galaxy far far away
    Onna apith awa ehenam :P

    elaz macho !!!!! digatama touch eke idapan !!!!

    Machan MADHURA i think u were right in the above matter .First of all i should say that the above code doesn't provide a diamond .Actually in that "yahoo answer " problem ,i saw the output as a a triangle ......and i didnt read the whole question .....(may be an error in loading the web page) then i started coding as an idiot :P and finally copy pasted the question and my answer here..........but only after reading this thread again i saw it was asked to design a DIAMOND output .:D .........and then i tried to achieve that particular output,diamond.!!!! as u have mentioned i couldn't get the real shape !!! The reason should be that .Thanks for your contribution.

    here is the updated code
    Code:
    package diamond;
    import java.util.Scanner;
    
    /**
     *
     * @author lakmal
     */
    class Diamond {
    
        /**
         * @param args the command line arguments
         */
        static int height;
        static char state;
        Diamond(int height){
            for (int i=1;i<=height;i++){
                if (i<=(height+1)/2){
                for (int j=1;j<=i;j++){
                    
                        for (int k=1;k<=((height+1)/2)-i;k++){
                            System.out.print(" ");
                        }
                    
                    System.out.print(" *");
                }
                }
              
                else {
                    for(int j=1;j<=(height+1-i);j++){
                        for (int k=1;k<=i-((height+1)/2);k++){
                        System.out.print(" ");
                    }
                        System.out.print(" *");
    
    
                    }
                }
                
                System.out.println();
        }
        }
        
    
        public static void main(String[] args) {
            // TODO code application logic here
            Scanner input=new Scanner(System.in);
            do{
    
            do{
            System.out.println("Enter your height");
             height=input.nextInt();
            Diamond d1=new Diamond(height);
            } while(height%2!=0);
            System.out.println("Enter a odd number!!");
            System.out.println("Do you want to continue?(y/n)");
            state=input.next().charAt(0);
    
    
    
        } while(state=='y');
    
      }
    }
     
    Last edited:

    ¤--bACarDi--¤

    Well-known member
  • Jan 9, 2009
    12,130
    288
    83
    124.43.xxx.xxx
    ප්‍රශ්න ඉදිරිපත් කරන්න :P
    නැත්නම් ඔයා හොයාගත්ත algorithms, routines මේකෙ පෝස්ට් කරන්න(හොයාගත්ත කියන්නෙ කලින් අහලවත් නැති වෙන්න ඕන නෑ ඔයා අලුත් ක්‍රමයක් හොයාගත්තා නම් ඒක දාන්න)

    denata nam prashna ne. Balamuko issarahata :P
     

    madurax86

    Member
    Jun 29, 2006
    4,385
    88
    0
    The code that draws a diamond [c++]
    this code only draws a proper diamond for odd integers

    Screenshot

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    void gotoxy(int x, int y)
    {
        COORD coord;
        coord.X = x;
        coord.Y = y;
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }
    main(int argc, char *argv[])
    {
        int i,j,k,l;
        cin>>k;
        for (i=0;i<=k;i++)
        {
            for (j=abs((k+1)/2-i)+1;j<=k-abs((k+1)/2-i);j++)
            {    
                gotoxy(j,i+2);
                cout<<"*";
            }    
        }
        cout<<endl;
        system("PAUSE");
    }
    hey lakmal, use the gotoxy function for java it saves alot of time when things associated with drawing
     
    Last edited:

    rclakmal

    Active member
  • May 8, 2008
    698
    183
    43
    In a galaxy far far away
    The code that draws a diamond [c++]
    this code only draws a proper diamond for odd integers

    Screenshot

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    void gotoxy(int x, int y)
    {
        COORD coord;
        coord.X = x;
        coord.Y = y;
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }
    main(int argc, char *argv[])
    {
        int i,j,k,l;
        cin>>k;
        for (i=0;i<=k;i++)
        {
            for (j=abs((k+1)/2-i)+1;j<=k-abs((k+1)/2-i);j++)
            {    
                gotoxy(j,i+2);
                cout<<"*";
            }    
        }
        cout<<endl;
        system("PAUSE");
    }
    hey lakmal, use the gotoxy function for java it saves alot of time when things associated with drawing

    Machan can u please explain a bit about that function ...from line to line ....what each line does ......little bit confused about that !!!!! thanks !!!!!
     

    madurax86

    Member
    Jun 29, 2006
    4,385
    88
    0
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <windows.h> //windows header file, we will be using some functions from this
    
    using namespace std;
    
    void gotoxy(int x, int y)   //a function i took from the internet that will set the cursor at a given point in the console 
    {
        COORD coord;    // COORD is a C,C++ type for 2D points
        coord.X = x;
        coord.Y = y;
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); //this line uses the functions from windows.h (header file) to set the cursor where we need
    }
    main(int argc, char *argv[]) // the main routine that is called when the program is started, the arguments are optional
    {
        int i,j,k,l;
        cin>>k; //read the number of lines that we need to draw a diamond
        for (i=0;i<=k;i++) //count from 0 to k
        {
            for (j=abs((k+1)/2-i)+1;j<=k-abs((k+1)/2-i);j++) //i adjusted this after some serious testing, I'll explain it below it takes alot of space
            {    
                gotoxy(j,i+2); //goto the point j,i+10 in console
                cout<<"*"; // write a "*" at that point
            }    
        }
        cout<<endl;
        system("PAUSE"); //gives 'press any key to continue'
    }

    When it counts form 0 to k that changes the y coordinate of the point where we are going to print the *

    because the for loops are nested
    for example
    Code:
    for (x=0;x<10;x++){
        for (y=0;y<10;y++){
            gotoxy(x,y);
            cout<<"*";
        }
    }
    will print a 10x10 square

    so we have configured what we want to do with the y coordinate it just keeps going down ..but the x coordinate is not like that it should goto where it prints the top * first then in the next line it should go where the second line's first star prints and should print 2 stars instead of one
    abs(q) gives the absolute value of q, |q|
    i have used it to get a series of numbers going up once and then coming down after a number for example
    the graph, y=abs(10-x) or y=|10-x| is like that draw it and see if you want.
    i used that to draw the diamond it sounds complex but actually i didnt go thru much complex stuff i just got the pattern and adjusted it!..
    keep in mind that I did NOT write that code on my 1st try it was after a lot of compilations that I got the code working the way I want :P
     
    Last edited:

    rclakmal

    Active member
  • May 8, 2008
    698
    183
    43
    In a galaxy far far away
    so we have configured what we want to do with the y coordinate it just keeps going down ..but the x coordinate is not like that it should goto where it prints the top * first then in the next line it should go where the second line's first star prints and should print 2 stars instead of one
    abs(q) gives the absolute value of q, |q|
    i have used it to get a series of numbers going up once and then coming down after a number for example
    the graph, y=abs(10-x) or y=|10-x| is like that draw it and see if you want.
    i used that to draw the diamond it sounds complex but actually i didnt go thru much complex stuff i just got the pattern and adjusted it!..
    keep in mind that I did NOT write that code on my 1st try it was after a lot of compilations that I got the code working the way I want
    Thanks macho!!!!!
     

    rclakmal

    Active member
  • May 8, 2008
    698
    183
    43
    In a galaxy far far away
    Hey Guys check out this code !!!! get two points and return the line !! checked for all occurrences !!!! Please tell me if there are any errors !!!!!!

    Code:
    package line;
    import java.util.Scanner;
    
    /**
     *
     * @author lakmal
     */
     class Line {
         Points p1=new Points();
         Points p2=new Points();
         static boolean isContinue=true;
         static Scanner input=new Scanner(System.in);
         static  String op;
    
        
    
         void getLine(Points p1,Points p2){
             System.out.println("Your two points are ");
             System.out.println("("+p1.xCordinate+","+p1.yCordinate+")");
             System.out.println("("+p2.xCordinate+","+p2.yCordinate+")");
             double yCoeff=p2.xCordinate-p1.xCordinate;
             double xCoeff=p2.yCordinate-p1.yCordinate;
             double constant=p1.yCordinate*(p2.xCordinate-p1.xCordinate)-p1.xCordinate*(p2.yCordinate-p1.yCordinate);
             System.out.println("Your Line is:");
             if (yCoeff==0){
                 System.out.println("X="+p2.xCordinate);
             }
             else if (xCoeff==0)
                 System.out.println("Y="+p2.yCordinate);
             else{
             if (constant!=0){
             if ((constant/yCoeff)>0){
                  op = "+";
             }
             else if ((constant/yCoeff)<0){
                    op="";
             }
    
             System.out.println("Y="+xCoeff/yCoeff+"X" + op + (constant)/yCoeff);
         }
             else
                 System.out.println("Y="+ xCoeff/yCoeff+"X");
         }
         }
    
    
         void getPoints(){
             System.out.println("Enter your First Point:");
             System.out.print("X=");
    
             this.p1.xCordinate=input.nextInt();
             System.out.print("Y=");
             this.p1.yCordinate=input.nextInt();
    
             System.out.println("Enter your Second Point:");
             System.out.print("X=");
             this.p2.xCordinate=input.nextInt();
             System.out.print("Y=");
             this.p2.yCordinate=input.nextInt();
             this.getLine(this.p1,this.p2);
         }
    
    
         public static void main(String[] args) {
             do {
    
             Line l1=new Line();
             l1.getPoints();
             
             System.out.println("Do you want to Continuw with anothe line:(y/n)");
             char state=input.next().charAt(0);
             if (state=='n')
                 isContinue=false;
    
             } while (isContinue=true);
    
        }
    }
    
    class Points {
          int xCordinate;
          int yCordinate;
    
    }

    Happy Codings !!!!! For More Java And C++ codes Visit

    http://www.elakiri.com/forum/showthread.php?t=245895