You are on page 1of 6

C++ ARRAYS

ASSIGNMENT AND CAT

1. Define an Array and explain how to declare and initialize it.


An array is a linear data structure that stores items of the same data type.
Declaration: data_type array_name[size];
Initialization: data_type array_name[size] = {array items};
Example: int numbers[3];
int numbers [3] = {1,2,3};

2. Using an appropriate C ++, explain the THREE types of Arrays


a) Single dimensional array
#include<iostream>
using namespace std;

int main () {

int numbers [5] = {56,34,23,67,78};

for (int i =0; 1<5; i++) {

cout << numbers[i] << “ ”;

return 0;

b) Two dimensional array


#include<iostream>
using namespace std;

int main () {
int i, j;
int value [2][3] = {{1,2,3}, {7,8,9}};
for (i=0; i<2; i++) {
for (j=0; j<3; j++) {
cout<< value[i][j] <<” ”;
}
cout << endl;
}
return 0;
}
c) Multidimensional array

#include<iostream>
using namespace std;

int main () {
int i, j, k, y;
int value [2][3][3][2] = {{1,2,3}, {7,8,9}};
//traverse array items
for (i=0; i<2; i++) {
for (j=0; j<3; j++) {
for (k=0; k<3; k++) {
for (y=0; y<2; y++) {
cout<< value[i][j][k][y] <<" ";
}

}
}
cout << endl;
}
return 0;
}

3. Write a program in C++ to find and print smallest number in an array using a pointer

#include<iostream>
using namespace std;
int main ()
{
int arr [100], elements, i, s, *ptr;
cout <<"Enter the Size for Array: ";
cin>>elements;
cout<<"Enter "<<elements<<" Array Elements: "<<endl;
for (i=0; i<elements; i++)
cin>>arr[i];
ptr = &arr [0];
s = *ptr;
while(*ptr)
{
if(s>(*ptr))
s = *ptr;
ptr++;
}
cout<<"\n Smallest Number = "<< s;
cout<<endl;
return 0;
}

4. Write a program in C++ to insert an element at the end of an array and then another
program to delete an element from the same array
inserting
#include<iostream>
using namespace std;
int main ()
{
int arr [100], i, num;
int found = 0;
cout<<"Enter 5 Array Elements: "<<endl;
for (i=0; i<5; i++)
cin>>arr[i];
cout<<"\n Enter Element to Insert: ";
cin>>num;
arr[i] = num;
cout<<"\n The New Array is:\n";
for (i=0; i<6; i++)
cout<<arr[i]<<",";
cout<<endl;
return 0;
}
Deleting
#include<iostream>
using namespace std;
int main ()
{
int arr [100], i, num, del, j;
int found = 0;
cout<<"Enter 5 Array Elements: "<<endl;
for (i=0; i<5; i++)
cin>>arr[i];
cout<<"\n Enter Element to Insert: ";
cin>>num;
arr[i] = num;
cout<<"\n The New Array is:\n";
for (i=0; i<6; i++)
cout<<arr[i]<<",";
cout<<endl;

cout<<"\n Enter Element to Delete: "<<endl;


cin>>del;
for (i=0; i<5; i++)
{
if(arr[i]==del)
{
For (j=i; j<(5-1); j++)
arr[j] = arr[j+1];
found++;
i--;
}
}
if(found==0)
cout<<"\n Element is not found in the Array!";
else
cout<<"\n Element Deleted!";
cout<<endl;

for (i=0; i<5; i++) {


cout<<arr[i]<<",";
}
return 0;
}

5. Write a program in C++ to reverse an array using a function


#include<iostream>
using namespace std;
void reversal (int [], int);
int main ()
{
int arr [100], tot, i, j, temp;
cout<<"Enter the Size for Array: "<<endl;
cin>>tot;
cout<<"Enter "<<tot<<" Array Elements: "<<endl;
for (i=0; i<tot; i++)
cin>>arr[i];
cout<<"\n Array Values :\n";
for (i=0; i<tot; i++)
cout<<arr[i]<<" ";
reversal (arr, tot);
cout<<"\n\n The Reverse is:\n";
for (i=0; i<tot; i++)
cout<<arr[i]<<" ";
cout<<endl;
return 0;
}
void reversal (int a[], int t)
{
int i, j, temp;
j = t-1;
for (i=0; i<j; i++, j--)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}

6. Write a C++ program to display the elements of a 3D array using a nested FOR.....LOOP

#include<iostream>
using namespace std;

int main () {
int i, j, k, y;
int value [2][3][3] = {{1,2,3}, {7,8,9}};
//traverse array items
for (i=0; i<2; i++) {
for (j=0; j<3; j++) {
for (k=0; k<3; k++) {
cout<< value[i][j][k][y] <<" ";

}
}
cout << endl;
}
return 0;
}

You might also like