Thread: Writing programs in C

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    3

    Writing programs in C

    Write a program that prints a diamond as demonstrated next.

    *
    * *
    * *
    * *
    * *
    * *
    * *
    *


    This is what I have come up with:


    Code:
    #include"stdio.h"
    int main()
    {
      int a, i, j;
      scanf("%d", &a);
      for (i = 1; i <= a; i++) {
        for (j = a; j >= i; j--)
          printf(" ");
        for (j = 1; j <= i; j++)
          printf(" *");
        printf("\n");
      }
      for (i = a; i >= 1; i--) {
        for (j = a; j >= i; j--)
          printf(" ");
        for (j = 1; j <= i; j++)
          printf(" *");
        printf("\n");
      }
    }
    Last edited by Salem; 10-28-2011 at 11:26 AM. Reason: fixed code - remember, use "COPY AS TEXT" before pasting

  2. #2
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    Welcome to the C forum, Mz. Jackee. :-)

    The shape you posted doesn't look like diamond. Please let us know what the actual problem is and the problem you are having with the code.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    3
    I am trying to write a program that will print a diamond shape. The shape should look like this:

    *
    * *
    * *
    * *
    * *
    * *
    *







    I don't know anything about programming, so I was just messing around with something after reading the textbook...and, I hoping that somebody can help me!
    Last edited by Mz. Jackee; 10-28-2011 at 10:54 AM. Reason: After I type the diamond shape...it automatically flushes to the left and I don't know why it's doing that!

  4. #4
    spaghetticode
    Guest
    It's meant to look like that? ->

    Code:
    Use code tags:
    
       *
      * *
     *   *
    *     *
     *   *
      * *
       *

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    3
    Exactly, Dennis! So, can you check my code to see if I have done this correctly...thank you!


    Program:


    Code:
    #include"stdio.h"int main(){  int a, i, j;  scanf("%d", &a);  for (i = 1; i <= a; i++) {    for (j = a; j >= i; j--)      printf(" ");    for (j = 1; j <= i; j++)      printf(" *");    printf("\n");  }  for (i = a; i >= 1; i--) {    for (j = a; j >= i; j--)      printf(" ");    for (j = 1; j <= i; j++)      printf(" *");    printf("\n");  }}

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Mz. Jackee View Post
    So, can you check my code to see if I have done this correctly
    Does the output look like what you expect?

    a) Yes. Then you have done it correctly.
    b) No. Then you have not done it correctly.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Last edited by Salem; 12 Hours Ago at 06:26 PM. Reason: fixed code - remember, use "COPY AS TEXT" before pasting
    Notice how nice the code (now) looks in post #1

    Now look at the dog-food in post #5

    Now back to post #1

    Do you see it yet?



    Next time you post, click the one which says "Go Advanced" and then the one which says "Preview Post".
    MAKE SURE what you post is readable.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Jan 19-20 Class: Writing Application Programs for RDMA using OFA Software
    By sanding07 in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 01-04-2011, 03:22 PM
  2. Replies: 2
    Last Post: 02-25-2010, 03:54 AM
  3. Replies: 1
    Last Post: 11-09-2009, 07:03 AM
  4. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 08:13 PM
  5. Writing Setup programs
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 12-31-2001, 06:12 PM

Tags for this Thread