2d Array problems

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2009
Posts: 44
Reputation: RobBrown is an unknown quantity at this point 
Solved Threads: 0
RobBrown's Avatar
RobBrown RobBrown is offline Offline
Light Poster

2d Array problems

 
-1
  #1
Aug 2nd, 2009
I am having some problems with writing a 2d array, then displaying it, and then showing the sum of all of the elements. A major part of this problem is that i nave to write three seperate functions to achieve this. it is supposed to do this:

For this exercise you will use both arrays and functions! Write a program called matrix.c that adds every element in a 2-dimensional array (matrix). Your matrix will be 5 x 5 (5 rows and 5 columns). The requirement is to use nested "for loops" in your program.

You must develop 3 functions within this program: One to read user input; another to display the 2-D array; and a third that returns the total of all elements in the 2-D array. The 3rd function should NOT print the value, rather the value should be returned from the function to main, and main will display the result. Your function prototype might look like this: int addArray( int arr[ ][ COLSIZE ] );

Make the program flexible so that you can easily change the size of the table. A suggestion for doing so is to use a define, for example: #define rowSIZE 5.

Input
5 x 5 matrix

Output
Output the matrix

Output the matrix sum

Below is a sample of expected output (my input is in blue - this is to illustrate only, yours will not be blue):

Please enter enter data for a 5 by 5 array:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

Our Array to sum is:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

The total of all elements in our array is: 325
Press any key to continue . . .
this is the code i have, i fear it is way wrong as i am getting error codes of "expected primary-expression before 'int' " , "expected ';' before 'int' " (both of these in my main staement line 47), and "expected '}' at end of input " (line 78 in my return statement)...

