C program with math.h

C program with math.h

von Utpal Das -
Anzahl Antworten: 12
Following code (prog.c) with math.h cannot be compiled in Coderunner environment:
#include <stdio.h>
#include <math.h>
int main()
{
    float a;
    scanf("%f",&a);
    printf("%f", sqrt(a));
    return 0;
}

Because on Ubuntu, compilation of math.h will require an extra option "-lm" (refer https://askubuntu.com/questions/332884/how-to-compile-a-c-program-that-uses-math-h for details)
gcc -o prog prog.c (does not work)
gcc -o prog prog.c -lm (works)

For minimal change in the code, I had modified the following line under c_task .php:
$cmd = "gcc " . implode(' ', $compileargs) . " -o $execFileName $src" . implode(' ', $linkargs) ;
as:
$cmd = "gcc " . implode(' ', $compileargs) . " -o $execFileName $src" . implode(' ', $linkargs) . " -lm";

However, the compilation is still not going thru in Coderunner environment. Please help.
Tags:
Als Antwort auf Utpal Das

Re: C program with math.h

von Richard Lobb -

You can pass compile and link arguments to the C compiler by customising the question (click the "Customise" checkbox), opening the Advanced Customisation section, and setting an appropriate value in the Sandbox > Parameters field. In this case the value you want is

{"linkargs":["-lm"]}

Note that that's an 'el', not a 'one'.

You shouldn't ever need to modify the source code on Jobe.

Richard

Als Antwort auf Richard Lobb

Re: C program with math.h

von Miguel López -

Is there any way to use M_PI ?

Als Antwort auf Miguel López

Re: C program with math.h

von Utpal Das -

Sure. While editing the question, use Customization and specify Parameters under Advanced Customisation as:

{"linkargs":["-lm"], "compileargs": ["-std=gnu99"] }

Specifying linkargs is essential for math.h, and compileargs is required for M_PI (refer link for details).

Als Antwort auf Utpal Das

Re: C program with math.h

von Miguel López -

Many, many  thanks for your last answer it Works perfectly. Now, is there any kind of question in coderunner to process pseudocode ?

Als Antwort auf Miguel López

Re: C program with math.h

von Richard Lobb -

There's no built-in question type for this. You can define your own question types but the problem with processing pseudo code is that by its very nature it has no formal specification. It's *pseudo* code. So how would you define whether it was right or not?

You could specify a basic set of pseudo-code control structures that students were obliged to follow and then check for the expected ones but even that's likely to be ill-defined. There are usually multiple ways of solving a problem, and *while* and *for* loops are potentially interchangeable. The principle behind CodeRunner is that correctness is determined by executing the code to see if it does the right thing. That's just not possible with pseudocode.

Als Antwort auf Richard Lobb

Re: C program with math.h

von Miguel López -

Thank you for your kind and very complete response. So, strictly establishing the set of structures to be used in pseudocode (like a programming language) and having a compiler for that set, would it be possible?
Als Antwort auf Miguel López

Re: C program with math.h

von Miguel López -

Although recognize the sqrt function, don't recognizes the pow function, what might be happening?

Als Antwort auf Miguel López

Re: C program with math.h

von Richard Lobb -

We need more information to answer this question.

I suggest you first click the Template Debugging checkbox in the question editing form, save the question with validation turned off, then run it from a question preview window. If you study the code that's run, you will likely be able to spot the problem yourself. If not, please paste the code that's being run into your next posting, tell us what question type you're using, and include a screen dump showing what error you're getting, or at least the text of the error message.

Als Antwort auf Richard Lobb

Re: C program with math.h

von Miguel López -

Hi, I'm using the c_via_python question type.

Here's the code:

#include <stdio.h>
#include <math.h>
int main()
{ int cont, nc, ng;
printf(\"No. de casillero: \");
scanf(\"%d\", &nc);
for(cont=1; cont <=nc; cont++)
{
ng = pow(2, cont-1); 
printf(\"%d \", ng);
}
}

and, here's the error: 

***Error*** /tmp/ccmzaEe7.o: In function `main': prog.c:(.text+0x68): undefined reference to `pow' collect2: error: ld returned 1 exit status ** Compilation failed. Testing aborted **

Template's parameters:

{"linkargs":["-lm"], "compileargs":["-std=c89"] }

Als Antwort auf Miguel López

Re: C program with math.h

von Richard Lobb -

The linkargs and compileargs sandbox parameters are relevant to C jobs but you're actually running a Python job. Those parameters aren't relevant to Python.

The c_via_python question type is not intended to be the primary way to run a C program. It's an example of how to implement a different language by writing a Python script to compile and run a program in that new language. The commands to compile and build the C program are embedded within the Python script. If you wish to set compile args and link args for this question type you'll have to insert them into the Python script.

Or you can just go back to using one of the built-in C question types like c_function or c_program, as you apparently were doing at the start of this thread.