943,630 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1549
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 16th, 2008
0

MultiDimension Array help

Expand Post »
Hi guys, need some help with this.
I googl'ed but found no solution.

cpp Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4. using namespace std;
  5.  
  6. void createRationalNo (int, int);
  7. int RationalInfo (int []);
  8. //void printArray (int [], int);
  9.  
  10. const int MAX = 20;
  11. const int setArray = 5;
  12. int initialArray [setArray][MAX];
  13.  
  14.  
  15. int main()
  16. {
  17. cout << "No\tP\tQ\tQuo\tRem\tValue\n\n";
  18. srand(time(NULL));
  19. int sizeArray = rand () % MAX;
  20.  
  21. createRationalNo (setArray, sizeArray);
  22. //RationalInfo(initialArray);
  23. //printArray (initialArray);
  24.  
  25.  
  26. cout << "\n\nSize of Array is " << sizeArray << "\n\n";
  27.  
  28. system ("pause");
  29. return 0;
  30. }
  31.  
  32.  
  33. void createRationalNo (int set, int size)
  34. {
  35. for (int i = 0; i <set; i++)
  36. {
  37. for (int j = 0; j < size; j++)
  38. initialArray [i][j] = rand () % 100;
  39. }
  40. }
  41.  
  42.  
  43. int RationalInfo (int initialArray[])
  44. {
  45. int quotient, remainder;
  46. float value;
  47. for (int i = 0; i <setArray; i++)
  48. {
  49. for (int j = 0; j < MAX; j++)
  50. {
  51. initialArray [i+2][j] = initialArray [i][j] / initialArray [i+1][j];
  52.  
  53. quotient = a/b
  54. remainder = a%b
  55. value = a/b
  56. }
  57. }
  58. }

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.
Last edited by dexter1984; Feb 16th, 2008 at 2:59 pm.
Similar Threads
Reputation Points: 31
Solved Threads: 2
Light Poster
dexter1984 is offline Offline
49 posts
since Jan 2008
Feb 16th, 2008
1

Re: MultiDimension Array help

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[][MAX])
{
    // You still need to declare the variables a and b here

    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 ;           
             }
     }

     // You need to return something from this function
     return ???
}
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Feb 16th, 2008
1

Re: MultiDimension Array help

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.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Feb 16th, 2008
0

Re: MultiDimension Array help

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

C++ Syntax (Toggle Plain Text)
  1. #include <vector>
  2.  
  3. template <typename T>
  4. class dynamic_array
  5. {
  6. public:
  7. dynamic_array(){};
  8. dynamic_array(int rows, int cols)
  9. {
  10. for(int i=0; i<rows; ++i)
  11. {
  12. data_.push_back(std::vector<T>(cols));
  13. }
  14. }
  15.  
  16. // other ctors ....
  17.  
  18. inline std::vector<T> & operator[](int i) { return data_[i]; }
  19.  
  20. inline const std::vector<T> & operator[] (int i) const { return data_[i]; }
  21.  
  22. // other accessors, like at() ...
  23.  
  24. void resize(int rows, int cols)
  25. {
  26. data_.resize(rows);
  27. for(int i = 0; i < rows; ++i)
  28. data_[i].resize(cols);
  29. }
  30.  
  31. // other member functions, like reserve()....
  32.  
  33. private:
  34. std::vector<std::vector<T> > data_;
  35. };
  36.  
  37.  
  38. int main()
  39. {
  40. dynamic_array<int> a(3, 3);
  41. a[1][1] = 2;
  42. int x = a[1][1];
  43. return 0;
  44. }
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Feb 16th, 2008
0

Re: MultiDimension Array help

Click to Expand / Collapse  Quote originally posted by mitrmkar ...
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.

Click to Expand / Collapse  Quote originally posted by vmanes ...
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?

Click to Expand / Collapse  Quote originally posted by SpS ...
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.
Reputation Points: 31
Solved Threads: 2
Light Poster
dexter1984 is offline Offline
49 posts
since Jan 2008
Feb 17th, 2008
0

Re: MultiDimension Array help

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

