Friday, September 23, 2011

Mini project “Calendar Application” in C – Free Code Download.

Mini project “Calendar Application” is also a simple project built in C. This project has following features.

  1. It displays a nicely formatted calendar of every month.
  2. You can find the day by entering the day, month and year. For example, if you enter day = 10, month = 03 and year = 1991, it gives you the day 'Sunday'.
  3. You can add the note for a particular day.
  4. If the given month has a note in it, it will display || in that day. 
You can play with the source code and modify as per your need. Please remember it is just a sample project. Do not submit it as it is, as a part of your project. The source code for this project is also available on GitHub.

Here are the full lists of the Mini Projects in C/C++

Thursday, September 22, 2011

Mini project “Library management system” using C – Free Code

The project "Library Management System" is a sample of mini project in C. If you need to create a mini project in C and if you don't know where and how to start,  I hope this sample project I created will help you to get started. Please remember that the purpose of this mini project is to guide you create a mini project using C. Please just don't copy/paste and submit it. Use the code to learn and do the project yourself. Or you can modify the project, add features and submit. This project has 2 sections.

  1. Section for a librarian
  2. Section for a student
A librarian can add, search, edit and delete books. This section is password protected. That means you need administrative credentials to log in as a librarian. The credentials are created when the program is executed from the first time. A student can search for the book and check the status of the book if it is available.

Here is list of features that you can add to the project.
  1.  You can create a structure for a student that uniquely identify each student. When a student borrows a book from the library, you link his ID to Book ID so that librarian can find how burrowed particular book. 
  2. You can change the program so that it looks more interactive. 
  3. You can create a feature to bulk import the books from CSV file. 
  4. You can add REGEX to search so that a book can be searched using ID, title, author or any of the field.
  5. You can add the student login section.
  6. and much more
Here are the lists of the Mini Projects in C/C++

The sample code is also available on GitHub
#include 
#include 
#include 

// Copied from 
// https://stackoverflow.com/questions/35103745/read-a-string-as-an-input-using-scanf
void flush()
{
    int c;
    while ((c = getchar()) != '\n' && c != EOF);
}

typedef struct {
 char username[15];
 char password[15];
} User;

typedef struct {
 int id; // must be unique
 char title[50];
 char author[50]; // if more than two, separate using ,(COMMA)
 char ISBN[50];
 char category[50];
 char publication[50];
 char description[255];
 int taken;
} Book;

