Is there any way to put Input automatically?

Asked

Viewed 182 times

0

Recently in a programming contest I came across the problem of having to put more than 250 inputs in one problem. I wonder if there is any way to put the Inputs other than 1 by 1, at hand!

  • html input? "put over 250 inputs in one problem"... what problem?

  • Has some code?

  • For example: Make the average of 200 numbers. In this case would have to make a large number of inputs! If I was wrong in one number I would have to repeat all the inputs!

  • Right now I don’t have the code since the contest was held at a university and I didn’t have a chance to keep it!

  • You can use a scripting language like php?

  • The authorized languages in the contest if I remember were: C, C++, Java, VB and Pascal!

  • I answered before you specify which languages were allowed, you want me to delete my reply?

  • If you don’t mind! It can cause confusion to people with the same doubt as me!

  • Hello @Diorrini11, I found a topic in the meta (http://meta.pt.stackoverflow.com/questions/2465/respostas-na-linguagem-errada-devem-ser-exclu%C3%Addas/2466#2466) that explains this initial communication failure, I will be leaving the answer (I will edit to clarify the mistake) to serve as a subsidy to future responses

  • The question is very vague, there is an initial code, in the language you will do this, maybe the loop for can help.

  • 1

    As the colleague @qmechanik has already mentioned, the question is very vague. By input I also understood that you mean "data entry", and in this case you can do it in any language following the principle of the answer you already have. Even in C++ you can make a loop (a while) that repeats a question to the user and stores it in an array. That is, it’s really trivial. The fundamental question is: why is this important? (It would be the user’s difficulty to manually type 250 entries?)

  • In the case of the example with the average of 200 numbers, isn’t it simpler to pass a text file with the numbers to the program? So, if user "misses" a number, just edit the file, fix it, and run again.

Show 7 more comments

3 answers

0

I answered the question before the questioner specified all the permitted languages, but this answer can be used as a subsidy (the idea is literally the same only changes the language) to construct the answer, for details on this language conflict see this discussion: meta.pt.stackoverflow

If you can use a Language (PHP in my example) Voce can go printando what you want on the page.

foreach($sites as $site){
    printSite($site);
}

function printSite($site){
    echo '
    <div class="checkbox">
         <label><input type="checkbox" name="site[]" value="'.$site.'">'.$site.'</label>
    </div>
    ';
 }

0

The possibility of redirecting input is a matter of survival for development, testing. Even for the organization of computer tournaments...

Taking the case referred "average of two hundred numbers" follows some examples of tests (Unix+bash, adaptable to other environments)

0) if we have a known file

./myprogram < file

1) the average of 200 equal numbers has to give that number

yes 33 | head -200 | ./myprogram 

has to give 33;

2) the average of the numbers from 1 to 200

seq 200 | ./myprogram

has to be 100.5

3) Idem for this sequence after shuffle:

seq 200 | shuf | ./myprogram

4) possibly add the test to the Makefile:

test: myprogram
    diff <(./myprogram < file) <(echo 53)                 && echo OK
    diff <(yes 33 | head -200| ./myprogram) <(echo 33)    && echo OK
    diff <(seq 200 | shuf    | ./myprogram) <(echo 100.5) && echo OK

0


The most practical answer is yes. You can throw the inputs you need into a TXT file and read into the program.

If you are using Linux from to use an input redirect. A very crude example:

Suppose you will receive the Inputs in the following format

N
o

Where 'N' is the total number of inputs and 'o' is the input of each value, ex.

10
1
2
3
4
5
6
7
8
9
10

I saved this guy as a test.txt

Ex. reading in C++

#include <iostream>

int main() {
        int N = 0;

        std::cin >> N;

        for (int i = 0; i < N; i++) {
                int o;
                std::cin >> o;
                std::cout << o << std::endl;
        }
}

compiling

g++ -o teste teste.c

Running on some *Nix with input redirect and the result

adirkuhn: ~ $ ./teste < teste.txt        
1
2
3
4
5
6
7
8
9
10
adirkuhn: ~ $ 

Example 2: teste2.txt

5
31
32
33
34
35

Running and result:

adirkuhn: ~ $ ./teste < teste2.txt 
31
32
33
34
35
adirkuhn: ~ $ 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.