cpp Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4. using namespace std;
  5.  
  6. void createRationalNo (int, int);
  7. int RationalInfo (int []);
  8. //void printArray (int [], int);
  9.  
  10. const int MAX = 20;
  11. const int setArray = 5;
  12. int initialArray [setArray][MAX];
  13.  
  14.  
  15. int main()
  16. {
  17. cout << "No\tP\tQ\tQuo\tRem\tValue\n\n";
  18. srand(time(NULL));
  19. int sizeArray = rand () % MAX;
  20.  
  21. createRationalNo (setArray, MAX);
  22. RationalInfo (initialArray, MAX);
  23. //printArray (initialArray);
  24.  
  25.  
  26. cout << "\n\nSize of Array is " << sizeArray << "\n\n";
  27.  
  28. system ("pause");
  29. return 0;
  30. }
  31.  
  32.  
  33. void createRationalNo (int set, int size)
  34. {
  35. for (int i = 0; i <set; i++)
  36. {
  37. for (int j = 0; j < size; j++)
  38. initialArray [i][j] = rand () % 100;
  39. }
  40. }
  41.  
  42.  
  43. int RationalInfo (int initialArray[][MAX])
  44. {
  45. int quotient, remainder;
  46. float value;
  47. int i = 0;
  48. for (int j = 0; j < MAX; j++)
  49. {
  50. initialArray [i+2][j] = initialArray [i][j] / initialArray [i+1][j];
  51. initialArray [i+3][j] = initialArray [i][j] & initialArray [i+1][j];
  52. initialArray [i+4][j] = initialArray [i][j] / initialArray [i+1][j];
  53. }
  54. return 1;
  55. }

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.
Reputation Points: 31
Solved Threads: 2
Light Poster
dexter1984 is offline Offline
49 posts
since Jan 2008
Feb 17th, 2008
0

Re: MultiDimension Array help

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?
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Feb 17th, 2008
0

Re: MultiDimension Array help

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.

cpp Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4. using namespace std;
  5.  
  6. const int MAX = 20;
  7. const int setArray = 5;
  8. int initialArray [setArray][MAX];
  9. void createRationalNo (int, int);
  10. int RationalInfo (int [][MAX], int);
  11. void printArray (int [][MAX], int);
  12.  
  13. int main()
  14. {
  15. cout << "No\tP\tQ\tQuo\tRem\tValue\n\n";
  16. srand(time(NULL));
  17. int sizeArray = rand () % MAX;
  18.  
  19. createRationalNo (setArray, MAX-1);
  20. RationalInfo (initialArray, sizeArray-1);
  21. printArray (initialArray, sizeArray-1);
  22.  
  23.  
  24. cout << "\n\nSize of Array is " << sizeArray << "\n\n";
  25.  
  26. system ("pause");
  27. return 0;
  28. }
  29.  
  30. void createRationalNo (int set, int size)
  31. {
  32. int i;
  33. int j;
  34. for (i = 0; i <set; i++)
  35. {
  36. for (j = 0; j < size; j++)
  37. initialArray [i][j] = rand () % 100;
  38. }
  39. }
  40.  
  41. int RationalInfo (int initialArray[][MAX], int size)
  42. {
  43. int quotient, remainder;
  44. float value;
  45. int i = 0;
  46. for (int j = 0; j < size; j++)
  47. {
  48. initialArray [i+2][j] = initialArray [i][j] / initialArray [i+1][j];
  49. initialArray [i+3][j] = initialArray [i][j] % initialArray [i+1][j];
  50. initialArray [i+4][j] = initialArray [i][j] / initialArray [i+1][j];
  51. }
  52. return 1;
  53. }
  54.  
  55. void printArray (int initialArray[][MAX], int size1)
  56. {
  57. int count = 1;
  58. int j = 0;
  59. int i = 0;
  60.  
  61. while (size1 > 1)
  62. {
  63. cout << count << "\t" << initialArray [i][j]
  64. << "\t" << initialArray [i+1][j]
  65. << "\t" << initialArray [i+2][j]
  66. << "\t" << initialArray [i+3][j]
  67. << "\t" << initialArray [i+4][j] << endl;
  68. j++; count ++; size1--;
  69. }
  70. }

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.
Reputation Points: 31
Solved Threads: 2
Light Poster
dexter1984 is offline Offline
49 posts
since Jan 2008
Feb 17th, 2008
0

