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);
}