Update:
First I want to thank you all! Really! :)
Now to the point. I read all your suggestions one by one. I can't write replys for all though, it would take many lines.!
I saved all of your comments, but I have my own style to write code. My problem isn't the style as it is but more the working of my code and if everything is in place.
I also want to tell you that the numbers in the file are all represent heights of childrens ( that's why I used unsihned short).
I make an update and I want to know if it's better.
Remember! --> I want to know if my programm work perfectly! I don't want to check the readability. Also I don't need to take restrictions for the input file.
My programm enviroment is Linux so I want to check that all works in that platform!
Updated Code!
/*---------------------*/
/* included files */
/*---------------------*/
#include <stdio.h>
#include <stdlib.h>
/*---------------------*/
/* defined constants */
/* for restriction */
/*---------------------*/
#define MIN 1
#define MAX 1000000
#define IOERROR 74 //EXIT_IOERR 'input/output error'
/*---------------------*/
/* function prototypes */
/*---------------------*/
int main(void);
FILE *read_input(const char *filename_r);
size_tint count_children(FILE *input);
int pass_heights(FILE *input, int *children,const size_tunsigned int size);
int check_tallchck_tall(const int *children,const size_tunsigned int size);
void write_output(intconst unsigned short total,const char *filename_w);
/*----------------------------------*/
/* start of program - Main Function */
/*----------------------------------*/
int main(void) {
const char *filename_r = "xxx.in";
const char *filename_w = "xxx.out";
FILE *input = read_input(filename_r);
size_tunsigned int size = count_children(input);
int *children = malloc(size * sizeof(unsigned *childrenshort));
if (children==NULL)
exit(1); //General application error
pass_heights(input, children, size);
fclose(input);
intunsigned short total = check_tallchck_tall(children, size);
free(children);
write_output(total,filename_w);
return 0;
}
FILE *read_input(const char *filename_r) {
FILE *input = fopen(filename_r, "r");
if(input == NULL)
exit(IOERROR66); //EXIT_NOINPUT 'cannot open input'
return input;
}
size_tint count_children(FILE *input) {
size_tunsigned int count = 0;
fscanf(input, "%d",&count);
if(count > MAX || count < MIN)
exit(1); //General application error
return count;
}
int pass_heights(FILE *input, int *children,const size_tunsigned int size) {
for(size_tregister int i = 0; i < size; i++)
fscanf(input, "%d",&children[i]);
return *children;
}
int check_tallchck_tall(const int *children,const size_tunsigned int size) {
intunsigned short total = 0;
size_tunsigned short tmp_max = 0;
for(register int i = size - 1; i >= 0; i--)
{
if(children[i] > tmp_max) {
tmp_max = children[i];
total++;
}
}
return total;
}
void write_output(intconst unsigned short total,const char *filename_w) {
FILE *output = fopen(filename_w, "w");
if(output == NULL)
exit(IOERROR74); //EXIT_IOERR 'input/output error'
fprintf(output, "%d\n", total);
fclose(output);
}