Click here to Skip to main content
15,877,637 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Q15) A class Numbers contains the following data members and member functions to fill the cell of NXN matrix with the triangular numbers. [ A triangular number is formed by the addition of a consecutive sequence of integers starting from ‘Num’].
e.g. if Num=1 then

1 + 2 = 3
1 + 2 + 3 = 6
1 + 2 + 3 + 4 = 10
1 + 2 + 3 + 4 + 5 = 15

Therefore, 3, 6, 10, 15 etc. are triangular numbers.

Class name: : Numbers

Data members/ instance variables :
N : integer to store order of matrix.
Num : integer to store starting value of triangular numbers.
Prev :integer to store last value which gave triangular number.
Arr[][] : array to store triangular numbers in array

Member functions/methods
Numbers(int a, int b) :constructor to assign a to N and create matrix of order NXN. Also assign Num=b
Numbers check(int n) : to check if ‘n’ is triangular and initialize ‘n’ to Num if triangular or initialize
Num by next triangular greater than ‘n’and initialize Prev with last value which
gave triangular number.
void fill( ) : to fill the boundary cells of Arr[][]by triangular number beginning from ‘Num’.
void display() :to display the matrix Arr[].
Specify the class Numbers giving details of the functions intcheck(int) and void display( ).

What I have tried:

cannot understand what is to be done in check function
Updated 18-Dec-21 19:56pm
Comments
Richard MacCutchan 19-Dec-21 3:38am    
You need to check if it is a triangular number. So you just add 1 +2 + 3 etc. until the total is equal or greater than the value of n. If it equals n then it is a triangular number, if it is greater than n then it is not. For example if the input n is 8 then 1 + 2 + 3 + 4 gives 10 which is greater, so 8 is not triangular.
Can you help me with the code
Richard MacCutchan 19-Dec-21 12:37pm    
bool check(int n)
{
    int sum = 0;
    for (int i = 1; sum < n; i++)
    {
        sum += i;
    }
    if (sum == n)
        return true;
    else
        return false;
}

1 solution

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900