Search
Search titles only
By:
Search titles only
By:
Log in
Register
Search
Search titles only
By:
Search titles only
By:
Menu
Install the app
Install
Forums
New posts
All threads
Latest threads
New posts
Trending threads
Trending
Search forums
What's new
New posts
New ads
New profile posts
Latest activity
Free Ads
Latest reviews
Search ads
Members
Current visitors
New profile posts
Search profile posts
Contact us
Latest ads
Colombo
Red Hat Certified System Administrator (RHCSA) - RHEL 10
Sanjeewani95
Updated:
Friday at 7:43 PM
NURSING , CAREGIVER , HOTEL & BEAUTY COURSES
IVA Para Medical Campus
Updated:
Thursday at 9:24 AM
Handmade Character Soft Toys Peppa Pig Family
anil1961
Updated:
Wednesday at 9:58 PM
Ad icon
Video Content Creator
pramukag
Updated:
Jun 28, 2026
Ad icon
QA Engineer Intern
pramukag
Updated:
Jun 28, 2026
Electronics
Vehicles
Property
Search
Reply to thread
Forums
General
Education
Thread for Programming and Realeted Topics
Get the App
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Message
<blockquote data-quote="madurax86" data-source="post: 5589127" data-attributes="member: 1293"><p>[code]</p><p>#include <cstdlib></p><p>#include <iostream></p><p>#include <windows.h> //windows header file, we will be using some functions from this</p><p></p><p>using namespace std;</p><p></p><p>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 </p><p>{</p><p> COORD coord; // COORD is a C,C++ type for 2D points</p><p> coord.X = x;</p><p> coord.Y = y;</p><p> SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); //this line uses the functions from windows.h (header file) to set the cursor where we need</p><p>}</p><p>main(int argc, char *argv[]) // the main routine that is called when the program is started, the arguments are optional</p><p>{</p><p> int i,j,k,l;</p><p> cin>>k; //read the number of lines that we need to draw a diamond</p><p> for (i=0;i<=k;i++) //count from 0 to k</p><p> {</p><p> 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</p><p> { </p><p> gotoxy(j,i+2); //goto the point j,i+10 in console</p><p> cout<<"*"; // write a "*" at that point</p><p> } </p><p> }</p><p> cout<<endl;</p><p> system("PAUSE"); //gives 'press any key to continue'</p><p>}</p><p>[/code]</p><p></p><p>When it counts form 0 to k that changes the y coordinate of the point where we are going to print the *</p><p></p><p>because the for loops are nested </p><p>for example</p><p>[code]</p><p>for (x=0;x<10;x++){</p><p> for (y=0;y<10;y++){</p><p> gotoxy(x,y);</p><p> cout<<"*";</p><p> }</p><p>}</p><p>[/code]</p><p>will print a 10x10 square </p><p></p><p>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</p><p>abs(q) gives the absolute value of q, |q|</p><p>i have used it to get a series of numbers going up once and then coming down after a number for example </p><p>the graph, y=abs(10-x) or y=|10-x| is like that draw it and see if you want.</p><p>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!..</p><p>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 <img src="/styles/default/xenforo/smilies/default/P.gif" class="smilie" loading="lazy" alt=":P" title=":P :P" data-shortname=":P" /></p></blockquote><p></p>
[QUOTE="madurax86, post: 5589127, member: 1293"] [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' } [/code] 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<<"*"; } } [/code] 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 [/QUOTE]
Insert quotes…
Verification
Hath warak paha keeyada? (hatha wadikireema paha)
Post reply
Top
Bottom