Need help with a functions/arrays.

This is a discussion on Need help with a functions/arrays. within the C Programming forums, part of the General Programming Boards category; Somewhat a beginner, I'm trying to make a program that separates 50 integer numbers into two separate arrays(with 25 each ...

  1. #1
    Blk
    Blk is offline
    Registered User
    Join Date
    Nov 2012
    Posts
    4

    Question Need help with a functions/arrays.

    Somewhat a beginner, I'm trying to make a program that separates 50 integer numbers into two separate arrays(with 25 each naturally). I then need to make a function that takes the two numbers and if they are the same returns true, and false otherwise. I also need to find the cell number that they are located in so I made a pointer for that.

    Here is the start of my function.

    Code:
    int
    find_same(int check1[], int check2[], int size, int*loc)
    {
        int i;
        int same;
        loc = 0;
        
        for(i=0; i<size; ++i)
             if(check1[i] == check2[i])
            {
               same = check1[i];
               *loc = i;     
            }  
            
        return(same);   
    
    }

    Here is the rest of my program. I've comprehended it as got the numbers from the file, put them into two arrays and sent them to the function and then print the results. Am I missing anything?

    Code:
    int
    main (void)
    
    
    {
      int array1[25], array2[25], i, samenum, position;
      FILE *input;
      
      input = fopen("data.txt", "r");
       
      
      for(i=0; i<25; ++i)
          fscanf(input, "%d", &array1[i]);
        
      for(i=1; i<25; ++i)
        fscanf(input, "%d", &array2[i]);    
        
            
            samenum = find_same(array1,array2,25,&position);
            printf("%d  ", samenum);        
            printf("%d ", position);
         
            
            
            
            
            fclose(input);
            
            return(0);
      
       
    }

    Thanks for reading/your help.
    Last edited by Blk; 11-10-2012 at 02:51 PM.

  2. #2
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    it looks like it would only print the last number that was the same and not all numbers that are the same. Is that what you wanted?

  3. #3
    Blk
    Blk is offline
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    I want all the numbers that are the same e.g.

    1st array 11 22 33 44 55 66 etc.
    2nd array 43 96 33 23 52 66 etc.

    Giving me 33,66.
    Would I have to put it in a loop or if statement?

  4. #4
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    i guess it depend on your overall goal. You could just printf evertime you find an equivalent number inside the find_same() function. Or if you wanted to save it as data to acess later you could use another set of int arrays to save the number value and index location. Or You could use a two dimensional array as well using one index as the numbers value and the other as the the position index. There are other ways to do this as well.

    Do you need to save the data for later?
    Or is it just being printed on the screen for the users benifit?

    if just for the user just format a table out of your already existing loop so the output could be like"
    Code:
    Found     Value of Number       At index
    1.          33                             2
    2.          66                             5

  5. #5
    Blk
    Blk is offline
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    It's for the users benefit to display the number and the cell they are located in. The data does not need to be saved for later.

    Would the printf just go in my if statement like this?

    Code:
       if(check1[i] == check2[i])
        {
            same = check1[i];
            *loc = i;    
            printf("%d", same); 
        }

  6. #6
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    Thats how i would do it and just label the columns before the loop so the user knew what number was what.

  7. #7
    Blk
    Blk is offline
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    Alright I'll try it out and fiddle with it a little more, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. functions with arrays
    By confusedd in forum C Programming
    Replies: 7
    Last Post: 11-03-2012, 12:41 PM
  2. help with arrays and functions
    By joeman in forum C++ Programming
    Replies: 3
    Last Post: 03-27-2010, 11:17 AM
  3. functions using arrays
    By trprince in forum C Programming
    Replies: 30
    Last Post: 11-17-2007, 06:10 PM
  4. Arrays and functions
    By pete@notts in forum C Programming
    Replies: 2
    Last Post: 11-26-2005, 05:26 PM
  5. arrays and functions
    By dantestwin in forum C++ Programming
    Replies: 9
    Last Post: 07-08-2004, 04:30 PM

Tags for this Thread


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