int main(){
 char option, admin_option, username[15], password[15], edit_option;
 int first_time, c, i, j, id, found;
 FILE *f;
 User user;
 Book book;

 f = fopen("librarian.check", "r");
 if (f == NULL) {
  fclose(f);
  f = fopen("librarian.check", "w");
  fputc(1, f);
  fclose(f);
 } else {
  fclose(f);
 }

 f = fopen("id.check", "r");
 if (f == NULL) {
  fclose(f);
  f = fopen("id.check", "w");
  fputc(0, f);
  fclose(f);
 } else {
  fclose(f);
 }
 
 printf("Log in as \n");
 printf("1:  Librarian\n");
 printf("2: Student\n");
 printf("3: Exit\n");
 printf("Enter your choice: ");
 scanf("%c", &option);
 switch(option) {
  case '1':
  f = fopen("librarian.check", "r");
  if (f == NULL) {
   printf("Couldn't read file\n");
   exit(0);
  } else {
   first_time = fgetc(f);
   if (first_time == 1) {
    fclose(f);
    flush();
    printf("Provide username and password to setup\n");
    printf("Username (14 characters max): ");
    fgets(user.username, 15, stdin);
    printf("Password (14 characters max): ");
    fgets(user.password, 15, stdin);

    // write this credential to file
    f = fopen("credential.bin", "wb");
    if (f == NULL) {
     printf("Someting went wrong!!\n");
     exit(0);
    }
    fwrite(&user, sizeof(User), 1, f);
    fclose(f);
    printf("Exit and login again to continue\n");
    f = fopen("librarian.check", "w");
    fputc(0, f);
    fclose(f);
   } else {
    fclose(f);
    flush();
    printf("Provide credential to login\n");
    printf("Username: ");
    fgets(username, 15, stdin);
    printf("Password: ");
    fgets(password, 15, stdin);

    // read the credential from file
    f = fopen("credential.bin", "rb");
    if (f == NULL) {
     printf("Someting went wrong!!\n");
     exit(0);
    }
    fread(&user, sizeof(User), 1, f);
    if (strcmp(username,user.username) != 0 || strcmp(password, user.password) != 0) {
     printf("Username or password invalid\n");
     exit(0);
    }
    printf("Login Successful!!\n");
    printf("1: Add Book\n");
    printf("2: Search Book\n");
    printf("3: Edit Book\n");
    printf("4: Delete Book\n");
    printf("4: Moderate Student Request\n");
    printf("Enter your choice: ");
    scanf("%c", &admin_option);
    switch(admin_option) {
     case '1':
     flush();
     printf("Provide the following information\n");
     printf("Title: ");
     fgets(book.title, 50, stdin);
     printf("Author: ");
     fgets(book.author, 50, stdin);
     printf("ISBN: ");
     fgets(book.ISBN, 50, stdin);
     printf("Category: ");
     fgets(book.category, 50, stdin);
     printf("Publication: ");
     fgets(book.publication, 50, stdin);
     printf("Description: ");
     fgets(book.description, 50, stdin);
     book.taken = 0;

     // increment ID
     f = fopen("id.check", "r");
     if (f == NULL) {
      printf("Someting went wrong\n");
      exit(1);
     }

     id = fgetc(f);
     fclose(f);
     book.id = id;

     // save the record
     f = fopen("book.record", "a");
     fwrite(&book, sizeof(Book), 1, f);
     fclose(f);

     id++;
     f = fopen("id.check", "w");
     fputc(id, f);
     fclose(f);

     printf("Book insertion Successful!!\n");
     break;
     case '2':
     printf("Enter the book id to search: ");
     scanf("%d", &id);

     // search in the database
     f = fopen("book.record", "rb");
     found = 0;
     i = 0;
     while(fread(&book, sizeof(Book), 1, f)) {
      if (book.id == id) {
       // matched
       found = 1;
       printf("Book Found!!\n");
       printf("Title: %s", book.title);
       printf("Author: %s", book.author);
       printf("ISBN: %s", book.ISBN);
       printf("Category: %s", book.category);
       printf("Publication: %s", book.publication);
       printf("Description: %s", book.description);
       break;
      }
      i++;
     }
     if (found == 0) {
      printf("Sorry!! The book is not in the database\n");
     }
     fclose(f);
     break;
     case '3':
     printf("Enter the book id to edit: ");
     scanf("%d", &id);
     f = fopen("book.record", "rb+");
     found = 0;
     while(fread(&book, sizeof(Book), 1, f)) {
      if (book.id == id) {
       // matched
       found = 1;
       break;
      }
     }
     if (found == 0) {
      printf("Sorry!! The book is not in the database\n");
     } else {
      printf("What field do you want to edit:\n");
      printf("1. Title\n");
      printf("2. Author\n");
      printf("3. ISBN\n");
      printf("4. Category\n");
      printf("5. Publication\n");
      printf("6. Description\n");
      printf("Enter your choice: ");
      scanf("\n%c", &edit_option);
      switch(edit_option) {
       case '1':
       flush();
       printf("Enter new title: ");
       fgets(book.title, 50, stdin);
       break;
       case '2':
       printf("Enter new author: ");
       fgets(book.author, 50, stdin);
       break;
       case '3':
       printf("Enter new ISBN: ");
       fgets(book.ISBN, 50, stdin);
       break;
       case '4':
       printf("Enter new Category: ");
       fgets(book.category, 50, stdin);
       break;
       case '5':
       printf("Enter new Publication: ");
       fgets(book.publication, 50, stdin);
       break;
       printf("Enter new Description: ");
       fgets(book.description, 255, stdin);
       break;
       default:
       printf("Enter 1 to 6\n");
       break;
      }
      fseek(f, i, SEEK_SET);
      fwrite(&book, sizeof(Book), 1, f);
      fclose(f);
      printf("Book record modified in the database!!\n");
     }
     break;
     case '4':
     printf("Enter the book id to Delete: ");
     scanf("%d", &id);
     f = fopen("book.record", "rb");
     found = 0;
     while(fread(&book, sizeof(Book), 1, f)) {
      if (book.id == id) {
       // matched
       found = 1;
       break;
      }
     }
     fclose(f);
     if (found == 0) {
      printf("Sorry!! The book is not in the database\n");
     } else {
      // create a temporary file
      FILE * temp;
      temp = fopen("book.temp", "a");
      if (temp == NULL) {
       printf("Something went wrong\n");
       exit(1);
      }

      f = fopen("book.record", "rb");

      // copy all the items except item to delete 
      // to temporary file
      while(fread(&book, sizeof(Book), 1, f)) {
       if (book.id != id)
        fwrite(&book, sizeof(Book), 1, temp);
      }
      fclose(f);
      fclose(temp);

      // delete original file
      remove("book.record");

      // rename the temporary file
      rename("book.temp", "book.record");
      printf("Book record deleted from the database!!\n");
     }
     break;
     default:
     printf("Enter only 1 - 4\n");
     break;
    }    
   }
  } 
  
  break;
  case '2':
  printf("Enter the book id to search: ");
  scanf("%d", &id);

  // search in the database
  f = fopen("book.record", "rb");
  found = 0;
  i = 0;
  while(fread(&book, sizeof(Book), 1, f)) {
   if (book.id == id) {
    // matched
    found = 1;
    printf("Book Found!!\n");
    printf("Title: %s", book.title);
    printf("Author: %s", book.author);
    printf("ISBN: %s", book.ISBN);
    printf("Category: %s", book.category);
    printf("Publication: %s", book.publication);
    printf("Description: %s", book.description);
    printf("Status: ");
    if (book.taken == 1) {
     printf("Not Available");
    } else {
     printf("Available\n");
    }
    break;
   }
   i++;
  }
  if (found == 0) {
   printf("Sorry!! The book is not in the database\n");
  }
  fclose(f);
  break;
  case '3':
  printf("Bye!!\n");
  exit(0);
  default:
  printf("Enter either 1 or 2 only\n");
  break;
 }
 return 0;
}

Tuesday, September 20, 2011

Mini project “Quiz (Who will be the millionaire)” Using C/C++ – Free download

Mini project “Quiz” is a simple project made using C. It is most useful for those who are new to C and don’t know how to start to make a mini project on C. The source code of the project is divided into many header files so as to make it more manageable. The whole project is divided into two parts: administrator mode and play mode. The questions can be added, modified or deleted via administrator mode and one can play the Quiz game via player mode. Although no graphics are included, the project uses some functions of windows.h to make it colorful.

The source code is available on GitHub. Please note that this project runs only on Windows platform.

Here are the full lists of the Mini Projects in C/C++

Some screenshots of the project are given below:

Quiz1
Quiz2

Sunday, September 18, 2011

Numerical Methods: Condition number and ill condition checking using C

Source Code:

#include<stdio.h>
#include<math.h>
void inverse(float matrix[6][6], int n){
    int i,j, k;
    float ratio,a;
    for(i = 0; i < n; i++){
        for(j = n; j < 2*n; j++){
            if(i==(j-n))
                matrix[i][j] = 1.0;
            else
                matrix[i][j] = 0.0;
        }
    }
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            if(i!=j){
                ratio = matrix[j][i]/matrix[i][i];
                for(k = 0; k < 2*n; k++){
                    matrix[j][k] -= ratio * matrix[i][k];
                }
            }
        }
    }
    for(i = 0; i < n; i++){
        a = matrix[i][i];
        for(j = 0; j < 2*n; j++){
            matrix[i][j] /= a;
        }
    }
    for(i = 0; i < n; i++){
        for(j = n; j < 2*n; j++){
            matrix[i][j-n] = matrix[i][j];
        }
    }
}
float norms(float matrix[][6],int n){
    int i,j;
    float sum = 0;
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            sum += pow(matrix[i][j], 2);
        }
    }
    return sqrt(sum);
}
int main(){
    float matrix[6][6], cond_no, norm1, norm2, det;
    int i,j,n;
    printf("Enter order of matrix: ");
    scanf("%d",&n);
    printf("Enter the matrix: \n");
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            scanf("%f",&matrix[i][j]);
        }
    }
    norm1 = norms(matrix,n);
    inverse(matrix,n);
    norm2 = norms(matrix,n);
    cond_no = norm1*norm2;
    printf("\nCondition No: %.2f\n", cond_no);
    return 0;
}

Numerical Methods: Greatest Eigen value and corresponding Eigen vector using power method in C

Source Code:

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void matrix_mul(float matrix1[3][3],float matrix2[3][1],float matrix3[3][1]){
    int i,j,k;
    for(i = 0; i < 3; i++){
        for(j = 0; j < 1; j++){
            matrix3[i][j] = 0;
            for(k = 0; k < 3; k++){
                matrix3[i][j] += matrix1[i][k]*matrix2[k][j];
            }
        }
    }
}
float findmax(float array[3][1]){
    int i;
    float  maximum;
    maximum = array[0][0];
    for(i = 0; i < 3; i++){
        if(maximum < array[i][0])
            maximum = array[i][0];
    }
    return maximum;
}
int isEqual(float matrix1[3][1], float matrix2[3][1]){
    if(matrix1[0][0] == matrix2[0][0]
    && matrix1[1][0] == matrix2[1][0]
    && matrix1[2][0] == matrix2[2][0])
        return 1;
    return 0;
}
int main(){
    float matrix1[3][3], matrix2[3][1],result[3][1];
    float eigenValue;
    int i,j,k;
    printf("Enter 3x3 matrix:\n");
    for(i = 0; i < 3; i++){
        for(j = 0; j < 3; j++){
            scanf("%f",&matrix1[i][j]);
        }
    }
    for(i = 0; i < 3; i++){
        for(j = 0; j < 1; j++){
            matrix2[i][j] = 1.0;
        }
    }
    while(1){
        matrix_mul(matrix1,matrix2,result);
        eigenValue = findmax(result);
        for(i = 0; i < 3; i++){
            result[i][0]/=eigenValue;
        }
        if(isEqual(matrix2,result)==1){
            break;
        }
        for(i = 0; i < 3; i++){
            matrix2[i][0] = result[i][0];
        }
    }
    printf("Greatest Eigen Value = %f", eigenValue);
    printf("\nAny one of Eigen Vector: \n");
    for(i = 0; i < 3; i++){
        for(j = 0; j < 1; j++){
            printf("%.2f",result[i][j]);
            printf("\t");
        }
        printf("\n");
    }
    return 0;
}

Numerical Methods: Integration of given function using Simpson’s 1/3 rule in C

Source Code:

///integration of given function using Simpson's 1/3 rule
#include<stdio.h>
float y(float x){
    return 1/(1+x*x);
}
int main(){
    float x0,xn,h,s;
    int i,n;
    printf("Enter x0, xn, no. of subintervals: ");
    scanf("%f%f%d",&x0,&xn,&n);
    h = (xn - x0)/n;
    s = y(x0)+y(xn)+4*y(x0+h);
    for(i = 3; i<=n-1; i+=2){
        s += 4*y(x0+i*h) + 2*y(x0+(i-1)*h);
    }
    printf("Value of integral is %6.4f\n",(h/3)*s);
    return 0;
}

Numerical Methods: Integration of given function using Simpson’s 3/8 rule in C

Source Code:

//integration of given function using Simpson's 3/8 rule
#include<stdio.h>
float y(float x){
    return 1/(1+x*x); //function of which integration is to be calculated
}
int main(){
    float x0,xn,h,s;
    int i,n,j,flag;
    printf("Enter x0, xn, no. of subintervals: ");
    scanf("%f%f%d",&x0,&xn,&n);
    h = (xn-x0)/n;
    s = y(x0)+y(xn);
    for(i = 1; i<=n-1;i++){
        for(j=1;j<=n-1;j++){
            if(i==3*j){
                flag = 1;
                break;
            }
            else
                flag = 0;
        }
        if(flag==0)
            s += 3*y(x0+i*h);
        else
            s += 2*y(x0+i*h);
    }
    printf("Value of integral is %6.4f\n",(3*h/8)*s);
    return 0;
}

