Solved

Trying to do some tricky char output

Posted on 2003-11-03
9
229 Views
Last Modified: 2010-04-02
I'm doing a problem from a c++ book and am lost and clueless.... the jist of the problem is that the user enters a char from the keyboard then it prints the letters out in a triangle starting from the inside&last row first, for instance if the char was d the out put would look like
      A
    ABA
  ABCBA
ABCDCBA

I'v figured that that a for loop with 2 nested loops (one for spacing, and one for lines) are needed, at least I think........
if anyone could point me in the right direction on how to implement them, and get the output

thanks for the help.......

-orlando-
0
Comment
Question by:Orlando15767
  • 4
  • 4
9 Comments
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9676193
This sounds like homework, so here's a few hints:
Yes, two nested loops seem sufficient. The outer should be used to control the "row" of the triangle (e.g. if the letter is 'D' how many rows are there? how about 'E'? what's the relationship here?)

The inner loop is used to displayed each row. Notice that you can start from 'A' going to 'D' and coming back to A, so something like:
    Say A + n is the letter (in case letter is 'D' n is 3)
(A) (A+1) (A+2) ... (A + (n - 1)) (A+n) (A + (n - 1)) ... (A + 2) (A + 1) (A)
Everything revolves around n, pretty much. Remember that the center letter varies for each row (so does n), and this is what your outer loop should be keeping track off.
0
 
LVL 3

Assisted Solution

by:freewell
freewell earned 20 total points
ID: 9676420
int i,j,nDepth = 4;
char ch = 'A';

for(i=0;i<nDepth;i++)
{
      for(j=1;j<nDepth-i;j++)
            printf(" ");
      for(j=ch;j<=ch+i;j++)
            printf("%c",j);
      for(j=ch+i-1;j>=ch;j--)
            printf("%c",j);

      printf("\r\n");
}
0
 
LVL 1

Author Comment

by:Orlando15767
ID: 9679444
how can you figure out what the position of the alphabet the letters are in (d=5,E=6) to figure out the number of lines and rows?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 4

Expert Comment

by:n_fortynine
ID: 9680130
Well, the simplest way to find that out:
ch = toupper(ch); //make everything to upper case (or to lower and then subtract 'a'.
int pos = ch - 'A';
0
 
LVL 1

Author Comment

by:Orlando15767
ID: 9690726
Below is the code that I came up with( I put the '-' in to see where a space would go.......
there is a few problems with it though, the output is all on one line, and I can't get the letters to decrease.....  any suggestions?

this is what the out put looks like...... '- - - DDD - - DD - D'

 int i,j;
      char letter;

      cout << "Enter a char to start the triangle: "; cin >> letter;

      letter = toupper(letter);

      int pos = (letter - 'A')+1;

      cout << letter << "'s position in the alphebet is: " << pos << endl;
      cout << "\n" << endl;

      for ( ; pos >= 1; pos--)
      {
      for (i=1; i<pos; i++)
            cout << "-";
      for (j=1; j<pos; j++)
            cout << letter;
      }

thanks for the help
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9690895
I'm not going to write any code here, since freewell has already given you some code that would work for you (sigh). I'll just explain what he did in case you didn't understand it. (If you use his code please give him credit for it)

for(i=0;i<nDepth;i++)
{
     for(j=1;j<nDepth-i;j++) //Display the preceding spaces here.
          printf(" ");                //Notice that you would go to nDepth - 1, meaning this limit would change every time.
                                         //and reaches 0 on the last row.
     for(j=ch;j<=ch+i;j++)    //Printing the first segment of the row: A, AB, ABC etc. The limit would also change.
          printf("%c",j);
     for(j=ch+i-1;j>=ch;j--)  //Printing the remaining segment of the row.
          printf("%c",j);

     printf("\r\n");
}

You could replace printf() with the corresponding C++ syntax cout.
0
 
LVL 1

Author Comment

by:Orlando15767
ID: 9690955
I tried his code and it doesn't work how I thought it would, and wound't use his anyway... I want to work it though myself....
such as his code doesn't work if you change the ch to 'z'
in his code what does "%c" mean and such......


thanks for all the help with this....
0
 
LVL 4

Accepted Solution

by:
n_fortynine earned 105 total points
ID: 9691051
well, the thing is you need to calculate nDepth the way you did, i.e. if the letter is 'D' nDepth is 4 etc.
for all his C syntax:
printf(" "); is equivalent to cout << ' ';
printf("%c",j); is equivalent to cout << j; ("%c" saying this variable is of type char)
printf("\r\n"); is equivalent to cout << endl;

If you want to work it through by yourself, well, you can still break the task into 3 (or 2 if you try to compress the last 2) smaller portions (per row):
1. Display the preceding spaces.
2. Display the first half segment.
3. Display the rest.

Have fun.
0
 
LVL 1

Author Comment

by:Orlando15767
ID: 9692154
this is what I'v got so from peicing this together. the thing is it is outputing numbers instead of letters
thanks for all the help


int i,j;
char ch;

      cout << "Enter a char to start the triangle: "; cin >> ch;

      ch = toupper(ch);

      int pos = (ch - 'A')+1;

for(i=0;i<pos;i++)
{
    for(j=1;j<pos-i;j++)
         cout << "-";
    for(j=ch;j<=ch+i;j++)
      {
            //char j;
            cout << j;
      }
    for(j=ch+i-1;j>=ch;j--)
      {
            //char j;
            cout << j;
      }
    cout << endl;
}
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem with SqlConnection 4 178
Copy output image from TWindowsMediaPlayer 6 48
No module found pypyodbc, 3 33
Need some help with mailto 16 21
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question