Writing programs in C

This is a discussion on Writing programs in C within the C Programming forums, part of the General Programming Boards category; Write a program that prints a diamond as demonstrated next. * * * * * * * * * * ...

  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 12:26 PM. 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
    507
    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 11: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
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    It's meant to look like that? ->

    Code:
    Use code tags:
    
       *
      * *
     *   *
    *     *
     *   *
      * *
       *
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Debian Testing/Jessie / Linux 3.16-2-amd64 / KDE SC 4.14.1
    Compiler: gcc 4.9.1-16 with KDevelop 4.7.0

  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
    32,823
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 09: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21