IS1001
IS1001

Reputation: 43

Using octave built-in function in C

I am told to compile this program using C in octave. I have succeeded in compiling a simple C program before using Octave, but for this one, I have tried and I have no idea how to compile it in C even though I have tried to change some of the C++ language into C language.

 #include <iostream>

 #include <octave/oct.h>

 int main (void)

 {
 std::cout << "Hello Octave world!\n";

 int n = 2;

 Matrix a_matrix = Matrix (n, n);

 for (octave_idx_type i = 0; i < n; i++)

 for (octave_idx_type j = 0; j < n; j++)

 a_matrix(i,j) = (i + 1) * 10 + (j + 1);

 std::cout << a_matrix;

 return 0;
 }

So is there any solution to this problem? Thank you very much and sorry for any mistakes in the post that I made since I'm pretty new in using octave and in this forum.

EDIT (After Alter Mann's Answer):

So this is my C program code

#include <stdio.h>

int main(void)
{
        printf("Hello Octave World!");

    int n=2;
    Matrix a_matrix = Matrix (n, n);

    for (octave_idx_type i=0; i<n; i++)
    {
            for (octave_idx_type j=0; j<n; j++)
            {
                    a_matrix(i,j) = (i+1) * 10 + (j+1);
            }

            printf ("%d", &a_matrix);
    }
    return 0;
}

But I got this error

standalone.c: In function ‘main’:
standalone.c:8: error: ‘Matrix’ undeclared (first use in this function)
standalone.c:8: error: (Each undeclared identifier is reported only once
standalone.c:8: error: for each function it appears in.)
standalone.c:8: error: expected ‘;’ before ‘a_matrix’
standalone.c:10: error: ‘octave_idx_type’ undeclared (first use in this function)
standalone.c:10: error: expected ‘;’ before ‘i’
standalone.c:10: error: ‘i’ undeclared (first use in this function)
standalone.c:12: error: expected ‘;’ before ‘j’
standalone.c:12: error: ‘j’ undeclared (first use in this function)
standalone.c:14: warning: implicit declaration of function ‘a_matrix’
standalone.c:17: error: ‘a_matrix’ undeclared (first use in this function)

Upvotes: 3

Views: 1778

Answers (1)

David Ranieri
David Ranieri

Reputation: 41017

#include <iostream>

should be

#include <stdio.h>

and

std::cout << a_matrix;

should be

printf("%d", a_matrix);

But <octave/oct.h> can not be used in C:

A.1.1 Getting Started with Oct-Files

The first critical line is #include which makes available most of the definitions necessary for a C++ oct-file. Note that octave/oct.h is a C++ header and cannot be directly #include’ed in a C source file, nor any other language.

Alternative:

The interface is centered around supporting the languages C++, C, and Fortran. Octave itself is written in C++ and can call external C++/C code through its native oct-file interface. The C language is also supported through the mex-file interface for compatibility with MATLAB. Fortran code is easiest to reach through the oct-file interface.

Upvotes: 2

Related Questions