Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9224565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:21:16+00:00 2026-06-18T04:21:16+00:00

I am solving a problem on USACO. In the problem, I have to take

  • 0

I am solving a problem on USACO. In the problem, I have to take two strings as inputs and calculate the numerical values modulo 47. If the values are same, then GO is to be printed otherwise STAY has to be printed. The initial numerical value will be calculated by taking the product of the numerical values of the alphabets ( 1 for A and similarily 26 for Z ) and then the final number will be calculated by using modulo.

My program is being compiled withour any error and the first case is also a success. But the problem is in the second case and the way my file is being appended. The program is as follows:-

#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define MAX 6
main()
{
    int cal(char *ptr);
    int a,b;
    char *comet,*group;
    FILE *fptr;
    comet=malloc(6*sizeof(char));
    group=malloc(6*sizeof(char));
    scanf("%s",comet);
    a=cal(comet);
    scanf("%s",group);
    b=cal(group);
    fptr=fopen("ride.out","a+");                (1)
    //fptr=fopen("ride.txt","a+");              (2)
    if(a==b)
        fprintf(fptr,"GO\n");               (3)
        //printf("GO\n");                    (4)
    else
        fprintf(fptr,"STAY\n");              (5)
        //printf("STAY\n");                   (6)   
    fclose(fptr);
    return 0;
}
int cal(char *ptr)
{
    int c,prod=1,mod;
    while(*ptr)
        {
                c=(*ptr++)-'A'+1;
                prod=prod*c;
        }
    mod=prod%47;
    return mod;
}

OUTPUT:-

output_image

The first case is the set two strings:-

    COMETQ
    HVNGAT

and the second case is given in the error notification itself.

If I remove the comment symbols from (2) and put it on (1), then the program is working fine because I can see the contents of the file and they appear just as the grader system wants. It isn’t happening for the actual statement of (1). The comments of line (4) and (6) are also fine but not the line (1). I am not able figure this out. Any help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:21:17+00:00Added an answer on June 18, 2026 at 4:21 am

    First a few notes:

    • main(): a decent main is either:

      int main(void)
      or
      int main(int argc, char *argv[])
      
    • Using malloc() you should always check if it returns NULL, aka fail, or not.

    • Always free() malloc’ed objects.
    • Everyone has his/hers/its own coding style. I have found this to be invaluable when it comes to C coding. Using it as a base for many other’s to. Point being structured code is so much easier to read, debug, decode, etc.

    More in detail on your code:

    Signature of cal()

    First line in main you declare the signature for cal(). Though this works you would probably put that above main, or put the cal() function in entirety above main.

    Max length

    You have a define #define MAX 6 that you never use. If it is maximum six characters and you read a string, you also have to account for the trailing zero.

    E.g. from cplusplus.com scanf:

    specifier ‘s’: Any number of non-whitespace characters, stopping at the first whitespace character found. A terminating null character is automatically added at the end of the stored sequence.

    Thus:

    #define MAX_LEN_NAME    7
    ...
    comet = malloc(sizeof(char) * MAX_LEN_NAME);
    

    As it is good to learn to use malloc() there is nothing wrong about doing it like this here. But as it is as simple as it is you’d probably want to use:

    char comet[MAX_LEN_NAME] = {0};
    char group[MAX_LEN_NAME] = {0};
    

    instead. At least: if using malloc then check for success and free when done, else use static array.

    Safer scanf()

    scanf() given "%s" does not stop reading at size of target buffer – it continues reading and writing the data to consecutive addresses in memory until it reads a white space.
    E.g.:

    /* data stream = "USACOeRRORbLAHbLAH NOP" */
    comet = malloc(szieof(char) * 7);
    scanf("%s", buf);
    

    In memory we would have:

    Address (example)
    0x00000f           comet[0]
    0x000010           comet[1]
    0x000011           comet[2]
    0x000012           comet[3]
    0x000013           comet[4]
    0x000014           comet[5]
    0x000015           comet[6]
    0x000016           comet[7]
    0x000017           /* Anything; Here e.g. group starts, or perhaps fptr */
    0x000018           /* Anything; */
    0x000019           /* Anything; */
    ...
    

    And when reading the proposed stream/string above we would not read USACOe in to comet but we would continue reading beyond the range of comet. In other words (might) overwriting other variables etc. This might sound stupid but as C is a low level language this is one of the things you have to know. And as you learn more you’ll most probably also learn to love the power of it 🙂

    To prevent this you could limit the read by e.g. using maximum length + [what to read]. E.g:

     scanf("%6[A-Z]", comet);
             | |      |
             | |      +------- Write to `comet`
             | +-------------- Read only A to Z
             +---------------- Read maximum 6 entities
    

    Input data

    Reading your expected result, your errors, your (N) comments etc. it sound like you should have a input file as well as an output file.

    As your code is now it relies on reading data from standard input, aka stdin. Thus you also use scanf(). I suspect you should read from file with fscanf() instead.

    So: something like:

    FILE *fptr_in;
    char *file_data = "ride.in";
    int res;
    ...
    if ((fptr_in = fopen(file_data, "r")) == NULL) {
        fprintf(stderr, "Unable to open %s for reading.\n", file_data);
        return 1; /* error code returned by program */
    }
    if ((res = fscanf(fptr_in, "%6[A-Z]%*[ \n]", comet)) != 1) {
        fprintf(stderr, "Read comet failed. Got %d.\n", res);
        return 2;
    }
    
    b = cal(comet);
    
    if ((res = fscanf(fptr_in, "%6[A-Z]%*[ \n]", group)) != 1) {
        fprintf(stderr, "Read group failed. Got %d.\n", res);
        return 2;
    }
    
    ...
    

    The cal() function

    First of, the naming. Say this was the beginning of a project that eventually would result in multiple files and thousand of lines of code. You would probably not have a function named cal(). Learn to give functions good names. The above link about coding style gives some points. IMHO do this in small projects as well. It is a good exercise that makes it easier when you write on bigger to huge ones. Name it e.g. cprod_mod_47().

    Then the mod variable (and might c) is superfluous. An alternative could be:

    int cprod_mod_47(char *str)
    {
        int prod = 1;
    
        while (*str)
            prod *= *(str++) - 'A' + 1;
    
        return prod % 47;
    }
    

    Some more general suggestions

    When compiling use many warning and error options. E.g. if using gcc say:

    $ gcc -Wall -Wextra -pedantic -std=c89 -o my_prog my_prog.c
    

    This is tremendous amount of help. Further is the use of tools like valgrind and gdb invaluable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am solving a problem on USACO. In the problem, I have to take
I need some help in solving this problem. We have a large amount of
I have more of a 'problem solving' question than a syntax related problem. Briefly,
Please advise the best way for solving my problem. I have a problem figuring
I have tried solving this problem by posting other related questions here that focused
I am solving following problem - I have on my page statement of items
I have an exercise in problem solving for those who like that kind of
I have a problem with browsers window managament with javascript. I have two page
I am solving a problem in three different ways, two are recursive and I
Any simple way of solving the problem where you have a component and get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.