I wanna solve this problem

This is a discussion on I wanna solve this problem within the C Programming forums, part of the General Programming Boards category; Seriously, I'm having an exam after 2 hours and 20 minutes so I'm training. I'm trying to solve this problem ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    2

    Unhappy I wanna solve this problem

    Seriously, I'm having an exam after 2 hours and 20 minutes so I'm training. I'm trying to solve this problem for days but I can't

    http://ace.delos.com/usaco/cow1.jpg Your Ride Is Here

    It is a well-known fact that behind every good comet is a UFO. These UFOs often come to collect loyal supporters from here on Earth. Unfortunately, they only have room to pick up one group of followers on each trip. They do, however, let the groups know ahead of time which will be picked up for each comet by a clever scheme: they pick a name for the comet which, along with the name of the group, can be used to determine if it is a particular group's turn to go (who do you think names the comets?). The details of the matching scheme are given below; your job is to write a program which takes the names of a group and a comet and then determines whether the group should go with the UFO behind that comet.
    Both the name of the group and the name of the comet are converted into a number in the following manner: the final number is just the product of all the letters in the name, where "A" is 1 and "Z" is 26. For instance, the group "USACO" would be 21 * 19 * 1 * 3 * 15 = 17955. If the group's number mod 47 is the same as the comet's number mod 47, then you need to tell the group to get ready! (Remember that "a mod b" is the remainder left over after dividing a by b; 34 mod 10 is 4.)
    Write a program which reads in the name of the comet and the name of the group and figures out whether according to the above scheme the names are a match, printing "GO" if they match and "STAY" if not. The names of the groups and the comets will be a string of capital letters with no spaces or punctuation, up to 6 characters long.
    Examples:

    Input Output
    COMETQ HVNGAT GO
    ABSTAR USACO STAY
    PROGRAM NAME: ride

    This means that you fill in your header with:
    PROG: ride INPUT FORMAT

    Line 1: An upper case character string of length 1..6 that is the name of the comet.
    Line 2: An upper case character string of length 1..6 that is the name of the group.
    NOTE: The input file has a newline at the end of each line but does not have a "return". Sometimes, programmers code for the Windows paradigm of "return" followed by "newline"; don't do that! Use simple input routines like "readln" (for Pascal) and, for C/C++, "fscanf" and "fid>>string".
    SAMPLE INPUT (file ride.in)

    COMETQ HVNGAT OUTPUT FORMAT

    A single line containing either the word "GO" or the word "STAY". SAMPLE OUTPUT (file ride.out)

    GO

    Submission file Name: USACO Gateway | Comment or Question
    Here's my code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        FILE *in = fopen("ride.in","r");
        FILE *out = fopen("ride.out","w");
        int i,flag=0,fin1[6],fin2[6],fin2f=0;
        char fin[12];
        fscanf(in,"%s",&fin);
        for (i=0;i<12;i++)
        {
            printf("Hello");
            if(flag!=1)
            {
                if(fin[i]!='\n')
                {
                    fin1[i]=(fin[i]-46);
                }
                else
                {
                    flag=1;
                }
            }
            else
            {
                fin2[fin2f]=(fin[i]-46);
                fin2f++;
            }
        }
        printf("\n%d",fin1);
        printf("\n%d",fin2);
        return 0;
    }
    It isn't finished yet

    But even that doesn't work

    Can someone help me PLEASE?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What part can't you get to work? You don't need the & when you are scanning into a character array as a whole:
    Code:
        char fin[12];
        fscanf(in,"%s",&fin);
    This is only going to give you 5 useable characters. %s isn't going to stop after 5 characters on its own though. You need to limit it, or hope you've got some white space after the fifth character in your file.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Do you know own functions in C?
    You should calculate the product with a function like
    Code:
    unsigned long myproduct(const char *s)
    {
       unsigned long p=1;
       while( s[0] ) { p = p * (s[0]-'A'+1); ++s; };
       return p;
    }
    Then you can read your names with one fscanf upto end of file like
    Code:
    char comet[7],group[7]; /* note 7 chars needed ; terminating '\0' */
    ...
    while( 2==fscanf(in,"%6s%6s",comet,group) )
      if( (myproduct(comet)%47) == (myproduct(group)%47) )
        puts("GO");
      else
        puts("STAY");
    Now its easy or?

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    2
    @quzah, strlen? o.o

    @BillyTKid, I can't understand anything of what you've written

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to solve this problem?
    By lijr07 in forum C++ Programming
    Replies: 13
    Last Post: 07-30-2011, 01:53 PM
  2. How is it possible to solve this problem
    By ahmet858585 in forum C Programming
    Replies: 7
    Last Post: 05-01-2007, 01:28 PM
  3. Can someone solve this problem for me?
    By hykyit in forum C Programming
    Replies: 2
    Last Post: 03-17-2005, 02:57 AM
  4. how to solve this problem???
    By moon in forum C++ Programming
    Replies: 3
    Last Post: 01-21-2002, 03:19 AM
  5. problem cant solve please help.
    By sarah in forum C Programming
    Replies: 6
    Last Post: 09-03-2001, 02:32 PM

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