views:

260

answers:

6

I'm relatively new to programming and I have to write a function that reads in input from the user and to fill two arrays then compare them. I guess what I'm confused on is how to read in both arrays.

This is what I'm supposed to do,

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

345 & 345 --> -1 (same)

345 & 346 --> 2 (differ at index 2)

1234 & 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");                      
}
A: 

This doesn't really answer your question, but in your "while" condition, you already check for "r != 1", so within the while block, checking "if(r == 1) ..." is unnecessary, and technically unreachable code.

What is the point of the program? The way it looks now, you read into array ptra until a blank line, then read into ptrb until a blank line... then you say you need to compare the two, but what do you mean by that? what should the result be? compare the arrays item by item? or just by contents not by position? need more info...

rally25rs
Travis G
+1  A: 

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 higher precedence than ++.

Daniel Spiewak
Good catch, thank you!
Travis G
A: 

Also, you have a case of copy - paste here. You do the same thing twice. What do you do thus? Write a function with the common code and call it with just the respective parameters.

Svante
A: 

It looks like you are missing the actual checking part. Now that you ahve read in each array you should walk across it with another loop and check the value in each one.

Karl
Did you mean comparing the arrays? I had to write the function to compare the two, and have that done. I had to write this "table_fill" functions to read in the values to be compared
Travis G
A: 

I don't think scanf works the way you're thinking it works. It's hard to say without seeing how you do your input; in order to loop like that, I think you'd need to be sending an EOF (CTRL-D from stdin) after every integer entry. If that is how you're doing it, then the only thing that will terminate your loop is the pointer comparison. Do you have a known maximum array size? If, on the other hand, your input is "1234^D", you're going to end up with one array entry of the integer 1234, rather than 1,2,3,4.

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

mbyrne215
Thank you! That cleared things up quite a bit. I made the dumb mistake of assuming i need two while loops or two functions, when I can just have one while loop and call it twice. Appreciate the help!
Travis G
A: 

One of the ways to fix your problems with loops in programming is one the things I learned in one of my university courses about 15 years ago. That is the "loop invariant".

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

Like:

"The first i positions of the arrays are the same"

So start with 0 (always true), and during the loop check first if the first position is the same. If true, you are able to increase the 'i' variable with one. Do the same with the 2nd , etc. So basically the i variable determines which next index will be checked.

I'm sure the internet will provide with some better examples, but it might help you and at the same time increase your developers skill.

Roalt