How to scan in two arrays from the user?

I am relatively new to programming and I need to write a function that reads input from the user and populate two arrays and then compare them. I guess I am confused about how to read in both arrays.

This is what I have to do

Write a function table_diff that compares two arrays of integers and returns the index of the first place where they differ. If the arrays are the same, the function should return -1 ex:

345 and 345 → -1 (same)

345 and 346 → 2 (differ by index 2)

1234 and 123 → 3 (differ at index 3)

This is what I have, any help is appreciated!

while((r = scanf("%i", &value)) != 1 && ptra < endptra)
{
     *ptra ++ = value;                      

     if (r==1)
         printf("No room after reading values\n\n");
     else if(r != EOF)
         printf("invalid char");
}   

while((r = scanf("%i\n", &value))!= 1 && ptrb < endptrb){
    *ptrb ++ = value;

    if (r==1)
        printf("No room after reading values\n\n");       
    else if(r != EOF)
        printf("invalid char");                      
}

      

0


source to share


6 answers


I think you want to change your code to the following:

while((r = scanf("%i", &value)) != 1 && ptra < endptra)
{
     *(ptra++) = value;                      

     if (r==1)
         printf("No room after reading values\n\n");
     else if(r != EOF)
         printf("invalid char");
}   

while((r = scanf("%i\n", &value))!= 1 && ptrb < endptrb){
    *(ptrb++) = value;

    if (r==1)
        printf("No room after reading values\n\n");       
    else if(r != EOF)
        printf("invalid char");                      
}

      



The operator *

has a higher priority than ++

.

+1


source


This does not answer your question, but in your "while" state you are already checking "r! = 1", so inside the while block, checking "if (r == 1) ..." is unnecessary and is technically unreachable code.



What is the point of the program? The way it looks now, you read into the ptra array to an empty line, then read into ptrb to an empty line ... then you say that you need to compare the two, but what do you mean by that? what should be the result? compare array elements by position? or just by content not by position? need more information ...

0


source


Also, there is an example here copy - paste

. You do the same thing twice. What are you doing this way? Write a function with generic code and call it only with appropriate parameters.

0


source


It looks like you are missing the actual control part. Now that you read in each array, you should loop through it with a different loop and check the value in each of them.

0


source


I don't think scanf works the way you think it does. It's hard to tell without seeing how you contribute; for this, I think you will need to send EOF (CTRL-D from stdin) after every whole record. If you do this, then the only thing that will end your loop is the pointer comparison. Do you have a maximum size for the array? If, on the other hand, your input is "1234 ^ D", you end up with one array entry of the integer 1234, not 1,2,3,4.

Finally, the if statement looks like you are trying to determine why the loop exited; if so, it should be outside of the while loop.

0


source


One way to fix your looping problems in programming is something I learned in one of my university courses about 15 years ago. This is a " loop invariant ".

Make one statement that will always be true. Inside the loop, the condition may change, but make sure that if you do the next iteration, your invariant will be valid again.

how

"The first positions and arrays are the same"

So, start at 0 (always true), and while checking the loop again if the first position is the same. If true, you can increment the variable i with one. Do the same with the second, etc. So basically the variable i determines which next index to check.

I'm sure the internet will provide some better examples, but it can help you and at the same time improve your developer skills.

0


source







All Articles