Question

In: Computer Science

Write a C++ Program to print the first letter of your first and last name using...

Write a C++ Program to print the first letter of your first and last name using stars.
Note:
1) Using nested For Loop
2) The number of lines is given by user.
3) Using one Outer loop to print your letters.
4) Print the letters beside each other

Solutions

Expert Solution

//Assuming that First and last letter of your name are M and K


#include <iostream>
using namespace std;

int abs(int d)
{
   return d < 0 ? -1 * d : d;
}

// Function to print the pattern of 'A'
void printA(int height,int width)
{
   int n = width / 2, i, j;
   for (i = 0; i < height; i++) {
       for (j = 0; j <= width; j++) {
           if (j == n || j == (width - n)
               || (i == height / 2 && j > n
                   && j < (width - n)))
               cout<<"*";
           else
               cout<<" ";
       }
       cout<<endl;
       n--;
   }
}

// Function to print the pattern of 'B'
void printB(int height,int width)
{
   int i, j, half = (height / 2);
   for (i = 0; i < height; i++) {
       cout<<"*";
       for (j = 0; j < width; j++) {
           if ((i == 0 || i == height - 1 || i == half)
               && j < (width - 2))
               cout<<"*";
           else if (j == (width - 2)
                   && !(i == 0 || i == height - 1
                       || i == half))
               cout<<"*";
           else
               cout<<" ";
       }
       cout<<endl;
   }
}

// Function to print the pattern of 'C'
void printC(int height,int width)
{
   int i, j;
   for (i = 0; i < height; i++) {
       cout<<"*";
       for (j = 0; j < (height - 1); j++) {
           if (i == 0 || i == height - 1)
               cout<<"*";
           else
               continue;
       }
       cout<<endl;
   }
}

// Function to print the pattern of 'D'
void printD(int height,int width)
{
   int i, j;
   for (i = 0; i < height; i++) {
       cout<<"*";
       for (j = 0; j < height; j++) {
           if ((i == 0 || i == height - 1)
               && j < height - 1)
               cout<<"*";
           else if (j == height - 1 && i != 0
                   && i != height - 1)
               cout<<"*";
           else
               cout<<" ";
       }
       cout<<endl;
   }
}

// Function to print the pattern of 'E'
void printE(int height,int width)
{
   int i, j;
   for (i = 0; i < height; i++) {
       cout<<"*";
       for (j = 0; j < height; j++) {
           if ((i == 0 || i == height - 1)
               || (i == height / 2
                   && j <= height / 2))
               cout<<"*";
           else
               continue;
       }
       cout<<endl;
   }
}

// Function to print the pattern of 'F'
void printF(int height,int width)
{
   int i, j;
   for (i = 0; i < height; i++) {
       cout<<"*";
       for (j = 0; j < height; j++) {
           if ((i == 0) || (i == height / 2
                           && j <= height / 2))
               cout<<"*";
           else
               continue;
       }
       cout<<endl;
   }
}

// Function to print the pattern of 'G'
void printG(int height,int width)
{
   int i, j;
   width--;
   for (i = 0; i < height; i++) {
       for (j = 0; j < width; j++) {
           if ((i == 0 || i == height - 1)
               && (j == 0 || j == width - 1))
               cout<<" ";
           else if (j == 0)
               cout<<"*";
           else if (i == 0 && j <= height)
               cout<<"*";
           else if (i == height / 2
                   && j > height / 2)
               cout<<"*";
           else if (i > height / 2
                   && j == width - 1)
               cout<<"*";
           else if (i == height - 1
                   && j < width)
               cout<<"*";
           else
               cout<<" ";
       }
       cout<<endl;
   }
}

// Function to print the pattern of 'H'
void printH(int height,int width)
{
   int i, j;
   for (i = 0; i < height; i++) {
       cout<<"*";
       for (j = 0; j < height; j++) {
           if ((j == height - 1)
               || (i == height / 2))
               cout<<"*";
           else
               cout<<" ";
       }
       cout<<endl;
   }
}

// Function to print the pattern of 'I'
void printI(int height,int width)
{
   int i, j;
   for (i = 0; i < height; i++) {
       for (j = 0; j < height; j++) {
           if (i == 0 || i == height - 1)
               cout<<"*";
           else if (j == height / 2)
               cout<<"*";
           else
               cout<<" ";
       }
       cout<<endl;
   }
}

