Thread: input files using command line arguments possible?

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    42

    Question input files using command line arguments possible?

    I'm trying to test an input(which is a text file) on my code but instead of using file-I/O I added command line arguments to main, initial lines are like these:
    Code:
    int main(int argc, char** argv){
        int n = atoi(argv[1]);
        int* values = malloc(n* sizeof(int));
        for(int i=2; i<n; i++){
            values[i-2]=atoi(argv[i]);
        }
    //...rest of code
    and then compiled and called the program from terminal like this:

    Code:
    cat input4.txt | ./best_index_cli
    //(best index is the name of the problem).

    but it gives a "segmentation fault(core dumped)" error. This error happens when we try to access some part of memory that doesn't belong to our program, but this time I don't think problem is with my code. I've tested the code already without using CL arguments (using scanf) and it works fine. So maybe the terminal command I have used is "impossible"??

    (I also have question about optimizing my code since it passes 10 test cases but fails on other 2 other ones, but I didn't want to put two questions in one thread and make it unreadable, if someone can help me I'll post it in the comments.)
    Last edited by narniat; 12-14-2018 at 06:14 AM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You don't need an argument to tell your program how many arguments there are. "argc" can be used instead.

    You assume that argv[1] exists, which isn't the case when you run it with no arguments.
    Devoted my life to programming...

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You don't actually seem to be making use of command line arguments. You're piping input from cat to your program, but that will appear in standard input, not command line arguments.

    If you do want to use command line arguments, then you should be using argc, not parsing the first command line argument as an int, to control your loop.
    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

  4. #4
    Registered User
    Join Date
    Oct 2018
    Posts
    42
    Hey, thanks, got it. Would I ask another question too? There's a competitive programming question here: Best Index | Basics of Input/Output & Basic Programming Practice Problems | HackerEarth and this is my solution to it:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int* get_prefix_operation_array(int* values, int count);
    
    
    int main(){
        int n;
        scanf("%d",&n);
        int* values = malloc(n* sizeof(int));
        for(int i=0; i<n; i++){
            scanf("%d",&values[i]);
        }
    
    
        int* prefix_sum_array = get_prefix_operation_array(values,n);
         long long int best = values[0];
        for(int i=0; i<n; i++){
             long long int special_sum = values[i];
            int step_length=2;
            for(int j=i+2;j<n;j+=step_length){
                special_sum += prefix_sum_array[j]-prefix_sum_array[j-step_length];
                step_length++;
            }
            if(special_sum>best){ best = special_sum; }
        }
    
    
        free(values);
        free(prefix_sum_array);
    
    
        printf("%d",best);
        return 0;
    }
    
    
    int* get_prefix_operation_array(int* values, int count){
        int* prefix_array;
        prefix_array = malloc(sizeof(int)*count);
        prefix_array[0]=values[0];
        for(int i=1; i<count; i++){
            prefix_array[i]=prefix_array[i-1]+values[i];
        }
        return prefix_array;
    }
    the weird thing is the website's response is that from all 12 inputs 10 of them are giving the right result, but only 2 of them are giving the wrong result. This is weird for me because I have never experienced such thing, I'm usually "very wrong". This is NOT a homework by the way, I swear . Any help is appreciated. The input is very big so I can not test it on my own computer or even if I do it has 10000 numbers in it so I can not debug it using gdb. I don't know what I should do in such cases. I can't debug. I can't say where I am wrong :/. Link to input: (https://he-s3.s3.amazonaws.com/media...ICDAQTL3Q6ZBKA) (Also if this question is irrelevant here and there's another forum exclusively for competitive programming questions please tell me. thanks)
    Last edited by narniat; 12-14-2018 at 07:48 AM.

  5. #5
    Registered User
    Join Date
    Dec 2018
    Posts
    38
    Maybe two of your inputs are overflowing and your prefix_sum_array should be long long? It looks like you could use just one array, too.

  6. #6
    Registered User
    Join Date
    Oct 2018
    Posts
    42
    Hey bruteforce, thanks for your help. I tried long long too but it says the answer is wrong. About using one array, I also did that but it's still wrong.
    Here's the code using one array instead of two(if I'm not mistaken in understanding what you meant):
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void to_prefix_sum_array(long long int *values, int count);
    
    
    int main() {
        int n;
        scanf("%d", &n);
        long long int *values = malloc(n * sizeof(long long int));
        for (int i = 0; i < n; i++) {
            scanf("%lld", &values[i]);
        }
    
    
        to_prefix_sum_array(values, n);
        long long int best;
        long long int special_sum = values[0];
        int step_length = 2;
        for (int j = 2; j < n; j += step_length) {
            special_sum += values[j] - values[j - step_length];
            step_length++;
        }
        best = special_sum;
        for (int i = 1; i < n; i++) {
            special_sum = 0;
            step_length = 1;
            for (int j = i; j < n; j += step_length) {
                special_sum += (values[j] - values[j - step_length]);
                step_length++;
            }
            if (special_sum > best) { best = special_sum; }
        }
    
    
        free(values);
    
    
        printf("%lld", best);
        return 0;
    }
    
    
    void to_prefix_sum_array(long long int *values, int count) {
        for (int i = 1; i < count; i++) {
            values[i] = values[i - 1] + values[i];
        }
        return;
    }

  7. #7
    Registered User
    Join Date
    Dec 2018
    Posts
    38
    This seems to work. I started with your code but tried to simplify it, to my eyes at least, and debug it. I can't remember all that I changed but maybe you can compare it to yours to see the difference. I haven't actually run it on that site, though, so let me know if it even works! I also removed the free as a bit of an "optimization" (maybe) since the system will clean up the memory for you.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    int main() {
        int n;
        scanf("%d", &n);
     
        long long v, *values = malloc(n * sizeof *values);
     
        scanf("%lld", &values[0]);
        for (int i = 1; i < n; i++) {
            scanf("%lld", &v);
            values[i] = values[i - 1] + v;
        }
     
        long long best = values[0];
        for (int j = 2, step = 2; j < n; j += step) {
            best += values[j] - values[j - step];
            step++;
        }
     
        for (int i = 1; i < n; i++) {
            long long sum = values[i] - values[i - 1];
            for (int j = i + 2, step = 2; j < n; j += step) {
                sum += values[j] - values[j - step];
                step++;
            }
            if (sum > best)
                best = sum;
        }
     
        printf("%lld\n", best);
     
        return 0;
    }

  8. #8
    Registered User
    Join Date
    Dec 2018
    Posts
    38
    Here's a bit more of a simplification. I still haven't tested it except for the 2 little examples since the site wants me to join to do more.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
     
    int main() {
        int n;  // 1 <= n <= 100000
        scanf("%d", &n);
     
        long long v, *values = malloc((n + 1) * sizeof *values);
     
        values[0] = 0;
        scanf("%lld", &values[1]);
        for (int i = 2; i <= n; i++) {
            scanf("%lld", &v); // -10000000 <= v <= 10000000
            values[i] = values[i - 1] + v;
        }
     
        long long best = LLONG_MIN;
        for (int i = 1; i <= n; i++) {
            long long sum = values[i] - values[i - 1];
            for (int j = i + 2, step = 2; j <= n; j += step)
                sum += values[j] - values[j - step++];
            if (sum > best)
                best = sum;
        }
     
        printf("%lld\n", best);
     
        return 0;
    }

  9. #9
    Registered User
    Join Date
    Oct 2018
    Posts
    42
    WoWw...!! Yes it works!! Thanks very very much! I will not submit it because it's not my own work but I compiled it using custom input and gave the exact input (the 11th one) that failed my code to it and it works perfectly! It's even faster than the best answer I guess!!! I didn't think defining a function or other delicate changes you have made could really be that important!! I was uncomfortable making these changes since I thought they were not much heavy duties... or maybe I thought the code might be more beautiful if it had them and if it had functions...... but it turns out I was wrong. At least in competitive programming context where time and memory matter like hell Again, thanks for taking your time. \0

    (P.S: Also I want to ask: are there some general rules which make program faster and lighter(then where can I read more about it?) or it's just you used your experience? thx.)
    Last edited by narniat; 12-14-2018 at 04:19 PM.

  10. #10
    Registered User
    Join Date
    Oct 2018
    Posts
    42
    The interesting thing that I can not still understand is your code and my code both give 419872311 as the result of the 11th input on my own computer (tried that using cat input11.txt | ./program) however your answer gets accepted on the site outputting 2683246 and mine gets rejected outputting 2386270 and neither is the number 419872311 that I said both of them give on my own computer. o-O

  11. #11
    Registered User
    Join Date
    Dec 2018
    Posts
    38
    Quote Originally Posted by narniat View Post
    I will not submit it because it's not my own work but I compiled it using custom input and gave the exact input (the 11th one) that failed my code to it and it works perfectly! It's even faster than the best answer I guess!!!
    I doubt it's faster than all the other answers. And it is 90 percent your code, really, since I didn't think about how to solve the problem at all but just followed your very good idea. I only added at most 10 percent, so you certainly have my permission to submit it and I think it is fair enough that you do.


    Starting the array with a 0 value and starting the "best" at LLONG_MIN is just a trick to get rid of the duplicated code. I can't say that this version is actually an improvement on the previous one and the previous one is even closer to your original code and should be just as fast.

  12. #12
    Registered User
    Join Date
    Dec 2018
    Posts
    38
    Quote Originally Posted by narniat View Post
    The interesting thing that I can not still understand is your code and my code both give 419872311 as the result of the 11th input on my own computer (tried that using cat input11.txt | ./program) however your answer gets accepted on the site outputting 2683246 and mine gets rejected outputting 2386270
    That's strange. I was going to say maybe your computer is 32-bits, but long long should be 64 bits even on a 32-bit machine. Is your computer 64 bits? What OS? What compiler?

    You could try ./program < input11.txt instead, but I don't think it should make a difference.

    Maybe you have a corrupted version of the input. Try downloading the file again. If the problem persists, you could upload that file somewhere and I could try it.

  13. #13
    Registered User
    Join Date
    Oct 2018
    Posts
    42
    Yes this is strange. No my computer is 64 bits, my OS is Windows10 but indeed I should say it's Ubuntu since I'm using WSL (which is a Linux distribution developed by Microsoft for Windows users, it's not a virtual machine, it's exactly an Ubuntu and for example, although it's installed in C, but hard drives like C,D,E... are mounted on it in /mnt directory. It's weird for me but it's true , here's my `uname -a`:"Linux DESKTOP-QOJFPAF 4.4.0-17134-Microsoft #345-Microsoft x86_64 GNU/Linux") and the compiler I use is GCC (gcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0). I also tried `./program < input11.txt` as you said and the same number: 419872311. (It's very difficult to say there's a problem in the site's compiler since they are experts but I also had an obvious issue on another problem, so... MAYBE THEY really have an issue...). About uploading the file that you said, I don't think it's needed, because there's a direct link to it even if you're not logged in to the website, here's the link: (https://he-s3.s3.amazonaws.com/media...ICDAQTL3Q6ZBKA) (Please tell me in case it doesn't work.)(Thanks)

    (Something funny in parentheses: I submitted it and then checked answers of others to see how everyone else has solved it and then saw someone has just submitted the same answer as mine (copy pasting from here) even sooner than myself LOL, I think I'll be jailed in future for plagiarism from him)

  14. #14
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    Best Index | Basics of Input/Output & Basic Programming Practice Problems | HackerEarth

    "You are given an array of elements. Now you need to choose the best index of this array . An index of the array is called best if the special sum of this index is maximum across the special sum of all the other indices."


    Interesting, but I don't understand the exercise really. This example is clear, that have I understand . . .

    Input6
    -3 2 3 -4 3 1
    Output
    3
    ... but why is the solution 8 and 9
    input files using command line arguments possible?-index2_2018-12-15_231155-jpg

    5
    A1 - 1 + 3 + 1 + 2 + 5 = 12
    A2 - 3 + 1 + 2 + 5 = 11
    A3 - 1 + 2 + 5 = 8
    A4 - 2 + 5 = 7
    A5 - 5
    Why is the solution here 8?

    Thanks!

  15. #15
    Registered User
    Join Date
    Dec 2018
    Posts
    38
    I think I've come up with a much faster solution. I wanted to PM it to you but either I can't figure out how to PM or I'm not allowed to. Let me know if you are monitoring this thread and I'll post it as soon as possible. I'm pretty sure it'll beat the "plagiarits's" time! (If it's even correct, since again I haven't tested it much.)

    BTW, your link to the test file doesn't work for me. Did you re-download the input file to ensure you have a correct copy? I'm sure it's not a bug in the site's compiler, though! Could be something about your slightly odd system. Who knows?

    @Kernelpanic, I think you need to read the problem description more carefully.
    Last edited by bruteforce; 12-15-2018 at 06:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. command line arguments
    By KBriggs in forum C Programming
    Replies: 7
    Last Post: 06-25-2009, 01:57 PM
  2. Handling multiple input files from command line
    By cnfwriter in forum C Programming
    Replies: 13
    Last Post: 08-25-2008, 08:07 AM
  3. command line arguments
    By St0rM-MaN in forum C Programming
    Replies: 10
    Last Post: 05-07-2007, 04:40 AM
  4. Command line arguments
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 05-02-2002, 11:59 AM
  5. command line arguments
    By zbap in forum C++ Programming
    Replies: 1
    Last Post: 03-13-2002, 11:29 AM

Tags for this Thread