Re: MultiDimension Array help

cpp Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4. #include <iomanip>
  5. using namespace std;
  6.  
  7. const int MAX = 21;
  8. const int setArray = 5;
  9. int initialArray[setArray][MAX];
  10.  
  11. void createRationalNo (int, int);
  12. int RationalInfo (int[][MAX], int);
  13. void printArray (int[][MAX], int);
  14.  
  15. int main()
  16. {
  17. cout << "No\tP\tQ\tQuo\tRem\tValue\n\n";
  18. srand(time(NULL));
  19. int sizeArray = rand () % MAX;
  20.  
  21. createRationalNo (setArray, MAX-1);
  22. RationalInfo (initialArray, sizeArray-1);
  23. printArray (initialArray, sizeArray-1);
  24.  
  25.  
  26. cout << "\n\nSize of Array is " << sizeArray << "\n\n";
  27.  
  28. system ("pause");
  29. return 0;
  30. }
  31.  
  32. void createRationalNo (int set, int size)
  33. {
  34. int i;
  35. int j;
  36. for (i = 0; i <set; i++)
  37. {
  38. for (j = 0; j < size; j++)
  39. initialArray [i][j] = rand () % 100;
  40. }
  41. }
  42.  
  43. int RationalInfo (int initialArray[][MAX], int size)
  44. {
  45. int quotient, remainder;
  46. float value;
  47. int i = 0;
  48. for (int j = 0; j < size; j++)
  49. {
  50. initialArray[i+2][j] = initialArray[i][j] / initialArray[i+1][j];
  51. initialArray[i+3][j] = initialArray[i][j] % initialArray[i+1][j];
  52.  
  53. }
  54. return 1;
  55. }
  56.  
  57. void printArray (int initialArray[][MAX], int size1)
  58. {
  59. int count = 1;
  60. int j = 0;
  61. int i = 0;
  62.  
  63. while (size1 > 1)
  64. {
  65. float value, a, b;
  66. a = initialArray[i][j];
  67. b = initialArray[i+1][j];
  68. value = a/b ;
  69.  
  70. cout<< setiosflags(ios::fixed);
  71. cout<< setiosflags(ios::showpoint);
  72. cout<< setprecision(2);
  73.  
  74. cout << count
  75. << "\t" << initialArray[i][j] << "\t" << initialArray[i+1][j]
  76. << "\t" << initialArray[i+2][j] << "\t" << initialArray[i+3][j]
  77. << "\t" << value << endl;
  78.  
  79. j++; count ++; size1--;
  80. }
  81. }

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...
Reputation Points: 31
Solved Threads: 2
Light Poster
dexter1984 is offline Offline
49 posts
since Jan 2008
Feb 17th, 2008
0

Re: MultiDimension Array help

A couple of things to fix ...

cpp Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. srand(time(NULL));
  4. int sizeArray = rand () % MAX;
  5. // at this point, sizeArray may very well be zero,
  6. // probably you don't want that, so add a check against
  7. // that condition ...
  8. <snip>
  9. }
  10. int RationalInfo (int initialArray[][MAX], int size)
  11. { <snip>
  12. for (int j = 0; j < size; j++)
  13. {
  14. // if initialArray[i+1][j] is zero, your program will fail here,
  15. // you must not divide by zero, ever
  16. initialArray[i+2][j] = initialArray[i][j] / initialArray[i+1][j];
  17.  
  18. // the same thing with % operator, initialArray[i+1][j] must not
  19. // be zero here
  20. initialArray[i+3][j] = initialArray[i][j] % initialArray[i+1][j];
  21. }
  22. return 1;
  23. }
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: can somebody please explain these codes for me step by step.
Next Thread in C++ Forum Timeline: Beginners problems with user defined functions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC