473,385 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

c# alignment question

Hi I am new to c# and working through a problem in a book:

Design and develop a program that prints the following diamond shape.
You may use output statements that print a single asterisk ( * ), a
single space or a single newline character. Maximize your use of
repetition ( with nested loop structures) and minimize the number of
output statements.

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

Ive got so far;

static void Main(string[] args)
{
int count=0, print=0;

while(count<6)
{
print = count + 1;
while(print>1)
{
printstar();
print--;
}
Console.WriteLine(" ");
count++;
}
while(count>0)
{
print = count + 1;
while(print>1)
{
printstar();
print--;
}
Console.WriteLine(" ");
count--;
}
Console.WriteLine(count);
Console.WriteLine(print);
}

static void printstar()
{
Console.Write("*");
}

which produces the following output -

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

I am stuck on producing the diamond shape and aligning all the start
centered in the dos console.

Can some please help me with the code or even just the algorithm to
produce it.

Thanks
Nov 15 '05 #1
2 6702
Hello Brendan,

The first line is five spaces, and one star
the second line is four spaces, and three stars
the third line is three spaces, and five stars
....
the sixth line is zero spaces, and eleven stars

(look for the pattern... do you see it? 5,4,3,2,1,0 and 1,3,5,7,9,11 )

So you need to calculate the second pattern from the first.

there's a loop variable (X) going from (5) to (0) increment by (-1)
inside the loop
perform a loop to print a space character X times
perform a loop to print a star character ((5-X) * 2) + 1 times <--
can you come up with a better function than this?
end loop

That will get your top half of the diamond.

Now, if you look at your code, you've hard-coded three values... the (5),
the (0), and the (-1) in the FOR loop statement

Your assignment is to use loops, right. so, how can you use the code you
have in another loop?

Think about it. The pattern that was decending, is now ascending.

so, enclose the entire function in another loop, one that only executes
twice.
The first time through, use 5, 0, and -1 in the embedded FOR statement
The second time, use 1, 5, and 1 in the embedded FOR statement

I hope this helps,
--- Nick
"Brendan" <br*****@fastmail.com.au> wrote in message
news:7a**************************@posting.google.c om...
Hi I am new to c# and working through a problem in a book:

Design and develop a program that prints the following diamond shape.
You may use output statements that print a single asterisk ( * ), a
single space or a single newline character. Maximize your use of
repetition ( with nested loop structures) and minimize the number of
output statements.

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

Ive got so far;

static void Main(string[] args)
{
int count=0, print=0;

while(count<6)
{
print = count + 1;
while(print>1)
{
printstar();
print--;
}
Console.WriteLine(" ");
count++;
}
while(count>0)
{
print = count + 1;
while(print>1)
{
printstar();
print--;
}
Console.WriteLine(" ");
count--;
}
Console.WriteLine(count);
Console.WriteLine(print);
}

static void printstar()
{
Console.Write("*");
}

which produces the following output -

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

I am stuck on producing the diamond shape and aligning all the start
centered in the dos console.

Can some please help me with the code or even just the algorithm to
produce it.

Thanks

Nov 15 '05 #2
Here is one way that uses lots of lovely loops

static void Main(string[] args)
{
for (int i=5; i>= -5; i--)
{
int absVal = Math.Abs(i);

PrintSpace(40);
PrintSpace(absVal);
PrintStar(1 + (5-absVal)*2);
Console.WriteLine();
}
}
static void PrintStar(int count)
{
for (int i=0; i < count; i++)
{
Console.Write("*");
}
}
static void PrintSpace(int count)
{
for (int i=0; i < count; i++)
{
Console.Write(" ");
}
}

br*****@fastmail.com.au (Brendan) wrote in message news:<7a**************************@posting.google. com>...
Hi I am new to c# and working through a problem in a book:

Design and develop a program that prints the following diamond shape.
You may use output statements that print a single asterisk ( * ), a
single space or a single newline character. Maximize your use of
repetition ( with nested loop structures) and minimize the number of
output statements.

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

Ive got so far;

static void Main(string[] args)
{
int count=0, print=0;

while(count<6)
{
print = count + 1;
while(print>1)
{
printstar();
print--;
}
Console.WriteLine(" ");
count++;
}
while(count>0)
{
print = count + 1;
while(print>1)
{
printstar();
print--;
}
Console.WriteLine(" ");
count--;
}
Console.WriteLine(count);
Console.WriteLine(print);
}

static void printstar()
{
Console.Write("*");
}

which produces the following output -

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

I am stuck on producing the diamond shape and aligning all the start
centered in the dos console.

Can some please help me with the code or even just the algorithm to
produce it.

Thanks

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: J. Campbell | last post by:
I posted a question some time back about accessing a char array as an array of words. In order not to overrun the char array, I padded it with enough 0x00 bytes to ensure that when accessed as...
5
by: Clint Olsen | last post by:
I did a Google about this, and I've seen this covered quite a few times - some leading to useful tricks with offsetof() to discern alignment and others using a big-union of intrinsic types to...
67
by: S.Tobias | last post by:
I would like to check if I understand the following excerpt correctly: 6.2.5#26 (Types): All pointers to structure types shall have the same representation and alignment requirements as each...
17
by: puzzlecracker | last post by:
> 2) >>struct B >>{ >> int i; >>short int si; >> char c; >> char d; >> int j; >>};
5
by: pt | last post by:
Hi, i am wonderng what is faster according to accessing speed to read these data structure from the disk in c/c++ including alignment handling if we access it on little endian system 32 bits...
3
by: Bill Pursell | last post by:
I have a program that does most of its work traversing a bunch of lists. The lists contain a void *, and I spent some time today replacing the void *'s with a copy of the data at the end of the...
10
by: haomiao | last post by:
I want to implement a common list that can cantain any type of data, so I declare the list as (briefly) --------------------------------------- struct list { int data_size; int node_num;...
11
by: Brian Gladman | last post by:
A lot of low level cryptographic code and some hardware cryptographic accelerators either fail completely or perform very poorly if their input, output and/or key storage areas in memory are not...
2
by: somenath | last post by:
Hi All, I have one question regarding the alignment of pointer returned by malloc. In K&R2 page number 186 one union is used to enforce the alignment as mentioned bellow. typedef long...
15
by: Matthew | last post by:
Hi, I'm mainly a coder, PHP at the moment, but from time to time need to design and use some css. I've a css text alignment issue. Mostly to align text neatly in the past I've used tables....
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.