Numerical Methods: Integration of given function using Trapezoidal rule in C

Source Code:

///integration of given function using Trapezoidal rule
#include<stdio.h>
float y(float x){
    return 1/(1+x*x);
}
int main(){
    float x0,xn,h,s;
    int i,n;
    printf("Enter x0, xn, no. of subintervals: ");
    scanf("%f%f%d",&x0,&xn,&n);
    h = (xn-x0)/n;
    s = y(x0) + y(xn);
    for(i = 1; i < n; i++){
        s += 2*y(x0+i*h);
    }
    printf("Value of integral is %6.4f\n",(h/2)*s);
    return 0;
}

Numerical Methods: Parabolic curve fitting using C

Source Code:

#include<stdio.h>
#include<math.h>
int main(){
    float xy[20][20], matrix[3][4], ratio, a;
    float sum_x = 0, sum_y = 0, sum_x2 = 0, sum_x3 = 0, sum_x4 = 0, sum_xy = 0, sum_x2y = 0;
    int i, j , k, n;
    printf("Enter no of data: ");
    scanf("%d", &n);
    printf("Enter the data: \n");
    for(i = 0; i < 2; i++){
        for(j = 0; j < n; j++){
            scanf("%f", &xy[i][j]);
        }
    }
    for(i = 0; i < n; i++){
        sum_x += xy[0][i];
        sum_y += xy[1][i];
        sum_x2 += pow(xy[0][i], 2);
        sum_x3 += pow(xy[0][i], 3);
        sum_x4 += pow(xy[0][i], 4);
        sum_xy += xy[0][i]*xy[1][i];
        sum_x2y += pow(xy[0][i], 2) * xy[1][i];
    }
    matrix[0][0] = n;
    matrix[0][1] = sum_x;
    matrix[0][2] = sum_x2;
    matrix[0][3] = sum_y;
    matrix[1][0] = sum_x;
    matrix[1][1] = sum_x2;
    matrix[1][2] = sum_x3;
    matrix[1][3] = sum_xy;
    matrix[2][0] = sum_x2;
    matrix[2][1] = sum_x3;
    matrix[2][2] = sum_x4;
    matrix[2][3] = sum_x2y;
    for(i = 0; i < 3; i++){
        for(j = 0; j < 3; j++){
            if(i!=j){
                ratio = matrix[j][i]/matrix[i][i];
                for(k = 0; k < 4; k++){
                    matrix[j][k] -= ratio * matrix[i][k];
                }
            }
        }
    }
    for(i = 0; i < 3; i++){
        a = matrix[i][i];
        for(j = 0; j < 4; j++){
            matrix[i][j] /= a;
        }
    }
    for(i = 0; i < 3; i++){
        printf("\n%c => %.2f", 97+i, matrix[i][3]);
    }
}

Numerical Methods: Inverse of nxn matrix using C

Source Code:

#include<stdio.h>
int main(){
    float matrix[10][10], ratio,a;
    int i, j, k, n;
    printf("Enter order of matrix: ");
    scanf("%d", &n);
    printf("Enter the matrix: \n");
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            scanf("%f", &matrix[i][j]);
        }
    }
    for(i = 0; i < n; i++){
        for(j = n; j < 2*n; j++){
            if(i==(j-n))
                matrix[i][j] = 1.0;
            else
                matrix[i][j] = 0.0;
        }
    }
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            if(i!=j){
                ratio = matrix[j][i]/matrix[i][i];
                for(k = 0; k < 2*n; k++){
                    matrix[j][k] -= ratio * matrix[i][k];
                }
            }
        }
    }
    for(i = 0; i < n; i++){
        a = matrix[i][i];
        for(j = 0; j < 2*n; j++){
            matrix[i][j] /= a;
        }
    }
    printf("The inverse matrix is: \n");
    for(i = 0; i < n; i++){
        for(j = n; j < 2*n; j++){
            printf("%.2f", matrix[i][j]);
            printf("\t");
        }
        printf("\n");
    }
    return 0;
}

