Monday, May 24, 2010

Whow to create a c program which would accept 10 integers and store in an array?

Here is a simple implementation. It reads from command line arguments and does no error checking on its input...





void usage(int exitcode) {


printf("Usage:\n");


printf("./progname int1 int2 int3 ... int10\n\n");


exit(exitcode);


}





int main(int argc, char ** argv) {


int count = 0;


int * num = NULL;


if (argc != 11) usage(1);


num = malloc(10 * sizeof(int));


if (num == NULL) return 1;


for (count = 1; count %26lt;= 10; count++) {


num[count - 1] = atoi(argv[count]);


}





for (count = 0; count %26lt; 10; count++) {


printf("Integer at pos %d is %d\n", count + 1, num[count]);


}





free(num);


exit(0);


}

floral deliveries

No comments:

Post a Comment