Saturday, 25 February 2017

Array2D

import java.util.Scanner;

public class Runner {
static NewArray2D a = new NewArray2D();

protected static int rows;
protected static String val;
protected static int r;
protected static int c;

public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("Enter Number of records:");
rows=in.nextInt();

a.create(rows, 5);

System.out.println();

a.put(0, 0, (String) "Customers Name");
a.put(0, 1, (String) "Brand Preffered");
a.put(0, 2, (String) "Avg. Monthly shopping Bill");
a.put(0, 3, (String) "Monthly Income");
a.put(0, 4, (String) "Monthly Expenses");

for( r=1; r<rows; r++){
for( c=0; c<5; c++){

System.out.println("Enter " + c +" record");
val=in.next();

if(c<2 && Character.isLetter(val.charAt(0)))
{a.put(r, c, val);System.out.println();}

else if(c<2 && Character.isDigit(val.charAt(0)))
{System.out.println("Invalid input");System.out.println();}

else if(c>=2 && Character.isDigit(val.charAt(0)))
{a.put(r, c, val);System.out.println();}
else
{System.out.println("Invalid input");}
}
}

System.out.println(a.toString());

System.out.println();
System.out.println("Do you want to change any block?:" + " " + "(Y/N)");
char choice=in.next().charAt(0);

if(choice=='Y'|| choice=='y'){
System.out.println("Which block do you want to edit:");
System.out.println("Row: "); r=   in.nextInt();
System.out.println("Column: ");  c=   in.nextInt();
System.out.println("Value: ");   val= in.next();

if(c<2 && Character.isLetter(val.charAt(0)))
{a.put(r, c,val );System.out.println();}

else if(c<2 && Character.isDigit(val.charAt(0)))
{System.out.println("Invalid input");
System.out.println();}

else if(c>=2 && Character.isDigit(val.charAt(0)))
{a.put(r, c, val);System.out.println();}

else
{System.out.println("Invalid input");}
}

if(choice=='N' || choice=='n')
{System.exit(0);}
System.out.println(a.toString());
in.close();

}
}


public class NewArray2D {
protected int tcols;
protected int trows;
protected Object[] elem;

public NewArray2D(int row,int col) {
if(row<1||col<1)throw new IllegalArgumentException();

elem=new Object[row*col];
tcols=col;
trows=row;
}

public NewArray2D() {
trows=tcols=0;
}

public void create(int row,int col) {
if(row<1||col<1)throw new IllegalArgumentException();

elem=new Object[row*col];
tcols=col;
trows=row;
}

public void checkIndex(int row,int col){
if(row<0||col<0)throw new IndexOutOfBoundsException();
}

public void put(int row,int col,Object telem){
checkIndex(row, col);
elem[row*tcols+col]=telem;return;
}

public Object get(int row,int col){
checkIndex(row, col);
return elem[row*tcols+col];
}

public Object remove(int row,int col){
checkIndex(row, col);
Object ob=elem[row*tcols+col];
elem[row*tcols+col]=null;
return ob;


}
public int nrows(){return trows;}
public int ncols(){return tcols;}

public String toString(){
StringBuffer s= new StringBuffer("[");

for(int r=0;r<trows;r++){
for(int c=0;c<tcols;c++){
if(elem[r*tcols+c]==null)s.append("_, ");
else s.append(elem[r*tcols+c].toString()+", ");
}
s.delete(s.length()-2, s.length());
s.append(" # ");
}
if(trows>0&&tcols>0)
s.delete(s.length()-2, s.length());

s.append("]");
return new String (s);
}
}

Generic Array2D


public class Array2D <T>{
protected int trows;
protected int tcols;
protected T[] elements;


@SuppressWarnings("unchecked")
public Array2D( int rows, int cols )
{

if( rows<1 || cols<1 )
throw new IllegalArgumentException ("initialCapacity must be >= 1");

elements =(T[]) new Object[rows*cols];
trows = rows;
tcols = cols;
}

public Array2D()
{
trows = tcols = 0;
}

@SuppressWarnings("unchecked")
public void create( int rows, int cols )
{
if( rows<1 || cols<1 )
throw new IllegalArgumentException ("initialCapacity must be >= 1");

elements = (T[]) new Object[rows*cols];
trows = rows;
tcols = cols;
}

void checkIndex(int row, int col)
{
if( row < 0 || row>=trows || col < 0 || col>=tcols )
throw new IndexOutOfBoundsException ("Row: "+ row + ", Range: 0 - "+trows+"Col: "+ col + ", Range: 0 - "+tcols);
}

public void put( int row, int col, T theElement )
{
checkIndex(row, col);

elements[row*tcols+col] = theElement;
return;
}

public T get( int row, int col )
{
checkIndex(row, col);
return
(T)elements[row*tcols+col];
}


public Object remove( int row, int col )
{
checkIndex(row, col);

Object ob = elements[row*tcols+col];
elements[row*tcols+col] = null;

return ob;
}

public int nrows()
{
return trows;
}

public int ncols()
{
return tcols;
}

public String toString()
{
StringBuffer s = new StringBuffer("[");

for (int r=0; r<trows; r++) {
for (int c=0; c<tcols; c++) {
if(elements[r*tcols+c] ==null)
s.append("_, ");
else s.append(elements[r*tcols+c].toString() + ", ");
}

s.delete(s.length() -2, s.length());
s.append(" # ");
}

if(trows>0 && tcols>0)
s.delete(s.length() -2, s.length());

s.append("]");
return new String(s);
}
}

import java.util.Scanner;

public class Runner {
public static void main(String[] args) {
int i, j;

int tRows;
int tCols;

Array2D<Double> Ob1;
Array2D<Double> Ob2;
Array2D<Double> Ob3;

Scanner s = new Scanner(System.in);

System.out.print("Rows   : "); tRows = s.nextInt();
System.out.print("Columns: "); tCols = s.nextInt();

Ob1=new Array2D<Double>( tRows, tCols );
Ob2=new Array2D<Double>( tRows, tCols );
Ob3=new Array2D<Double>( tRows, tCols );

System.out.println( "--------------------" );
System.out.println( "Enter values for first matrix" );

for( i=0 ; i<tRows ; i++ )
{
for( j=0 ; j<tCols ; j++ )
{
Ob1.put( i, j,  s.nextDouble() );
}
}

System.out.println("--------------------");
System.out.println( "Enter values for second matrix" );

for( i=0 ; i<tRows ; i++ )
{
for( j=0 ; j<tCols ; j++ )
{
Ob2.put( i, j,  s.nextDouble() );
}
}

for( i=0 ; i<tRows ; i++ )
{
for( j=0 ; j<tCols ; j++ )
{
Ob3.put( i, j, Ob1.get(i,j)+ Ob2.get(i,j) );
}
}


System.out.println("---Results----");

for( i=0 ; i<tRows ; i++ )
{
for( j=0 ; j<tCols ; j++ )
{
System.out.print( Ob3.get(i,j)+"\t" );
}
System.out.println();
}

s.close();
}
}

Saturday, 18 February 2017

Guess Game

#include <stdlib.h>
#include <iostream>
#include <time.h>

using namespace std;
int main()
{
srand(time(NULL));  
int number=rand()%100;
int guess=-1;
int trycount=0;
while(guess!=number && trycount<8)

{
cout<<"Please enter a guess: ";
cin>>guess;

if(guess<number)
cout<<"Too low"<<endl;

if(guess>number)
 cout<<"Too high"<<endl;

trycount++;
}
if(guess==number)
cout<<"You guessed the number";
else
cout<<"Sorry, the number was: "<<number;
return 0;
}

Tuesday, 14 February 2017

Array Manipulation (C++)


#include <iostream>    
      using namespace std;
      int main()
{
  int array[3];
  for(int x=0; x<3; x++){
  cin>>array[x];
  }
  for (int loop=0;loop<3 ; loop++){
  cout<<array[loop];if(loop<2){cout<<",";}
 
}
return 0;
}

Sunday, 5 February 2017

Pime Number

#include<stdio.h>

int main()
{
   int n, i = 3, count, c;

   printf("Enter the number of prime numbers required\n");
   scanf("%d",&n);

   if ( n >= 1 )
   {
      printf("First %d prime numbers are :\n",n);
      printf("2\n");
   }

   for ( count = 2 ; count <= n ;  )
   {
      for ( c = 2 ; c <= i - 1 ; c++ )
      {
         if ( i%c == 0 )
            break;
      }
      if ( c == i )
      {
         printf("%d\n",i);
         count++;
      }
      i++;
   }

   return 0;
}

Thursday, 2 February 2017

Bookclub

Bookclub is available on the following link:

https://drive.google.com/open?id=0B5D2vtZ05oIsQmY5NUpoMlBmYmc

Payroll

Payroll system project is available on the following link:

https://drive.google.com/open?id=0B5D2vtZ05oIsa0ZnTVJER1RFbUU

Tuesday, 31 January 2017

Even/Odd

#include <stdio.h>
#include <conio.h>

int main(){
int num;

printf("Enter a number:\n");
scanf("%d",&num);

if(num%2==0){
printf("\nEven");
}

else
printf("\nOdd");
}

Saturday, 28 January 2017

Maximum and Minimum

#include <stdio.h>
#include <conio.h>

