### C Program Question

The smallest number in the array is 2, but for some reason my program is saying the smallest number is 1, which isn't even in the array. The part in the code that isn't working is part (d). Any help would be greatly appreciated, thank you in advance!

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576`` ``````#include void print(int a[], int size) { int i; for (i = 0; i < size; i++) { printf("%d", a[i]); printf("\t"); } } int findSmallest(int a[], int size) { int min = 0; int i; for (i = 0; i < size; i++) { if(a[min] > a[i]) min = i; } return min; } int main() { int a[] = {5, 15, 34, 54, 14, 2, 52, 72}; int *p = &a[1], *q = &a[5]; int temp; int min; printf("*~*~The original array*~*~\n"); print(a, 8); printf("\n"); /* (a) Swap element 1 with element 5 */ temp = *p; *p = *q; *q = temp; printf("\n"); printf("*~*~Swap element 1 and 5*~*~\n"); print(a, 8); printf("\n"); /* (b) Swap element 2 with element 0 */ temp = a[2]; a[2] = a[0]; a[0] = temp; printf("\n"); printf("*~*~Swap element 2 and 0*~*~\n"); print(a, 8); printf("\n"); /* (c) Swap element 4 with element 7 */ temp = a[4]; a[4] = a[7]; a[7] = temp; printf("\n"); printf("*~*~Swap element 4 and 7*~*~\n"); print(a, 8); printf("\n\n"); /* (d) Find the smallest element */ printf("*~*~Find the Smallest Element*~*~\n"); min = findSmallest(a, 8); printf("The smallest element is %d\n", min); /* (e) Swap the smallest number with the first element in the array */ printf("*~*~Set the smallest number to the first position in array*~*~\n"); temp = a[0]; a[0] = a[min]; a[min] = temp; print(a, 8); printf("\n"); return 0; }``````
`return a[min];`
thanks so much!
@mysiarobin1987

Your function, findSmallest(int a[], int size), is wrong.

try
 ``1234567891011`` ``````int findSmallest(int a[], int size) { int min = a[0]; // Assign min to first value in a array int i; for (i = 0; i < size; i++) { if(a[i] < min ) // Check if next value is lower than min min = a[i]; // if yes, give min the value of a[i] } return min; // return lowest value }``````
Last edited on
i got the program to print the smallest element correctly but now part e isn't working when it was working before
Last edited on
You are getting confused between the value of the smallest element in the array and its index. Your original code was returning its index, which you would need for your part (e).

Perhaps you had better go back to returning min as the index (as in your original function) but on line 65 where you printf it then output a[min] rather than min.
@mysiarobin1987

I added in the swap in section e.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283`` ``````#include void print(int a[], int size) { int i; for (i = 0; i < size; i++) { printf("%d", a[i]); printf("\t"); } } int findSmallest(int a[], int size) { int min = a[0]; // Assign min to first value in a array int i; for (i = 0; i < size; i++) { if(a[i] < min ) // Check if next value is lower than min min = a[i]; // if yes, give min the value of a[i] } return min; // return lowest vlaue } int main() { int a[] = {5, 15, 34, 54, 14, 2, 52, 72}; int *p = &a[1], *q = &a[5]; int temp; int min; printf("*~*~The original array*~*~\n"); print(a, 8); printf("\n"); /* (a) Swap element 1 with element 5 */ temp = *p; *p = *q; *q = temp; printf("\n"); printf("*~*~Swap element 1 and 5*~*~\n"); print(a, 8); printf("\n"); /* (b) Swap element 2 with element 0 */ temp = a[2]; a[2] = a[0]; a[0] = temp; printf("\n"); printf("*~*~Swap element 2 and 0*~*~\n"); print(a, 8); printf("\n"); /* (c) Swap element 4 with element 7 */ temp = a[4]; a[4] = a[7]; a[7] = temp; printf("\n"); printf("*~*~Swap element 4 and 7*~*~\n"); print(a, 8); printf("\n\n"); /* (d) Find the smallest element */ printf("*~*~Find the Smallest Element*~*~\n"); min = findSmallest(a, 8); printf("The smallest element is %d\n", min); /* (e) Swap the smallest number with the first element in the array */ printf("*~*~Set the smallest number to the first position in array*~*~\n"); temp = a[0]; // set temp to first value in array for(int i=0;i<8;i++) { if(a[i]==min)// If value at location is lowest { a[i]=temp; // Assign first element of array at lowest number location i=8; // exit loop } } a[0] = min; // Assign first array location with lowest value print(a, 8); printf("\n"); return 0; }``````