Rabu, 17 Desember 2025

listing4.5.cpp Analyzing the Null Terminator in a C-Style String

//listing4.5.cpp
#include <iostream>
using namespace std;

int main()
{
    char sayHello[]={'H','e','l','l','o',' ','W','o','r','l','d','\0'};
    cout << sayHello << endl;
    cout << "Size of array: " << sizeof(sayHello) << endl;

    cout << "Replacing space with null" << endl;
    sayHello[5]='\0';
    cout << sayHello << endl;
    cout << "Size of array: " << sizeof(sayHello) << endl;

    return 0;
}

output:
 
Hello World
Size of array: 12
Replacing space with null
Hello
Size of array: 12

Process returned 0 (0x0)   execution time : 0.408 s
Press any key to continue.

ex1303.c Let Me In

/* ex1303.c */
#include <stdio.h>
#include <string.h>

int main()
{
    char password[]="taco";
    char input[15];
    int match;

    printf("Password: ");
    scanf("%s",input);

    match=strcmp(input,password);
    if(match==0)
        puts("Password accepted");
    else
        puts("Invalid password. Alert the authorities.");

    return(0);
}

output:
 
Password: Pepaya
Invalid password. Alert the authorities.

Process returned 0 (0x0)   execution time : 4.751 s
Press any key to continue.

Selasa, 16 Desember 2025

listing4.4.cpp Creating A Dynamic Array of Integers and Inserting Values Dynamically

//listing4.4.cpp
#include <iostream>
#include <vector>

using namespace std;

int main()
{
        vector<int> dynArray(3); //dynamic array of int

        dynArray[0]=365;
        dynArray[1]=-421;
        dynArray[2]=789;

        cout << "Number of integers in array: " << dynArray.size() << endl;

        cout << "Enter another element to insert" << endl;
        int newValue=0;
        cin >> newValue;
        dynArray.push_back(newValue);

        cout << "Number of integers in array: " << dynArray.size() << endl;
        cout << "Last element in array: ";
        cout << dynArray[dynArray.size()-1] << endl;
        return 0;
}

output:
 
Number of integers in array: 3
Enter another element to insert
2025
Number of integers in array: 4
Last element in array: 2025

Process returned 0 (0x0)   execution time : 19.301 s
Press any key to continue.

ex1302.c A Yorn Problem

/* ex1302.c */
#include <stdio.h>
#include <ctype.h>

int main()
{
    char answer;

    printf("Would you like to blow up the moon? ");
    scanf("%c",&answer);
    answer == toupper(answer);
    if(answer=='Y')
        puts("BOOM!");
    else
        puts("The moon is safe.");
    return(0);
}

output:
 
Would you like to blow up the moon? Y
BOOM!

Process returned 0 (0x0)   execution time : 1.400 s
Press any key to continue.

Senin, 15 Desember 2025

listing4.3.cpp Accessing Elements in a Multidimensional Array

//listing4.3.cpp
#include <iostream>
using namespace std;

int main()
{
    int threeRowsThreeColumns[3][3]=\
    {{-501,205,2016},{989,101,206},{303,456,596}
    };

    cout << "Row 0: " << threeRowsThreeColumns[0][0] << " " \
        << threeRowsThreeColumns[0][1] << " " \
        << threeRowsThreeColumns[0][2] << endl;

    cout << "Row 1: " << threeRowsThreeColumns[1][0] << " " \
        << threeRowsThreeColumns[1][1] << " " \
        << threeRowsThreeColumns[1][2] << endl;

    cout << "Row 2: " << threeRowsThreeColumns[2][0] << " " \
        << threeRowsThreeColumns[2][1] << " " \
        << threeRowsThreeColumns[2][2] << endl;

    return 0;
}

output:
 
Row 0: -501 205 2016
Row 1: 989 101 206
Row 2: 303 456 596

Process returned 0 (0x0)   execution time : 0.433 s
Press any key to continue.

ex1301.c Text Statistics

/* ex1301.c */
#include <stdio.h>
#include <ctype.h>

int main()
{
    char phrase[]="When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation.";

    int index,alpha,space,punct;
    alpha=space=punct=0;

    /*gather data*/
    index=0;
    while(phrase[index])
    {
        if(isalpha(phrase[index]))
            alpha++;
        if(isspace(phrase[index]))
            space++;
        if(ispunct(phrase[index]))
            punct++;
        index++;
    }

    /*print result*/
    printf("\"%s\"\n\n",phrase);
    puts("Statistics: ");
    printf("%d alphabetic characters\n", alpha);
    printf("%d spaces\n",space);
    printf("%d punctuation symbols\n",punct);

    return(0);
}

output:
 
"When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation."

Statistics:
330 alphabetic characters
70 spaces
6 punctuation symbols

Process returned 0 (0x0)   execution time : 0.070 s
Press any key to continue.

Sabtu, 13 Desember 2025

listing4.2.cpp Assigning Values to Elements in an Array

//listing4.2.cpp
#include <iostream>
using namespace std;
constexpr int Square(int number){return number*number;}

int main()
{
    const int ARRAY_LENGTH=5;

    //array of 5 integers, initialized to 5 values
    int myNumbers[ARRAY_LENGTH]={5,10,0,-101,201};

    //using a constexpr for array of 5*5=25 integers
    int moreNumbers[Square(ARRAY_LENGTH)];

    cout << "Enter index of the element to be changed: ";
    int elementIndex=0;
    cin >> elementIndex;

    cout << "Enter new value: ";
    int newValue=0;
    cin >> newValue;

    myNumbers[elementIndex]=newValue;
    moreNumbers[elementIndex]=newValue;

    cout << "Element " << elementIndex << " in array myNumbers is: ";
    cout << myNumbers[elementIndex] << endl;

    cout << "Element " << elementIndex << " in array moreNumbers is: ";
    cout << moreNumbers[elementIndex] << endl;

    return 0;
}

