[U]C programming[Below is my question and the code, but I donno the code is correct or not, can anyone please help me to correct it if I wrong.
Write a program that will swap two values in an array. If the swap is successfull, the
program will display the updated list. If it contains any error, for example the values
are not in the list, the program will display appropriate error messages. Use related
functions to develop this program.
Example of outputs that the program must fulfill is as shown below (user input is in
bold font):
Example Output 1:
Key in value, 0 to stop: 6 8 11 35 64 0
list now : 6 8 11 35 64
value to change: 35
want to cahnge with: 8
list now: 6 35 11 8 64
Example Output 2:
Key in value, 0 to stop: 6 8 11 35 64 0
list now: 6 8 11 35 64
value to change: 35
want to cahnge with: 27
ERROR!! : no this number 27!
list now: 6 8 11 35 64
Example Output 3:
Key in value, 0 to stop: 0
list now: EMPTY LIST!
This is my answer, but my answer maximum is to key in 5 value, how to modify if I want to key in more than 5 value? and my problem is I key in 0 also cannot stop, where is my mistake?
Write a program that will swap two values in an array. If the swap is successfull, the
program will display the updated list. If it contains any error, for example the values
are not in the list, the program will display appropriate error messages. Use related
functions to develop this program.
Example of outputs that the program must fulfill is as shown below (user input is in
bold font):
Example Output 1:
Key in value, 0 to stop: 6 8 11 35 64 0
list now : 6 8 11 35 64
value to change: 35
want to cahnge with: 8
list now: 6 35 11 8 64
Example Output 2:
Key in value, 0 to stop: 6 8 11 35 64 0
list now: 6 8 11 35 64
value to change: 35
want to cahnge with: 27
ERROR!! : no this number 27!
list now: 6 8 11 35 64
Example Output 3:
Key in value, 0 to stop: 0
list now: EMPTY LIST!
This is my answer, but my answer maximum is to key in 5 value, how to modify if I want to key in more than 5 value? and my problem is I key in 0 also cannot stop, where is my mistake?
Code:
#include <stdio.h>
void main(){
int num[5], i= 0;
int end0 = 0;
int swapFromNum = 0;
int swapToNum = 0;
int swapFromArrIndex = 0;
int swapToArrIndex = 0;
bool swapFromNumFound = false, swapToNumFound = false;
printf("Key in value, 0 to stop: ");
scanf("%d%d%d%d%d%d",&num[0],&num[1],&num[2],&num[3],&num[4],&end0);
printf("list now: ");
for(i=0;i<5;i++){
printf("%d ",num[i]);
}
printf("\n");
printf("value to change:");
scanf("%d",&swapFromNum);
printf("want to cahnge with:");
scanf("%d",&swapToNum);
for(i=0;i<5;i++){
if(num[i] == swapFromNum){
swapFromNumFound = true;
swapFromArrIndex = i;
}
}
for(i=0;i<5;i++){
if(num[i] == swapToNum){
swapToNumFound = true;
swapToArrIndex = i;
}
}
if(swapFromNumFound == false){
printf("ERROR!! : no this number\n");
}
if(swapToNumFound == false){
printf("ERROR!! : no this number\n");
}
if(swapFromNumFound && swapToNumFound){
num[swapFromArrIndex] = swapToNum;
num[swapToArrIndex] = swapFromNum;
printf("list now: ");
for(i=0;i<5;i++){
printf("%d ",num[i]);
}
printf("\n");
}
}
Comment