// Function to print the pattern of 'J'
void printJ(int height,int width)
{
   int i, j;
   for (i = 0; i < height; i++) {
       for (j = 0; j < height; j++) {
           if (i == height - 1 && (j > 0
                                   && j < height - 1))
               cout<<"*";
           else if ((j == height - 1
                   && i != height - 1)
                   || (i > (height / 2) - 1
                       && j == 0 && i != height - 1))
               cout<<"*";
           else
               cout<<" ";
       }
       cout<<endl;
   }
}

// Function to print the pattern of 'K'
void printK(int height,int width)
{
   int i, j, half = height / 2, dummy = half;
   for (i = 0; i < height; i++) {
       cout<<"*";
       for (j = 0; j <= half; j++) {
           if (j == abs(dummy))
               cout<<"*";
           else
               cout<<" ";
       }
       cout<<endl;
       dummy--;
   }
}

// Function to print the pattern of 'L'
void printL(int height,int width)
{
   int i, j;
   for (i = 0; i < height; i++) {
       cout<<"*";
       for (j = 0; j <= height; j++) {
           if (i == height - 1)
               cout<<"*";
           else
               cout<<" ";
       }
       cout<<endl;
   }
}

// Function to print the pattern of 'M'
void printM(int height,int width)
{
   int i, j, counter = 0;
   for (i = 0; i < height; i++) {
       cout<<"*";
       for (j = 0; j <= height; j++) {
           if (j == height)
               cout<<"*";
           else if (j == counter
                   || j == height - counter - 1)
               cout<<"*";
           else
               cout<<" ";
       }
       if (counter == height / 2) {
           counter = -99999;
       }
       else
           counter++;
       cout<<endl;
   }
}

// Function to print the pattern of 'N'
void printN(int height,int width)
{
   int i, j, counter = 0;
   for (i = 0; i < height; i++) {
       cout<<"*";
       for (j = 0; j <= height; j++) {
           if (j == height)
               cout<<"*";
           else if (j == counter)
               cout<<"*";
           else
               cout<<" ";
       }
       counter++;
       cout<<endl;
   }
}

// Function to print the pattern of 'O'
void printO(int height,int width)
{
   int i, j, space = (height / 3);
   width = height / 2 + height / 5 + space + space;
   for (i = 0; i < height; i++) {
       for (j = 0; j <= width; j++) {
           if (j == width - abs(space)
               || j == abs(space))
               cout<<"*";
           else if ((i == 0
                   || i == height - 1)
                   && j > abs(space)
                   && j < width - abs(space))
               cout<<"*";
           else
               cout<<" ";
       }
       if (space != 0
           && i < height / 2) {
           space--;
       }
       else if (i >= (height / 2 + height / 5))
           space--;
       cout<<endl;
   }
}

// Function to print the pattern of 'P'
void printP(int height,int width)
{
   int i, j;
   for (i = 0; i < height; i++) {
       cout<<"*";
       for (j = 0; j < height; j++) {
           if ((i == 0 || i == height / 2)
               && j < height - 1)
               cout<<"*";
           else if (i < height / 2
                   && j == height - 1 && i != 0)
               cout<<"*";
           else
               cout<<" ";
       }
       cout<<endl;
   }
}

// Function to print the pattern of 'Q'
void printQ(int height,int width)
{
   printO(height,width);
   int i, j, d = height;
   for (i = 0; i < height / 2; i++) {
       for (j = 0; j <= d; j++) {
           if (j == d)
               cout<<"*";
           else
               cout<<" ";
       }
       cout<<endl;
       d++;
   }
}

// Function to print the pattern of 'R'
void printR(int height,int width)
{
   int i, j, half = (height / 2);
   for (i = 0; i < height; i++) {
       cout<<"*";
       for (j = 0; j < width; j++) {
           if ((i == 0 || i == half)
               && j < (width - 2))
               cout<<"*";
           else if (j == (width - 2)
                   && !(i == 0 || i == half))
               cout<<"*";
           else
               cout<<" ";
       }
       cout<<endl;
   }
}

// Function to print the pattern of 'S'
void printS(int height,int width)
{
   int i, j;
   for (i = 0; i < height; i++) {
       for (j = 0; j < height; j++) {
           if ((i == 0 || i == height / 2
               || i == height - 1))
               cout<<"*";
           else if (i < height / 2
                   && j == 0)
               cout<<"*";
           else if (i > height / 2
                   && j == height - 1)
               cout<<"*";
           else
               cout<<" ";
       }
       cout<<endl;
   }
}

