Thread: im a programming noob, having trouble with arrays

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    2

    Unhappy Help getting chart to add correctly

    I am having trouble getting the chart to print correctly. I attached the whole source so anyone can run and see the problem. The total for each size is wrong except for the extra large.
    I am new at this and I could be missing something obvious. Please help. Thanks. I noted what I think is the problem below, along with the full attachment.

    Code:
     /* /* mp4id230.c - sweatshop inventory program 
      /* find_college_total() - this function finds the total number shirts per type.         
       */
      void find_college_total(int shirts[][SHIRTS], int colleges[]) {
              int total = 0, i, j;
              for (i = 0; i < SHIRTS; i++) {
              total = 0;
                      for (j = 0; j < COLLEGES; j++) {
                              total += shirts[i][j];
                      }
                         colleges[i] = total;
              }
      }
      
      ------------------------------------------------------------------------
      /* output_mainbody() - This function is just a loop that goes through the 2D
       *    array so the chart has some numbers in it.  It from time to time calls
       *     the output_left() function so it has names on the left side
       */
      void output_mainbody(int shirts[COLLEGES][SHIRTS], int totalcollege[]) {
          int j, i;
              for(j = 0; j < SHIRTS; j++) { 
              NL;
                      output_left(j);
                      for(i = 0; i < COLLEGES; i++) { 
                              printf("%d    ",shirts[i][j]);
                      }
                      printf("%d\n",totalcollege[j]);
              }
      }
      
      /* output_chart() - the chart that gets printed out.  pretty much this just runs the
       *    other functions like main()
       */
      void output_chart(int shirts[COLLEGES][SHIRTS], int colleges[], int inv[]) {
          int total = 0, type = 0;
      
          output_header(COLLEGES);
      
          output_mainbody(shirts, colleges);
          output_bottom(inv, colleges);
      
      }    
      
              return 0;
      } */

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    This:
    >>for (j = 0; j <= COLLEGES; j++)
    ... would suggest you're accessing the array incorrectly. You should go from 0 to n-1, which means your code would be
    >>for (j = 0; j < COLLEGES; j++)

    There are other occurences of the type type of error you'll need to fix. This type of thing won't get picked up by the compiler, and will be hard to trace/debug.

    In your function find_college_total(), your loop control is incorrect. I think you're after doing this:
    Code:
     void find_college_total(int shirts[][SHIRTS], int colleges[])
     {
       int total,
           i,
           j;
     
       for (i = 0; i < COLLEGES; i++)
       {
         total = 0;
         for (j = 0; j < SHIRTS; j++)
         {
           total += shirts[i][j];
         }
     
         colleges[i] = total;
       }
     }
    If you're having trouble getting a program to work, break it down into managable chunks. Here's some sample code I wrote to determine what that last function was doing.
    Code:
     #include <stdio.h>
     
     #define SHIRTS	5
     #define COLLEGES  3
     
     void find_college_total(int shirts[][SHIRTS], int colleges[])
     {
       int total,
     	  i,
     	  j;
     
       for (i = 0; i < COLLEGES; i++)
       {
     	total = 0;
     	for (j = 0; j < SHIRTS; j++)
     	{
     	  total += shirts[i][j];
     	}
     
     	colleges[i] = total;
       }
     }
     
     int main(void)
     {
       int shirts[COLLEGES][SHIRTS] = 
       { 
     	{ 1, 4, 7, 10, 11 }, /* College 0, total value 33 */
     	{ 2, 5, 8, 12, 13 }, /* College 1, total value 40 */
     	{ 3, 6, 9, 13, 15 }  /* College 2, total value 46 */
       };
       
       int colleges[3] = { 0 };
       int i;
     
       find_college_total(shirts, colleges);
       
       for (i = 0; i < COLLEGES; i++)
       {
     	printf("%d\n", colleges[i]);
       }
     
       return(0);
     }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    2
    Thanks. It worked out fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 07:59 AM
  2. Manipulating Character arrays in functions.
    By kbro3 in forum C++ Programming
    Replies: 11
    Last Post: 08-16-2008, 02:24 AM
  3. Replies: 7
    Last Post: 06-04-2008, 10:39 PM
  4. Noob having trouble storing strings
    By wiramu in forum C++ Programming
    Replies: 6
    Last Post: 12-26-2003, 06:22 AM
  5. arrays arrays arrays....
    By Jan79 in forum C++ Programming
    Replies: 1
    Last Post: 11-11-2002, 07:16 AM