Tuesday, December 23, 2014

Pointers to structures Quotient and Remainder using vector and class in C++ pro



Pointers to structures
 
#include 
#include
#include 
#include 
using namespace std;
struct movies_t 
{
  string title;
  int year;
};
int main ()
{
  string mystr;
  movies_t amovie;
  movies_t * pmovie;
  pmovie = &amovie;
  cout << "Enter title: ";
  getline (cin, pmovie->title);
  cout <<"Enter year: ";
  getline (cin, mystr);
  (stringstream) mystr >> pmovie->year;
  cout << "\nYou have entered:\n";
  cout << pmovie->title;
  cout <<"("<< pmovie->year <<")\n";
  return 0;
}

Adds two numbers and prints their sum

 
#include 
#include 
int main()
{
  int a;
  int b;
  int sum;
  sum = a + b;
  std::cout << "The sum of " << a << " and " << b << " is " << sum << "\n";
  return 0;
}
 
Two numbers add the sum
 
#include 
#include
using namespace std;
int main()
{
  int a = 123, b (456), sum = a + b;
  cout << "The sum of " << a << " and " << b << " is " << sum << endl;
  return 0;
}
 
Adding integer numbers
 
#include
#include
using namespace std;
int main()
{
    int a = 3, b = 5;
    cout << a << '+' << b << '=' << (a+b);
    return 0;
}
 
Quotient and Remainder
 
#include
#include 
using namespace std;
int main()
{
    int a = 33, b = 5;
    cout << "Quotient = " << a / b << endl;
    cout << "Remainder = "<< a % b << endl;
    return 0;
}

Using vector
 
#include 
#include 
using namespace std;
vector<int> pick_vector_with_biggest_fifth_element(
    vector<int> left, vector<int> right)
    {
    if( (left[5]) < (right[5]) )
   {
        return( right );
    };
    return( left );
}
int vector_demo(void)
{
    cout << "vector demo" << endl;
    vector<int> left(7);
    vector<int> right(7);
    left[5] = 7;
    right[5] = 8;
    cout << left[5] << endl;
    cout << right[5] << endl;
    vector<int> biggest(
        pick_vector_with_biggest_fifth_element( left, right )
    );
    cout << biggest[5] << endl;
 
    return 0;
}
 int main(void)
{
    vector_demo();
}

Using a class
 
#include 
#include 
class myClass
{
public:
 myClass(int);
private:
 int i;
};
myClass::myClass(int x) : i(x) {}
int main()
{
  myClass my_class(5);
  myClass* my_class_ptr = new myClass(5);
  delete my_class_ptr;
  return 0;
}

No comments:

Post a Comment