| | |
MultiDimension Array help
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jan 2008
Posts: 49
Reputation:
Solved Threads: 2
Hi guys, need some help with this.
I googl'ed but found no solution.
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.
I googl'ed but found no solution.
cpp Syntax (Toggle Plain Text)
#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.
Last edited by dexter1984; Feb 16th, 2008 at 2:59 pm.
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
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
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 ??? }
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.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
dexter1984,
Why don't you simply use vector of vectors
Why don't you simply use vector of vectors
C++ Syntax (Toggle Plain Text)
#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; }
•
•
Join Date: Jan 2008
Posts: 49
Reputation:
Solved Threads: 2
•
•
•
•
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
•
•
•
•
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'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. •
•
Join Date: Jan 2008
Posts: 49
Reputation:
Solved Threads: 2
I need some help again, got stuck with one of the function calls...
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.
cpp Syntax (Toggle Plain Text)
#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?
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?
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Jan 2008
Posts: 49
Reputation:
Solved Threads: 2
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.
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.
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)
#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.
•
•
Join Date: Jan 2008
Posts: 49
Reputation:
Solved Threads: 2
cpp Syntax (Toggle Plain Text)
#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...
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
A couple of things to fix ...
cpp Syntax (Toggle Plain Text)
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; }
![]() |
Similar Threads
- Sort A Multidimension Array (Python)
- Return multidimension array (C)
Other Threads in the C++ Forum
- Previous Thread: open fil in same catalog as .exe file
- Next Thread: Array (sorting)
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






