Array item assignment gives unexpected results

Background: I have an exercise which asks me to create a function which compares 2 int arrays using the backtracking technique. The function should return 0 if the arrays are different and 1 if they are the same. The size of the arrays, the method of filling them and the output of the program are not specified in the question so I took the liberty of working them out my own way.

By using a for I made a simple fill function which fills out the two arrays in a simple way so if the user inputs s the result should be

A[0]=B[0]=0  
A[1]=B[1]=1  
...  
A[50]=B[50]=50

and if he inputs d it should be the same but

B[i]=A[i]+1

The problem: Instead of A[0]=0 it ends up being A[0]=50 (and A[0]=51 in the d case) which makes the whole function return 0 in every case. I have tried numerous things, but I can't get it to work properly

Here's the code:

#include <stdio.h>
#include <stdlib.h>

void fill(int a, int A[],int B[])
{
     int i,j;

    if (a)
    {
        for(i=0;i<=50;i++)
        {
        A[i]=i;     
        B[i]=A[i];
        }
    }
    else
    {
        for(i=0;i<=50;i++)
        {
            A[i]=i;    
            B[i]=i+1;
        }
    }
    A[0]=0;
    for (j=0;j<=50;j++)
        printf("\nka %d %d %d",j, A[j],B[j]); //the purpose of this is to check the state of the two arrays after filling them, it's how I spotted the problem, it will be deleted in the final form

}

int compare(int i, int A[],int B[])
{
    int a,b;

    a=A[i];
    b=B[i];
    printf("j %d %d\n", a,b);
    if (B[i+1]!='\0')
    {
        if (A[i]==B[i])
        {
            compare (i+1,A,B);
        }
        else
        {
            return 0;
        }
    }
    else
        return 1;


}


int main()
{
    int A[50], B[50], i=0;
    char s;

    printf("Do you want the arrays to be the same or different?\n");
    printf("Input 's' or 'd': ");


    scanf("%c", &s);

    switch(s)
    {
        case 's':
        case 'S':
            fill(1,A,B);
            break;

        case 'd':
        case 'D':
            fill(0,A,B);

            break;

        default:
            printf("Sorry incorrect input, please input 's' or 'd'");
            return 0;
            break;
    }


    if (compare(i,A,B))
       printf("They are the same");
    else
       printf("They are different");



return 0;
}

Upvotes: 0

Views: 113

Answers (2)

Rishikesh Raje
Rishikesh Raje

Reputation: 8614

The issue is in the for loop. It is looping for 51 elements.

for (i=0;i<=50;i++)

It should loop for 50 elements

for (i=0;i<50;i++)

Similarly for the loop for j, in the fill function.

There is also an issue in the compare function. The exit check is

if (B[i+1]!='\0')

This means that the input array must have the last element as '\0'. Which is not happening in your input array.

You have to either pass strings to this function, or modify this function to take care of the general array case.

Upvotes: 0

haccks
haccks

Reputation: 106042

You need to correct your both the functions fill and compare. In function fill you are accessing arrays out of bounds which will invoke undefined behavior. Loop should be iterated from 0 to 50 (excluding 50)

for(i=0;i<50;i++) { ... }

The function compare should be like

// Pass the size of array to the function.
int compare(int i, int A[],int B[], int n)
{
    if (i != n)
    {
        if (A[i]==B[i])
        {
            return compare (i+1, A, B, n);
        }
        else
        {
            return 0;
        }
    }
    else
        return 1;
}

Upvotes: 3

Related Questions