Thread: Matching numbers

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    21

    Matching numbers

    I have to print out the matching numbers from a text file. I have to rewrite my int lottery function to do the matching part but not sure how.

    here is the output:

    Sample input file (lottery.txt)
    19 12 26 34 5 17
    7 5 12 43 5 26
    15 19 3 30 27 41
    12 12 12 12 12 12
    34 12 5 19 17 26

    Sample Output
    Ticket #1 has 3 matching numbers
    Ticket #2 has 0 matching numbers
    Ticket #3 has 1 matching numbers
    Ticket #4 has 6 matching numbers


    Thanks for any input.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I think the matching part was the only part you had right.

    scanf("%d \n", readticket) makes no sense whatsoever.

    if (lottery==0) means you only print a message if they don't match at all.

    You use & when you do scanf, not when you do printf.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Just to give everyone the satisfaction of how bad this code is:

    Code:
    #include <stdio.h>
    
    void readticket(FILE* fin, int ticket[]);
    int lottery(int ticket[], int winning[]);
    
    int main(void)
    {
        FILE *fin;
        int winning[6],ticket[6];
        int i,j;
        fin = fopen("lottery.txt", "r");
        readticket(fin, winning);
        
        for(i=0; i<6;i++)
        {
        printf("&#37;d ", winning[i]);
    }
    
         while(!feof(fin))
         {
           scanf("%d \n",readticket);               //read ticket
           if(lottery==0)               //compare ticket, winning using lottery func
           printf("Ticket# %d has %d matching numbers \n",&ticket,&winning);               //print
         }
        system("PAUSE");
        return 0;
    }
    
    
    void readticket(FILE* fin, int ticket[])
    {
         int i;
         for(i=0;i<6;i++)
             fscanf(fin,"%d", &ticket[i]);
         
         }
         
    int lottery(int ticket[], int winning[])
    {
       {    
            int i;
            int k;
            int count=0;
    
            for (i=0;i<6;i++)
                for(k=0;k<6;k++)
        
                    if (ticket[i]==winning[k])
           
                         count++;
           
           return count; 
    }
    }
    Code:
    LotteryTicket.c: In function `main':
    LotteryTicket.c:23: warning: int format, different type arg (arg 2)
    LotteryTicket.c:23: error: syntax error before '/' token
    LotteryTicket.c:27: warning: implicit declaration of function `system'
    LotteryTicket.c:11: warning: unused variable `ticket'
    LotteryTicket.c:12: warning: unused variable `j'
    As tabstop and my compiler both pointed out, your scanf() is wrong. Always fix syntax errors first so you can test the logic itself instead of wasting time with stupid things like this. This will most likely just waste time crashing your program. If your compiler is not spitting out these types of errors, you need to turn the warning level up higher, preferably to max.

    Edit: And actually your entire program logic appears bizarre like you mixed and matched totally different strategies.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    21

    brick = prick

    Your posts are really useless so stop posting replies. If my code is bizzare why in the world you're even bothering to spend your precious time on my code. Get lost before i use my Mr. T line.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by kirksson View Post
    Your posts are really useless so stop posting replies. If my code is bizzare why in the world you're even bothering to spend your precious time on my code.
    Because you asked him to? Hello?

    Also, forgot to mention: to call a function, like lottery for instance, you need to put (stuff in parentheses) behind it, indicating what you want to pass to the function.

    Edit: Oh, and his point was also that if your code is only that long, there's no need to attach it, because, well, it's only that long. Making people have to click, and click, and then click again just to read it is not to your advantage. (I mean, yes, I'm that bored, but a lot of people aren't.)
    Last edited by tabstop; 07-21-2008 at 09:08 PM.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by kirksson View Post
    Your posts are really useless so stop posting replies.
    I'll post wherever I wish on these forums without your permission and without your blessing, as I told you more than once when you PM'ed me multiple times with such complaints.

    Quote Originally Posted by kirksson View Post
    If my code is bizzare why in the world you're even bothering to spend your precious time on my code.
    If you won't put the effort into getting a decent grade, why take a programming course?

    Quote Originally Posted by kirksson View Post
    Get lost before i use my Mr. T line.
    You already did that via PM over your prior homework topic:

    Quote Originally Posted by kirksson
    I hope that is not all you got! We are just warming up because " I pity you fool!" - (remeber that famous line.)

    I was actually waiting when you will use religious remarks so you can call me a buss driver.
    Why a bus driver ?
    I am taking you to school son!
    I don't mind you being an idiot entirely since it doesn't necessarily affect me if you mess your life up, but if you can't contain yourself in public, the mods might get upset and restrict your ability to post. I suggest you play nice in the sandbox, and work with those who are trying to help you.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    21
    Quote Originally Posted by MacGyver View Post
    I'll post wherever I wish on these forums without your permission and without your blessing, as I told you more than once when you PM'ed me multiple times with such complaints.

    You are pathetic.



    If you won't put the effort into getting a decent grade, why take a programming course?

    Nunya - translate that



    You already did that via PM over your prior homework topic:

    It looks like you need some new dose, you prick.


    I don't mind you being an idiot entirely since it doesn't necessarily affect me if you mess your life up, but if you can't contain yourself in public, the mods might get upset and restrict your ability to post. I suggest you play nice in the sandbox, and work with those who are trying to help you.
    No you are being an idiot and a total d-bag.
    Let some others who know more than you collaborate with me since you are lousy looser.

    Thank you, and you are welcome.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    /me shrugs.

    Your call to respond that way, but I wouldn't be entirely surprised, however, if this topic ends up getting locked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-01-2009, 07:54 PM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. Going out of scope
    By nickname_changed in forum C++ Programming
    Replies: 9
    Last Post: 10-12-2003, 06:27 PM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. Matching Numbers in Arrays
    By abrege in forum C++ Programming
    Replies: 4
    Last Post: 11-24-2002, 08:54 AM