5.2.6 Similarity between “drawing a shape using Scratch” and “printing a shape using C++”

View

Shape drawing is not only done in Scratch. It’s also done in other programming languages. Let’s see one of the programs that can print out a triangle in C++ programming language. Then we will show another program made in Scratch that draws the triangle too.

You will notice that a triangle drawn in Scratch is seamless shape made up of straight lines while a triangle printed out in C++ is made up of asterisks.

We can easily see that Scratch can be chosen over C++ if you want to quickly program a multimedia scenario.

C++ program that prints out a triangle shape

//C++
program to print triangle
#include<iostream>
Using
namespace STD;
int
main (){
  Cout<<"\"Triangle
Shape\"\n\n";
  int z=1;

 

  For (int i=0; i<7; i++) {for (int j=7; j>i;
j--) {   cout<<" "; //
displaying space her   }
 
    Cout<<"*";   // displaying asterisk here
    if (i!=0){
      for (int k=1; k<=z; k++){
        Cout<<" ";
      }
      Cout<<"*";
      z+=2;
    }
    Cout<<end; // endl is for new line
  }
 
  For (int i=0; i<=z+1; i++) {
    Cout<<"*";
  }
Cout<<endl;
return
0;
}

Scratch code that draws a triangle shape

 

Figure 146: Building a triangle in Scratch


Last modified: Tuesday, 21 December 2021, 3:27 PM