Wednesday, November 28, 2012

Structure : struct, enumeration, bit-field, typedef, union

Structure  : struct, enumeration, bit-field, typedef, union 
Nama      : Deab Sina
NIM        : 49012078
Jurusan    : D4 TMD ITB Batch 6

This is video about:struct, enumeration, bit-field, typedef,

union

 

Link to this video:


Source code: 

===================================================
+ struct.cpp
===================================================

#include <iostream>
using namespace std;

struct database{
int id_number;
int age;
float salary;
};
int main()
{
        database employee; // There is now an variable that has modifiable // variable inside it
        employee.age = 22;
        employee.id_number = 1;
        employee.salary = 12000.2;

        cout<<"employee age = "<<employee.age<<endl;
        cout<<"employee id_number = " <<employee.id_number<<endl;
        cout<<"employee salary = "<<employee.salary<<endl;
        system("pause");
        return 0;
} 




=================================================== 
+ union.cpp
===================================================

// union.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include <iostream>
using namespace std;

union sample{
int p;
};
int _tmain(int argc, _TCHAR* argv[])
{
        // Union data type
        union sample content;
        content.p =37;

        cout<<"Display the union storage content"<<endl;
        cout<<" ONLY one at a time!"<<endl;
        cout<<"---------------------------------"<<endl;
        cout<<"Integer : "<<content.p<<endl;

        system("pause");
        return 0;
}


===================================================
+ enumeration.cpp
===================================================


// enumeration.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
        enum days {mon, tue, wed, thu, fri, sat, sun};
        days daysinweek; // day in a week
        daysinweek = wed; // day in a week is wed

        switch (daysinweek){
        case sun: cout <<"Sunday"<<endl;
                break;
        case mon: cout <<"Monday"<<endl;
               break;
        case tue: cout <<"Tuesday"<<endl;
                break;
        case wed: cout <<"Wednesday"<<endl;
                break;
        case thu: cout <<"Thursday"<<endl;
                break;
        case fri: cout <<"Friday"<<endl;
                break;
        case sat: cout <<"Saturday"<<endl;
                break;
        }
        system("pause");
        return 0;
}



===================================================
+ typedef.cpp
===================================================

// typedef.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include <iostream>
using namespace std;

typedef struct TestStruct
{
        int p;
        char q;
        double r;
} mine;
int _tmain(int argc, _TCHAR* argv[])
{
        mine testvar; // the declaration becomes simpler
        testvar.p = 200;
        testvar.q = 'T';
        testvar.r = 1.234;

        cout<<"testvar.p = "<<testvar.p<<endl;
        cout<<"testvar.q = "<<testvar.q<<endl;
        cout<<"testvar.r = "<<testvar.r<<endl;
        system("pause");
        return 0;
}



===================================================
+ bit-field.cpp
===================================================

// bit_field.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include <iostream>
#include <assert.h>
using namespace std;

class MyDay{
        unsigned day : 31;// bit field (32 bit)
        public:
        void SetDay(int aDay){
        assert ( aDay <31);
        day = aDay;
        }
        void Print(){
                cout<<"Day is : "<<day<<endl;
        }
};
int _tmain(int argc, _TCHAR* argv[])
{
        MyDay d;
        d.SetDay(20);
        d.Print();

        cout<<"Size of MyDay = "<<sizeof(d)<<endl;

        system("pause");
        return 0;
}



 

Show detail about this Modul.

 

Monday, November 26, 2012

Array (One dimension and Multi dimension)

Modul_7  : Array
Nama      : Deab Sina
NIM        : 49012078
Jurusan    : D4 TMD ITB Batch 6

This is video about: Array (One dimension and Multi dimension) 


Link to this video:


Source code: 

===================================================
+ Array One dimension
===================================================

// Modul_7: Array
// Nama   : Deab Sina
// NIM    : 49012078
// Jurusan: D4 TMD ITB Batch 6

// one dimension array
#include <iostream>
using namespace std;

int main()
{
    // Declare int variable one dimension
    int arr1dimension[10];

    for (int x=0; x<10; x++)
    {
        cout<<"Please input value of element array number"<<x<<" : ";
        cin>>arr1dimension[x];
    }
    cout<<"The value taht you input is : "<<endl;
    for (int x=0; x<10; x++)
    {
        cout<<"Element number"<<x<<" = "<<arr1dimension[x]<<endl;
    }
    system("pause");
    return 0;
}
===================================================
+ Array Multi dimension
===================================================
// multi_dimension.cpp : Defines the entry point for the console application.
// Nama : Deab Sina
// NIM  : 49012078
// Jurusan : D4 TMD ITB Batch 6

#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    // Declare char variable multi dimension
    char color[7][7] = {"Red","Blue", "Yellow", "Green","Orange", "Pink", "Purple"};

    for(int k=0; k<7; k++)
    {
        cout<<"Rainbow color is : "<<color[k]<<endl;
    }

    system("pause");
    return 0;
}


Show detail about this Modul.

Looping (while, do...while, for)

Modul_6  : Looping (Part 2)
Nama      : Deab Sina
NIM        : 49012078
Jurusan    : D4 TMD ITB Batch 6

This is video about:Looping


Link to this video:


Source code: 

===================================================
+ while loop:
===================================================

/*
Modul_6: LOOPING
Nama   : Deab Sina
NIM    : 49012078
Jurusan: D4 TMD ITB Batch 6
*/
#include <iostream>
using namespace std;

int main()
{
    // Local variable declaration
    int a = 10;

    // while loop execution
    while(a<20)
    {
        cout<<"D4 TMD ITB Batch 6 (Using while loop)"<<endl;
        a++;
    }
    system("pause");
    return 0;
}

===================================================
+ do...while loop:
===================================================

// do_while_loop.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include <iostream>
using namespace std;
// do...while loop
int _tmain(int argc, _TCHAR* argv[])
{
    //Local variable declaration
    int a = 10;

    //do loop excution
    do
    {
        cout<<"D4 TMD ITB Batch 6 (Using do...while loop)"<<endl;
        a = a + 1;
    }while(a < 20);
    system("pause");
    return 0;
}

===================================================
+ for loop:
===================================================

// for_loop.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    // for loop execution
    for (int a = 10; a < 20; a = a + 1)
    {
        cout<<"D4 TMD ITB Batch 6 (Using for loop)"<<endl;

    }
    system("pause");
    return 0;
}


Show detail about this Modul.