Hi guys, need some help with this.
I googl'ed but found no solution. :icon_sad:

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

void createRationalNo (int, int);
int RationalInfo (int []);
//void printArray (int [], int);

const int MAX = 20;
const int setArray = 5;
int initialArray [setArray][MAX];                     


int main()
{
    cout << "No\tP\tQ\tQuo\tRem\tValue\n\n";
    srand(time(NULL));
    int sizeArray = rand () % MAX;
    
    createRationalNo (setArray, sizeArray);
    //RationalInfo(initialArray);
    //printArray (initialArray);
    
    
    cout << "\n\nSize of Array is " << sizeArray << "\n\n";
    
    system ("pause");
    return 0;
}


void createRationalNo (int set, int size)
{
     for (int i = 0; i <set; i++)
     {
         for (int j = 0; j < size; j++)
             initialArray [i][j] = rand () % 100;
     }
 }
 
 
int RationalInfo (int initialArray[])
{
    int quotient, remainder;
    float value;
    for (int i = 0; i <setArray; i++)
     {
         for (int j = 0; j < MAX; j++)
             {
                  initialArray [i+2][j] = initialArray [i][j] / initialArray [i+1][j];
                  
                  quotient = a/b
                  remainder = a%b
                  value = a/b             
             }
     }
}

It's not yet complete but I ran into a problem for int RationalInfo (int initialArray[]) at line 51.
I can't spot anything wrong with this.
An error invalid types int [int] for array subscript occurs when I try to compile.
Thanks for helping.

Recommended Answers

All 11 Replies

The errors boil down to the RationalInfo() function, even though you are not calling
it from anywhere, the compiler wants it to be OK, so fix it first or remove it if don't need it

int RationalInfo (int initialArray[][B][MAX][/B])
{
    [B]// You still need to declare the variables a and b here[/B]

    int quotient, remainder;
    float value;
    for (int i = 0; i <setArray; i++)
     {
         for (int j = 0; j < MAX; j++)
             {
                  initialArray [i+2][j] = initialArray [i][j] / initialArray [i+1][j];
                  
                  quotient = a/b [B];[/B]
                  remainder = a%b [B];[/B]
                  value = a/b [B];[/B]           
             }
     }

     [B]// You need to return something from this function[/B]
     return ???
}

Also, you indexing into the array at [i+1] and [i+2] will give you odd results for the first setArray-1 rows, and will go out of bounds in the last row.

dexter1984,
Why don't you simply use vector of vectors

#include <vector>

template <typename T>
class dynamic_array
{
public:
  dynamic_array(){};
  dynamic_array(int rows, int cols)
  {
    for(int i=0; i<rows; ++i)
    {
      data_.push_back(std::vector<T>(cols));
    }
  }
  
  // other ctors ....

  inline std::vector<T> & operator[](int i) { return data_[i]; }

  inline const std::vector<T> & operator[] (int i) const { return data_[i]; }

  // other accessors, like at() ...

  void resize(int rows, int cols)
  {
    data_.resize(rows);
    for(int i = 0; i < rows; ++i)
      data_[i].resize(cols);
  }

  // other member functions, like reserve()....

private:
  std::vector<std::vector<T> > data_;  
};


int main()
{
  dynamic_array<int> a(3, 3);
  a[1][1] = 2;
  int x = a[1][1];
  return 0;
}

The errors boil down to the RationalInfo() function, even though you are not calling
it from anywhere, the compiler wants it to be OK, so fix it first or remove it if don't need it

Thanks for pointing me out in the right direction, I didn't release the [MAX] was missing, first time using multi-dimensional arrays, so I'm still green at it.

Also, you indexing into the array at [i+1] and [i+2] will give you odd results for the first setArray-1 rows, and will go out of bounds in the last row.

Sorry I don't get you. The code's suppose to find the quotient, remainder and float value when 2 digits are compared. Should I put Max -1 as the value?