Here is my "sure to be flawed" code:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define rowSIZE 5
  5. #define colSIZE 5
  6.  
  7. int myMatrix[rowSIZE][colSIZE];
  8.  
  9. int myFunction(void)
  10. {
  11. int row = 0,
  12. col = 0;
  13.  
  14. for (row = 0; row < rowSIZE; row++)
  15. for (col = 0; col < colSIZE; col++)
  16. printf("Please enter the data for our %d by %d array: ", rowSIZE, colSIZE);
  17. scanf("%i", &myMatrix[row][col]);
  18. }
  19.  
  20.  
  21. int printArray(int myMatrix[rowSIZE][colSIZE])
  22. {
  23. int row, col;
  24.  
  25. for ( row = 0; row < rowSIZE ; row++ )
  26.  
  27. for ( col = 0; col < colSIZE ; col++ )
  28.  
  29. printf("%d\t", myMatrix[ row ][ col ]);
  30.  
  31. printf("\n");
  32. }
  33.  
  34. int sumArray(int myMatrix[rowSIZE][colSIZE])
  35. {
  36. int row, col, sum = 0;
  37.  
  38. for ( row = 0; row < rowSIZE ; row++ ) {
  39.  
  40. for ( col = 0; col < colSIZE ; col++ )
  41.  
  42. sum += myMatrix[row][col];
  43.  
  44. return sum ;
  45. }
  46.  
  47. int main (void)
  48.  
  49. {
  50. int sum = 0;
  51. int row, col;
  52. int arrayToSum[rowSIZE][colSize];
  53.  
  54. for ( row = 0; row < ROWSIZE ; row++ ) {
  55.  
  56. for ( col = 0; col < COLSIZE ; col++ )
  57. {
  58. arrayToSum[row][col] = myFunction(myMatrix[row][col]);
  59. }
  60.  
  61. for ( row = 0; row < ROWSIZE ; row++ ) {
  62.  
  63. for ( col = 0; col < COLSIZE ; col++ )
  64. {
  65. arrayToSum[row][col] = printArray(myMAtrix[row][col])
  66. }
  67.  
  68. for ( row = 0; row < ROWSIZE ; row++ ) {
  69.  
  70. for ( col = 0; col < COLSIZE ; col++ )
  71. {
  72. arrayToSum[row][col] = sumArray(int myMatrix[row][col])
  73. }
  74.  
  75. printf("This is the sum of our matrix: %d\n", sum);
  76.  
  77. return sum;
  78. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 36
Reputation: 9868 is on a distinguished road 
Solved Threads: 7
9868 9868 is offline Offline
Light Poster

Re: 2d Array problems

 
0
  #2
Aug 2nd, 2009
Your code is full of errors.
1. Check for matching curly braces.
2. main is returning "sum", it should return 0
3. Check for semicolons
4. C is case sensitive. What are ROWSIZE, COLSIZE?
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 44
Reputation: RobBrown is an unknown quantity at this point 
Solved Threads: 0
RobBrown's Avatar
RobBrown RobBrown is offline Offline
Light Poster

Re: 2d Array problems

 
0
  #3
Aug 2nd, 2009
I want main to return the sum, will it not work like that?
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 36
Reputation: 9868 is on a distinguished road 
Solved Threads: 7
9868 9868 is offline Offline
Light Poster

Re: 2d Array problems

 
0
  #4
Aug 2nd, 2009
No. According to C standards main should only return 0, nothing else. Keep in mind it's a special function, you can't do things you do with other ones.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 44
Reputation: RobBrown is an unknown quantity at this point 
Solved Threads: 0
RobBrown's Avatar
RobBrown RobBrown is offline Offline
Light Poster

Re: 2d Array problems

 
0
  #5
Aug 2nd, 2009
okay so here is something else i have been working on for this project, i am having problems with using nested for statements with the functions, and i havent set up the sum function either. any suggestions on getting the nested for loops to work how the assignment says they should would help, also how to return a sum function to main would too.

Here is what i have:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define R_SIZE 5
  5. #define C_SIZE 5
  6.  
  7. void userInput (int matrix [R_SIZE][C_SIZE]) { //gathering user input
  8. scanf("%d", &matrix [R_SIZE][C_SIZE]);
  9. }
  10. void displayArray(int matrix [R_SIZE][C_SIZE]) { //dissplaying the Matrix
  11. printf("%d)", matrix [R_SIZE][C_SIZE]);
  12. }
  13.  
  14. int main(void)
  15. {
  16. int row, col;
  17. int array[R_SIZE][C_SIZE];
  18.  
  19. //UI Header
  20. printf("Please enter enter data for a 5 by 5 array:\n");
  21.  
  22. //Gather User Input
  23. for (row = 0; row < R_SIZE; row++)
  24. for (col = 0; col < C_SIZE; col++)
  25.  
  26. {
  27. userInput(array[row][col]);
  28. }
  29.  
  30.  
  31. //Display User Input
  32. printf("\n\n\nOur Array to sum is:\n");
  33. for (row = 0; row < R_SIZE; row++)
  34. for (col = 0; col < C_SIZE; col++)
  35.  
  36. {
  37. displayArray(array[row][col]);
  38. printf("\n");
  39.  
  40. }
  41.  
  42. system("PAUSE");
  43. return 0;
  44. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 44
Reputation: RobBrown is an unknown quantity at this point 
Solved Threads: 0
RobBrown's Avatar
RobBrown RobBrown is offline Offline
Light Poster

Re: 2d Array problems

 
0
  #6
Aug 2nd, 2009
I think this is really close it just some tweaking that after hours of looking at a screen is hard to find. for some reason i am having a problem with the matrix inout, after i enter the 5x5 numbers it does not print them it just waits for more input and never gets to a point where it stops and scans into the array....I am confused about what is going on here can anyone help?


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define R_SIZE 5
  5. #define C_SIZE 5
  6.  
  7. void userInput (int matrix[R_SIZE][C_SIZE], int a, int b) //gathering user input
  8. {
  9. int row, col;
  10. for (row = 0; row < R_SIZE; row++)
  11. for (col = 0; col < C_SIZE; col++)
  12. scanf("%d", &matrix[R_SIZE][C_SIZE]);
  13. }
  14.  
  15. void displayArray(int matrix[R_SIZE][C_SIZE], int a, int b) //displaying the Matrix
  16. {
  17. int row, col;
  18. for (row = 0; row < R_SIZE; row++)
  19. for (col = 0; col < C_SIZE; col++)
  20. printf("%d)", matrix[R_SIZE][C_SIZE]);
  21. }
  22.  
  23. int sumArray(int sum) //summing the matrix
  24. {
  25. int row, col, sum;
  26. for (row = 0; row < R_SIZE; row++)
  27. for (col = 0; col < C_SIZE; col++)
  28. sum += matrix[row][col];
  29.  
  30. return sum;
  31. }
  32.  
  33.  
  34. int main(void)
  35. {
  36. int row, col, sum = 0;
  37. int array[R_SIZE][C_SIZE];
  38.  
  39. //UI Header
  40. printf("Please enter enter data for a 5 by 5 array:\n");
  41.  
  42. //Gather User Input
  43. for (row = 0; row < R_SIZE; row++)
  44. for (col = 0; col < C_SIZE; col++)
  45.  
  46. {
  47. userInput(array, row, col);
  48. }
  49.  
  50.  
  51. //Display User Input
  52. printf("\n\n\nOur Array to sum is:\n");
  53. for (row = 0; row < R_SIZE; row++)
  54. for (col = 0; col < C_SIZE; col++)
  55.  
  56. {
  57. displayArray(array, row, col);
  58. printf("\n");
  59.  
  60. }
  61.  
  62. printf("The sum of our array is: %d", sumArray(sum));
  63.  
  64. system("PAUSE");
  65. return 0;
  66. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 201
Reputation: yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold 
Solved Threads: 35
yellowSnow's Avatar
yellowSnow yellowSnow is offline Offline
Posting Whiz in Training

Re: 2d Array problems

 
0
  #7
Aug 2nd, 2009
Just a few points:

Always use curly braces for your for statements (and other control mechanisms as well e.g. while) - it will save you a lot of heartache especially later on if you decide to add more lines of code that should be executed as part of the for statement.

Keep your for loops encapsulated within your functions - there should be no reason to have them in main(). Your main function should be very simple and short - it should only have to call the three functions for creating, displaying and summing the array. For example:

  1. int main (void) {
  2.  
  3. int myArray[ROWSIZE][COLSIZE];
  4.  
  5. createArray(myArray);
  6. printArray(myArray);
  7. printf("The total of all elements in our array is %d\n", sumArray(myArray));
  8.  
  9. return 0;
  10. }

If you have learnt about function prototypes, then use them to declare your functions.

As I said in another thread earlier, write some pseudocode to work out in your head how to solve the problem before any coding. Get one function working first (e.g. getting user input into 2-d array) before moving onto the next function.

Good luck!
Manic twiddler of bits
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 46
Reputation: zalezog is an unknown quantity at this point 
Solved Threads: 11
zalezog zalezog is offline Offline
Light Poster

Re: 2d Array problems

 
0
  #8
Aug 2nd, 2009
hmm, let's C
  1. void userInput (int matrix[R_SIZE][C_SIZE], int row, int col) //gathering user input
  2. {
  3. /*You are using a for loop in main().This function just accepts input for a paticular row and column*/
  4. /* This is the trouble >>
  5.   scanf("%d", &matrix[R_SIZE][C_SIZE]);
  6.   Here is the major problem.This should have resulted in a crash or seg - fault after a few runs owing to
  7.  "array index out of bounds".
  8. you specified that row < R_SIZE
  9. Should be
  10. */
  11. scanf("%d", &matrix[row][col]);
  12.  
  13.  
  14. }
  15.  
  16. void displayArray(int matrix[R_SIZE][C_SIZE], int row, int col) //displaying the Matrix
  17. {
  18. /*Same here
  19.   printf("%d)", matrix[R_SIZE][C_SIZE]);
  20. */
  21. printf("%d)", matrix[row][col]);
  22.  
  23. }
  24.  
  25. int sumArray(int sum) //summing the matrix
  26. {
  27. int row, col, sum;
  28. for (row = 0; row < R_SIZE; row++)
  29. for (col = 0; col < C_SIZE; col++)
  30. sum += matrix[row][col];
  31.  
  32. return sum;
  33. }
  34.  
  35.  
  36. int main(void)
  37. {
  38. int row, col, sum = 0;
  39. int array[R_SIZE][C_SIZE];
  40.  
  41. //UI Header
  42. printf("Please enter enter data for a 5 by 5 array:\n");
  43.  
  44. //Gather User Input
  45. for (row = 0; row < R_SIZE; row++)
  46. for (col = 0; col < C_SIZE; col++)
  47.  
  48. {
  49. userInput(array, row, col);
  50. }
  51.  
  52.  
  53. //Display User Input
  54. printf("\n\n\nOur Array to sum is:\n");
  55. for (row = 0; row < R_SIZE; row++)
  56. for (col = 0; col < C_SIZE; col++)
  57.  
  58. {
  59. displayArray(array, row, col);
  60. printf("\n");
  61.  
  62. }
  63.  
  64. printf("The sum of our array is: %d", sumArray(sum));
  65.  
  66. /*system("PAUSE"); Hey don't use this .*/
  67. getchar(); /*or its equivalent loop which eats characters until
  68.   \n is encountered*/
  69. return 0;
  70. }
Actually the reason why its stopping even after you entered 25 elements is that it actually expects 625 elements. Going by your 'original 'user input function.I changed just the names of the function parameters to row and col in both user input and output functions.
Last edited by zalezog; Aug 2nd, 2009 at 5:34 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 201
Reputation: yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold 
Solved Threads: 35
yellowSnow's Avatar
yellowSnow yellowSnow is offline Offline
Posting Whiz in Training

Re: 2d Array problems

 
0
  #9
Aug 2nd, 2009
I still think the OP's better option is to keep main() simple and let the other functions (as prescribed in the assignment) do "the work". Use the main code in my previous post as a template.

@RobBrown
Here's a heads up for the function that would prompt the user for input and load the array.

  1. void createArray(int arr2D[][COLSIZE]) {
  2.  
  3. int row = 0, col = 0;
  4.  
  5. printf("Please enter the data for our %d by %d array:\n", ROWSIZE, COLSIZE);
  6. for (row = 0; row < ROWSIZE; row++) {
  7. for (col = 0; col < COLSIZE; col++) {
  8. scanf("%d", &arr2D[row][col]);
  9. }
  10. }
  11. }

I would suggest adding another line of code (via printf) to instruct the user of the program to enter COLSIZE integers each separated by a space and then pressing ENTER. That way you get a row of input at a time.

Using the above code as a template it should be a relatively straightforward exercise to code the "print array " and "sum array" functions.

A little hint for the "print array " function: you need to check when to print a newline character so that your output matches the requirement specified in the assignment (i.e. printing 5 integers per line of output). You can do this using modulo arithmetic (via the % operator).

Cheers,
JD
Last edited by yellowSnow; Aug 2nd, 2009 at 6:24 am.
Manic twiddler of bits
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 44
Reputation: RobBrown is an unknown quantity at this point 
Solved Threads: 0
RobBrown's Avatar
RobBrown RobBrown is offline Offline
Light Poster

Re: 2d Array problems

 
0
  #10
Aug 2nd, 2009
I'm still having a hard time with the create array function. I use this code:

  1. void createArray(int arr2D[][C_SIZE])
  2. {
  3. int row = 0, col = 0;
  4.  
  5. printf("Please enter the data for our %d by %d array:\n", R_SIZE, C_SIZE);
  6.  
  7. for (row = 0; row < R_SIZE; row++)
  8. {
  9.  
  10. for (col = 0; col < C_SIZE; col++)
  11. {
  12.  
  13. scanf("%d", &arr2D[row][col]);
  14.  
  15. }
  16.  
  17. }
  18.  
  19. }

the scanf function is still waiting for more than 25 variables, i am not sure what in my code is causing this too happen...

Any suggestions?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1053 | Replies: 25
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC