Search This Blog

Friday 3 June 2016

WAP that obtains the rotation factor and rotates the array as per it.

/*
WAP that obtains the rotation factor and rotates the array as per it.
*/
#include<iostream.h>
int main()
{
      int Arr[15] = {22, 31, 12, 44, 5, 17, 1, 69, 11, 23, 6, 9, 23, 67, 90};
      int r;
      cout<<"\nEnter rotation factor (ie rotate by ?): ";
      cin>>r;
      cout<<"Originally array is: "<<endl;
      for(int i=0;i<15;i++)
      cout<<Arr[i]<<",";
      cout<<":::"<<endl;
      //now rotate the array
      int temp[15];
      for(i=14;i>=0;i--)
      {
            if((i+r) >=15)
                  temp[i + r - 15] = Arr[i];
            else
                  temp[i+r] = Arr[i];
      }
      cout<<"Array after Rotation by factor "<<r<<" is: "<<endl;
      for(i=0;i<15;i++)
      {
            Arr[i] = temp[i];
            cout<<Arr[i]<<",";
      }
      cout<<":::"<<endl;
}

OUTPUT




WAP to create a single file and then display its contents.

//WAP to create a single file and then display its contents.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
void main()
{
      system("cls");
      ofstream fout("student", ios::out);
      char name[30], ch;
      float marks =0.0;
      //Loop to get 5 records
      for(int i=0;i<5;i++)
      {
            cout<<"Student "<<(i+1)<<":\tName: ";
            cin.get(name,30);
            cout<<"\t\tMarks: ";
            cin>>marks;
            cin.get(ch);      //to empty input buffer write to the file
            fout<<name<<"\n"<<marks<<"\n";
      }
      fout.close();     //disconnect student file from fout
      ifstream fin("student", ios::in);   //connect student file to input stream fin
      fin.seekg(0);     //To bring file pointer at the file beginning
      cout<<"\n";
      for(i=0;i<5;i++)  //Display records
      {
            fin.get(name,30); //read name from file student
            fin.get(ch);
            fin>>marks; //read marks from file student
            fin.get(ch);
            cout<<"Student Name: "<<name;
            cout<<"\tMarks: "<<marks<<"\n";
      }
      fin.close();      //disconnect student file from fin stream
      getch();

}
OUTPUT



WAP to display contents of a file using get() function.

//WAP to display contents of a file using get() function.
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
int main()
{
      system("cls");
      char ch;
      ifstream fin;     //create input stream
      fin.open("marks.dat", ios::in);     //open file
      if(!fin)    //if fin stores zero i.e., false value
      {
            cout<<"Cannot open file!!\n";
            return 1;
      }
      while(fin)
      {     fin.get(ch);      //read a character
            cout<<ch;   //display the character
      }
      fin.close();
      return 0;
}
OUTPUT


WAP to get roll numbers and marks of the student of a class (get from the user) and store these details into a file called Marks.dat

/*WAP to get roll numbers and marks of the student of a class (get from the user) and store these details into a file called Marks.dat*/
#include<iostream.h>
#include<fstream.h>
int main()
{     ofstream filout;
      filout.open("marks.dat",ios::out);
      char ans = 'y';
      int rollno;
      float marks;
      while(ans =='y'||ans =='Y')
      {
            cout<<"\nEnter Roll no.: ";
            cin>>rollno;
            cout<<"\nEnter Marks : ";
            cin>>marks;
            filout<<rollno<<'\n'<<marks<<'\n';
            cout<<"\nWant to enter more records? (y/n)... ";
            cin>>ans;
      }
      filout.close();
      return 0;
}


OUTPUT

WAP using a class to store price list of 5 items and to print the largest prices as well as the sum of all prices.

/*WAP using a class to store price list of 5 items and to print the largest prices as well as the sum of all prices.*/
#include<iostream.h>
#include<stdlib.h>
class ITEM{ int itemcode[5];
                  float it_price[5];
            public:
                  void initialize (void);
                  float largest (void);
                  float sum (void);
                  void display_items (void);
            };
//.....Member Function Definitions Follow.....
void ITEM::initialize (void)
{ for(int i=0;i<5;i++)
      {     cout<<"\n"<<"Item No.: "<<(i+1);
            cout<<"\n"<<"Enter item code: ";
            cin>>itemcode[i];
            cout<<"\n"<<"Enter item price: ";
            cin>>it_price[i];
            cout<<"\n";
      }
}
float ITEM::largest(void)
{     float large = it_price[0];
      for(int i=1;i<5;i++)
      {
            if(large<it_price[i])
                  large = it_price[i];
      }
      return large;
}
float ITEM::sum (void)
{     float sum = 0;
      for(int i = 0; i<5;i++)
      sum+=it_price[i];
      return sum;
}
void ITEM::display_items(void)
{     cout<<"\nCode Price\n";
      for(int i = 0; i<5;i++)
      {     cout<<"\n"<<itemcode[i];
            cout<<" "<<it_price[i];
      }
      cout<<"\n";
}
int main()
{
      ITEM order;
      order.initialize();     //initialize arrays
      float total, biggest;
      int ch = 0;
      system ("cls");
      do
      {
            cout<<"\nMain Menu.\n";
            cout<<"\n1.Display largest price.";
            cout<<"\n2.Display sum of prices.";
            cout<<"\n3.Display item list.";
            cout<<"\nEnter your choice(1-3): ";
            cin>>ch;
            switch(ch)
            {     case 1: biggest = order.largest();
                              cout<<"The Largest price is "<<biggest<<"\n";
                              break;
                  case 2: total = order.sum();
                              cout<<"The sum of prices is "<<total<<"\n";
                              break;
                  case 3: order.display_items();
                              break;
                  default: cout<<"\nWrong choicee!\n";
                               break;
            } //end of switch
      }
      while(ch>=1&&ch<=3);
      return 0;
      }
OUTPUT


WAP to illustrate working of default arguments. Calculate interest amount making use of default arguments.

/*
WAP to illustrate working of default arguments. Calculate interest amount
making use of default arguments.
*/
#include<iostream.h>
#include<stdlib.h>
void amount(float princ, int time = 2, float rate = 0.08); //prototype
void amount(float princ, int time, float rate)
{
      cout<<"\nPrincipal Amount: "<<princ;
      cout<<"\tTime: "<<time<<" years;";
      cout<<"\tRate: "<<rate;
      cout<<"\nInterest Amount: "<<(princ*time*rate)<<"\n";
}
int main()
{
      system ("cls");
      cout<<"Case 1";
            amount(2000);
      cout<<"Case 2";
            amount(2500, 3);
      cout<<"Case 3";
            amount(2300, 3, 0.11);
      cout<<"Case 4";
            amount(2500, 0.12);
      return 0;
}
OUTPUT



WAP to illustrate working of call-by-value method of a function invoking.

/*
WAP to illustrate working of call-by-value method of a function invoking.
*/
#include<iostream.h>
#include<stdlib.h>
int main()
{
      system ("cls");
      void change(int);
      int orig = 10;    //original value is 10.
      cout<<"The original value is: "<<orig<<"\n";
      change(orig);
      cout<<"Value after change() is over: "<<orig<<"\n";
      return 0;
}
void change(int orig)
{     orig = 20;  //value of orig (the formal parameter) becomes 20.
      cout<<"Value of orig in function change() is: "<<orig<<"\n";
      return;
}


OUTPUT

WAP to illustrate working of call-by-reference method of a function invoking.

/*
WAP to illustrate working of call-by-reference method of a function invoking.
*/
#include<iostream.h>
#include<stdlib.h>
int main()
{
      system ("cls");
      void change(int &);     //notice prototype
      int orig = 10;    //original value is 10.
      cout<<"The original value is: "<<orig<<"\n";
      change(orig);
      cout<<"Value after change() is over: "<<orig<<"\n";
      return 0;
}
void change(int &a)
{     a = 20;
      cout<<"Value of orig in function change() is: "<<a<<"\n";
      return;
}


OUTPUT


//WAP using for loop to print numbers from 1 to 10.

//WAP using for loop to print numbers from 1 to 10.
#include<iostream.h>
#include<stdlib.h>
int main()
{
      system("cls");
      int i;      //declaring variable i
      for(i=1;i<=10;++i)
            cout<<i<<"\t";
      return 0;
}



OUTPUT

How do I crack the GSOC in the field of machine learning?

How do I crack the GSOC in the field of machine learning? Working as a student at Google Summer of Code demands for your dedication an...