Velocity Reviews > C++ > A begginer simple problem !!!

# A begginer simple problem !!!

Guest
Posts: n/a

 10-20-2006
Dear Friends:

I started learning c/c++ 2 weeks ago and so known a little about c++
!!!
now the problem is that, I found an excercise that I couldn't solve it
in no ways !!!

The problem is that:

make this shape with 2 or 3 "for" Repetition Statement. (It means that
it is possible even if with 2 !):

**********
********
******
****
**
****
******
****
**
****
******
********
**********

I programmed one, but it isn't what I want ! but it's this:

// Assignment 1: Excercise 1: Shape
// Making a strange shape !!!
// Programmed By: Mohammad Dashti

#include <iostream>
using namespace std;

int main()
{
for (int i = 0 ; i <= 12 ; i++){
if (i<=7){
for (int j = 1 ; j <= 10-i ; j++){
if (i<=4 && j<=i) cout << " ";
else if (i<=4 && j>i) cout << "*";
if ((i==5 || i==7) && j==2) cout << " ";
else if ((i==5 || i==7) && j==3) cout << "****";
if ( i==6 && j<=2) cout << " ";
else if (i==6 && j==3) cout << "******";
}
}

if(i>={
for (int j = 11 ; j >= 14-i ; j--){
if (j>=i) cout << " ";
else cout << "*";
}
}

cout << endl;
}
return 0; // indicate successful termination
} // end main

You will glad me, if you send me the solution as soon as possible !

Phlip
Guest
Posts: n/a

 10-20-2006

> make this shape with 2 or 3 "for" Repetition Statement. (It means that
> it is possible even if with 2 !):
>
> **********
> ********
> ******
> ****
> **

Start again, and program just this shape. Note that the left side progresses
from 0 to 4, while the right side descends from 10 to 6. With one loop
statement, how would you make two variables progress like that?

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!

Earl Purple
Guest
Posts: n/a

 10-20-2006

> Dear Friends:
>
> I started learning c/c++ 2 weeks ago and so known a little about c++
> !!!

You are learning C or learning C++ or both, as C++ with "C first" ?

> now the problem is that, I found an excercise that I couldn't solve it
> in no ways !!!
>
> The problem is that:
>
> make this shape with 2 or 3 "for" Repetition Statement. (It means that
> it is possible even if with 2 !):
>
> **********
> ********
> ******
> ****
> **
> ****
> ******
> ****
> **
> ****
> ******
> ********
> **********
>
> I programmed one, but it isn't what I want ! but it's this:
>
> // Assignment 1: Excercise 1: Shape
> // Making a strange shape !!!
> // Programmed By: Mohammad Dashti
>
> #include <iostream>
> using namespace std;
>
> int main()
> {
> for (int i = 0 ; i <= 12 ; i++){
> if (i<=7){
> for (int j = 1 ; j <= 10-i ; j++){
> if (i<=4 && j<=i) cout << " ";
> else if (i<=4 && j>i) cout << "*";
> if ((i==5 || i==7) && j==2) cout << " ";
> else if ((i==5 || i==7) && j==3) cout << "****";
> if ( i==6 && j<=2) cout << " ";
> else if (i==6 && j==3) cout << "******";
> }
> }
>
> if(i>={
> for (int j = 11 ; j >= 14-i ; j--){
> if (j>=i) cout << " ";
> else cout << "*";
> }
> }
>
> cout << endl;
> }
> return 0; // indicate successful termination
> } // end main
>
>
> You will glad me, if you send me the solution as soon as possible !

if we're allowed to create a table:

const int num_stars[] = { 10, 8, 6, 4, 2, 4, 6, 4, 2, 4, 6, 8, 10 };

We can now iterate through that collection, and then through all the
numbers from 0 to 9 with an if statement as to whether to print a star
or a space. The if statement would take the column number and the the
number of stars in that row. You would probably use abs( col*2 - 9 ) <
num_stars[col] to determine this.

something like:

const int num_rows = sizeof( num_stars ) / sizeof( int );
const int num_cols = 10;
for ( int row = 0; row < num_rows; ++ row )
{
for ( int col = 0; col < num_cols; ++col )
{
std::cout << ( abs( col * 2 - 9 ) < num_stars[ col ] ) ? '*' :
' ';
}
std::cout << '\n';
}

In fact you could write it in C just replacing std::cout with putc (or
fputc with stdout)

LR
Guest
Posts: n/a

 10-20-2006

> make this shape with 2 or 3 "for" Repetition Statement. (It means that
> it is possible even if with 2 !):
>
> **********
> ********
> ******
> ****
> **
> ****
> ******
> ****
> **
> ****
> ******
> ********
> **********

Think about how these numbers are related:
-4 5 10
-3 4 8
-2 3 6
-1 2 4
0 1 2
1 2 4
2 3 6

> You will glad me, if you send me the solution as soon as possible !

LR

Jon Rea
Guest
Posts: n/a

 10-23-2006
> Dear Friends:
>
> I started learning c/c++ 2 weeks ago and so known a little about c++
> !!!
> now the problem is that, I found an excercise that I couldn't solve it
> in no ways !!!
>
> The problem is that:
>
> make this shape with 2 or 3 "for" Repetition Statement. (It means that
> it is possible even if with 2 !):
>
> **********
> ********
> ******
> ****
> **
> ****
> ******
> ****
> **
> ****
> ******
> ********
> **********
>
> I programmed one, but it isn't what I want ! but it's this:
>
> // Assignment 1: Excercise 1: Shape
> // Making a strange shape !!!
> // Programmed By: Mohammad Dashti
>
> #include <iostream>
> using namespace std;
>
> int main()
> {
> for (int i = 0 ; i <= 12 ; i++){
> if (i<=7){
> for (int j = 1 ; j <= 10-i ; j++){
> if (i<=4 && j<=i) cout << " ";
> else if (i<=4 && j>i) cout << "*";
> if ((i==5 || i==7) && j==2) cout << " ";
> else if ((i==5 || i==7) && j==3) cout << "****";
> if ( i==6 && j<=2) cout << " ";
> else if (i==6 && j==3) cout << "******";
> }
> }
>
> if(i>={
> for (int j = 11 ; j >= 14-i ; j--){
> if (j>=i) cout << " ";
> else cout << "*";
> }
> }
>
> cout << endl;
> }
> return 0; // indicate successful termination
> } // end main
>
>
> You will glad me, if you send me the solution as soon as possible !
>

This certainly does the job, not sure if its the *best* way though ...

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

void PrintLine(int x,int width)
{
if( x == 0 ) return;
std::cout << std::string((width-x)/2,' ') << std::string(x,'*') <<
std::endl;
}

void DrawHumps( int startWidth, int minima, int centerWidth, int step )
{
bool switchMe = false;
int i;
for (i = startWidth; switchMe ? i <= startWidth : i >= step; switchMe ?
i+=step : i-=step )
{
PrintLine(i,startWidth);
if (i == minima)
{
for (i = minima + step; switchMe ? i >= 2 : i <= centerWidth;
switchMe ? i-=step : i+=step )
{
PrintLine(i,startWidth);
if (i == centerWidth)
{
switchMe = true;
}
}
i+=step;
}
}
return;
}

int _tmain(int argc, _TCHAR* argv[])
{
DrawHumps(10,2,6,2);
}

Wayne Marsh
Guest
Posts: n/a

 10-23-2006
Jon Rea wrote:

> This certainly does the job, not sure if its the *best* way though ...

I love the way you decided to show off...

> int _tmain(int argc, _TCHAR* argv[])
> {
> DrawHumps(10,2,6,2);
> }

....but then showed that you don't know what you're doing here with this.
_tmain? _TCHAR*? Get out.

Cinder6
Guest
Posts: n/a

 10-23-2006
On Oct 20, 2:18 am, "Mohammad" <(E-Mail Removed)> wrote:
> Dear Friends:
>
> I started learning c/c++ 2 weeks ago and so known a little about c++
> !!!
> now the problem is that, I found an excercise that I couldn't solve it
> in no ways !!!
>
> The problem is that:
>
> make this shape with 2 or 3 "for" Repetition Statement. (It means that
> it is possible even if with 2 !):
>
> **********
> ********
> ******
> ****
> **
> ****
> ******
> ****
> **
> ****
> ******
> ********
> **********
>
> I programmed one, but it isn't what I want ! but it's this:

<snip>

I just went ahead and did this myself. Basically, the easiest way to
do it is to look at the problem as three parts: the first (top)
triangle, the (middle) diamond, and the last (bottom) triangle. Write
three loops, one for each of the parts. Instead of using literal
values, use variables for the maximum width and the minimum width (you
can also use one for the starting width). Then you integrate the three
loops into one function. One easy way to have it switch to
decrementing and back to incrementing is to make a 'starsAdded'
variable that has a value of 2 (for putting on more stars), then
multiply it by -1 (for reducing the number of stars on the line).

There are tons of ways to do this. Mine is just one of them (and it's
not the most elegant of them, but whatever). I think it should be
possible to do it with just one for loop and some bools, but the code
might become a bit obfuscated. If you are still stumped after all the
advice you have gotten, just ask, and I will provide my answer to it.
I just didn't want to spoil the fun for you

janusz
Guest
Posts: n/a

 10-24-2006
Cinder6 wrote:
> On Oct 20, 2:18 am, "Mohammad" <(E-Mail Removed)> wrote:
>
>>Dear Friends:
>>
>>I started learning c/c++ 2 weeks ago and so known a little about c++
>>!!!
>>now the problem is that, I found an excercise that I couldn't solve it
>>in no ways !!!
>>
>>The problem is that:
>>
>>make this shape with 2 or 3 "for" Repetition Statement. (It means that
>>it is possible even if with 2 !):
>>
>>**********
>> ********
>> ******
>> ****
>> **
>> ****
>> ******
>> ****
>> **
>> ****
>> ******
>> ********
>>**********
>>
>>I programmed one, but it isn't what I want ! but it's this:

One for-loop is enough (if you _must_ use a for-loop).
<code for use with character set where ' '==32 and '*'==42 >

#include <stdio.h>
#include <stdlib.h>
main(){
int i,j,k;
putchar('\n');
for(i=0;i<130;i++)
{
j=i%10;
k=i/10;
putchar( 32+10*((j>=(4-abs(abs(k-6)-2)))*(j<=(5+abs(abs(k-6)-2)))) );
if(9==j) putchar('\n');
}
return 0;
}

</code>