Thread: print first and print last function

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    1

    Unhappy print first and print last function

    I'm completely new to C-programming. One question i have is...our most recent assignment asks us to modify the Hello program so that it also asks the user what their first and last initials are. It then takes these initials and prints them on the screen. The only problem is, our instructor wantds the program to print the initials in big letters that are made up of little letters. For example...my initials are R.M.. She wants the program to make a big "R" made up of text R's (regular R's are arranged to shape a big R). She says that our program should involve the PrintFirst and PrintLast function. Can anyone give me any tips?
    Thanks, Rawley

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    Well, I'm assuming that she meant to write the PrintFirst and PrintLast functions yourself, because, AFAIK, those aren't standard functions.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    That's an interesting problem that a bunch of people seem to have right now. There are two ways to approach it:

    The first way is to actually calculate the letter you wish to print out and use loops to do so, like so:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX 5
    
    int main ( void )
    {
      int i, j, x;
      for ( i = 1, j = MAX; i < MAX, j > 0; i++, j-- ) {
        for ( x = 0; x <= MAX; x++ ) {
          if ( x == i || x == j )
            printf ( "x" );
          else
            printf ( " " );
        }
        printf ( "\n" );
      }	  
      return EXIT_SUCCESS;
    }
    Another method is to place the required row and column coordinates in an array and use that to loop through and print your letter. This is the easiest way for complex letters and shapes.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define ROWS 5
    #define COLS 5
    
    int a[5][5] = { {3},{2,4},{1,2,3,4,5},{1,5},{1,5} };
    int main ( void )
    {
      int x, y, z, flag = 0;
      for ( x = 0; x < ROWS; x++ ) {
        for ( y = 1; y <= COLS; y++ ) {
          for ( z = 0; z < COLS; z++ ) {
            if ( y == a[x][z] ) {
              flag = 1;
              break;
            }
            else
              flag = 0;
          }
          if ( flag == 1 )
            printf ( "a" );
          else
            printf ( " " );
        }
        printf ( "\n" );
      }
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

  4. #4
    Unregistered
    Guest
    I think he means something like this:

    RRRRRR
    R R
    RRRRRR
    R R
    R R
    R R

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Um, that's what my suggestions do. :P

    -Prelude
    My best code is written with the delete key.

  6. #6
    Unregistered
    Guest
    Prelude, I think your solution is excellent, but they might give him a zero for cheating, because they'd know he copied it from a professional.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Heheh, not really. I was just offering two solutions and a template to start with, our friends' final program would need to have an array for each letter of the alphabet as well as input routines.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed