#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <fstream>

using namespace std;

const int MAX = 51;
const int setArray = 5;
float initialArray[MAX][setArray];  
                   
void createRationalNo (int, int);
float RationalInfo (float[][setArray], int);
void printArray (float[][setArray], int);

int main()
{
    cout << "No\tP\tQ\tQuo\tRem\tValue\n\n";
    srand(time(NULL));
    int setPQ = 2;
    
    createRationalNo (MAX, setPQ);
    
    RationalInfo (initialArray, MAX);
    printArray (initialArray, MAX);
    cout << endl;
    
    system ("pause");
    return 0;
}

void createRationalNo (int size, int set)
{ 
     int i;
     int j;
     ofstream outfile;
     outfile.open("infile.txt");
     
     for (i = 0; i <size; i++)
     {
         for (j = 0; j < set; j++)
         {
             initialArray[i][j] = (rand () % 99) +1;
              outfile << initialArray[i][j] << " ";
              }
     }
     outfile.close();
 }
 
float RationalInfo (float initialArray[][setArray], int size)
{
    int quotient, remainder;
    float value;
    int j = 0;
    ifstream infile; 
    infile.open("infile.txt");
    for (int i = 0; i < size; i++)
    {
        int x, y;
        float z;
        infile >> x >> y;
        initialArray[i][j+2] = x / y;
        initialArray[i][j+3] = x % y;

        z = x / y;
        initialArray[i][j+4] = z;
        infile.close();
        
    }
     return 1;
} 
 
void printArray (float initialArray[][setArray], int size1)
{
     int count = 1;
     int j = 0;
     int i = 0;
     
     while (size1 > 1)
     {
           cout<< setiosflags(ios::fixed);
           cout<< setiosflags(ios::showpoint);
           cout<< setprecision(2);
           
           int p, q, r, s;
           p = initialArray[i][j];
           q = initialArray[i][j+1];
           r = initialArray[i][j+2];
           s = initialArray[i][j+3];
           
           cout << count 
                << "\t" << p << "\t" << q
                << "\t" << r << "\t" << s
                << "\t" << initialArray[i+4][j] << endl;
               
           j++; count ++; size1--;        
     }
 }

Hi guys, I need some help with my assignment.
The program's suppose to generate 50 random numbers, store them in a text file, then use those numbers and calculate their quotient, remainder and value.

Example:
No P Q Quo Rem Value

1 1 15 0 1 0.07
2 9 36 0 9 0.25
3 84 8 10 4 10.50
4 52 70 0 52 0.74
5 24 47 0 24 0.51
6 62 86 0 62 0.72
7 21 25 0 21 0.84
8 70 84 0 70 0.83
9 47 71 0 47 0.66
10 96 6 16 0 16.00

What my code produces now.

No P Q Quo Rem Value

1 31 21 1 10 88.00
2 21 1 10 1 63.00
3 1 10 1 80 1.00
4 10 1 80 48 10.00
5 1 80 48 1 1.00
6 80 48 1 10 68.00
7 48 1 10 1 33.00
8 1 10 1 58 1.00
9 10 1 58 84 10.00
10 1 58 84 1 1.00
11 58 84 1 10 53.00
12 84 1 10 1 53.00

This weird result pops up. Can anyone help me with it?
I'm trying to troubleshoot it now but no results...

Recommended Answers

All 5 Replies

>>The program's suppose to generate 50 random numbers
It isn't -- its generating 255 random numbers.

line 63: That is doing integer arithmethic because both the numberator and denominator are integers. If you want decimal places in the result of the division then you have to convert them to floats or doubles by either typecasting them or declaring x and y as floats. initialArray[i][j+2] = (float)x / (float)y;

The program's an array of 5 by 50 but I'm generating only 2 by 50 values as random, the rest are for allocating the results of remainder/quotient/value.

And thanks for the tip on line 63. I need that to be an integer.

Guess I got to revamp my whole code, looks way too messy.
I copied paste this code from my previous assignment cause the current one requires me to edit it.
=\

how do you expect to get a value like 0.07 with integers ?

Thanks for the replying, the code's suppose to produce for i.e. numbers 50 and 40.
quotient is 1, remainder is 1 and value is 1.25.
where both quotient and remainder = 50/40 but value is a float.

So I'm trying to get that value to be a float.
I decided to revamp the whole code to figure out what's wrong and tidy it up.

Hey, Why are you closing the input file inside the loop (line 68)?? surely you want to read all the data before closing the file!

commented: Nice catch +3
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.