1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
#include <iostream>
using namespace std;
void initialize(double inputArray[], int initialValue, int arraySize);
void printArray(double* inputArray, int arraySize);
void printBytes(double* inputArray, int arraySize);
void setByte(double* inputArray, int newByteValue, int arraySize);
int main() {
int size = 10;
double dArray[size];
initialize(dArray, 0, size);
cout<<"Array Initialized:\n"<<endl;
printArray(dArray, size);
cout<<"Each individual byte:"<<endl;
printBytes(dArray, size);
setByte(dArray, 254, size);
cout<<"Array contents after individual byte setting:\n"<<endl;
printArray(dArray, size);
cout<<"Each individual byte:"<<endl;
printBytes(dArray, size);
system("pause");
}
void initialize(double inputArray[], int initialValue, int arraySize){
for(int i = 0; i < arraySize; i ++) {
inputArray[i] = (double)initialValue;
}
}
void printArray(double* inputArray, int arraySize) {
for(int i = 0; i < arraySize; i++) {
if(i == arraySize-1) {
cout<<i+1<<": "<<inputArray[i]<<endl;
cout<<"-------------------"<<endl;
}else {
cout<<i+1<<": "<<inputArray[i]<<endl;
}
}
}
void printBytes(double* inputArray, int arraySize) {
unsigned char* startAddr = (unsigned char*)inputArray;
for(int i = 0; i < (sizeof(double) * arraySize); i++) {
if(i % 8 == 0) {
cout<<"\nBytes of element #"<<i/8+1<<endl;
cout<<"-------------------"<<endl;
}
if(i == (sizeof(double) * arraySize)-1) {
cout<<"0x"<<hex<<(long)startAddr<<" :: "<<dec<<(long)*startAddr<<endl;
cout<<"-------------------\n"<<endl;
} else {
cout<<"0x"<<hex<<(long)startAddr<<" :: "<<dec<<(long)*startAddr<<endl;
startAddr++;
}
}
}
void setByte(double* inputArray, int newByteValue, int arraySize) {
unsigned char* startAddr = (unsigned char*)inputArray;
for(int i = 0; i < (sizeof(double) * arraySize); i++) {
*startAddr = newByteValue;
startAddr++;
}
}
|