// Function to print the pattern of 'T'
void printT(int height,int width)
{
   int i, j;
   for (i = 0; i < height; i++) {
       for (j = 0; j < height; j++) {
           if (i == 0)
               cout<<"*";
           else if (j == height / 2)
               cout<<"*";
           else
               cout<<" ";
       }
       cout<<endl;
   }
}

// Function to print the pattern of 'U'
void printU(int height,int width)
{
   int i, j;
   for (i = 0; i < height; i++) {
       if (i != 0 && i != height - 1)
           cout<<"*";
       else
           cout<<" ";
       for (j = 0; j < height; j++) {
           if (((i == height - 1)
               && j >= 0
               && j < height - 1))
               cout<<"*";
           else if (j == height - 1 && i != 0
                   && i != height - 1)
               cout<<"*";
           else
               cout<<" ";
       }
       cout<<endl;
   }
}

// Function to print the pattern of 'V'
void printV(int height,int width)
{
   int i, j, counter = 0;
   for (i = 0; i < height; i++) {
       for (j = 0; j <= width; j++) {
           if (j == counter
               || j == width - counter - 1)
               cout<<"*";
           else
               cout<<" ";
       }
       counter++;
       cout<<endl;
   }
}

// Function to print the pattern of 'W'
void printW(int height,int width)
{
   int i, j, counter = height / 2;
   for (i = 0; i < height; i++) {
       cout<<"*";
       for (j = 0; j <= height; j++) {
           if (j == height)
               cout<<"*";
           else if ((i >= height / 2)
                   && (j == counter
                       || j == height - counter - 1))
               cout<<"*";
           else
               cout<<" ";
       }
       if (i >= height / 2) {
           counter++;
       }
       cout<<endl;
   }
}

// Function to print the pattern of 'X'
void printX(int height,int width)
{
   int i, j, counter = 0;
   for (i = 0; i <= height; i++) {
       for (j = 0; j <= height; j++) {
           if (j == counter
               || j == height - counter)
               cout<<"*";
           else
               cout<<" ";
       }
       counter++;
       cout<<endl;
   }
}

// Function to print the pattern of 'Y'
void printY(int height,int width)
{
   int i, j, counter = 0;
   for (i = 0; i < height; i++) {
       for (j = 0; j <= height; j++) {
           if (j == counter
               || j == height - counter
                   && i <= height / 2)
               cout<<"*";
           else
               cout<<" ";
       }
       cout<<endl;
       if (i < height / 2)
           counter++;
   }
}

// Function to print the pattern of 'Z'
void printZ(int height,int width)
{
   int i, j, counter = height - 1;
   for (i = 0; i < height; i++) {
       for (j = 0; j < height; j++) {
           if (i == 0 || i == height - 1
               || j == counter)
               cout<<"*";
           else
               cout<<" ";
       }
       counter--;
       cout<<endl;
   }
}

// Function print the pattern of the
// alphabets from A to Z
void printPattern(char character,int height,int width)
{
   switch (character) {
   case 'A':
       printA(height,width);
       break;
   case 'B':
       printB(height,width);
       break;
   case 'C':
       printC(height,width);
       break;
   case 'D':
       printD(height,width);
       break;
   case 'E':
       printE(height,width);
       break;
   case 'F':
       printF(height,width);
       break;
   case 'G':
       printG(height,width);
       break;
   case 'H':
       printH(height,width);
       break;
   case 'I':
       printI(height,width);
       break;
   case 'J':
       printJ(height,width);
       break;
   case 'K':
       printK(height,width);
       break;
   case 'L':
       printL(height,width);
       break;
   case 'M':
       printM(height,width);
       break;
   case 'N':
       printN(height,width);
       break;
   case 'O':
       printO(height,width);
       break;
   case 'P':
       printP(height,width);
       break;
   case 'Q':
       printQ(height,width);
       break;
   case 'R':
       printR(height,width);
       break;
   case 'S':
       printS(height,width);
       break;
   case 'T':
       printT(height,width);
       break;
   case 'U':
       printU(height,width);
       break;
   case 'V':
       printV(height,width);
       break;
   case 'W':
       printW(height,width);
       break;
   case 'X':
       printX(height,width);
       break;
   case 'Y':
       printY(height,width);
       break;
   case 'Z':
       printZ(height,width);
       break;
   }
}

