Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with my C++ program! I need to print the starts **** in a V shape bu

ID: 3757532 • Letter: I

Question

I need help with my C++ program!
I need to print the starts **** in a V shape but it is nor working... this is the program I have so far...

#include <iostream>

using namespace std;

// Example program
#include <iostream>
using namespace std;

void
printV (int n)
{
int i, j;
for (i = n - 1; i >= 0; i--)
    {
      for (j = n - 1; j > i; j--)
{
   cout << "";
}

      cout << "*";

      for (j = 1; j < (i * 2); j++)
cout << "";

      if (i >= 1)
cout << " ";
    }
}

int
printlnvertedV (int n)
{
int i, j, k = 0;
for (i = n - 1; i >= 0; i--)
    {
      for (j = n - 1; j > k; j--)
{
   cout << "";
}

      cout << "*";

      for (j = 1; j < (k * 2); j++)
cout << "n";
      k++;
    }
}

int
main ()
{
cout << "Print V shape" << " ";
int n = 7;
printV (n);
cout << " Print Inverted V shape" << " ";
printlnvertedV (n);
return 0;
}

Output:
Print V shape
*
*
*
*
*
*
*

Print Inverted V shape
**n*nnn*nnnnn*nnnnnnn*nnnnnnnnn*nnnnnnnnnnn

Explanation / Answer

#include <iostream>
using namespace std;
void printV(int n)
{
int i, j;
for (i=n-1;i>=0;i--) {
for (j=n-1;j>i;j--) {
cout << " ";
}
cout <<"*";
for(j=1;j<(i*2);j++)
cout<<" ";

if(i>=1)
cout<<"*";
cout<< " ";
}
}
void printlnvertedV (int n)
{
int i,j,k=0;
for(i=n-1;i>=0;i--){
for(j=n-1;j>k;j--){
cout<< " ";
}
cout<<"*";
for(j=1;j<(k*2);j++)
cout<<" ";

if(i<n-1)
cout<<"*";
cout<<" ";
k++;
}
}
int main ()
{
cout << "Print V shape" << " ";
int n = 7;
printV (n);
cout << " Print Inverted V shape" << " ";
printlnvertedV (n);
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote