2D Dynamically allocated pointer arrays
Hi!
I'm trying to create (insert subject)!
I get an error "missing ';' before type" on the line where I declare my pointer. Can anyone tell me what I'm doing wrong?
Code:
#include<stdio.h>
//#include<stdlib.h>
/*
This program demonstrates recursive functions and dynamic, multi-dimensional pointer arrays.
It will calculate the total factorial value for multiple groups of items.
You enter the total number of items, and the number of groups you will have. Then you enter the
number of items in each group. The program error-checks to make sure the number of items
entered in each groups adds up to be the total number of items.
*/
// Prototypes.
unsigned long factr (unsigned long n);
void main ()
{
// These are in order of appearance.
unsigned long num_items = 0;
unsigned long num_groups = 0;
unsigned long loop = 0;
unsigned long total = 0;
unsigned long quantity = 0;
unsigned long numerator = 0;
unsigned long denominator = 1;
printf ("\n\nHow many items total?\n");
scanf ("%u", &num_items);
printf ("\n\nHow many groups?\n");
scanf ("%u", &num_groups);
if (num_groups > num_items)
{
printf("\n\nError! Can't have more groups than items!\n\n");
exit (1);
}
// Array dimensioning.
unsigned long *group_total[num_groups][2];
group_total = malloc (num_groups*2*sizeof(int));
if(!group_total)
{
puts ("\n\nERROR! Not enough Memory!\n\n");
exit (1);
}
// Array initialization.
for (loop = 1; loop <= num_items; loop++)
{
group_total[loop][1] = 0;
group_total[loop][2] = 0;
}
loop = 0;
// Array data-entry.
for (loop = 1; loop <= num_groups; loop++)
{
printf ("\n\nHow many in group %d\n", loop);
scanf ("%u", &quantity);
group_total[loop][1] = quantity;
total = total + quantity;
}
loop = 0;
// Error checking and factorial calculations for each group
if (total = num_groups);
{
for (loop = 1; loop <= num_groups; loop++)
{
group_total[loop][2] = factr (quantity);
}
loop = 0;
}
else
{
printf ("\n\nTotal number of items not equal to total items entered!\n\n");
free (group_total);
exit (1);
}
total = 0;
// Final calculations.
numerator = factr (num_items);
for (loop = 1; loop <= num_groups; loop++)
{
denominator = denominator * group_total[loop][2];
}
total = numerator / denominator;
printf ("\n\nTotal factorial for this mess: %u\n\n", total);
total = 0;
loop = 0;
free (group_total);
}
// Factorial engine.
unsigned long factr (unsigned long n)
{
unsigned long answer;
if (n==1) return (1);
answer = n==0 ? 1: n * factr (n - 1); // recursive call
return (answer);
}