C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-11-2008, 09:09 PM   #1
Registered User
 
Join Date: Nov 2008
Posts: 4
Exclamation Function to read in two arrays

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)



But what im having trouble with is writing the function to read the two arrays in. Im not sure if i can do it in one single while loop or if i need two?

This is what i have, any help is appreciated!

Code:
int table_fill(int a[], int b[],int max)
{
    

 int value;
 int r;
 int *ptra = a;
 int *ptrb = b;
 int *endptra = ptra - max;
 int *endptrb = ptrb - max;
   
printf("please enter values \n\n");


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");                      
}
              
}
ssmokincamaro is offline   Reply With Quote
Old 11-11-2008, 10:17 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Maybe it's required for the assignment, but why write a function that reads into two arrays? Why not write a function that reads into one array, and then call it twice?
tabstop is offline   Reply With Quote
Old 11-11-2008, 10:23 PM   #3
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
> (r = scanf("%i", &value)) != 1
I'd say you would want == 1 here.

I'd also put the loop in another function, and just call it twice with different parameters.

> printf("No room after reading values\n\n");
What's this supposed to mean?
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 11-11-2008, 10:28 PM   #4
Registered User
 
Join Date: Nov 2008
Posts: 4
Yeah its required for this assignment. Ah yeah that would make sense, thanks! So you would suggest writing a function to read in each array?
ssmokincamaro is offline   Reply With Quote
Old 11-11-2008, 10:31 PM   #5
Registered User
 
Join Date: Nov 2008
Posts: 4
Oh and I'm supposed to do an error check, but was unsure how to do it. Its supposed to check to see if there is enough room in the array and check to see if a invalid character was entered
ssmokincamaro is offline   Reply With Quote
Old 11-11-2008, 10:34 PM   #6
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
> So you would suggest writing a function to read in each array?
I believe I just did.

> Its supposed to check to see if there is enough room in the array and check to see if a invalid character was entered
The 'room' is the ptra < endptra test
The 'valid' is the == test

However, what you don't do at the moment is clean up the input stream if scanf() returns something other than 1
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 11-11-2008, 10:40 PM   #7
Registered User
 
Join Date: Nov 2008
Posts: 4
Ok i'll give that a shot right now, thanks Salem!

If my memory serves me correctly, I think I used ( in an older program)

while(getchar() != '\n');

to flush it out?
ssmokincamaro is offline   Reply With Quote
Old 11-12-2008, 07:59 AM   #8
Registered User
 
Join Date: Sep 2008
Posts: 45
Code:
while(((r = scanf("%i", &value)) != 1 && ptra < endptra)
You seem to missing that extra '(' also.
blurx is offline   Reply With Quote
Reply

Tags
arrays.while, pointers, scanf

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
"sorting news" assignment prljavibluzer C Programming 7 02-06-2008 06:45 AM
Arrays as function arguments :( strokebow C Programming 10 11-18-2006 03:26 PM
Bisection Method function value at root incorrect mr_glass C Programming 3 11-10-2005 09:10 AM
Problem with Visual C++ Object-Oriented Programming Book. GameGenie C++ Programming 9 08-29-2005 11:21 PM
Read In From Many Files In One Function djwicks C Programming 12 03-24-2005 07:39 AM


All times are GMT -6. The time now is 04:38 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22