I am trying to write a program that shows a histogram. I keep getting segmentation fault (core dumped) each time I run it. I am reading in a file with numbers in it and trying to find the occurrences of each number and to print a histogram from it. I have looked all over but can not seem to find why this is happening. Please tell me why.
INPUT random.txt
1405413042132140413140
DESIRED OUTPUT
0 ****
1 ******
2 **
3 ***
4 ******
5 *
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <unistd.h>
int main(int argc, char *argv[]){
int n = 1001;
int a[n];
int opt;
FILE *f;
if(argc == 5){
f = fopen(argv[5], "r");
}
else if(argc == 4){
f = fopen(argv[4], "r");
}
else if(argc == 3){
f = fopen(argv[3], "r");
}
else if(argc == 2){
f = fopen(argv[2], "r");
}
else if(argc == 1){
f = fopen(argv[1], "r");
}
else if(f == NULL){
printf("ERROR: File Not Found\n");
return 1;
}
}