"Grafik-Aufgabe" (split aus Zinseszins)



  • Write a C++ program that asks the user to input an odd integer value (>=5). Your program produces a diamond enclosed by a square of the given size.

    For example,

    Enter an odd integer
    
    9
    
      *********
      *  * *  *
      * *   * *
      **     **
      *       *
      **     **
      * *   * *
      *  * *  *
      *********
    


  • cardanadam schrieb:

    Write a C++ program that asks the user to input an odd integer value (>=5). Your program produces a diamond enclosed by a square of the given size.

    First, you should open a new topic for this question and not just write in an existing one.
    Second, you actually didn't ask a question 😕
    Third, don't you have any idea how to solve it? Or do you already have any skills in C++?



  • Fourth, I would use a for-loop to build up each row of the output (just look at the example to determine how each row ist constructed).

    (solves "first" by using his mod-powers)



  • I think that this is a really interesting thread and so, i sat down and wrote a little bit of code for that problem but attention: The code ISN'T optimized, so there will be much faster programs to solve the problems, I'm sure.

    Have fun with the code and I hope, it'll be useful for you

    #include <iostream>
    
    using namespace std;
    
    int plotdiamond(int links, int rechts, int max)
    {
    	cout << "*";
    	for(int i = 2; i < max; i++)
    	{
    		if(i == links || i == rechts)
    		{
    			cout << "*";
    		}
    		else
    		{
    			cout << " ";	
    		}
    	}
    	cout << "*" << endl;
    }
    
    int plotdiamond(int max)
    {
    	for(int i = 1; i <= max; i++)
    	{
    		cout << "*";	
    	}
    	cout << endl;
    }
    
    int main()
    {
    	int first, input, xl, xr;
    	bool change = false;
    
    	cout << "Insert an odd integer value:";
    	cin >> input;
    
    	first = (input+1)/2;
    	xl = first;
    	xr = first;
    
    	plotdiamond(input);
    
    	for(int i = 1; i < input-1; i++)
    	{
    		if(xl-1 == -1 || xr+1 == input+1 || change == true)
    		{
    			change = true;
    			plotdiamond(++xl, --xr, input);
    		}
    		else
    		{
    			plotdiamond(--xl, ++xr, input);
    		}
    	}
    
    	plotdiamond(input);
    }
    

Anmelden zum Antworten