944,027 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 43761
  • C++ RSS
Jul 3rd, 2005
0

Homework - error C2059: syntax error : ']'

Expand Post »
Last error Im getting is error C2059: syntax error : ']'
Anyone have any ideas?


what it needs to do:write a c++ program that creates using a random number generation a 2 dimentional array of integers whos values range from -9 to 9. The array size is to be chosen by the user of the program. Your program will then print the sum of the numbers that are in the same rowor column as the smallest entry. If the smallest entry occurs more then once you can pick which you want it to use (*I am just trying to use the first*) The main program is to use a function that returns the row and column position of the smallest entry. Allow for arrays up to 10x10 and the user to input the array info.




C++ Syntax (Toggle Plain Text)
  1.  
  2. #include <iostream>
  3. #include <ctime>
  4. using namespace std;
  5.  
  6. void small(int [],int, int, int, int &,int &);
  7.  
  8. int main()
  9. {
  10. int r,c,q=0,h=0,w=0,array[10][10],sumr=0,sumc=0;
  11.  
  12. srand((unsigned)time(NULL));
  13.  
  14. cout << "Enter the hight of the array: ";
  15. cin >> h;
  16. cout << "Enter the width of the array: ";
  17. cin >> w;
  18. cout << endl;
  19.  
  20. for( r = 0 ; r < h ; r++ ){
  21. for( c = 0 ; c < w ; c ++ ){
  22. array[r][c] = rand()%19-9;
  23. }
  24. }
  25. for( r = 0 ; r < h ; r++ ){
  26. for( c = 0 ; c < w ; c ++ ){
  27. cout << array[r][c] << ' ';
  28. }
  29. cout << endl;
  30. }
  31. small(*array[], &sumc, &sumr, r, h, w);
  32.  
  33. return 0;
  34. }
  35.  
  36. void small(int *array[],int &sumc,int &sumr,int r,int h, int w)
  37. {
  38. int i = -9;
  39. cout << "The row sum is: " << sumr;
  40. for(int r = 0 ; r < h ; r++ )
  41. {
  42. for(int c = 0 ; c < w ; c ++ )
  43. {
  44. if(array[r][c]==i)
  45. {
  46. cout << "smallest row is: " << r << endl << "smallest column is: " << c;
  47. for (int g = 0; g < 10; g++)
  48. {
  49. sumc = sumc + array[r][g];
  50. sumr = sumr + array[g][c];
  51.  
  52. }
  53. }
  54. else
  55. i--;
  56. }
  57. }
  58. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
renork is offline Offline
3 posts
since Jun 2005
Jul 3rd, 2005
0

Re: Homework - error C2059: syntax error : ']'

>small(*array[], &sumc, &sumr, r, h, w);
The empty subscript only works for declarations. Either remove [], or remove * and change [] to [0]. Also, &sumc and &sumr are pointers, not int as small expects. This will compile, but it may not do what you want because you clearly have issues with the difference between single and multi-dimensional arrays:
C++ Syntax (Toggle Plain Text)
  1. small(array[0], sumc, sumr, r, h, w);
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 3rd, 2005
0

Re: Homework - error C2059: syntax error : ']'

yea, I changed it around a little bit before but am now getting a new error.
error C2664: 'small' : cannot convert parameter 2 from 'int *__w64 ' to 'int'


C++ Syntax (Toggle Plain Text)
  1.  
  2. #include <iostream>
  3. #include <ctime>
  4. using namespace std;
  5.  
  6. void small(int [10][10],int, int, int, int &,int &);
  7.  
  8. int main()
  9. {
  10. int r,c,q=0,h=0,w=0,array[10][10],sumr=0,sumc=0;
  11.  
  12. srand((unsigned)time(NULL));
  13.  
  14. cout << "Enter the hight of the array: ";
  15. cin >> h;
  16. cout << "Enter the width of the array: ";
  17. cin >> w;
  18. cout << endl;
  19.  
  20. for( r = 0 ; r < h ; r++ ){
  21. for( c = 0 ; c < w ; c ++ ){
  22. array[r][c] = rand()%19-9;
  23. }
  24. }
  25. for( r = 0 ; r < h ; r++ ){
  26. for( c = 0 ; c < w ; c ++ ){
  27. cout << array[r][c] << ' ';
  28. }
  29. cout << endl;
  30. }
  31. small(array, &sumc, &sumr, r, h, w); // error C2664: 'small' : cannot convert parameter 2 from 'int *__w64 ' to 'int'
  32.  
  33. getchar();
  34. getchar();
  35. return 0;
  36. }
  37.  
  38. void small(int array[10][10],int &sumc,int &sumr,int r,int h, int w)
  39. {
  40. int i = -9;
  41. cout << "The row sum is: " << sumr;
  42. for(int r = 0 ; r < h ; r++ )
  43. {
  44. for(int c = 0 ; c < w ; c ++ )
  45. {
  46. if(array[r][c]==i)
  47. {
  48. cout << "smallest number is:" << array[r][c] << endl;
  49. for (int g = 0; g < 10; g++)
  50. {
  51. sumc = sumc + array[r][g];
  52. sumr = sumr + array[g][c];
  53.  
  54. }
  55. }
  56. else
  57. i--;
  58. }
  59. }
  60. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
renork is offline Offline
3 posts
since Jun 2005
Jul 3rd, 2005
0

Re: Homework - error C2059: syntax error : ']'

Okay, first, make sure that your function declaration and defintion match (copy/paste is useful). As it is, your declaration says that the last two parameters are references while the definition says that the second and third parameters are references and the last two are simple integers. Second, you only need to use the ampersand in a function call when the function expects a pointer:
C++ Syntax (Toggle Plain Text)
  1. // Declaration and definition types must match. Parameter names
  2. // don't matter, but they can be useful documentation
  3. void small(int array[10][10],int &sumc,int &sumr,int r,int h, int w);
  4.  
  5. void small(int array[10][10],int &sumc,int &sumr,int r,int h, int w)
  6. {
  7. }
  8.  
  9. small(array, sumc, sumr, r, h, w); // This will compile now
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 20th, 2010
-1
Re: Homework - error C2059: syntax error : ']'
Narue is right.. must remove []

and make that as
C++ Syntax (Toggle Plain Text)
  1. small(array, sumc, sumr, r, h, w);
Last edited by icasta13; Dec 20th, 2010 at 7:48 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
icasta13 is offline Offline
18 posts
since Dec 2010

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: cpp
Next Thread in C++ Forum Timeline: Debug vs Release





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


Follow us on Twitter


© 2011 DaniWeb® LLC