Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

A begginer simple problem !!!

 
 
Mohammad
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 !

 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      10-20-2006
Mohammad wrote:

> 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!!!


 
Reply With Quote
 
 
 
 
Earl Purple
Guest
Posts: n/a
 
      10-20-2006

Mohammad wrote:
> 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)

 
Reply With Quote
 
LR
Guest
Posts: n/a
 
      10-20-2006
Mohammad wrote:


> 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

 
Reply With Quote
 
Jon Rea
Guest
Posts: n/a
 
      10-23-2006
Mohammad 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:
>
> // 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);
}
 
Reply With Quote
 
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.
 
Reply With Quote
 
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

 
Reply With Quote
 
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>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
begginer question Micha³ S Cisco 5 04-19-2005 05:28 PM
new page - begginer ablyplus ASP .Net 2 03-27-2005 07:46 AM
Begginer question: How do I replace passwords with * in the textbox control Tom ASP .Net 2 11-24-2004 10:15 AM
Begginer Question about TK::filevent dhillon2004 Perl 0 09-03-2004 09:01 PM
Web Application begginer grzybek ASP .Net 2 11-24-2003 08:52 AM



Advertisments