Skip to main content
deleted 41 characters in body; edited tags
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

I created a simple program in C and I want to hear your opinions.

The program reads a .txt file that contains lines of numbers. The first one has one number N and the second one has N\$N\$ numbers as the first line says.

The N\$N\$ numbers is 1 <= N <= 1.000\$1 \le N \le 1.000.000\$.000

The program read the N\$N\$ numbers in an array and makeperforms the following procedure. Read the number one by one backwards in the array and check to find the max value. Every time a new max value is found the int total increases by one. In the end we write the total value in an output file.:

Read the number one by one backwards in the array and check to find the max value. Every time a new max value is found the int total increases by one. In the end we write the total value in an output file.

I created a simple program in C and I want to hear your opinions.

The program reads a .txt file that contains lines of numbers. The first one has one number N and the second one has N numbers as the first line says.

The N numbers is 1 <= N <= 1.000.000

The program read the N numbers in an array and make the following procedure. Read the number one by one backwards in the array and check to find the max value. Every time a new max value is found the int total increases by one. In the end we write the total value in an output file.

The program reads a .txt file that contains lines of numbers. The first one has one number N and the second one has \$N\$ numbers as the first line says.

The \$N\$ numbers is \$1 \le N \le 1.000.000\$.

The program read the \$N\$ numbers in an array and performs the following procedure:

Read the number one by one backwards in the array and check to find the max value. Every time a new max value is found the int total increases by one. In the end we write the total value in an output file.

Rollback to Revision 4
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

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

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_t count_children(FILE *input);
int pass_heights(FILE *input, int *children, size_t size);
int check_tall(const int *children, size_t size);
void write_output(int total,const char *filename_w);


/*---------------------*/
/* start of program    */
/*---------------------*/
int main(void) {

    const char *filename_r = "xxx.in";
    const char *filename_w = "xxx.out";

    FILE *input = read_input(filename_r);

    size_t size = count_children(input);

    int *children = malloc(size * sizeof *children);
    if (children==NULL)
    exit(1); //General application error

    pass_heights(input, children, size);

    fclose(input);

    int total = check_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(IOERROR);

    return input;
}

size_t count_children(FILE *input) {

    size_t 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, size_t size) {

    for(size_t i = 0; i < size; i++)
    fscanf(input, "%d",&children[i]);

    return *children;
}
 

int check_tall(const int *children, size_t size) {
    
    int total = 0;
    size_t tmp_max = 0;
    for(int i = size - 1; i >= 0; i--)
    {
        if(children[i] > tmp_max) {
            tmp_max = children[i];
            total++;
        }
    }
    return total;
}

void write_output(int total,const char *filename_w) {

    FILE *output = fopen(filename_w, "w");

    if(output == NULL)
    exit(IOERROR); 

    fprintf(output, "%d\n", total);
    fclose(output);
}
#include <stdio.h>
#include <stdlib.h>

/*---------------------*/
/* defined constants   */
/* for restriction     */
/*---------------------*/
#define MIN 1
#define MAX 1000000

/*---------------------*/
/* function prototypes */
/*---------------------*/
int main(void);
FILE *read_input(char *filename_r);
int count_children(FILE *input);
int pass_heights(FILE *input, int *children,const unsigned int size);
int chck_tall(const int *children,const unsigned int size);
void write_output(const unsigned short total,char *filename_w);


/*----------------------------------*/
/* start of program - Main Function */
/*----------------------------------*/
int main(void) {

    char *filename_r = "xxx.in";
    char *filename_w = "xxx.out";

    FILE *input = read_input(filename_r);

    unsigned int size = count_children(input);

    int *children = malloc(size * sizeof(unsigned short));
    if (children==NULL)
    exit(1); //General application error

    pass_heights(input, children, size);

    fclose(input);

    unsigned short total = chck_tall(children, size);

    free(children);

    write_output(total,filename_w);

    return 0;
}

FILE *read_input(char *filename_r) {

    FILE *input = fopen(filename_r, "r");

    if(input == NULL)
    exit(66); //EXIT_NOINPUT 'cannot open input'

    return input;
}

int count_children(FILE *input) {

    unsigned 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 unsigned int size) {

    for(register int i = 0; i < size; i++)
    fscanf(input, "%d",&children[i]);

    return *children;
}

