Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Write a Program to print the Alternate Pattern in * and #.

Input as: One Line Containing an Integer,N.

Output as: An Alternate pattern of '*' and '#' patterns to be Printed.


Example:

Input: Output

1 *

5 *####
###**
***##
###**
*####

8 *#######
######**
***#####
####****
****####
#####***
**######
#######*

What I have tried:

I did Tried to solve it but Still I am Unsuccessful in deciphering how the code of # and * propagates over.

So, Help me if you can as how should i Understand it!

Thanks.

Sorry for the Blunt Question.
Posted
Updated 25-Nov-22 23:53pm
v3
Comments
Richard MacCutchan 12-Nov-16 9:24am    
Think abot how the different groups are calculated from the numbers. For example, input number 5 gives: 1,2,3,2,1 asterisks and 4,3,2,3,4 hashes. Ans each group of characters alternates whether at the beginning or end of the string. You should write these down on paper and then write down what other values you need to get the strings printed. It is less difficult than it looks.
[no name] 12-Nov-16 13:48pm    
"Write a Program ", okay done.

You need to start by prompting the user and reading a number from him -> N
Then, you need to print that number of lines.
Before the loop, start with a "stars count" of 1, and a "hashes count" of N - 1
inside the loop, check if the line number is odd or even (Hint: AND it with one. If that's zero, it's even)
Odd: Print "stars count" stars, then "hashes count" hashes.
Even: Print Odd: Print "hashes count" hashes, then "stars count" stars
Increment "stars count",and decrement "hashes count"
Go round for the next line.

But this is your homework, so no code!
 
Share this answer
 
Comments
[no name] 12-Nov-16 10:28am    
That's not my homework.
I got it while solving an online quiz code and I couldn't do it.

For God's sake, Why every one keep pushing at Homework Phrase!
OriginalGriff 12-Nov-16 10:32am    
"Why every one keep pushing at Homework Phrase!"

1) Because it looks like homework?
2) Because a student in his second week of his first computing course might have difficulties with it, but no-one with more experience should find it even slightly complicated?
3) Because "online code quizzes" set homework questions?
4) All of the above? :laugh:
Richard Deeming 12-Nov-16 10:35am    
Just because it wasn't set by a teacher, that doesn't mean it's not "homework"! :)

You've set yourself some "homework" to try to improve your programming skills, which is a noble aim. However, if someone else gives you the answer, that defeats the whole purpose of the task.
[no name] 12-Nov-16 10:38am    
I am trying to figure that out.
I couldn't that' why i wanted to know how to go for this.
And that's why i asked Sir.
but if it let me to have a mock on my expense, I just don't get it how help can be.
Richard Deeming 12-Nov-16 10:39am    
Nobody's mocking you. We're just trying to help you to find the answer for yourself. You'll learn much more that way. :)
We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.
Quote:
That's not my homework.
I got it while solving an online quiz code and I couldn't do it.
It is HomeWork because it is complicated only for newbie student, it is aimed to teach you how to combine things.

Advice:
Take a sheet of paper and make a table where you report facts in columns.
- line number
- line number is odd or even
- how many stars on the line
- how many # on the line
- starts on left or right
Make this table for 8 and 9, then look for patterns.

Training exercises:
Ask a number, then draw something:
-Draw a triangle
*
* *
* * *
* * * *
* * * * *
-Add # to make it a square
* # # # #
* * # # #
* * * # #
* * * * #
* * * * *
-Swap left-right
# # # # *
# # # * *
# # * * *
# * * * *
* * * * *
-Draw a pyramid
*
* *
* * *
* *
*
-Add # to make it a square
* # # # #
* * # # #
* * * # #
* * # # #
* # # # #
-Swap left-right
# # # # *
# # # * *
# # * * *
# # # * *
# # # # *

Being a programmer is also being at ease with mathematics
Check the behavior of the absolute function
For x= -2, -1, 0, 1 and 2 :
what is the result of abs(x)
what is the result of 3-abs(x)
Compare with the other patterns.
 
Share this answer
 
Comments
CPallini 12-Nov-16 13:26pm    
5.
Patrice T 12-Nov-16 13:44pm    
Thank you.
While it seems most aspects are being answered in the other solutions already, here's some more general advice on how to decipher the algorithm that determines the output.

You have to investigate all the examples to recognize the pattern. The longest example (in this case for the input 8) will typically provide the most information. If you think you can spot a property of the longest pattern, you can check the other examples to see if your conjecture holds.

Then you can check how the patterns depend on the input value. Again, if you think you can spot a dependency, check all examples to find out if you're right.

Each of the properties and dependencies that you found can be translated into a rule and incorporated into your program. Depending on the complexity of the pattern, it may require a lot of rules. If you think you've found them all, you can try to complete your program. However, if you find that your program is lacking information to correctly reproduce the patterns, you probably missed a rule and you need to check the examples again.

Alternately you can start writing your program and, while doing so, consider how much and what information you may need to reproduce each line of output. E. g. how many lines do you need to print, how long is each output line, what does a particular line consist of, etc.. Then you can look for clues on that specific information in the examples.
 
Share this answer
 
Comments
Richard Deeming 4-Apr-19 10:17am    
If the OP is still waiting for someone to do their homework for them three years later, they've already failed! :D

(Pushed back into the active list by the code-dump in Solution #3.)
public class Program
{
    public static void main(String[] args) {
        int k,i,j,n=3;
        int sc,hc;
        sc=1;
        hc=n-1;
        
        for(k=0;k<n;k++)
        {
            if(k%2==0)
            {
                for(i=1;i<=sc;i++)
                {
                    System.out.print("*");
                }
                for(j=1;j<=hc;j++)
                {
                 System.out.print("#");
                }
            }
        
            else
            { 
                for(i=1;i<=hc;i++)
                {
                    System.out.print("#");
                }
                for(j=1;j<=sc;j++)
                {
                 System.out.print("*");
                }
            }
             System.out.print("\n");
             if(k<n/2)
             {
            sc++;
            hc--;
             }
             else
            {
            sc--;
            hc++;
             }
                 
             
        }
    }
}
 
Share this answer
 
Comments
Dave Kreskowiak 3-Apr-19 13:24pm    
Read the other answers again. What part of "we do not do your HomeWork" do you not understand? You didn't help anyone by posting an answer to the question. You did their work for them and taught them NOTHING about how to build out the answer for themselves.

Congratulation on helping people FAIL their class, not pass it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900