// Driver Code
int main()
{
    int height;
   char first_name_initial = 'M';
   char second_name_initial = 'K';
   cout<<"Enter number of lines\n";
   cin>>height;
   int width = (2 * height) - 1;
   printPattern(first_name_initial,height,width);

   printPattern(second_name_initial,height,width);
   return 0;
}


Related Solutions

Write a C++ Program to print the first letter of your first and last name using...
Write a C++ Program to print the first letter of your first and last name using stars. Note: 1) Using nested For Loop 2) The number of lines is given by user. 3) Using one Outer loop to print your letters. 4) Print the letters beside each other.
Using jGRASP, write a Java program named LastnameFirstname10.java, using your last name and your first name,...
Using jGRASP, write a Java program named LastnameFirstname10.java, using your last name and your first name, that does the following: Create two arrays that will hold related information. You can choose any information to store, but here are some examples: an array that holds a person's name and an array that hold's their phone number an array that holds a pet's name and an array that holds what type of animal that pet is an array that holds a student's...
Using jGRASP, write a Java program named LastnameFirstname09.java, using your last name and your first name,...
Using jGRASP, write a Java program named LastnameFirstname09.java, using your last name and your first name, that does the following: Declare an array reference variable called myFavoriteSnacks for an array of String type. Create the array so that it is able to hold 10 elements - no more, no less. Fill the array by having each array element contain a string stating one of your favorite foods/snacks. Note: Only write the name of the snack, NO numbers (i.e. Do not...
Using jGRASP, write a Java program named LastnameFirstname11.java, using your last name and your first name,...
Using jGRASP, write a Java program named LastnameFirstname11.java, using your last name and your first name, that does the following: Defines a method called generateNums. This method will create an array of size x, fill it with random numbers between y and z, then return the array. When you call the method, you must specify the size of the array (x), the beginning of your random number range (y), and the ending of your random number range (z). Defines a...
C++ Write a program that asks a teacher to input a student’s first name, last name,...
C++ Write a program that asks a teacher to input a student’s first name, last name, and four test scores. The program should find the average of the four test scores and should then write the following information to a file named “students.txt” last_name first_name average A student's first name of “XX” should be used as a sentinel value and no numeric grades less than 0 or greater than 100 should be accepted.  The program should then read the information in...
A.Write a program that prompts for and reads the user’s first and last name (separately).Then print...
A.Write a program that prompts for and reads the user’s first and last name (separately).Then print the initials with the capital letters. (Note that we do not expect that inputs from users are case-sensitive. For example, if the user’s input of the first name is robert and of the last name is SMITH, you should print R.S.) B.Write a program that creates and prints a random phone number of the form XXX-XXX-XXXX. Include the dashed in the output. Do not...
Select a Fortune 500 company whose name starts with the first letter in your last name...
Select a Fortune 500 company whose name starts with the first letter in your last name and obtain the company’s most recent 10-K report. (You may use the same company that you used previously.) Be sure that the company has a complex capital structure. Attach the equity section of the balance sheet and the notes relevant to answering the following questions: (a) Does the company have preferred stock and common stock (or different classes of common stock)? Do its stocks...
Let M be the integer corresponding to the first letter of your last name. For example,...
Let M be the integer corresponding to the first letter of your last name. For example, if your last name begins with "A", M=1 and if your last name begins with "Z", M=26. Let k=1/M and consider the logistic equation dy/dt = k y (M - y). Construct a single figure including Title "Logistic Equation Slope Field and Euler's Method solutions by FirstName LastName" with your actual first and last names, along the top Labels for the axes a slope...
Let M be the integer corresponding to the first letter of your last name. For example,...
Let M be the integer corresponding to the first letter of your last name. For example, if your last name begins with "A", M=1 and if your last name begins with "Z", M=26. Let k=1/M and consider the logistic equation dy/dt = k y (M - y). Construct a single figure including Title "Logistic Equation Slope Field and Euler's Method solutions by FirstName LastName" with your actual first and last names, along the top Labels for the axes a slope...
Using c++, write a program that will display your name as a void function then will...
Using c++, write a program that will display your name as a void function then will perform the following by user-defined functions: a. to compute for the sum of two numbers (n1, n2) using function.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT