Thread: integers not being passed through functions

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    4

    integers not being passed through functions

    Hey guys, I am trying to create a program that reads my file filled with random words, it then compares the words after they are put into a 2d array and sees if there is any matching words.. unfortunately the count is not working for me (in function2 and function3) and I am not sure why.. could anyone please help?


    Code:
    #include<stdio.h>
    #include<string.h>
    char function1(char words_array[][17]);
    int function2(char words_array[][17]);
    void function3(int pairs, char words_array[][17]);
    
    
    int main( void )
    {  
    
    char words_array[20][17];
    int x = 0;
    int y = 0;
    int pairs = 0;
    
    
    system("clear");
    
    
    function1(words_array);
    
    
    function2(words_array);
    
    
    function3(pairs, words_array);
    
    
        return ( 0 ) ;
    
    }   
    
    
    char function1(char words_array[][17])
    	{
      FILE *words;
      int x = 0;
      int y = 0;
      words = fopen("words.dat","r");
    
     if(words==NULL)
                    printf("\n\nwords.dat was not properly opened.\n");
          else
                    {
                        for(x = 0 ; x < 20 ; x++ )
                             {
                            fscanf(words,"%s",&words_array[x][y]);
                             }
                             fclose(words);
    
    	}
    }
    
    
    
    int function2(char words_array[][17])
    	{
    	int x = 0;
    	int y = 0;
    	int pairs = 0;
    
    for(x=0; x<20; x++) 
    	{
        for(y=x+1; y<20; y++) 
    	 {
            if (strcmp (words_array[x], words_array[y])==0)
                {
    		pairs++;
    	    }
    	 } 
            }
    
    return(pairs);
    	}
    
    
    
    
    
    
    void function3(int pairs, char words_array[][17])
    	{
    
    	int x = 0;
    	//int y = 0;
    	
    	  for(x = 0 ; x < 20 ; x++ )
    	   {
                printf("Word #%d is %s \n", x + 1, words_array[x]);
    	   }
    
           printf("%d \n",pairs);
    	
    	}

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A few things to note:
    • You made some attempt at indentation, but it is still too poor to be readable. Indenting your code more consistently will help you see its structure more clearly.
    • function1, function2 and function3 are not descriptive names. function1 reads from a file into the array, so a name like read_words would be far better. function2 and function3 should likewise be renamed to describe their purpose.
    • In function1, y is effectively a constant. I would get rid of it and just write &words_array[x][0] or words_array[x].

    As for your program: what is a "pair" in this context? You must have tested this, so what is your test input, expected output and actual output?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    4
    @lasterlight

    the file it is reading from is:
    Code:
    Google Twitter Facebook Twitter gmail Flyer city phone Google cookie Facebook Flyer grill fork silver tornado dirty blue grill lemon
    my output is:
    Code:
    Word #1 is Google 
    Word #2 is Twitter 
    Word #3 is Facebook 
    Word #4 is Twitter 
    Word #5 is gmail 
    Word #6 is Flyer 
    Word #7 is city 
    Word #8 is phone 
    Word #9 is Google 
    Word #10 is cookie 
    Word #11 is Facebook 
    Word #12 is Flyer 
    Word #13 is grill 
    Word #14 is fork 
    Word #15 is silver 
    Word #16 is tornado 
    Word #17 is dirty 
    Word #18 is blue 
    Word #19 is grill 
    Word #20 is lemon 
    0

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I see. Notice that you are not assigning the return value of the call to function2 to pairs.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2013
    Posts
    4
    hmm how would i go about doing that?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Change this:
    Code:
    function2(words_array);
    into:
    Code:
    pairs = function2(words_array);
    Note that you also have some other problems with your code. You should compile at a higher warning level. Remember to indent your code properly and rename those functions too.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2013
    Posts
    4
    of course! for some reason I always forget to do the little things like that.... thank you very much! I am working on fixing my indentation now. 10000x easier to read this way I dont know why I never did it before..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-05-2013, 11:59 PM
  2. confused about arrays being passed between functions
    By c_weed in forum C Programming
    Replies: 7
    Last Post: 09-24-2011, 08:45 PM
  3. Question Functions (Integers & Floats)
    By mwong in forum C Programming
    Replies: 3
    Last Post: 04-12-2011, 02:42 AM
  4. Manipulating Args Passed to Variadic Functions
    By CandyMan80211 in forum C Programming
    Replies: 1
    Last Post: 07-05-2007, 03:35 PM
  5. Replies: 13
    Last Post: 02-09-2003, 04:52 PM