int main(){
int NOE;
int value;

printf("Enter number of elements: ");
scanf("%d",&NOE);

int array[NOE];

for(int c=0 ; c<NOE ; c++){
printf("Enter value %d", c+1);printf(") ");
scanf("%d",&value);
array[c]=value;
}

int max=array[0];
int min=array[0];

for(int c = 0 ; c<NOE ; c++){

if (array[c]>max){
max=array[c];
}

if(array[c]<min){
min=array[c];
}
}
printf("Min: %d",min);
printf("\nMax: %d",max);

}

Pharmacy Management System


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


int main(){

FILE *fp,*ft;
char another,choice;

struct companyStock {

char stocks[50], sells[50], medicines[50], company[50];
};


struct companyStock e;
char numberOfProducts[50];
long int strSize;

fp=fopen("Stock Details.txt","rb+");

if(fp==NULL) {

fp=fopen("Stock Detail.txt","wb+");

if(fp==NULL){
puts("Cannot open file");

return 0;
}
}

strSize=sizeof(e);

while(1){

system("cls");

printf("Pharmacy Management System") ;
printf("\n\n");
printf("1. Add") ;
    printf("\n2. Show");
    printf("\n3. Modify");
    printf("\n4. Delete");
    printf("\n5. Exit");
    printf( "\n\n");
   
printf( " Select Your Choice : ");
   
    fflush(stdin);
choice = getche();
switch(choice){

case '1':

fseek(fp,0,SEEK_END);
another='Y';
while(another=='Y' || another=='y'){

system("cls");
           
   printf ( "Enter the number of Stocks : ");
                scanf ("%s",&e.stocks);getchar();
               
printf ("Enter the number of Sells : ");
                scanf ("%s",&e.sells);getchar();
               
printf ( "Enter the number of Medecines: ");
                scanf ("%s", &e.medicines);getchar();
               
printf (  "Enter the name of Company: ");
                scanf("%s", &e.company);getchar();
               
fwrite(&e,strSize,1,fp);
               
printf( "\n Add Another Record (Y/N) ");
                fflush(stdin); //
               
another = getchar();
            }
            break;

case '2':

system("cls");
rewind(fp);

printf( "=== View the Records in the Database ===");
         printf( "\n");
         
  while (fread(&e,strSize,1,fp) == 1){
           
  printf( "\n");
           printf("\n") ;

  printf ("Number of Stocks: %s",e.stocks) ;
 
  printf ("\nNumber of sells: %s",e.sells);
 
  printf("\nNumber of Medecines: %s",e.medicines);
 
  printf ("\nName of company: %s",e.company);
         
  }
           printf( "\n\n");
         
  system("pause");
           break;

 case '3' :
            system("cls");
            another = 'Y';
         
 while (another == 'Y'|| another == 'y')
          {
              printf("\n Enter the number of Stocks : ");
              scanf("%d", numberOfProducts);

            rewind(fp);
            while (fread(&e,strSize,1,fp) == 1)
            {
                if (strcmp(e.stocks,numberOfProducts) == 0)
                {
                printf( "Enter new number of Stocks : ");
                scanf("%c", e.stocks);
               
printf( "Enter new number of Sells : ");
                scanf("%c", e.sells);
               
printf( "Enter new number of Medecines : ");
                scanf("%c", e.medicines);
               
printf( "Enter name of new Company : ");
                scanf("%c" ,e.company);
               
fseek(fp, - strSize, SEEK_CUR);
                fwrite(&e,strSize,1,fp);
               
break;
            }
                else
               
                printf("Record not found");
           
}
           
printf( "\n Modify Another Record? (Y/N) ");
                fflush(stdin);
                another = getchar();
            }
           
break;
           
            case '4':
        system("cls");
            another = 'Y';
           
          while (another == 'Y'|| another == 'y')
          {
              printf( "\n Enter the number of stocks to delete : ");
              scanf("%d", numberOfProducts);

              ft = fopen("temp.dat", "wb");

              rewind(fp);
         
     while (fread (&e, strSize,1,fp) == 1)

                 if (strcmp(e.stocks,numberOfProducts) != 0)
                {
                    fwrite(&e,strSize,1,ft);
                }
                fclose(fp);
                fclose(ft);
         
       remove("Stock Details.txt");
         
       rename("temp.dat","Stock Details.txt");

                fp=fopen("Stock Details.txt","rb+");

                printf( "\n Delete Another Record (Y/N) ");
                fflush(stdin);
                another = getchar();
            }

              break;

              case '5':
              fclose(fp);
              printf( "\n\n");
           
 printf("\t\t     THANK YOU FOR USING THIS SOFTWARE");
           
 printf("\n\n");
           
 exit(0);
          }
     }


system("pause");

return 0;
}