Hello, there is some problems in my program.
In the function void print(int num, int line, char letter[]),
I have to make 4 features there.
1. This function accepts three parameters: num, line and letter[ ] .
2. It first displays the values of num and line.
3. Suppose ‘A’ is 1, ‘B’ is 2, … ‘Z’ is 26, the letters corresponding to the factors of
num are printed in the format of pyramid as shown in the sample output.
4. The letter numbers are printed at the beginning of each line. Align the numbers to the right.
My trial so far:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
void init(int &, int &);
void print(int, int, char[]);
int main()
{
int i, num, line = 0;
cout << "Name: Luk Tsz Chun" << endl;
cout << "ID: 12316186A" << endl;
cout << "Class: C01C" << endl;
// initialize elements of array n to 0
char letter[27];
for (i = 1; i <= 26; i++)
{
letter[i] = (char)(64 + i);
}
cout << "Enter a seed value: ";
cin >> num;
init(num, line);
print(num, line, letter);
cout << endl;
cout << "Goodbye." << endl;
return 0;
}
// Your code of R6 and R7 is inserted here
void init(int &num, int &line)
{
srand(time(0));
int count = 0;
num = rand() % 26;
for (int i = 1; i <= num; i++)
{
if (num % i == 0)
{
line++;
cout << i << ' ';
}
}
}
void print(int num, int line, char letter[])
{
cout << "Random number: " << num << endl;
cout << "Lines to be printed: " << line << endl;
for (int i = 1; i <= line; i++)
{
cout << i << ':';
for (int j = 0; j < (line - i); j++)
cout << " ";
for (int j = 1; j <= i; j++)
cout << letter[i];
for (int k = 1; k < i; k++)
cout << letter[i];
cout << endl;
}
}
|
Take an example of random number = 15
15 has 4 factors: 1, 3, 5, 15
The Program compiles:
Random number = 15,
Lines to be printed = 4
1: A
2: BBB
3: CCCCC
4: DDDDDDD
|
What I want is:
Random number = 15,
Lines to be printed = 4
1: A
3: CCC
5: EEEEE
15: OOOOOOO
|
What has happened?