C programming help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lavender
    New Member
    • Feb 2007
    • 12

    C programming help

    [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?

    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");
    	}
    	
    }
    Last edited by horace1; Feb 28 '07, 03:22 PM. Reason: added code tags
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    there are a number of ways to read values until 0 is entered, e.g. a simple way
    Code:
        printf("Key in value, 0 to stop: ");
        while(1)
          {
           scanf("%d", &num[i]);   // read value
           if(num[i]==0)break;       // if 0 finished
           i++;
           }
    (1) you need to increase the size of your array num[] to take the maxiimum number of data values you may enter
    (2) you should check in the above code that the array size is not exceeded
    (3) on exit from the above loop i will hold the number of values entered - you need to change the rest of your code to reflect this

    Comment

    • lavender
      New Member
      • Feb 2007
      • 12

      #3
      Originally posted by horace1
      there are a number of ways to read values until 0 is entered, e.g. a simple way
      Code:
          printf("Key in value, 0 to stop: ");
          while(1)
            {
             scanf("%d", &num[i]);   // read value
             if(num[i]==0)break;       // if 0 finished
             i++;
             }
      (1) you need to increase the size of your array num[] to take the maxiimum number of data values you may enter
      (2) you should check in the above code that the array size is not exceeded
      (3) on exit from the above loop i will hold the number of values entered - you need to change the rest of your code to reflect this

      If I change my array num[] to maximum, the scanf() may be too long, got any code to replace it? The below code is up to 5 input only!!

      scanf("%d%d%d%d %d%d",&num[0],&num[1],&num[2],&num[3],&num[4],&end0)

      Comment

      • lavender
        New Member
        • Feb 2007
        • 12

        #4
        Originally posted by lavender
        If I change my array num[] to maximum, the scanf() may be too long, got any code to replace it? The below code is up to 5 input only!!

        scanf("%d%d%d%d %d%d",&num[0],&num[1],&num[2],&num[3],&num[4],&end0)

        I have modify the code, below is my new code

        #include <stdio.h>
        int main(){
        int num[20], i=0;
        int swapFromNum = 0;
        int swapToNum = 0;
        int swapFromArrInde x = 0;
        int swapToArrIndex = 0;
        bool swapFromNumFoun d = false, swapToNumFound = false;

        printf("Enter value, 0 to stop: \n");
        while(1)
        {
        scanf("%d", &num[i]); // read value
        if(num[i]==0)break; // if 0 finished
        i++;
        }

        printf("List now: ");
        for(i=0;i<20;i+ +){
        printf("%d ",num[i]);
        }
        printf("\n");

        printf("Value to change:");
        scanf("%d",&swa pFromNum);

        printf("Want to change with:");
        scanf("%d",&swa pToNum);

        for(i=0;i<20;i+ +){
        if(num[i] == swapFromNum){
        swapFromNumFoun d = true;
        swapFromArrInde x = i;
        }
        }

        for(i=0;i<20;i+ +){
        if(num[i] == swapToNum){
        swapToNumFound = true;
        swapToArrIndex = i;
        }
        }

        if(swapFromNumF ound == false){
        printf("ERROR!! : No this value\n");
        }

        if(swapToNumFou nd == false){
        printf("ERROR!! : No this value\n");
        }


        if(swapFromNumF ound && swapToNumFound) {
        num[swapFromArrInde x] = swapToNum;
        num[swapToArrIndex] = swapFromNum;
        printf("Senarai sekarang: ");
        for(i=0;i<20;i+ +){
        printf("%d ",num[i]);
        }
        printf("\n");
        }

        }


        but after I input 1 2 3 4 5 0, the output is
        1 2 3 4 5 0 32 24 48 2 37814096 1943608375 37814144 4198431 4202496 4202500 37814140 -1 4 37814192

        1. why the output still show 0? not show until 5 only?
        2. why after 0 got value show in the list which I didn't key in?
        3. where is my mistake?

        Comment

        Working...