int chck_tall(const int *children,const unsigned int size) {

    unsigned short total = 0;
    unsigned 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(const unsigned short total,char *filename_w) {

    FILE *output = fopen(filename_w, "w");

    if(output == NULL)
    exit(74); //EXIT_IOERR 'input/output error'

    fprintf(output, "%d\n", total);
    fclose(output);
}
added 21 characters in body
Source Link
Nikos KLon
  • 349
  • 2
  • 10
/*---------------------*/
/* 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);
intsize_t count_children(FILE *input);
int pass_heights(FILE *input, int *children, intsize_t size);
int check_tall(const int *children, intsize_t size);
void write_output(int total,const char *filename_w);


/*---------------------*/
/* start of program    */
/*---------------------*/
int main(void) {

    const char *filename_r = "xxx.in";
    const char *filename_w = "xxx.out";

    FILE *input = read_input(filename_r);

    intsize_t size = count_children(input);

    int *children = malloc(size * sizeof *children);
    if (children==NULL)
    exit(1); //General application error

    pass_heights(input, children, size);

    fclose(input);

    int total = check_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(IOERROR);

    return input;
}
 

intsize_t count_children(FILE *input) {

    intsize_t 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, intsize_t size) {

    for(intsize_t i = 0; i < size; i++)
    fscanf(input, "%d",&children[i]);

    return *children;
}
 


int check_tall(const int *children, intsize_t size) {
    
    int total = 0;
    size_t tmp_max = 0;
    for(int i = size - 1; i >= 0; i--)
    {
        if(children[i] > tmp_max) {
            tmp_max = children[i];
            total++;
        }
    }
    return total;
}
 

void write_output(int total,const char *filename_w) {

    FILE *output = fopen(filename_w, "w");

    if(output == NULL)
    exit(IOERROR); 

    fprintf(output, "%d\n", total);
    fclose(output);
}
/*---------------------*/
/* 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);
int count_children(FILE *input);
int pass_heights(FILE *input, int *children, int size);
int check_tall(const int *children, int size);
void write_output(int total,const char *filename_w);


/*---------------------*/
/* start of program    */
/*---------------------*/
int main(void) {

    const char *filename_r = "xxx.in";
    const char *filename_w = "xxx.out";

    FILE *input = read_input(filename_r);

    int size = count_children(input);

    int *children = malloc(size * sizeof *children);
    if (children==NULL)
    exit(1); //General application error

    pass_heights(input, children, size);

    fclose(input);

    int total = check_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(IOERROR);

    return input;
}
 

int count_children(FILE *input) {

    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, int size) {

    for(int i = 0; i < size; i++)
    fscanf(input, "%d",&children[i]);

    return *children;
}
 


int check_tall(const int *children, int size) {
    
    int total = 0;
    size_t tmp_max = 0;
    for(int i = size - 1; i >= 0; i--)
    {
        if(children[i] > tmp_max) {
            tmp_max = children[i];
            total++;
        }
    }
    return total;
}
 

void write_output(int total,const char *filename_w) {

    FILE *output = fopen(filename_w, "w");

    if(output == NULL)
    exit(IOERROR); 

    fprintf(output, "%d\n", total);
    fclose(output);
}
/*---------------------*/
/* 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_t count_children(FILE *input);
int pass_heights(FILE *input, int *children, size_t size);
int check_tall(const int *children, size_t size);
void write_output(int total,const char *filename_w);


/*---------------------*/
/* start of program    */
/*---------------------*/
int main(void) {

    const char *filename_r = "xxx.in";
    const char *filename_w = "xxx.out";

    FILE *input = read_input(filename_r);

    size_t size = count_children(input);

    int *children = malloc(size * sizeof *children);
    if (children==NULL)
    exit(1); //General application error

    pass_heights(input, children, size);

    fclose(input);

    int total = check_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(IOERROR);

    return input;
}

size_t count_children(FILE *input) {

    size_t 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, size_t size) {

    for(size_t i = 0; i < size; i++)
    fscanf(input, "%d",&children[i]);

    return *children;
}


int check_tall(const int *children, size_t size) {
    
    int total = 0;
    size_t tmp_max = 0;
    for(int i = size - 1; i >= 0; i--)
    {
        if(children[i] > tmp_max) {
            tmp_max = children[i];
            total++;
        }
    }
    return total;
}

void write_output(int total,const char *filename_w) {

    FILE *output = fopen(filename_w, "w");

    if(output == NULL)
    exit(IOERROR); 

    fprintf(output, "%d\n", total);
    fclose(output);
}
deleted 33 characters in body
Source Link
Nikos KLon
  • 349
  • 2
  • 10
Loading
Updated to v2 code
Source Link
Nikos KLon
  • 349
  • 2
  • 10
Loading
Tweeted twitter.com/#!/StackCodeReview/status/554412026966327296
Word usage change
Source Link
user34073
user34073
Loading
edited body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237
Loading
Source Link
Nikos KLon
  • 349
  • 2
  • 10
Loading