2. Write a simple program in C++ to investigate the safety of its enumeration types. Include at least 10 different operations on enumeration types to determine what incorrect or just silly things are legal. Now, write a C program that does the same things and run it to determine how many of the incorrect or silly things are legal. Compare your results.

Respuesta :

Answer:

Check the explanation

Explanation:

To solve the question above, we will be doing some arithmetic and bit wise operations on enum values. in addition to that, we are asserting the array with INTCOUNT  and also printing the size of array i.e 12 and we are adding BIG_COUNT enum value to 5 and the result is 25.

#include<stdio.h>

enum EnumBits

{

ONE = 1,

TWO = 2,

FOUR = 4,

EIGHT = 8

};

enum Randoms

{

BIG_COUNT = 20,

INTCOUNT = 3

};

int main()

{

// Basic Mathimatical operations

printf("%d\n",(ONE + TWO)); //addition

printf("%d\n",(FOUR - TWO)); //subtraction

printf("%d\n",(TWO * EIGHT)); //multiplication

printf("%d\n",(EIGHT / TWO)); //division

// Some bitwise operations

printf("%d\n",(ONE | TWO)); //bitwise OR

printf("%d\n",(TWO & FOUR)); //bitwise AND

printf("%d\n",(TWO ^ EIGHT)); //bitwise XOR

printf("%d\n",(EIGHT << 1)); //left shift operator

printf("%d\n",(EIGHT >> 1)); //right shift operator

// Initialize an array based upon an enum value

int intArray[INTCOUNT]; // declaring array

// Have a value initialized be initialized to a static value plus

int someVal = 5 + BIG_COUNT; //adding constant and BIG_COUNT variable

printf("%d\n",someVal); //value will be 25

printf("%d",sizeof(intArray)); //value will be 12

return 0;

}

Kindly check the attached image below for the Code and Output Screenshots:

Ver imagen temmydbrain