Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

8/13/2008

Cannot Compile C Programs in Initial Ubuntu

Ubuntu is very useful and one of Linux distribution I recommend for Linux beginner.

However, we can't compile C programs by gcc even "Hello, world!" because of not installed C developer's libraries. I think that it is a hole many beginners developing in Ubuntu fall into.

Therefore, I wrote how to install C developer's libraries "libc-dev" and editor "Emacs" in Ubuntu.

Debian Packages

Because Debian packages is available in Ubuntu, We can install many software by "apt-get" command. However, Ubuntu doesn't the administrator's account "root" from consideration on security. Therefore, we use "sudo" command instead of "su" command in order to run commands need administrative privilege. That is, we generally use "sudo" command to install a package. package means the package's name you install.

$ sudo apt-get install package

Developer's library in C: libc6-dev

Run the following command in a terminal(shell). Answer "y" to questions during install.

$ sudo apt-get install libc6-dev

Editor: emacs

$ sudo apt-get install emacs

Original Text(Japanese)

8/09/2008

Compute Determinant of 3x3-Matrix with Sarrus' Method

I wrote a program which computes determinant of 3x3-matrix with Sarrus' Method because someone accessed my blog in order to find a program computing determinant of 3x3-matrix. This program is written in C.

Of course, we can calculate it by Cofactor Expansion, but Sarrus' Method is much easier than it. So, I use Sarrus' Method.

Function to Compute Determinant of Matrix

int det(int a[3][3]) {
  return a[0][0]*a[1][1]*a[2][2]
       + a[0][1]*a[1][2]*a[2][0]
       + a[0][2]*a[1][0]*a[2][1]
       - a[0][2]*a[1][1]*a[2][0]
       - a[1][2]*a[2][1]*a[0][0]
       - a[2][2]*a[0][1]*a[1][0];
}

Main Function for Test

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
  int a[3][3], i, j;

  if(argc == 1) {
    a[0][0] = 1; a[0][1] = 2; a[0][2] = 3;
    a[1][0] = 4; a[1][1] = 5; a[1][2] = 6;
    a[2][0] = 7; a[2][1] = 8; a[2][2] = 9;
  } else if(argc == 10) {
    for(i=0; i<3; i++)
      for(j=0; j<3; j++)
 a[i][j] = atoi(argv[3*i+j+1]);
  } else {
    printf("Usage:\n");
    printf("./a.out\n");
    printf("   or\n");
    printf("./a.out a11 a12 a13 a21 a22 a23 a31 a32 a33");
  }

  printf("a =\n");
  for(i=0; i<3; i++) {
    for(j=0; j<3; j++) {
      printf("%3d", a[i][j]);
    }
    printf("\n");
  }
  printf("\n");

  printf("det(a) = %d\n", det(a));

  return 0;
}

Output

$ ./det
a =
  1  2  3
  4  5  6
  7  8  9

det(a) = 0
$ ./det 1 6 -2 5 -3 4 -1 -2 4
a =
  1  6 -2
  5 -3  4
 -1 -2  4

det(a) = -122

Main Function for Test(Simple Version)

I prepare simple version of test function for beginners of programming.

#include <stdio.h>

int main(int argc, char *argv[]) {
  int a[3][3];

  a[0][0] = 1; a[0][1] = 2; a[0][2] = 3;
  a[1][0] = 4; a[1][1] = 5; a[1][2] = 6;
  a[2][0] = 7; a[2][1] = 8; a[2][2] = 9;

  printf("det(a) = %d\n", det(a));

  return 0;
}

Original Text(Japanese)

Implement Simple Shell - UNIX System Programming

I implemented a simple shell in programming language C with UNIX system calls. Now it has only following functions, but I will add some functions(Redirection, Pipe, Background running, ...) in future.
  1. Run inputed commands.
  2. "exit" command.
Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <unistd.h>

#define MAX_ARGS 10
#define MAX_LEN  100

void child(int argc, char *argv[MAX_ARGS]);

int main()
{
  int argc, n = 0;
  int status;
  char input[MAX_LEN], *argv[MAX_ARGS], *cp;
  const char *delim = " \t\n"; /* delimiters of commands */

  while (1) {
    /* show a prompt */
    ++n;
    printf("command[%d] ", n); 

    /* read a line */
    if (fgets(input, sizeof(input), stdin) == NULL) {
      /* inputed EOF(Ctrl-D) */
      printf("Goodbye!\n");
      exit(0);
    }

    /* split the input to commands with blank and Tab */
    cp = input;
    for (argc = 0; argc < MAX_ARGS; argc++) {
      if ((argv[argc] = strtok(cp,delim)) == NULL)
        break;
      cp = NULL;
    }
    /* exit this shell when "exit" is inputed */
    if(strcmp(argv[0], "exit") == 0) {
      printf("Goodbye!\n");
      exit(0);
    }
    pid_t pid = fork();
    if(pid == -1) {
 /* failed to fork() */
      perror("fork");
      exit(1);
    } else if(pid == 0) {
 /* child process */
      child(argc, argv);
    } else {
 /* parent process */
      wait(&status);
    }
  }
}

void child(int argc, char *argv[MAX_ARGS]) {
  execvp(argv[0], argv);
}
Result
****@ubuntu-vm:~/work/os$ gcc -Wall s_shell.c
****@ubuntu-vm:~/work/os$ ./a.out
command[1] ls
#ex01.c#  execlp0.c   fileio1.c~  fileio4.c~  s_shell.c
a.out     execve0.c   fileio2.c   foo         system0.c
ex1.c     execve0.c~  fileio2.c~  fork0.c     wait0.c
ex1.c~    fileio0.c   fileio3.c   fork0.c~    wait0.c~
ex2.c     fileio0.c~  fileio3.c~  hello.c
ex2.c~    fileio1.c   fileio4.c   httpget.c
command[2] cat hello.c
#include <stdio.h>
int main() {
  printf("hello, world!\n");
  return 0;
}
command[3] gcc -Wall hello.c
command[4] ./a.out
hello, world!
command[5] exit
Goodbye!
****@ubuntu-vm:~/work/os$ 
Reference site Original Text(Japanese)