You have a large number of issue, Undefined Behavior being the most critical:
n -= 1;
for(i=0;i<=n;i++)
{
b[i]=a[n-i];
}
When i == n
, you index one beyond the end of your b[]
array. All arrays in C are zero-indexed, so your loop limits are 0 <= i < n
. By using i<=n
you loop one-too-many times.
You fail to check the return of any user-input. Try entering '"one"', or accidentally hitting 'r'
instead of 4
and see what happens. You must check the return for every user-input and handle the error. What happens if the user enters 1, 2, 'r', 5
? You must also empty stdin
of any extraneous characters or they will bite you again on your next input.
Don't use conio.h
. That makes your code 100% non-portable. You can use the functions in stdio.h
for your needs.
That said, when you need to do repetitive tasks like taking integer input, write a short function to do it. You can write one that takes a pointer to the integer to fill, and a pointer to the prompt to display and returns 0
on success or -1
if the user cancels input by generating a manual EOF
with Ctrl+d on Linux or Ctrl+z on windows. You can add a helper-function to empty stdin
, e.g.
void empty_stdin (void)
{
int c = getchar();
while (c != '\n' && c != EOF)
c = getchar();
}
int getint (int *n, const char *prompt)
{
int rtn;
for (;;) {
fputs (prompt, stdout);
rtn = scanf ("%d", n);
if (rtn == EOF) {
fputs (" (user canceled input)\n", stderr);
return -1;
}
else if (rtn == 0) {
fputs (" error: invalid integer input.\n", stderr);
empty_stdin();
}
else
break;
}
empty_stdin();
return 0;
}
The rest of your program to read an array in a[]
and reverse it in b[]
is simply:
int main (void) {
int i = 0, n;
if (getint (&n, "Enter array parameter: ") == -1)
return 0;
int a[n], b[n];
for (i = 0; i < n; i++) {
char buf[128];
sprintf (buf, "enter value for a[%d]: ", i+1);
if (getint (&a[i], buf) == -1)
return 0;
}
for (i = 0; i < n; i++)
b[i] = a[n - 1 - i];
puts ("\nvalue of array b[]:");
for (i = 0; i < n; i++)
printf (" %d", b[i]);
putchar ('\n');
#if defined (_WIN32) || defined (_WIN64)
getchar(); /* hold terminal open on windows - type any char, hit return */
#endif
}
Example Use/Output
All valid inputs:
$ ./bin/getarray
Enter array parameter: 5
enter value for a[1]: 5
enter value for a[2]: 4
enter value for a[3]: 3
enter value for a[4]: 2
enter value for a[5]: 1
value of array b[]:
1 2 3 4 5
Errors in input:
$ ./bin/getarray
Enter array parameter: foo
error: invalid integer input.
Enter array parameter: 5
enter value for a[1]: 5
enter value for a[2]: four
error: invalid integer input.
enter value for a[2]: 4
enter value for a[3]: 3
enter value for a[4]: I'm getting tired of this game...
error: invalid integer input.
enter value for a[4]: 2
enter value for a[5]: 1
value of array b[]:
1 2 3 4 5
User cancels input:
$ ./bin/getarray
Enter array parameter: 5
enter value for a[1]: 5
enter value for a[2]: 4
enter value for a[3]: 3
enter value for a[4]: (user canceled input)
Look things over and let me know if you have further questions.