C++/Data Structure/Array
Содержание
- 1 Array based on int pointer
- 2 Array Initialization
- 3 Assigning and Displaying Array Values
- 4 cin and cout work with char array
- 5 Outputs addresses and values of array elements.
- 6 To input numbers into an array and output after.
- 7 Using a Variable Pointer to Point to an Array
- 8 Using the cin and cout Objects with Arrays
Array based on int pointer
#include <iostream>
#include <stdlib.h>
using namespace std;
class array
{
int *p;
int size;
public:
array(int sz) {
p = new int[sz];
if(!p) exit(1);
size = sz;
}
~array() {delete [] p;}
array(const array &object);
void put(int i, int j){
if(i>=0 && i<size)
p[i] = j;
}
int get(int i) {return p[i];}
};
array::array(const array &object)
{
int lcl_i;
p = new int[object.size];
if (!p)
exit(1);
for(lcl_i=0; lcl_i < object.size; lcl_i++)
p[lcl_i] = object.p[lcl_i];
}
int main(void)
{
array num(10);
int lcl_i;
for (lcl_i=0; lcl_i<10; lcl_i++)
num.put(lcl_i, lcl_i);
for (lcl_i=9; lcl_i>=0; lcl_i--)
cout << num.get(lcl_i);
cout << endl;
// Create another array using the copy constructor
array x=num;
for (lcl_i=0; lcl_i<10; lcl_i++)
cout << x.get(lcl_i);
}
Array Initialization
#include <iostream>
using namespace std;
int main ()
{
char name[ ] = {"J", "e", "f", "f", "/0" };
cout << name;
return 0;
}
Assigning and Displaying Array Values
#include <iostream>
using namespace std;
int main ()
{
int testScore[3];
cout << "Enter test score #1: ";
cin >> testScore[0];
cout << "Enter test score #2: ";
cin >> testScore[1];
cout << "Enter test score #3: ";
cin >> testScore[2];
cout << "Test score #1: " << testScore[0] << endl;
cout << "Test score #2: " << testScore[1] << endl;
cout << "Test score #3: " << testScore[2] << endl;
return 0;
}
cin and cout work with char array
#include <iostream>
using namespace std;
int main ()
{
char name[ 80] = {"J", "e", "f", "f", "/0" };
cout << "Enter your name: ";
cin >> name;
cout << "Your name is " << name;
return 0;
}
Outputs addresses and values of array elements.
#include <iostream>
using namespace std;
int arr[4] = { 0, 10, 20, 30 };
int main()
{
cout << "\nAddress and value of array elements:\n"
<< endl;
for( int i = 0; i < 4; i++ )
cout << "Address: " << (void*)(arr+i) // &arr[i]
<< " Value: " << *(arr+i) // arr[i]
<< endl;
return 0;
}
To input numbers into an array and output after.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const int Length = 10;
float arr[Length], x;
int i, cnt;
cout << "Enter up to 10 numbers \n"
<< "(Quit with a letter):" << endl;
for( i = 0; i < Length && cin >> x; ++i)
arr[i] = x;
cnt = i;
cout << "The given numbers:\n" << endl;
for( i = 0; i < cnt; ++i)
cout << setw(10) << arr[i];
cout << endl;
return 0;
}
Using a Variable Pointer to Point to an Array
#include <iostream>
using namespace std;
const int MAX = 3;
int main ()
{
int testScore[MAX] = {4, 7, 1};
for (int i = 0; i < MAX; i++)
{
cout << "The address of index " << i << " of the array is "<< &testScore[i] << endl;
cout << "The value at index " << i << " of the array is "<< testScore[i] << endl;
}
return 0;
}
Using the cin and cout Objects with Arrays
#include <iostream>
using namespace std;
const int MAX = 3;
int main ()
{
char grades[MAX];
for (int i = 0; i < MAX; i++)
{
cout << "Enter grade for test #" << i + 1 << ":";
cin >> grades[i];
}
return 0;
}