dexter1984,
Why don't you simply use vector of vectors

Sorry I'm really new to C++, started learning it 1 month ago. I'm on the file input/output part only, haven't learn any vectors/classes/pointers yet.
I do hope to learn more when I have some holidays but my final C++ exam's in less than a month.

Thanks to all for helping a new guy like me :) Have a nice day.

I need some help again, got stuck with one of the function calls...

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

void createRationalNo (int, int);
int RationalInfo (int []);
//void printArray (int [], int);

const int MAX = 20;
const int setArray = 5;
int initialArray [setArray][MAX];                     


int main()
{
    cout << "No\tP\tQ\tQuo\tRem\tValue\n\n";
    srand(time(NULL));
    int sizeArray = rand () % MAX;
    
    createRationalNo (setArray, MAX);
    RationalInfo (initialArray, MAX);
    //printArray (initialArray);
    
    
    cout << "\n\nSize of Array is " << sizeArray << "\n\n";
    
    system ("pause");
    return 0;
}


void createRationalNo (int set, int size)
{
     for (int i = 0; i <set; i++)
     {
         for (int j = 0; j < size; j++)
             initialArray [i][j] = rand () % 100;
     }
 }
 
 
int RationalInfo (int initialArray[][MAX])
{
    int quotient, remainder;
    float value;
    int i = 0;
    for (int j = 0; j < MAX; j++)
    {
        initialArray [i+2][j] = initialArray [i][j] / initialArray [i+1][j];
        initialArray [i+3][j] = initialArray [i][j] & initialArray [i+1][j];
        initialArray [i+4][j] = initialArray [i][j] / initialArray [i+1][j];
    }
     return 1;
}

Got stuck at line 22, I don't understand what's the problem.
I put the array and call the function but can't compile due to some argument.

The program's suppose to produce results:

No P Q Quotient Remainder Value
1 4 5 0 4 0.50
2 9 4 2 1 2.25
3 58 5 11 3 11.60

Up to a random number below 20.
I feel so frustrated aaaa.

Check your function prototype, versus how you've defined it. When you fix the prototype, you'll probably find that you should have put the constant declarations before the prototypes, since they use one or more of the constants.

It's also generally considered bad form to use global variables (your array) - that should be declared in main( ) and be passed as a parameter to the functions that need to access it.

And just what is this program supposed to be doing?

Thanks for the tip. I looked it over and changed afew lines of code. Got it working for most of the times... Anyway, the program is suppose to do this,

No P Q Quo Rem Value

1 88 60 1 28 1
2 2 22 0 2 0
3 84 87 0 84 0

Where Quo = 5 / 2 = 2, Remainder = 5 / 2 = 1, and Value = 5 /2 = 2.5

But I don't understand how to change the value column to a float value with 2 decimal places.

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

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

int main()
{
    cout << "No\tP\tQ\tQuo\tRem\tValue\n\n";
    srand(time(NULL));
    int sizeArray = rand () % MAX;
    
    createRationalNo (setArray, MAX-1);
    RationalInfo (initialArray, sizeArray-1);
    printArray (initialArray, sizeArray-1);
    
    
    cout << "\n\nSize of Array is " << sizeArray << "\n\n";
    
    system ("pause");
    return 0;
}

void createRationalNo (int set, int size)
{ 
     int i;
     int j;
     for (i = 0; i <set; i++)
     {
         for (j = 0; j < size; j++)
             initialArray [i][j] = rand () % 100;
     }
 }
 
int RationalInfo (int initialArray[][MAX], int size)
{
    int quotient, remainder;
    float value;
    int i = 0;
    for (int j = 0; j < size; j++)
    {
        initialArray [i+2][j] = initialArray [i][j] / initialArray [i+1][j];
        initialArray [i+3][j] = initialArray [i][j] % initialArray [i+1][j];
        initialArray [i+4][j] = initialArray [i][j] / initialArray [i+1][j];
    }
     return 1;
} 
 
