Here are several questions inspired by various incorrect answers to Question 23 on midterm 2.
|
> gcc q1_1.c |
> cc q1_1.c |
> cc q1_1.c |
> cat q23c.c
/* q23.c
Incorrect Answer to Question 23, CISC105 Exam 2, Fall 2004
P. Conrad
*/
#include <stdio.h>
int main(void)
{
int i; /* loop counter */
int a[10]; /* array with 10 elements */
int min = a[0]; /* minimum element in array */
/* prompt user for ten integers */
printf("Please enter ten integers: ");
/* use scanf to get the integers and put the integers into an array */
for (i=0; i<10; i++)
scanf("%d",&a[i]);
/* print out the array on a single line */
printf("array is: ");
for (i=0; i<10; i++)
{
printf("%d ",a[i]);
if (a[i] < min)
min = a[i];
}
printf("\nmin is: %d\n",min);
return 0;
}
> gcc q23c.c -o q23c
> ./q23c
Please enter ten integers: 3 2 5 4 8 2 7 8 3 4
array is: 3 2 5 4 8 2 7 8 3 4
min is: -12791248
>
|
min = a[0]; min = a[i]; min = 0; min = 10;
#include <stdio.h>
int main(void)
{
int i; /* loop counter */
int a[10]; /* array with 10 elements */
int min; /* minimum element in array */
/* prompt user for ten integers */
printf("Please enter ten integers: ");
/* use scanf to get the integers and put the integers into an array */
for (i=0; i<10; i++)
{
scanf("%d",&a[i]);
}
/* print out the array on a single line */
printf("array is: ");
for (i=0; i<10; i++)
{
printf("%d ",a[i]);
}
printf("\n");
/* print the minimum value in the array */
@@@@@ /* initialize min = ??? */
for (i=1; i<10; i++)
{
if (a[i] < a[min])
min = i;
}
printf("min is: %d\n",a[min]);
return 0;
}
|