/* * program to count using a loop * this is the framework; the counting is done in a function to be supplied * by the student * * this is for homework 1, question 1, for ECS 36A Spring 2024 * * Matt Bishop, ECS 36A * version 1 April 6, 2024 * original version */ #include #include #include #include /* * m is the starting number * n is the ending number */ void loop(int m, int n) { /* YOUR CODE GOES HERE */ } /* * getint -- get an integer * * in parameters: * char *str the string to be converted to an integer * int *error location for integer error code * * out parameters: * int *error 1 if there is an error, 0 otherwise * * return value: * on success, integer resulting from converting str * on failure, -1; *error is set to 1 and error message is printed */ int getint(char *str, int *error) { long m; /* holds value of number in str */ char *mend = str; /* points to char beyond end of number */ /* * initialize error codes */ *error = 0; errno = 0; /* * convert the string to a number */ m = strtol(str, &mend, 10); /* possible error 1: non-integer char in string */ if (*mend != '\0' || str == mend){ fprintf(stderr, "%s is not an integer\n", str); *error = 1; return(-1); } /* it's a number but is too big or small to be an int */ if (errno == ERANGE){ fprintf(stderr, "%s is too big or small\n", str); *error = 1; return(-1); } /* okay, it's a valid integer; convert it and return */ return((int) m); } /* * the main return */ int main(int argc, char *argv[]) { int m; /* starting point */ int n; /* ending point */ int d; /* increment */ int err; /* place for error code */ /* * check that you got 2 arguments, * the starting point and the ending point */ if (argc != 3){ fprintf(stderr, "Usage: %s m n\n", argv[0]); return(1); } /* get the starting point */ if ((m = getint(argv[1], &err)) == -1 && err) return(1); /* get the ending point */ if ((n = getint(argv[2], &err)) == -1 && err) return(1); /* * do the loop */ /* announce the for loop and do it */ loop(m, n); /* * all done -- give good exit code */ return(0); }