hi again!
this time im tasked to create 2 data structure types: RationalNo and RationalInfo. The structure for RationalInfo consists of RationalNo as a nested structure and a few other functions. RationalNo is an array with fixed size.

but i got no idea how to even begin and populate the array in the structure for both RationalNo and nest it in RationalInfo...

i dont think the way i started is even correct

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;


struct RationalNo
{
       RatNo [20];
}
struct RationalInfo
{
       RationalNo;
       int Quotient;
       int Remainder;
       int ValueDiv;
       
}



int main()
{
    srand(time(NULL));
    RatNo [20] = rand() % 101 + 1
}

Recommended Answers

All 8 Replies

struct RationalNo
{
RatNo [20];
}

What is a RatNo? You should give it a type like 'int':

struct RationalNo
{
       int RatNo [20];
};

Please note that I've added a semicolon next to the curly bracket. All struct-definitions require that.

Now the structure has a member that is an array of ints. So each struct has place for 20 ints

The next thing you need to do is to declare a variable of the type 'RationalNo' in your main():

int main()
{
    RationalNo test;
}

You've jusst created a struct of the type 'RationalNo' with the name 'test'. This works exactly the same as declaring a variable of type 'int' named 'a': int a Next thing you'll have to do, is populate the array inside the struct:

test.RatNo[19] = 1;

This only assigns a value to the 20th element of the array. So what you need to figure out, is how to loop from 0-19 and populate the array!

Something like this:

struct RationalNo
{
      int  RatNo [20];
};

int main()
{
    RationalNo test;
    loop from 0-19
    {
         test.RatNo[loopvalue] = some valuee;
    }
   return 0;
}

When you've got this working, come back and we'll have a look at rest

thanks for the explaination and really appreciate it ! i got this part of the code done. the next part of my task requires me to find: quotient, remainder and actual val of each integer of the array. and then print it out on scrn.

i do know the code/formula for quotient/remainder but
how do i access each integer in the array of the structure? and then print it out?

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;


struct RationalNo
{
       int RatNo [20];
};


int main()
{
    srand(time(NULL));
    RationalNo num;
    for (int i = 0; i < 20; i++) //20 elements in array
        {
             num.RatNo [i] = rand() % 101 + 1; //integers range from 1-100 in value
        }
}

how do i access each integer in the array of the structure? and then print it out?

Same way you assigned data to them. Just make another loop and read the value of each arrayelement and print them.

for (int i = 0; i < 20; i++) //20 elements in array
        {
            cout << num.RatNo [i] << endl;
        }

this time im tasked to create 2 data structure types: RationalNo and RationalInfo. The structure for RationalInfo consists of RationalNo as a nested structure and a few other functions. RationalNo is an array with fixed size.

But I might have misunderstood your assignment... I think the struct should look something like this:

struct RationalInfo
{
       int RationalNo [20];
       int few, other, things;
};

It's just a matter of changing some names, but your teacher might like it when you do the assignment by the requirements :)

for quotient use the ' / ' operator. For remainder use the ' % ' (modules) operator

thanks Nick!

just one last qn><
for the last part of my code at //value of division
how do i swtich the int values of my array back to float values?

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;


struct RationalNo
{
       int RatNo [20];
};

struct RationalInfo
{      
       RationalNo rational;
       int quotient;
       int remiander;  
       int value;
};

