Update: It could be complied in OS X but could not in Linux and Windows with MinGW.
Given the rainfall in mm for each month, display a horizontal, text-based histogram showing the rainfall per month.
Here's my code
// This variation prints out the actual names of the months instead of numbers.
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
const int WIDTH = 70;
const int NUM_MONTHS = 12;
int max = 0;
int rainfall[NUM_MONTHS];
char months[NUM_MONTHS][11] = { "January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
int i;
for(i = 0; i < NUM_MONTHS; i++)
{
printf("Enter %s rainfall: ", months[i]);
scanf("%d", &rainfall[i]);
if(rainfall[i] > max)
{
max = rainfall[i];
}
}
for(i = 0; i < NUM_MONTHS; i++)
{
printf("%s", months[i]);
int len = strlen(months[i]);
int j;
for(j = len; j < 10; j++)
{
printf(" ");
}
int num_stars = WIDTH * (rainfall[i]/(float)max);
for(j = 0; j < num_stars; j++)
{
printf("*");
}
printf("\n");
}
}
When I try to compile it with gcc, it shows error and warning.
.\rainfall-graph-names.c: In function 'main':
.\rainfall-graph-names.c:12:4: error: variable-sized object may not be initialized
char months[NUM_MONTHS][11] = { "January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
.\rainfall-graph-names.c:12:4: warning: excess elements in array initializer [enabled by default]
.\rainfall-graph-names.c:12:4: warning: (near initialization for 'months') [enabled by default]
I don't know why it doesn't work and I don't know why. Is there anyone could help? Thanks