for Robot Artificial Inteligence

9.Data Structure & Pointer

|

Example 1

#include <iostream>

using namespace std;

struct book
{
    string author;
    string title;
    int publicationYear;
};

void setBook(book&);

int main()
{
    book book1, book2, book3;

    setBook(book1);
    setBook(book2);
    setBook(book3);

    return 0;
}

void setBook(book &bookToSet)
{
    cout << "Who is the author of the book: ";
    cin >> bookToSet.author; // kind of def in python
    cout << "What is the title of the book: ";
    cin >> bookToSet.title;
    cout << "In what year was the book published: ";
    cin >> bookToSet.publicationYear;
}

Example 2

#include <iostream>

using namespace std;

struct book
{
    string author;
    string title;
    int publicationYear;
};

void setBook(book&);
void showBook(book);

int main()
{
    book book1, book2, book3;

    setBook(book1);
    setBook(book2);
    setBook(book3);


    showBook(book1);
    showBook(book2);
    showBook(book3);
    return 0;
}

void setBook(book &bookToSet)
{
    cout << "Who is the author of the book: ";
    getline(cin, bookToSet.author);
    cout << "What is the title of the book: ";
    getline(cin, bookToSet.title);
    cout << "In what year was the book published: ";
    cin >> bookToSet.publicationYear;
    cin.ignore();
}
void showBook(book bookToShow)
{
    cout << endl << bookToShow.author <<  " " << bookToShow.title <<  " " << bookToShow.publicationYear << endl;
}

Example 3

#include <iostream>

using namespace std;

int main ()
{

    int arraySize;

    cout << "Input the size of the dynamic array you want to create: ";
    cin >> arraySize;

    int *dynamicArray = new int[arraySize];


    for (int i = 0; i < arraySize; i++)
    {
        cout << "Input " << i+1 << " number: ";
        cin >> *(dynamicArray+i);
    }

    for (int i = 0; i < arraySize; i++)
    {
        cout << "Number " << i+1 << ": " << *(dynamicArray+i) << endl;
    }

    delete []dynamicArray;


}


Example 4

#include <iostream>

using namespace std;

int main ()
{

    int a = 5;
    int *p = &a;
    int **pp = &p;

    cout << "Value of the variable a is equal to: " << a << endl;
    cout << "The address of the variable a is equal to: " << &a << endl;
    cout << "The value stored in the pointer p is equal to: " << p << endl;
    cout << "To get the value of the variable a using pointer p one has to point to the address in the pointer p, *p = " << *p << endl << endl;
    cout << "Pointer p has its own address which is: " << &p << endl;
    cout << "The value stored in the pointer pp is equal to: " << pp << endl;
    cout << "To get the value of the pointer p using pointer pp one has to point to the address in the pointer pp, *pp = " << *pp << endl;
    cout << "To get the value of he variable a using pointer pp one has to point to the address in the pointer pp and p, **pp = " << **pp << endl;

    return 0;
}

Example 5

#include <iostream>

using namespace std;

int main ()
{
    int charactersNumber;

    cout << "How many characters would you like to input: ";
    cin >> charactersNumber;

    char *characters = new char[charactersNumber];

    for (int i = 0; i < charactersNumber; i++)
    {
        cout << i+1 << " character: ";
        cin >> characters[i];
    }

    cout << "The inputted string of characters was: ";

    for (int i = 0; i < charactersNumber; i++)
    {
        cout << *(characters+i);
    }


    delete [] characters;


    return 0;
}

Example 6

#include <iostream>

using namespace std;

int main ()
{
    int arr[5];
    int *pArr = arr;

    for (int i = 0; i < 5; i++)
    {
        cout << "Input " << i+1 << " number: ";
        cin >> *(pArr++);
    }

    for (int i = 0; i < 5; i++)
    {
        cout << "Number " << i+1 << ": " << arr[i] << endl;
    }

    return 0;
}

Comments