output:

Enter index of the element to be changed: 0
Enter new value: 13
Element 0 in array myNumbers is: 13
Element 0 in array moreNumbers is: 13

Process returned 0 (0x0)   execution time : 3.335 s
Press any key to continue.

ex1209.c showarray(int array[])

/* ex1209.c */
#include <stdio.h>

void showarray(int array[]);

int main()
{
    int n[]={2,3,5,7,11};

    puts("Here's your array: ");
    showarray(n);
    return(0);
}

void showarray(int array[])
{
    int x;

    for(x=0;x<5;x++)
        printf("%d",array[x]);
    putchar('\n');
}

output:
 
Here's your array:
235711

Process returned 0 (0x0)   execution time : 0.108 s
Press any key to continue.

Jumat, 12 Desember 2025

listing4.1.cpp myNumbers[5]

//listing4.1.cpp
#include <iostream>
using namespace std;

int main()
{
    int myNumbers[5]={34,56,-21,5002,365};

    cout << "First element at index 0: " << myNumbers[0] << endl;
    cout << "Second element at index 1: " << myNumbers[1] << endl;
    cout << "Third element at index 2: " << myNumbers[2] << endl;
    cout << "Fourth element at index 3: " << myNumbers[3] << endl;
    cout << "Fifth element at index 4: " << myNumbers[4] << endl;

    return 0;
}

output:
 
First element at index 0: 34
Second element at index 1: 56
Third element at index 2: -21
Fourth element at index 3: 5002
Fifth element at index 4: 365

Process returned 0 (0x0)   execution time : 0.095 s
Press any key to continue.

ex1208.c tictactoe[3][3][3]

/* ex1208.c */
#include <stdio.h>

int main()
{
    char tictactoe[3][3][3];
    int x,y,z;

    /*initialize matrix*/
    for(x=0;x<3;x++)
        for(y=0;y<3;y++)
            for(z=0;z<3;z++)
                tictactoe[x][y][z]='.';
    tictactoe[1][1][1]='X';

    /*display gameboard*/
    puts("Ready to play Tic-Tac-Toe?");
    for(z=0;z<3;z++)
    {
        printf("Level %d\n",z+1);
        for(x=0;x<3;x++)
        {
            for(y=0;y<3;y++)
                printf("%c ",tictactoe[x][y][z]);
            putchar('\n');
        }
    }
    return(0);
}
\
output:
 
Ready to play Tic-Tac-Toe?
Level 1
. . .
. . .
. . .
Level 2
. . .
. X .
. . .
Level 3
. . .
. . .
. . .

Process returned 0 (0x0)   execution time : 0.079 s
Press any key to continue.

Kamis, 11 Desember 2025

listing3.9.cpp Displaying Directions

// listing3.9.cpp
#include <iostream>
using namespace std;

enum CardinalDirections
{
    North=25,
    South,
    East,
    West
};

int main()
{
    cout << "Displaying directions and their symbolic values" << endl;
    cout << "North: " << North << endl;
    cout << "South: " << South << endl;
    cout << "East: " << East << endl;
    cout << "West: " << West << endl;

    CardinalDirections windDirection=South;
    cout << "Variable windDirection= " << windDirection << endl;
    return 0;
}

output:
 
Displaying directions and their symbolic values
North: 25
South: 26
East: 27
West: 28
Variable windDirection= 26

Process returned 0 (0x0)   execution time : 0.476 s
Press any key to continue.

ex1206.c ready to play tictactoe?

/* ex1206.c */
#include <stdio.h>

int main()
{
    char tictactoe[3][3];
    int x,y;

    /* initialize matrix */
    for(x=0;x<3;x++)
        for(y=0;y<3;y++)
            tictactoe[x][y]='.';
    tictactoe[1][1]='X';

    /* display game board */
    puts("Ready to play Tic-Tac-Toe?");
    for(x=0;x<3;x++)
    {
        for(y=0;y<3;y++)
            printf("%c",tictactoe[x][y]);
        putchar('\n');
    }
    return(0);
}

output:
 
Ready to play Tic-Tac-Toe?
...
.X.
...

Process returned 0 (0x0)   execution time : 0.116 s
Press any key to continue.

Rabu, 10 Desember 2025

listing3.8.cpp consteval & constexpr

//listing3.8.cpp
#include <iostream>
consteval double GetPi(){return 22.0/7;}
constexpr double XPi(int x){return x*GetPi();}
using namespace std;

int main()
{
    constexpr double pi=GetPi();

    cout << "constexpr pi evaluated by compiler to " << pi << endl;
    cout << "constexpr XPi(2) evaluated by compiler to " << XPi(2) << endl;

    int multiple=5;
    cout << "(non-const) integer multiple = " << multiple << endl;
    cout << "constexpr is ignored when XPi(multiple) is invoked, ";
    cout << "returns " << XPi(multiple) << endl;

    return 0;
}

output:
 
constexpr pi evaluated by compiler to 3.14286
constexpr XPi(2) evaluated by compiler to 6.28571
(non-const) integer multiple = 5
constexpr is ignored when XPi(multiple) is invoked, returns 15.7143

Process returned 0 (0x0)   execution time : 0.408 s
Press any key to continue.