HI! new here. I posted this question in another forum but some guy got super angry for me even asking any questions so i figured i'll try another forums. I'll try to make this explanation short.
I have a programming class, one of the work assigned to us was to make a diamond using cout statments.
// diamond.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout << " * " << endl;
cout << " *** " << endl;
cout << " ***** " << endl;
cout << " ******* " << endl;
cout << "*********" << endl;
cout << " ******* " << endl;
cout << " ***** " << endl;
cout << " *** " << endl;
cout << " * " << endl;
return 0;
}
so i made this.
for extra credit, he asked us to create a new Diamond class that uses Loops to create the diamond pattern using no more than two (2) cout statements.
i been trying to do this for weeks and this is what i've gotten.
int _tmain(int argc, _TCHAR* argv[])
{
for(int i = 0; i < 7; i++){
string Display;
cout << i << "*";
do{ cout << Display;
} while(false);
}
return 0;
}
I think i'm on the right track but some input would be nice. am i doing it right? am i doing it wrong? any tips as to what to do next? (kinda stumped)
this is c++, using visual c++ 2008 express edition to write/compile the code
14 replies to this topic
#1
Posted 26 May 2010 - 01:30 AM
|
|
|
#2
Posted 26 May 2010 - 06:37 AM
If I told you to draw a triangle, would you be able to implement it using only one statement including cout?
#3
Posted 26 May 2010 - 09:13 AM
bobdark said:
If I told you to draw a triangle, would you be able to implement it using only one statement including cout?
you mean something like ...?
cout
<< " * \n"
<<" *** \n"
<<"******\n"
#4
Posted 26 May 2010 - 09:38 AM
What he means is that you will need two nested for-loops... one to count the rows and one for columns... I hope that makes sense...
for example...
your first part that prints out
*
**
***
****
*****
Will be something like this:
for(int j = 0; j <= 4 ; j++)
{
for(int i = 0; i <= j; i++)
{
cout << "*";
}
cout<<endl;
}
You can do the rest yourself... ^_^
Hope this helps... However if you need more help, you know where to find us...
for example...
your first part that prints out
*
**
***
****
*****
Will be something like this:
for(int j = 0; j <= 4 ; j++)
{
for(int i = 0; i <= j; i++)
{
cout << "*";
}
cout<<endl;
}
You can do the rest yourself... ^_^
Hope this helps... However if you need more help, you know where to find us...
#5
Posted 26 May 2010 - 12:35 PM
Thank you! now i need yo try and center this and loops it so the bottom repeats the same thing but backwards (if that made sense)
i'll post the code here once i figure it out, thanks again
i'll post the code here once i figure it out, thanks again
#6
Posted 05 June 2010 - 09:52 PM
// Diamond.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
class Diamond
{
private:
int m_nMaxStars;
public:
Diamond(int nMaxStars = 9)
{
if ((nMaxStars % 2) == 0)
nMaxStars += 1;
m_nMaxStars = nMaxStars;
}
virtual ~Diamond() {}
void Draw()
{
for (int i = 1; i <= m_nMaxStars; i += 2)
{
string strLine = "";
for (int j = 0; j < ((m_nMaxStars - i) / 2); j++)
strLine += " ";
for (int j = 0; j < i; j++)
strLine += "*";
DrawLine(strLine);
}
for (int i = (m_nMaxStars - 2); i > 0; i -= 2)
{
string strLine = "";
for (int j = 0; j < ((m_nMaxStars - i) / 2); j++)
strLine += " ";
for (int j = 0; j < i; j++)
strLine += "*";
DrawLine(strLine);
}
}
private:
void DrawLine(string strLine)
{
cout << strLine.c_str() << endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Diamond theDiamond;
theDiamond.Draw();
cin.get();
return 0;
}
//
#include "stdafx.h"
#include <iostream>
using namespace std;
class Diamond
{
private:
int m_nMaxStars;
public:
Diamond(int nMaxStars = 9)
{
if ((nMaxStars % 2) == 0)
nMaxStars += 1;
m_nMaxStars = nMaxStars;
}
virtual ~Diamond() {}
void Draw()
{
for (int i = 1; i <= m_nMaxStars; i += 2)
{
string strLine = "";
for (int j = 0; j < ((m_nMaxStars - i) / 2); j++)
strLine += " ";
for (int j = 0; j < i; j++)
strLine += "*";
DrawLine(strLine);
}
for (int i = (m_nMaxStars - 2); i > 0; i -= 2)
{
string strLine = "";
for (int j = 0; j < ((m_nMaxStars - i) / 2); j++)
strLine += " ";
for (int j = 0; j < i; j++)
strLine += "*";
DrawLine(strLine);
}
}
private:
void DrawLine(string strLine)
{
cout << strLine.c_str() << endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Diamond theDiamond;
theDiamond.Draw();
cin.get();
return 0;
}
#7
Posted 06 June 2010 - 01:20 AM
Another easy way to do it is:
static const int MAXROW = 11;
static const int MAXCOL = 11;
int diamond[MAXROW][MAXCOL] = {
{0,0,0,0,0,1,0,0,0,0,2},
{0,0,0,0,1,1,1,0,0,0,2},
{0,0,0,1,1,1,1,1,0,0,2},
{0,0,1,1,1,1,1,1,1,0,2},
{0,1,1,1,1,1,1,1,1,1,2},
{0,1,1,1,1,1,1,1,1,1,2},
{0,1,1,1,1,1,1,1,1,1,2},
{0,0,1,1,1,1,1,1,1,0,2},
{0,0,0,1,1,1,1,1,0,0,2},
{0,0,0,0,1,1,1,0,0,0,2},
{0,0,0,0,0,1,0,0,0,0,2},
};
for (int row = 0; row < MAXROW; row++)
{
for (int col = 0; col < MAXCOL; col++)
{
if (diamond[row][col] == 0)
cout << " ";
if (diamond[row][col] == 1)
cout << "*";
if (diamond[row][col] == 2)
cout << "\n";
}
}
#8
Posted 06 June 2010 - 10:14 AM
I love these old school programming assignments!
Few minor nits with your program...
1. You don't need a destructor. And certainly not a virtual one at that.
2. The idea is to follow the boundaries of the star. To walk you through my example:
a. Suppose we have a maximum of 9 stars, and the inner k loop is the horizontal printing loop. Initially, we want to print a star when its index is between 4 and 6 (stars/2 and stars/2 + 2)
b. Those boundaries increase and decrease respectively as we loop vertically (the j loop).
c. We want to do this twice (i loop). The first pass we decrease the lower boundary, and increase the upper boundary.
d. On the second i loop pass, we increase the lower boundary, and decrease the upper.
For any ODD number of stars...
Few minor nits with your program...
1. You don't need a destructor. And certainly not a virtual one at that.
2. The idea is to follow the boundaries of the star. To walk you through my example:
a. Suppose we have a maximum of 9 stars, and the inner k loop is the horizontal printing loop. Initially, we want to print a star when its index is between 4 and 6 (stars/2 and stars/2 + 2)
b. Those boundaries increase and decrease respectively as we loop vertically (the j loop).
c. We want to do this twice (i loop). The first pass we decrease the lower boundary, and increase the upper boundary.
d. On the second i loop pass, we increase the lower boundary, and decrease the upper.
For any ODD number of stars...
#include <iostream>
class Diamond
{
int stars;
public:
Diamond(int stars);
void print();
};
Diamond::Diamond(int stars)
: stars(stars) {}
void Diamond::print()
{
int min = stars/2;
int max = min + 2;
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < stars/2 + i; j++)
{
for(int k = 1; k < stars + 1; k++)
{
if( k < max && k > min )
{
std::cout << "*";
}
else std::cout << " ";
}
std::cout << "\n";
if( i == 0 ) min--; else min++;
if( i == 0 ) max++; else max--;
}
}
}
int main()
{
Diamond diamond(21);
diamond.print();
return 0;
}
Edited by l@mbd@, 06 June 2010 - 10:30 AM.
none of your business
#9
Posted 14 June 2010 - 02:35 PM
Thanks guys!
#10
Posted 15 June 2010 - 03:03 AM
Blah. Classes? Arrays? Nested loops? Variables? Sooo complicated and error prone!
I love these old school programming assignments. If the students only knew how simple and short the answers could be, if they used the right tool for the job.
I love these old school programming assignments. If the students only knew how simple and short the answers could be, if they used the right tool for the job.
def repeat(n: Int)(code: => Unit) =
1 to n foreach {_ => code}
def diamond(n: Int) =
for (i <- -n to n) {
val m = math.abs(i)
repeat(m)(print(" "))
repeat((n - m) * 2 + 1)(print("*"))
println
}
Results:
scala> diamond(5)
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
Edited by JCoder, 15 June 2010 - 11:17 PM.
#11
Posted 15 June 2010 - 06:04 AM
#include <stdio.h>
// draw a diamond on the console
//
// first thing we need to know is the half height on the diamond - hh
// to be able to place our diamond freely, there is an optional displacement - d
void diamond (unsigned hh, unsigned d=0, char c='*');
// A diamond consists of lines. Each line is n spaces follow by m stars
// where n and m is dependent on the index of the line
//
void line(unsigned n, unsigned m, char c)
{
while(n--!=0)
printf(" ");
while(m--!=0)
printf("%c",c);
printf("\n");
}
void diamond(unsigned hh, unsigned d, char c)
{
unsigned i;
for(i=0; i<hh; ++i)
line(d+hh-i,2*i+1, c);
while(--i!=0)
line(d+hh-i+1,2*i-1, c);
}
int main(int argc, char* argv[])
{
diamond(6);
diamond(7,10);
diamond(5, 3, '$');
return 0;
}
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
*
***
*****
*******
*********
***********
*************
***********
*********
*******
*****
***
*
$
$$$
$$$$$
$$$$$$$
$$$$$$$$$
$$$$$$$
$$$$$
$$$
$
#12
Posted 15 June 2010 - 11:19 PM
@Lance: the best C version so far. :) However, still over 50% longer than mine and uses more than one statement per line.
If you used abs, you could make your version shorter.
If you used abs, you could make your version shorter.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









