C ++ masterpiece page-array operation. Dynamic Multi-dimensional array

Source: Internet
Author: User

I haven't moved the code for a long time. The following is a major C ++ assignment this semester.
The teacher is still reading the textbook, and then assigned the final exam for this big assignment 2, alas.
I really want to spend some time learning C ++ again. For example, I have read C ++ primier again, I have done the following questions, and Thinking in C ++, the data structure and algorithm C ++ are also described.
Written in Code: Blocks.
It has been stuck for a long time, that is, when the header file is referenced, if the relative path is used, the system prompts that the file cannot be found, and the absolute path is used to solve the problem.
Project 1:
Objective: To be familiar with c ++ dynamic memory allocation
Description: uses a c ++ program to define a dynamic array class. uses the new and delete operators to define a dynamic two-dimensional array and a three-dimensional array.
// Main. cpp
// Main program class
# Include <iostream>
# Include "/home/empty/c/Array/include/DynamicArray. h"
# Include "/home/empty/c/Array/include/DynamicArray3d. h"
Using namespace std;


Int main ()
{
Cout <"Hello world! "<Endl;


DynamicArray;
A. printArray ();
DynamicArray3d B;
B. printArray ();
Return 0;
}
// DynamicArray. h
// Two-dimensional array header file
# Ifndef DYNAMICARRAY_H
# Define DYNAMICARRAY_H
# Include <iostream>
Using namespace std;
Class DynamicArray
{
Public:
DynamicArray (int m = 5, int n = 5 );
~ DynamicArray ();
Void printArray ();


Private:
Int ** m_pArray; // used to store internal Arrays
Int m_iSize;
Int n_iSize; // used to record the memory used


};


# Endif // DYNAMICARRAY_H

 


// DynamicArray. cpp
// Two-character array source file
# Include "/home/empty/c/Array/include/DynamicArray. h"


DynamicArray: DynamicArray (int m, int n)
{
Int I, j;
M_iSize = m;
N_iSize = n;
M_pArray = new int * [m_iSize];
For (I = 0; I <m_iSize; I ++)
M_pArray [I] = new int [n_iSize];
For (I = 0; I <m_iSize; I ++)
For (j = 0; j <n_iSize; j ++)
{
M_pArray [I] [j] = I + 10 * j;
}
}


// Destructor
DynamicArray ::~ DynamicArray ()
{
For (int I = m_iSize; I> 0 ;)
Delete [] m_pArray [-- I];
Delete [] m_pArray;
}
Void DynamicArray: printArray ()
{
Int I, j;
Cout <"Print Array:" <endl;
For (I = 0; I <m_iSize; I ++)
{
For (j = 0; j <n_iSize; j ++)
Cout <m_pArray [I] [j] <"";
Cout <endl;
}


}

 


// DynamicArray3d. h
// 3D array header file
# Ifndef DYNAMICARRAY3D_H
# Define DYNAMICARRAY3D_H
# Include <iostream>
Using namespace std;


Class DynamicArray3d
{
Public:
DynamicArray3d (int m = 5, int n = 5, int o = 5 );
~ DynamicArray3d ();
Void printArray ();
Private:
Int *** m_pArray; // used to store internal Arrays
Int m_iSize;
Int n_iSize; // used to record the memory used
Int o_iSize;
};


# Endif // DYNAMICARRAY3D_H
// 3D array source file
# Include "/home/empty/c/Array/include/DynamicArray3d. h"


DynamicArray3d: DynamicArray3d (int m, int n, int o)
{
Int I, j, k;
M_iSize = m;
N_iSize = n;
O_iSize = o;
M_pArray = new int ** [m_iSize];
For (I = 0; I <m_iSize; I ++)
M_pArray [I] = new int * [n_iSize];
For (I = 0; I <m_iSize; I ++)
For (j = 0; j <n_iSize; j ++)
M_pArray [I] [j] = new int [o_iSize];
// Enter the 3D array value:
For (I = 0; I <m_iSize; I ++)
For (j = 0; j <n_iSize; j ++)
For (k = 0; k <o_iSize; k ++)
M_pArray [I] [j] [k] = I * 100 + j * 10 + k;
}


