Array

I made a code that asks "write a C++ program that declares an array alpha of 50 components of type double. Initialize the array so that the first 25 components are equal to the square of the index variable, and the last 25 components are equal to three times the index variable. Output the array so that 10 elements per line are printed."
my code is:
#include <iostream>
#include <iomanip>

using namespace std;
int main()
{
double alpha[50];

//if \(\(.* \+ 1\) % 10 == 0\)
for(int i=0; i < 50; i++)

{
alpha[i]= i*i;
if(i >=25)
{
alpha[i]= 3*i;
}
}

for(int i=0; i < 50; i++)
{
if(i%10 == 0)
{
cout << endl;
}
cout << setw(3) << alpha[i] << " ";
}
It works perfectly the but program I'm using asks to use specifically this patter in the code: if \(\(.* \+ 1\) % 10 == 0\)
any ideas how to make this possible without replacing the two for loops nor the output? I'm kind of confused... any help would appreciated. thanks.
Pattern:
if \(\(.* \+ 1\) % 10 == 0\)

The . means "any character".
The * means "previous character repeats 0 or more times".
The .* in pattern matches thus anything, simple or complex.

The code should thus have:
if ( ( expr + 1 ) % 10 == 0 )
where the expr you have to invent.

You do have
if ( i % 10 == 0 )
That almost matches the pattern, particularly if we tinker it a bit:
if ( ( i - 1 + 1 ) % 10 == 0 )
The i - 1 obviously is not what the teacher wants.

What does happen, if you change the order of operations into:
1
2
3
4
5
6
7
8
for(int i=0; i < 50; i++)
{
  cout << setw(3) << alpha[i] << " ";
  if(i%10 == 0)
  {
    cout << endl;
  }
}
Topic archived. No new replies allowed.