diamond

Good day Experts and Professionals :)
I would like to ask help how to make a diamond like this.
What is the possible code?

1
2
3
4
5
6
7
8
9
10
11
12
       *
      * *
     * * *
    * * * *
   * * * * *
  * * * * * * 
   * * * * *
    * * * *   
     * * *
      * *
       *                      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main() {
    std::cout << "       *\n"
              << "      * *\n"
              << "     * * *\n"
              << "    * * * *\n"
              << "   * * * * *\n"
              << "  * * * * * *\n"
              << "   * * * * *\n"
              << "    * * * *\n"
              << "     * * *\n"
              << "      * *\n"
              << "       *\n";
    return 0;
}
Last edited on

I think he wanted the program to calculate the output. How about this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>
using namespace std;

int main()
{
   const int number(6);

   for (int n = 0; n < number; ++n)
   {
      cout << string(number - n, ' ');

      for (int m = 0; m <= n; ++m)
      {
         cout << "* ";
      }

      cout << endl;
   }

   for (int n = 1; n < number; ++n)
   {
      cout << string(n+1, ' ');

      for (int m = 0; m < number - n; ++m)
      {
         cout << "* ";
      }

      cout << endl;
   }

   return 0;
}
Please note that this is not a homework site. We won't do your homework for you. The purpose of homework is that you learn by doing. However we are always willing to help solve problems you encountered, correct mistakes you made in your code and answer your questions.

We didn't see your attempts to solve this problem yourself and so we cannot correct mistakes you didn't made and answer questions you didn't ask. To get help you should do something yourself and get real problems with something. If your problem is "I don't understand a thing", then you should go back to basics and study again.
Im sorry i didn't post my work I always get these kind

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

it displays like these

c:\users\monsterzz\desktop\lab7-02\lab7-02.cpp(21) : error C2374: 'n' : redefinition; multiple initialization

c:\users\monsterzz\desktop\lab7-02\lab7-02.cpp(9) : see declaration of 'n'

Im sorry i didn't post my work I always get these kind

you don't print spaces, and you don't print the row with 2 asterix, that's all.
Last edited on

c:\users\monsterzz\desktop\lab7-02\lab7-02.cpp(21) : error C2374: 'n' : redefinition; multiple initialization

c:\users\monsterzz\desktop\lab7-02\lab7-02.cpp(9) : see declaration of 'n'


Well that's an easy fix for your compiler, although it should have worked, I used gcc and it would have worked with many others compilers too:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>
using namespace std;

int main()
{
   const int number(6);
   int n(0);

   for (n = 0; n < number; ++n)
   {
      cout << string(number - n, ' ');

      for (int m = 0; m <= n; ++m)
      {
         cout << "* ";
      }

      cout << endl;
   }

   for (n = 1; n < number; ++n)
   {
      cout << string(n+1, ' ');

      for (int m = 0; m < number - n; ++m)
      {
         cout << "* ";
      }

      cout << endl;
   }

   return 0;
}
Im sorry i didn't post my work I always get these kind

Without space before the stars?

You really should post your attempt. Then we can point out what is wrong in it and you will learn.


Can you explain what ajh32's code does, exactly?


The compiler error messages -- are they from the code that you copy-pasted from ajh32's post?

Do you notice a number in parentheses right after the filename in both messages? That is the linenumber. Helps you see where in the code the error appears to be. (IDE might point it in other ways too.)

You have one error. Identifier "n", a name, has been declared on line 9. That is ok. Then, later (line 21) in same scope the same name is declared again. If there are two Johns in a room and someone calls John, who should answer?

ajh32's is legal C++, yet there was time when it was not. The C++ standard has been updated to allow that code. The old compiler's that have not been updated to support the new standard, can think the code erroneous.

What C++ compiler are you using? Some MS Visual, but which version?
Or using just one loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>
#include <cmath>
using namespace std;

int main()
{
   //const int number(6);
   int n = 0;
   int t = 0;

   for (n = -6; n < 7; ++n)
   {
      if (n<0)
        t = abs(n);
      else
        t = n;
      cout << string(t, ' ');
      for (int m = 0; m <= 6-t; ++m)
      {
         cout << "* ";
      }

      cout << endl;
   }


   return 0;
}

Topic archived. No new replies allowed.