DynamicArray3d ::~ DynamicArray3d ()
{
// Dtor
}
Void DynamicArray3d: printArray ()
{
Int I, j, k;
For (I = 0; I <m_iSize; I ++)
{
For (j = 0; j <n_iSize; j ++)
{
For (k = 0; k <o_iSize; k ++)
Cout <m_pArray [I] [j] [k] <"";
Cout <"";
}
Cout <endl;
}


}

 

Project 1:
Objective: To be familiar with c ++ dynamic memory allocation
Description: uses a c ++ program to define a dynamic array class. uses the new and delete operators to define a dynamic two-dimensional array and a three-dimensional array.
// Main. cpp
// Main program class
# Include <iostream>
# Include "/home/empty/c/Array/include/DynamicArray. h"
# Include "/home/empty/c/Array/include/DynamicArray3d. h"
Using namespace std;


Int main ()
{
Cout <"Hello world! "<Endl;


DynamicArray;
A. printArray ();
DynamicArray3d B;
B. printArray ();
Return 0;
}
// DynamicArray. h
// Two-dimensional array header file
# Ifndef DYNAMICARRAY_H
# Define DYNAMICARRAY_H
# Include <iostream>
Using namespace std;
Class DynamicArray
{
Public:
DynamicArray (int m = 5, int n = 5 );
~ DynamicArray ();
Void printArray ();


Private:
Int ** m_pArray; // used to store internal Arrays
Int m_iSize;
Int n_iSize; // used to record the memory used


};


# Endif // DYNAMICARRAY_H

 


// DynamicArray. cpp
// Two-character array source file
# Include "/home/empty/c/Array/include/DynamicArray. h"


DynamicArray: DynamicArray (int m, int n)
{
Int I, j;
M_iSize = m;
N_iSize = n;
M_pArray = new int * [m_iSize];
For (I = 0; I <m_iSize; I ++)
M_pArray [I] = new int [n_iSize];
For (I = 0; I <m_iSize; I ++)
For (j = 0; j <n_iSize; j ++)
{
M_pArray [I] [j] = I + 10 * j;
}
}


// Destructor
DynamicArray ::~ DynamicArray ()
{
For (int I = m_iSize; I> 0 ;)
Delete [] m_pArray [-- I];
Delete [] m_pArray;
}
Void DynamicArray: printArray ()
{
Int I, j;
Cout <"Print Array:" <endl;
For (I = 0; I <m_iSize; I ++)
{
For (j = 0; j <n_iSize; j ++)
Cout <m_pArray [I] [j] <"";
Cout <endl;
}


}

 


// DynamicArray3d. h
// 3D array header file
# Ifndef DYNAMICARRAY3D_H
# Define DYNAMICARRAY3D_H
# Include <iostream>
Using namespace std;


Class DynamicArray3d
{
Public:
DynamicArray3d (int m = 5, int n = 5, int o = 5 );
~ DynamicArray3d ();
Void printArray ();
Private:
Int *** m_pArray; // used to store internal Arrays
Int m_iSize;
Int n_iSize; // used to record the memory used
Int o_iSize;
};


# Endif // DYNAMICARRAY3D_H
// 3D array source file
# Include "/home/empty/c/Array/include/DynamicArray3d. h"


DynamicArray3d: DynamicArray3d (int m, int n, int o)
{
Int I, j, k;
M_iSize = m;
N_iSize = n;
O_iSize = o;
M_pArray = new int ** [m_iSize];
For (I = 0; I <m_iSize; I ++)
M_pArray [I] = new int * [n_iSize];
For (I = 0; I <m_iSize; I ++)
For (j = 0; j <n_iSize; j ++)
M_pArray [I] [j] = new int [o_iSize];
// Enter the 3D array value:
For (I = 0; I <m_iSize; I ++)
For (j = 0; j <n_iSize; j ++)
For (k = 0; k <o_iSize; k ++)
M_pArray [I] [j] [k] = I * 100 + j * 10 + k;
}


DynamicArray3d ::~ DynamicArray3d ()
{
// Dtor
}
Void DynamicArray3d: printArray ()
{
Int I, j, k;
For (I = 0; I <m_iSize; I ++)
{
For (j = 0; j <n_iSize; j ++)
{
For (k = 0; k <o_iSize; k ++)
Cout <m_pArray [I] [j] [k] <"";
Cout <"";
}
Cout <endl;
}


}



 


From left-brain design, right-brain Programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.