CPPForSchool.com: Flow of Control Set 3: Assignment 1

1. Write a program to print following :
i)
**********
**********
**********
**********

ii)
*
**
***
****
*****

 iii)
        *
      **
    ***
  ****
*****


iv)
        *
      ***
    *****
  *******
*********


v)
        1
      222
    33333
  4444444
555555555

vi)

        1
      212
    32123
  4321234
543212345



Not sure how I accidentally skipped Flow of Control Set 3 Assignments, but I am now going to correct this oversight.  Nevertheless, for this assignment, I will need to utilize loops.  In regards to the screenshots, the source code was all placed into 1 file, but I commented out per series.  This is why some of the screenshots start at line 30+ and does not show "int main()" or the proper closing tags.

i) For this task, I was able to code it rather quickly.  Once I determined how many asterisks per line, I was able to create the correct algorithm.






ii)For this task, I experienced a small snag in the coding process.  However, once I relocated "cout << endl;" to the correct placement, everything fell into place.






iii)This task was quite interesting because I used the setw function.  However, when I compared my code to the sample source code, it was a different approach entirely.  Instead of utilizing the setw function, the sample source used a for loop and " " (spacing) and subtracted accordingly.  Nevertheless, below is my source code and output.






iv)Unfortunately, this task had me stumped.  Even though I had some ideas, I could not seem to execute it properly.  Nevertheless, after a quick peak at the sample source code, I was able to code it accordingly.  Granted, outside of changing the variable names, it mimics the code almost line by line.  Regardless, it was a great learning experience and I hope to retain this approach for later assignments and projects.





v) Learning from the prior task, I was able to code this printout very easily.  I had to change the asterisk to an integer and incremented it's value by 1. 







vi) Sadly, I could not figure out how to code this last printout.  Like iv, I had to review the sample source code.  From there, I was able to successfully code a program with the expected printout.  Due to my shortcomings, the code mimics the sample source code with some minor changes to the variable naming convention.






All in all, this was a great learning experience.  Hopefully I will be able to retain these new concepts, specifically the ways of nesting loops and approach to the algorithms.  Instead of providing a screenshot of the sample source code, I went ahead and just copied them directly.  I did this because the code exceeds the limitations of my screen. 

//Solution of (i)
#include<iostream>
using namespace std;

int main()
{
 int i,j;
 for(i=1;i<=4;i++)
 {
  for(j=1;j<=10;j++)
   cout<<'*';
  cout<<endl;
 }

 
 return 0;
}


//Solution of (ii)
#include<iostream>
using namespace std;

int main()
{
 int i,j;
 for(i=1;i<=5;i++)
 {
  for(j=1;j<=i;j++)
   cout<<'*';
  cout<<endl;
 }

 
 return 0;
}


//Solution of (iii)
#include<iostream>
using namespace std;

int main()
{
 int i,j,k;
 for(i=1;i<=5;i++)
 {
  for(j=5;j>i;j--)
   cout<<' ';
  for(k=1;k<=i;k++)
   cout<<'*';
  cout<<endl;
 }

 
 return 0;
}


//Solution of (iv)
#include<iostream>
using namespace std;

int main()
{
 int i,j,k;
 for(i=1;i<=5;i++)
 {
  for(j=5;j>i;j--)
   cout<<' ';
  for(k=1;k<2*i;k++)
   cout<<'*';
  cout<<endl;
 }

 
 return 0;
}


//Solution of (v)
#include<iostream>
using namespace std;

int main()
{
 int i,j,k;
 for(i=1;i<=5;i++)
 {
  for(j=5;j>i;j--)
   cout<<' ';
  for(k=1;k<2*i;k++)
   cout<<i;
  cout<<endl;
 }

 
 return 0;
}


//Solution of (vi)
#include<iostream>
using namespace std;

int main()
{
 int i,j,k,l;
 for(i=1;i<=5;i++)
 {
  for(j=5;j>i;j--)
   cout<<' ';
  for(k=i;k>=1;k--)
   cout<<k;
  for(l=2;l<=i;l++)
   cout<<l;
  cout<<endl;
 }

 
 return 0;
}

Comments

Popular posts from this blog

CPPForSchool.com: Variable, Operator & Expression Set 1: Assignment 9

CPPForSchool.com: Array - Single Dimension Set 1 Assignment 6

CPPForSchool.com: Variable, Operator & Expression Set 1: Assignment 8