void printArray (int initialArray[][MAX], int size1)
{
     int count = 1;
     int j = 0;
     int i = 0;
     
     while (size1 > 1)
     {
           cout << count << "\t" << initialArray [i][j] 
                << "\t" << initialArray [i+1][j] 
                << "\t" << initialArray [i+2][j]
                << "\t" << initialArray [i+3][j] 
                << "\t" << initialArray [i+4][j] << endl;
           j++; count ++; size1--;        
     }
 }

Sorry for the long code, it's the best I can do with my abilities now.
The program sometimes hang up when running, kinda weird.
And I can't get the last value column as a float value though.

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

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

int main()
{
    cout << "No\tP\tQ\tQuo\tRem\tValue\n\n";
    srand(time(NULL));
    int sizeArray = rand () % MAX;
    
    createRationalNo (setArray, MAX-1);
    RationalInfo (initialArray, sizeArray-1);
    printArray (initialArray, sizeArray-1);
    
    
    cout << "\n\nSize of Array is " << sizeArray << "\n\n";
    
    system ("pause");
    return 0;
}

void createRationalNo (int set, int size)
{ 
     int i;
     int j;
     for (i = 0; i <set; i++)
     {
         for (j = 0; j < size; j++)
              initialArray [i][j] = rand () % 100;
     }
 }
 
int RationalInfo (int initialArray[][MAX], int size)
{
    int quotient, remainder;
    float value;
    int i = 0;
    for (int j = 0; j < size; j++)
    {
        initialArray[i+2][j] = initialArray[i][j] / initialArray[i+1][j];
        initialArray[i+3][j] = initialArray[i][j] % initialArray[i+1][j];
        
    }
     return 1;
} 
 
void printArray (int initialArray[][MAX], int size1)
{
     int count = 1;
     int j = 0;
     int i = 0;
     
     while (size1 > 1)
     {
           float value, a, b;
           a = initialArray[i][j];
           b = initialArray[i+1][j];
           value  =   a/b ;

           cout<< setiosflags(ios::fixed);
           cout<< setiosflags(ios::showpoint);
           cout<< setprecision(2);
           
           cout << count 
                << "\t" << initialArray[i][j] << "\t" << initialArray[i+1][j] 
                << "\t" << initialArray[i+2][j] << "\t" << initialArray[i+3][j]
                << "\t" << value << endl;
               
           j++; count ++; size1--;        
     }
 }

I edited my code to make the last column value to be a float and 2 decimal places.
But I'm having weird problems when running, there's a chance of the program hanging and having an error.
Can't seem to find out where though...

A couple of things to fix ...

int main()
{
    srand(time(NULL));
    int sizeArray = rand () % MAX;
//  at this point, sizeArray may very well be zero, 
//  probably you don't want that, so add a check against 
//  that condition ...
<snip>
}
int RationalInfo (int initialArray[][MAX], int size)
{  <snip>
    for (int j = 0; j < size; j++)
    {
        // if initialArray[i+1][j] is zero, your program will fail here, 
        // you must not divide by zero, ever
        initialArray[i+2][j] = initialArray[i][j] / initialArray[i+1][j];

        // the same thing with % operator, initialArray[i+1][j] must not 
        // be zero here
        initialArray[i+3][j] = initialArray[i][j] % initialArray[i+1][j];
    }
     return 1;
}

Aaa I see, I didn't notice that those can't be 0, no wonder my program was hanging sometimes.
I'll do some troubleshooting and post back if I have any questions.
Thanks for pointing those out.

Wow that really worked! No more errors popping up (or maybe I'm just that lucky).
Thanks bro. I'll be sure to note that in my future codes.

you forgot this:

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

void createRationalNo (int, int);
int RationalInfo (int []);
//void printArray (int [], int);

const int MAX = 20;
const int setArray = 5;
int initialArray [setArray][MAX];                     


int main()
{
    cout <<
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.