Question

I'm trying to make a C++ program start creating an array and takes the values from the user , then print every value + star as much the value is .. Example : the user had entered 5 then the output must be like this 5***** Input

1

2

3

4

5

6

output

1*

2**

3***

4**** and so on

.. help :(

#include <iostream> 
using namespace std; 
void main() 
{
    int arr[10]; 
    for (int i = 0; i < 10; i++)
    {
        cin >> arr[i]; 
        int x = arr[i]; 
        for (int j = 0; x <= arr[i]; j++)
        {
            cout<< "*";
        }
    }
}

And another help please can you give me some useful link to practice on programming to be professional

Was it helpful?

Solution

Your code is wrong. Use the following code:

#include <iostream> 
using namespace std; 
int main()  {
  int arr[10]; 
  for (int i = 0; i < 10; i++)
  {
   cin >> arr[i]; 
   int x = arr[i]; 
   for (int j = 0; j < x; j++){ // your condition was wrong

   cout<< "*";
  }
   cout<<endl; // for better formatting
 }
 return 0;
}

For edited question

int main()  {
int arr[10];
for (int i = 0; i < 10; i++)
{
    cin >> arr[i];


}
for (int i = 0; i < 10; i++)
{
    int x = arr[i];
    cout << x;
    for (int j = 0; j < x; j++){ // your condition was wrong

        cout << "*";
    }
    cout << endl;
}

return 0;
} 

OTHER TIPS

#include <iostream> 
using namespace std; 
void main() 
{
    int nbValues = 10;
    int arr[nbValues];

    // First recover the values
    for (int i = 0; i < nbValues; i++)
    {
        cin >> arr[i];
    }

    // Then print the output
    for (int i = 0; i < nbValues; i++)
    {
        int x = arr[i];
        cout << x;// Print the number
        for (int j = 0; j < x; j++)
        {
            cout<< "*";// Then print the stars
        }
        cout << endl;// Then new line
    }
}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top