#include #include #include using namespace std; int main(int argc, char** argv)
ID: 3786883 • Letter: #
Question
#include
#include
#include
using namespace std;
int main(int argc, char** argv)
{
if(argc < 7) {
cout <<"ERROR !!!!"<<' ';
cout<< "Please pass 3 command line arguments denoting the size of the 3 dimensions, example 3 3 3 .";
exit(0);
}
int firstletter = atoi(argv [1]);
int secletter = atoi(argv [3]);
int thirdletter = atoi (argv [5]);
int firstDimensionSize = atoi(argv[2]);
int secondDimensionSize = atoi(argv[4]);
int thirdDimensionSize = atoi(argv[6]);
int*** myArray;
myArray = new int**[firstDimensionSize];
for(int i = 0; i < firstDimensionSize; i++) {
myArray[i] = new int*[secondDimensionSize];
}
for(int i = 0; i < firstDimensionSize; i++) {
for(int j = 0; j < secondDimensionSize; j++) {
myArray[i][j] = new int[thirdDimensionSize];
}
}
string choice = "";
while(choice != "N"){
// assign the random values between 1 to 9
srand((unsigned)time(NULL));
for(int i = 0; i < firstDimensionSize; i++) {
for(int j = 0; j < secondDimensionSize; j++) {
for(int k = 0; k < thirdDimensionSize; k++) {
myArray[i][j][k] = rand()%10;
}
}
}
// display the values:
for(int dim1 = 0; dim1 < firstDimensionSize; dim1++) {
cout << ' ' << ' ' << "Layer " << dim1 + 1;
cout << endl;
for(int i = 0; i < secondDimensionSize; i++)
cout << ' ' << " Column " << i + 1;
cout << endl;
for(int dim3 = 0; dim3 < thirdDimensionSize; dim3++) {
cout << " Row " << dim3 + 1 << ' ';
for(int dim2 = 0; dim2 < secondDimensionSize; dim2++)
cout << ' ' << myArray[dim1][dim2][dim3] << ' ';
cout << endl;
}
cout << endl;
}
cout << "Do you want to run the program again, Enter Y to yes, and N to exit ";
cin >> choice;
}
// delete the array
for(int dim1 = 0; dim1 < firstDimensionSize; dim1++) {
for(int dim2 = 0; dim2 < secondDimensionSize; dim2++) {
delete myArray[dim1][dim2];
}
delete[] myArray[dim1];
}
return 0;
}
Explanation / Answer
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
int length(char s[])
{
int i=0;
while(s[i]!='')i++;
return i;
}
int main(int argc, char** argv)
{
if(argc < 7) {
cout <<"ERROR !!!!"<<' ';
cout<< "Please pass 3 command line arguments denoting the size of the 3 dimensions, example 3 3 3 .";
exit(0);
}
int firstletter = atoi(argv [1]);
int secletter = atoi(argv [3]);
int thirdletter = atoi (argv [5]);
int firstDimensionSize = atoi(argv[2]);
int secondDimensionSize = atoi(argv[4]);
int thirdDimensionSize = atoi(argv[6]);
int*** myArray;
myArray = new int**[firstDimensionSize];
for(int i = 0; i < firstDimensionSize; i++) {
myArray[i] = new int*[secondDimensionSize];
}
for(int i = 0; i < firstDimensionSize; i++) {
for(int j = 0; j < secondDimensionSize; j++) {
myArray[i][j] = new int[thirdDimensionSize];
}
}
string choice = "";
while(choice != "N"){
// assign the random values between 1 to 9
srand((unsigned)time(NULL));
for(int i = 0; i < firstDimensionSize; i++) {
for(int j = 0; j < secondDimensionSize; j++) {
for(int k = 0; k < thirdDimensionSize; k++) {
myArray[i][j][k] = rand()%10;
}
}
}
// display the values:
for(int dim1 = 0; dim1 < firstDimensionSize; dim1++) {
cout << ' ' << ' ' << "Layer " << dim1 + 1;
cout << endl;
for(int i = 0; i < secondDimensionSize; i++)
cout << ' ' << " Column " << i + 1;
cout << endl;
for(int dim3 = 0; dim3 < thirdDimensionSize; dim3++) {
cout << " Row " << dim3 + 1 << ' ';
for(int dim2 = 0; dim2 < secondDimensionSize; dim2++)
cout << ' ' << myArray[dim1][dim2][dim3] << ' ';
cout << endl;
}
cout << endl;
}
cout << "Do you want to run the program again, Enter Y to yes, and N to exit ";
cin >> choice;
////modified............
if(choice=="Y")
{
char *s[6];
cout<<"Enter dimensions:";cin>>s[0];
cin>>s[1];
cin>>s[2];
cin>>s[3];
cin>>s[4];
cin>>s[5];
firstletter = atoi(s[0]);
secletter = atoi(s[2]);
thirdletter = atoi (s[4]);
firstDimensionSize = atoi(s[1]);
secondDimensionSize = atoi(s[3]);
thirdDimensionSize = atoi(s[5]);
}
}
// delete the array
for(int dim1 = 0; dim1 < firstDimensionSize; dim1++) {
for(int dim2 = 0; dim2 < secondDimensionSize; dim2++) {
delete myArray[dim1][dim2];
}
delete[] myArray[dim1];
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.