C programming

Dear experts

I am a beginner in any programming language. I am currently learning C.

I have managed to write a code in bits and pieces to achieve the following

Model 1:

AAASAAA
AASSSAA
ASSSSSA
SSSSSSS
ASSSSSA
AASSSAA
AAASAAA


Model 2

AAAAAA
AASSAA
ASSSSA
AASSAA
AAAAAA


My code

#include<stdio.h>
#include<math.h>
main()
{
      int r,c;
      for(r=3;r>=1;r--)
      {
            for(c=1;c<=r;c++)
            {
                  printf(" A");
            }
            for(c=3-r;c>=1;c--)
            {
                  printf(" S");
            }
                        for(c=3-r;c>=1;c--)
            {
                  printf(" S");
            }
            for(c=1;c<=r;c++)
            {
                  printf(" A");
            }
            
      printf(" \n");      
      }
      
}


Can anyone please update my code.

Thank you
ExcellearnerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenData Warehouse Architect / DBACommented:
Hi Learner,

Are looking to add functionality, edit for style, shorten the code, etc.?
0
ExcellearnerAuthor Commented:
Hi Kent,

Thank you.

Can i please request my code to be updated to deliver the two models. That is one code for each model. My current capabilities are only so much.

I am still learner.

Thank you. Please help.
0
Kent OlsenData Warehouse Architect / DBACommented:
Sure.  :)

I would just put each model into a separate function and call them from main().

#include<stdio.h>
#include<math.h>

model_1 ()
{
}

model_2 ()
{
      int r,c;
      for(r=3;r>=1;r--)
      {
            for(c=1;c<=r;c++)
            {
                  printf(" A");
            }
            for(c=3-r;c>=1;c--)
            {
                  printf(" S");
            }
            for(c=3-r;c>=1;c--)
            {
                  printf(" S");
            }
            for(c=1;c<=r;c++)
            {
                  printf(" A");
            }
            
      printf(" \n");      
      }
}

main()
{
  model_1 ();
  model_2 ();
}

Open in new window


If you've still got the code to produce Model 1, just plug it into the function.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ExcellearnerAuthor Commented:
Hi Kent,

I do not have any model for either. Please help.
0
Kent OlsenData Warehouse Architect / DBACommented:
The code that you've got looks like something that can be found on the internet.  In fact, it looks like codefights.com question and solution.

But let's make some sense out of it.  That code loosely used variables *r* and *c* for Row and Column.  If you look at the resulting pattern(s) you'll see that's not enough.  Each solution has 4 identical quarters.  When there are an odd number of rows, draw a line through the middle row and column.  The resulting quarters are mirror images of each other.  Left to right, or Over to under.  The same applies when there are an even number of rows, but the line is drawn between rows not over one.

That said, all you need to do is draw the left and right half of each line as mirror images, and apply the same mirror technique up and down.

The code below is similar to what you've posted, except that it introduces the variable Brk, to indicate where on the line the break between the 'A' and 'S' character lines.

Kent


#include <stdio.h>
#include<math.h>

#define C1 'A'
#define C2 'S'

model_1 (int width)    // odd
{
	int Half = width / 2;
	int Brk = Half;
	int Row;
	int Col;

	puts ("\r\n\r Model 1\r\n\r");
	for (Brk = Half; Brk; --Brk)
	{
		for (Col = 0; Col < Half; ++Col)
			if (Col < Brk)
				putch (C1);
			else
				putch (C2);
		putch (C2);
		for (--Col; Col >= 0 ; --Col)
			if (Col < Brk)
				putch (C1);
			else
				putch (C2);
		puts ("\r");
	}
	for (Brk = 0; Brk <= Half; ++Brk)
	{
		for (Col = 0; Col < Half; ++Col)
			if (Col < Brk)
				putch (C1);
			else
				putch (C2);
		putch (C2);
		for (--Col; Col >= 0 ; --Col)
			if (Col < Brk)
				putch (C1);
			else
				putch (C2);
		puts ("\r");
	}
}

model_2 (int width)    // even
{
	int Half = width / 2;
	int Brk = Half;
	int Row;
	int Col;

	puts ("\r\n\r Model 2\r\n\r");

	for (Brk = Half; Brk; --Brk)
	{
		for (Col = 0; Col < Half; ++Col)
			if (Col < Brk)
				putch (C1);
			else
				putch (C2);
		for (--Col; Col >= 0 ; --Col)
			if (Col < Brk)
				putch (C1);
			else
				putch (C2);
		puts ("\r");
	}
	for (Brk = 1; Brk <= Half; ++Brk)
	{
		for (Col = 0; Col < Half; ++Col)
			if (Col < Brk)
				putch (C1);
			else
				putch (C2);
		for (--Col; Col >= 0 ; --Col)
			if (Col < Brk)
				putch (C1);
			else
				putch (C2);
		puts ("\r");
	}
}

main()
{
	model_1 (7);
	model_2 (6);
	return 0;
}

Open in new window

0
ExcellearnerAuthor Commented:
Kent,
Firstly, i am a beginner in programming. (C is the first language i am learning). The hotch-potch code posted in the question was written by me.

I liked your idea : "That said, all you need to do is draw the left and right half of each line as mirror images, and apply the same mirror technique up and down."