Numerical Methods: Determinant of nxn matrix using C

Source Code:

#include<stdio.h>
int main(){
    float  matrix[10][10], ratio, det;
    int i, j, k, n;
    printf("Enter order of matrix: ");
    scanf("%d", &n);
    printf("Enter the matrix: \n");
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            scanf("%f", &matrix[i][j]);
        }
    }
    /* Conversion of matrix to upper triangular */
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            if(j>i){
                ratio = matrix[j][i]/matrix[i][i];
                for(k = 0; k < n; k++){
                    matrix[j][k] -= ratio * matrix[i][k];
                }
            }
        }
    }
    det = 1; //storage for determinant
    for(i = 0; i < n; i++)
        det *= matrix[i][i];
    printf("The determinant of matrix is: %.2f\n\n", det);
    return 0;
}

Numerical Methods: Solution of simultaneous algebraic equations using Gauss Jordan method in C

Source Code:

/*****************************************************
    Program: solution of linear algebraic equations
    using Gauss Jordan Method
    Language used: C
    Program written by Bibek Subedi
    July 2, 2011
    Tribhuvan University
*****************************************************/
#include<stdio.h>
int main()
{
    double matrix[10][10],a,b;
    int  i, j, k, n;
    printf("Enter the no of variables: ");
    scanf("%d", &n);
    printf("Enter the agumented matrix:\n");
    for(i = 0; i < n ; i++){
        for(j = 0; j < (n+1); j++){
            scanf("%lf", &matrix[i][j]);
        }
    }
    for(i = 0; i < n; i++){
       for(j = 0; j < n; j++){
            if(i != j){
                a = matrix[j][i];
                b = matrix[i][i];
                for(k = 0; k < n+1; k++){
                    matrix[j][k] = matrix[j][k] - (a/b) * matrix[i][k];
                }
            }
        }
    }
    for(i = 0; i < n; i++){
         a = matrix[i][i];
        for(j = 0; j < n+1; j++){
            matrix[i][j] /= a;
        }
    }
    printf("The required solution is: \n\n");
    for(i = 0; i < n ; i++){
        printf("%c => %.2f", i+97, matrix[i][n]);
        printf("\n");
    }
    return 0;
}

Numerical Methods: Solution of simultaneous algebraic equations using Gauss Elimination method in C

Source Code:

/*****************************************************
    Program: Solution of Linear algebraic equations
    using Gauss Elimination method
    Language used: C
    Program written by Bibek Subedi
*****************************************************/
#include<stdio.h>
int main()
{
    double matrix[10][10],a,b, temp[10];
    int  i, j, k, n;
    printf("Enter the no of variables: ");
    scanf("%d", &n);
    printf("Enter the agumented matrix:\n");
    for(i = 0; i < n ; i++){
        for(j = 0; j < (n+1); j++){
            scanf("%lf", &matrix[i][j]);
        }
    }
    for(i = 0; i < n; i++){
       for(j = 0; j < n; j++){
           if(j>i){
                a = matrix[j][i];
                b = matrix[i][i];
                for(k = 0; k < n+1; k++){
                    matrix[j][k] = matrix[j][k] - (a/b) * matrix[i][k];
                }
            }
       }
    }
    printf("The Upper triangular matrix is: \n");
    for( i = 0; i < n; i++){
        for(j = 0; j < n+1; j++){
            printf("%.2f", matrix[i][j]);
            printf("\t");
        }
        printf("\n");
    }
    printf("\nThe required result is: ");
    for(i = n-1; i>=0; i--){
        b = matrix[i][n];
        for(j = n-1 ; j > i; j--){
            b -= temp[n-j]*matrix[i][j];
        }
        temp[n-i] = b/matrix[i][i];
        printf("\n%c => %.2f",97+i, temp[n-i]);
    }
}