Click here to Skip to main content
15,904,416 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hey guys
I am having an issue passing my test cases provided by the teacher
This is for binary subtraction as i have written the algorithm allready
please help me with what i am doing wrong
#include <stdio.h>

int main(void)
{
    int borrow, i, n, j, f;
    int x[100]= {0}, y[100]= {0}, difference[100]= {0};
    scanf("%d", &x[i]);
    scanf("%d", &y[i]);

    borrow = 0;
    for (i == 0; n - 1; i++ )
    if(y[i] <= x[i])
    {
    difference[i] = x[i]- y[i];
     }
    else if (i = n - 1)
    {
        borrow = 1;
        x[i] = x[i] + 10;
        difference[i] = x[i] - y[i];
    }
    else (j = i + 1);
     while (x[j] == 0 && j < n)
    {
        j = j + 1;
        j = j - 1;

    }
    while (j > i)
    {
        x[j] = x[j] + 10 - 1;
        j = j - 1;
        difference[i] = x[i] - y[i];

    }
        
         printf("%d",difference[i]);


    return 0;
}


What I have tried:

I have tried to run the code with the test cases provided, but it saying that the code could not compile or run
Posted
Updated 11-Sep-18 0:25am
Comments
Patrice T 11-Sep-18 6:55am    
What are the test cases?

Quote:
scanf("%d", &x[i]);
Here (and in the following line) the i variable is NOT initialized.
You should gather the user input in a loop, in order to initialize the arrays.
You did similar errors in the remaining part of the program.
 
Share this answer
 
Comments
hello3332 11-Sep-18 16:19pm    
the input is provided by the teacher, which i am trying to read using that command. He has x and y
Usman Hunjra 11-Sep-18 17:40pm    
Dear first of all "n" is not initialized !!!
In addition to what Carlo has told you, do yourself a couple of favours:
1) Indent your code properly, and always use curly brackets, even if it's "only one statement". Particularly when you are just starting, this makes your code easier to read and modify.
Here is your (unmodified) code indented and bracketed:
C++
int main(void)
{
    int borrow, i, n, j, f;
    int x[100]= {0}, y[100]= {0}, difference[100]= {0};
    scanf("%d", &x[i]);
    scanf("%d", &y[i]);
    
    borrow = 0;
    for (i == 0; n - 1; i++ )
    {
        if(y[i] <= x[i])
        {
            difference[i] = x[i]- y[i];
        }
        else if (i = n - 1)
        {
            borrow = 1;
            x[i] = x[i] + 10;
            difference[i] = x[i] - y[i];
        }
        else 
        {
            j = i + 1;
        }
    }
    while (x[j] == 0 && j < n)
    {
        j = j + 1;
        j = j - 1;
    }
    while (j > i)
    {
        x[j] = x[j] + 10 - 1;
        j = j - 1;
        difference[i] = x[i] - y[i];
    }
    printf("%d",difference[i]);
    return 0;
}
Can you see how much easier it is to tell where your for loop ends?

2) Stop using single character variable names: it's quick to type, but it makes it harder to read and understand. Using "descriptive" names that tell you what the variable is used for helps your code become self documenting, and with modern IDE's takes hardly any extra time to use! OK - borrow and difference are descriptively named, but what are x, y, i, n, j, and f for? You don't even use one of them!

3) Do you want to tell me what this code does?
C++
while (x[j] == 0 && j < n)
{
    j = j + 1;
    j = j - 1;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900