Homework - error C2059: syntax error : ']'

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2005
Posts: 3
Reputation: renork is an unknown quantity at this point 
Solved Threads: 0
renork renork is offline Offline
Newbie Poster

Homework - error C2059: syntax error : ']'

 
0
  #1
Jul 3rd, 2005
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.




  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. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,728
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 737
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #2
Jul 3rd, 2005
>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:
  1. small(array[0], sumc, sumr, r, h, w);
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 3
Reputation: renork is an unknown quantity at this point 
Solved Threads: 0
renork renork is offline Offline
Newbie Poster

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

 
0
  #3
Jul 3rd, 2005
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'


  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. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,728
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 737
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #4
Jul 3rd, 2005
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:
  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
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC