Define An Array and Explain How To Declare and Initialize It
Define An Array and Explain How To Declare and Initialize It
Define An Array and Explain How To Declare and Initialize It
int main () {
return 0;
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;
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;
}