int main()
{
    srand(time(NULL));
    
    RationalNo intNum;
    RationalInfo modNum;
    
    cout << "Original Integer" << endl;
    
    for (int i = 0; i < 10; i++)//Array Generation
        {
             intNum.RatNo [i] = rand() % 101 + 1;
             cout << intNum.RatNo [i] << "\t";
		
        }
    cout << endl;

    cout << "Quotients of integers" << endl;//Quotient
    for (int n = 0; n < 10; n++)
        {
             modNum.quotient = intNum.RatNo [n] / 10;
             cout << modNum.quotient << "\t";             
             
        }
    cout << endl;

    cout << "Remainders Of Integers" << endl;//Remainder
    for (int m = 0; m < 10; m++)
        {
             modNum.remiander = intNum.RatNo [m] % 10;
             cout << modNum.remiander << "\t";
        }
    cout << endl;
    
    cout << "Actual Value of integer division" << endl; //value of division
    for (int q = 0; q < 10; q++)
        {
             modNum.value = intNum.RatNo [q] / 10;
             cout << setiosflags (ios::fixed);
             cout << setiosflags (ios::showpoint);
             cout << setprecision (2);
             cout << modNum.value << "\t";
        }
        cout << endl;
}

Change this bunch:

modNum.value = intNum.RatNo [q] / 10.0;
cout << setiosflags (ios::fixed);
cout << setiosflags (ios::showpoint);
cout << setprecision (2);
cout << modNum.value << "\t";

to: cout << intNum.RatNo [q] / 10.0 << "\t" ; and you're done.

If you want to store the value in the struct, you'll need to change the int value in the struct to : float value; and then do something like:

modNum.value = intNum.RatNo [q] / 10.0;
cout << modNum.value << "\t";

In a loop.

Now you have your code working, but there is always room for improvement. You have 4 loops that count from 0-9 and do stuff. You might want to do all that stuff in just one loop from.
Also: you're array is 20 ints wide. But you loop from 0-9, so why not from 0-19. Else you should change the size of the array back to 10.

:icon_eek: i just realised i got my definition of a rational number wrong...rational number is in the form n = a/b. i dont think c++ has a way of displaying a number in such a form right?

only way i figured possible maybe was to make a 2-D array? but how should i modify my struct or should i just leave it as it is ?

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;


struct RationalNo
{
       int RatNo [20];
};

struct RationalInfo
{      
       RationalNo rational;
       int quotient;
       int remiander;  
       float value;
};

int main()
{
    srand(time(NULL));
    
    RationalNo intNum;
    RationalInfo modNum;
    
    cout << "Original Integers" << endl;
    
	
    for (int i = 0; i < 20; i++)//Array Generation
        {
             intNum.RatNo [i] = rand() % 101 + 1;
             cout << intNum.RatNo [i] << "\t";
		
        }
    cout << endl;

    cout << "Quotients of integers" << endl;//Quotient
    for (int n = 0; n < 20; n++)
        {
             modNum.quotient = intNum.RatNo [n] / 10;
             cout << modNum.quotient << "\t";             
             
        }
    cout << endl;

    cout << "Remainders Of Integers" << endl;//Remainder
    for (int m = 0; m < 10; m++)
        {
             modNum.remiander = intNum.RatNo [m] % 10;
             cout << modNum.remiander << "\t";
        }
    cout << endl;
    
    cout << "Actual Value of integer division" << endl; //value of division
    for (int q = 0; q < 10; q++)
        {
             modNum.value = intNum.RatNo [q] / 10;
             cout << setiosflags (ios::fixed);
             cout << setiosflags (ios::showpoint);
             cout << setprecision (2);
             cout << modNum.value << "\t";
        }
        cout << endl;
}

:icon_eek: i just realised i got my definition of a rational number wrong...rational number is in the form n = a/b. i dont think c++ has a way of displaying a number in such a form right?

I don't think there is a method in the C++ Standard. I would not be surprised at all though if someone has written a library/class that has such a display function and has shared it with the world. Boost libraries maybe? I've always just written my own function though.

only way i figured possible maybe was to make a 2-D array? but how should i modify my struct or should i just leave it as it is ?

I don't think you'd need a 2-D array. I'm a little confused about your structs, but I've always just had a struct called "rational" with two integer data members. One called "numerator" and one called "denominator". I don't know where the 2-D array part comes in and where you're thinking of putting it.

as VernonDozier suggested make a structure of rational no. and then in another structure if needed make array of rational structure.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.