#include <stdio.h>
#include <conio.h>
int main()
{
int array[100], num1, c, n, num2;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]);
printf("Enter the number to swap\n");
scanf("%d", &num1);
printf("Enter the number to swap with\n");
scanf("%d", &num2);
printf("%d is swap place with %d.\n", num1, num2);
for (c = 0; c < n; c++)
{
if (array[c] == num1)
{
array[c] = num2;
}
else if (array[c] == num2)
{
array[c] = num1;
}
}
printf("The new output will be\n");
getch();
return 0;
}
Hi,I’m doing half way of my code but i have no idea how to continue.
I’m coding out to swap number in a list. could someone help me out?
Enter number of element in array:
5
Enter 5 elements
2
4
6
8
0
Enter the first number to swap:
8
Enter the second number to swap:
2
8 is swap place with 2.
The output will be:
8 4 6 2 0
How can i input -1 to end program?
example: Enter number of elements in array: -1
output: End program.
As long as there is no further instructions, you just have to run through your array, and change each occurrence of
8to2and vice versa.