in my program i learnt to achieve only part of it as of now.


I ran your code and on line 19-putch (C1); Error message: Putch was not declared in scope.

Kindly help.

Thank you
0
Kent OlsenData Warehouse Architect / DBACommented:
Sorry, old habit with regards to putch.  It's a non-standard way to put a character on the console that writing it to STDOUT (I hope that makes sense).

Trying including conio.h

#include <conio.h>

If that doesn't work, change the putch back to a style that you know....

Kent
0
ExcellearnerAuthor Commented:
Kent,

Phenomenal it worked.

I have a person who is teaching me C. Let me run pass through your and understand it. If I have any question I will raise it. I will close the question by Monday evening India time.

Thank you
0
sarabandeCommented:
or you may try the below:

#include <stdio.h>

void printchars(char c, int n)
{
    while (--n >= 0)
         printf("%c", c);
}

void makemodel(int maxa, int odd)
{
     int l = 0;
     int a = maxa;
     int s = odd;
     
     for (l = 0; l < maxa+odd; ++l)
     {
           printchars('A', a); printchars('S', s); printchars('A', a); printf("\n");
           a--; s+=2;
     }
     a++; s -= 2; 
     for (l = 1-odd; l < maxa; ++l)
     {
           a++; s-=2;
           printchars('A', a); printchars('S', s); printchars('A', a); printf("\n");
     }
}

int main()
{
     printf("\nModel 1:\n\n");
     makemodel(3, 1);     
     printf("\n\nModel 2:\n\n");
     makemodel(3, 0);
     printf("\n\n");
     return 0;
}

Open in new window


Sara
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ExcellearnerAuthor Commented:
Kent/Sara

Thank you
I have been away from my table. I will close it by EOD tomorrow.
Apologies for the delay.
0
ExcellearnerAuthor Commented:
Kent/Sara

Thank you.

Can I request either of you to help me understand the two different approaches.

Also, please guide me how to allocate the points between the two of you.

I have noticed that the website has undergone change. Now I find it challenging to manoeuvre.

Thank you
0
Kent OlsenData Warehouse Architect / DBACommented:
Hi Learner,

They may not look like it, but the two programs are essentially the same.  Well, actually all 3 are.

I took the program you provided and improved it a bit, mostly for readability.  (If you were writing a single function (for the odd or even solution) you could easily start there.)  I probably wouldn't use it in that form as it doesn't read very easily, but it is a likely evolutionary step in designing a program that will eventually look like the one that Sara posted.  :)  

The printchars() function in Sara's program takes a lot of the wordiness out of the program.  Printing one character at a time gets you something like I showed:

            for (Col = 0; Col < Half; ++Col)
                  if (Col < Brk)
                        putch (C1);
                  else
                        putch (C2);

In Sara's program, that looks like:

  putchars (C1, Brk - 1);
  putchars (C2, Half - Brk);

Sara incorporated the odd/even test into a single function.  :)  As your skills grow, you'll go from writing code similar to the first example to code that looks like Sara's.  

And the guy that wrote the program you found should have his keyboard taken away from him.  That style works for him because he wrote it and knows what it does.  Anybody that looks at it has to develop an understanding of what it does to understand what the single-character variables are.  That's backwards.
0
ExcellearnerAuthor Commented:
Kent,

Thank you. Your explanation is quite helpful.

Now, how do i award points to both of you?
0
Kent OlsenData Warehouse Architect / DBACommented:
Click on the "Assisted Solution" and/or "Best Solution" buttons on the responses you like.
0
sarabandeCommented:
to add to Kent's explanation:

there are principally two approaches to design functions in c. first is bottom-up, second is top-down.

with first i would have first written the printchars function, then the makemodel and finally main.

but actually, i checked first if model 1 and model 2 could be done with one function and after i found out that it could be done, the design of main function was nearly done. it simply would call makemodel twice with different parameters such that model 1 and model 2 was printed as required. note, at this point of design i didn't know exactly which arguments the makemodel would need or better which arguments would provide the smallest possible interface without redundancy and no dependencies. also note, the decision to print the title line of each model in main and not in makemodel, would be made different by most other programmers. i decided for main because the title line made the code much better understandable especially as there were no comments in my code. code without comments normally is bad practice. i did it most likely for the same reason, Kent has not given the code for model 1 in his first code snippet: you as a beginner should not get full (documented) code but should make some own efforts to understand and make it run, perhaps even try some other variants. this task best could be done by commenting the code yourself. if you would do that both for my code and Kent's you surely will full understand what Kent meant when he said, that the programs are (doing) the same.

now to the design of the makemodel function:

Kent already explained that each string is symmetric. that's why the first argument to makemodel is the number of 'A' characters left from the middle. the second argument tells whether the strings start with one 'S' or with none 'S' at all. the int variable a means how many A to print in sequence in the current loop cycle and the s variable how many S to print. note, a proper naming of variables is crucial for a good design.

you may have recognized that i put more than 1 statement in one line. some experts would say that this is not good practice, but you may judge yourself which variant is better readable. i even would say, that the line with the four print statements within each loop is better readable than it would be with a new function 'printcode(int a, int s);'. i would recommend you to add such a function (after printchars and above makemodel) and check what you like better.

Sara
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.