mardi 11 septembre 2018

Else statement causes improper output

I found and fixed an error in a program I needed to make for an assignment. But I don't understand why the error occurs and I would like to understand what is happening for future uses.

The program asks the user for the size of 2 int arrays, then prompts the user to enter the values these arrays will hold. It then asks the user for a range of indices from array 1 to add the values to array 2 before a given position. Then 2 more arrays are generated to show the new array 2 with the new values added from array one.

#include <stdio.h>

void fillArray(int x[], int size);

int main(){
        int size1, size2, start, end, position;

        //getting input and creating array one
        printf("How big is array one?: ");
        scanf("%d", &size1);
        int inputArray1[size1];
        printf("Please enter the numbers for array one: ");
        fillArray(inputArray1, size1);

        //getting input and creating array two
        printf("How big is array two?: ");
        scanf("%d",&size2);
        int inputArray2[size2];
        printf("Please enter the numbers for array two: ");
        fillArray(inputArray2, size2);

        printf("Enter the start and end index of array one to be removed: ");
        scanf("%d %d", &start, &end);

        printf("Enter the position of array two to be added before: ");
        scanf("%d", &position);

        int size3 = ((end - start) + size2+1), size4 = (size1 - (end - start));
        int outputArray2[size3], kIterator=0; 


         for(int i=0; i<size3; i++){
                if(i == position){
                        for(int j=start; j<=end; j++){
                                outputArray2[i] = inputArray1[j];
                                i++;
                        }
                }else{
                //HERE IS WHERE I FIXED THE BUG
                outputArray2[i] = inputArray2[kIterator];
                kIterator++;
                }
        }


        for(int i=0; i<size3; i++){
                printf("%d ", outputArray2[i]);
        }
        return 0;
    }


//Function to fill an array with user inputs
void fillArray(int x[], int size){
        for(int i=0; i<size; i++)
                scanf("%d", &x[i]);
    }

How big is array one?: 8
Please enter the numbers for array one: 9 12 8 41 38 12 67 49
How big is array two?: 3
Please enter the numbers for array two: 11 27 6
Enter the start and end index of array one to be removed: 2 5
Enter the position of array two to be added before: 1
11 8 41 38 12 32767 27

Why does this output occur? The output should be: 11 8 41 38 12 27 6
When I remove the else block but leave the code to look like:

         for(int i=0; i<size3; i++){
                if(i == position){
                        for(int j=start; j<=end; j++){
                                outputArray2[i] = inputArray1[j];
                                i++;
                        }
                }
                //HERE IS WHERE I FIXED THE ERROR
                outputArray2[i] = inputArray2[kIterator];
                kIterator++;

        }

Suddenly the output is correct and I don't get that large number. Why is this?

Aucun commentaire:

Enregistrer un commentaire