Login


Lost your password?

Don't have an account ? Register ×
  
CPP All : Arrays Concepts and 1-D Arrays
Concept Learning Code Sheets

Preparing basic arrays #1593

Here we shall prepare some basic arrays of integers, fractions, characters and strings.

#include <iostream>
using namespace std;
int main()
{
int p[3];
p[0]=1; p[1]=2; p[2]=3;
int q[3]={10,20,30};
cout<<p[0]+p[1]+p[2]<<endl;
cout<<q[0]+q[1]+q[2]<<endl;

float f[3]={1.2,1.3,1.4};
cout<<f[0]+f[1]+f[2]<<endl;
char ch[]="Hello";
cout<<ch[0]<<endl;
if(ch[5]=='') cout<<"Null Confirmed"<<endl;

string str[]={"Jack ","and ","Jill "};
cout<<str[0]+str[1]+str[2]<<endl;

return 0;
}

Largest number in integer array #1610

Traversing an integer array linearly to find the largest number in the array.

#include <iostream>
using namespace std;
int main()
{
int A[]={14,9,11,19,3};
int max=A[0];
int len=sizeof(A)/sizeof(A[0]);
for(int i=1;i<len;i++)
if(A[i]>max) max=A[i];
cout<<"Largest number = "<<max;
return 0;
}

Char array variations #1623

Learning character array variations and need for null terminator

#include <iostream>
using namespace std;
int main()
{
char ca1[]={'F','A','C','E'};
char ca2[4]={'N','O','S','E'};
char ca3[5]={'N','O','S','E'};
char ca4[]="CHIN";
cout<<"array 1 contains "<<ca1<<endl;
cout<<"array 2 contains "<<ca2<<endl;
cout<<"array 3 contains "<<ca3<<endl;
cout<<"array 4 contains "<<ca4<<endl;
cout<<"array 1 by individual characters = "
<<ca1[0]<<ca1[1]<<ca1[2]<<ca1[3]<<endl;
}

Linear search in array for search term presence and location #1630

Search for presence of element in an integer array for search term presence and location of element where it is found first.

#include <iostream>
using namespace std;
int main()
{
int st,found=0,pos=0;
int ar[]={25,10,32,44,99};
int size=sizeof(ar)/sizeof(ar[0]);
cout<<"Enter search term: ";
cin>>st;
for(int i=0;i<size;i++)
{
if(ar[i]==st)
{
found=1;
pos=i+1;
}
}
if(found==1)
cout<<"Search term "
<<st<<" found at position "<<pos<<endl;
else
cout<<"Search term not found"<<endl;
return 0;
}

Finding count of members in a linear array #2502

Finding the number of members in an array using the sizeof operator

#include <iostream>
using namespace std;
int main()
{
int a[]={1,2,3,4,5};
int cnt=sizeof(a)/sizeof(a[0]);
cout<<"Count of members in given array = "<<cnt<<endl;
}

Reversing an array #2506

Reversing an integer array using the swapping of members around midpoint.

#include <iostream>
using namespace std;
int main()
{
int a[]={1,2,3,4,5};
int cnt=sizeof(a)/sizeof(a[0]);
cout<<"array members before reversal are :"<<endl;
for(int i=0;i<cnt;++i) cout<<a[i]<<" ";
for(int i=0;i<cnt/2;++i)
{
int temp=a[i];
a[i]=a[cnt-i-1];
a[cnt-i-1]=temp;
}
cout<<endl<<"array members after reversal are :"<<endl;
for(int i=0;i<cnt;++i) cout<<a[i]<<" ";
}

Collecting array entries and displaying it like its formal representation #2510

Taking array entries from the user and displaying it like its formal representation as {1,2,..5}

#include <iostream>
using namespace std;
#define asize 5
int main()
{
int a[asize];
int cnt=asize;
for(int i=0;i<cnt;++i)
{
cout<<"Enter member "<<i+1<<" : ";
cin>>a[i];
}
cout<<endl<<"The array you entered is :"<<endl;
cout<<'{';
for(int i=0;i<cnt;++i)
{
cout<<a[i]<<((i<=cnt-2)?",":"");
}
cout<<'}';
}

Copying an Integer Array #3091

Demonstrating how an array can be copied to another array of same size by copying each member of the array.

#include <iostream>
using namespace std;
int main()
{
int a1[5]={25,10,32,44,99};
int a2[5];
cout<<"Members of source array : "<<endl;
for(int i=0;i<5;i++) cout<<a1[i]<<" ";
for(int i=0;i<5;i++) a2[i]=a1[i];
cout<<endl<<"Members of destination array : "<<endl;
for(int i=0;i<5;i++) cout<<a2[i]<<" ";
return 0;
}

printing filtered array content #3095

Demonstrating how to print the filtered array content.

#include <iostream>
using namespace std;
int main()
{
int ar[]={32,10,19,44,99};
for(int i=0;i<5;i++)
if(ar[i]>30) cout<<ar[i]<<" ";
return 0;
}

copying filtered array content to another array #3102

Demonstrating how to filter the array content and then copy it to another array.

#include <iostream>
using namespace std;
int main()
{
int ar[5]={32,10,19,44,99};
int arf[5];

for(int i=0;i<5;i++)
if(ar[i]>30) arf[i]=ar[i];
else arf[i]=-1;

for(int i=0;i<5;i++)
if(arf[i]>=0) cout<<arf[i]<<" ";

return 0;
}

copying filtered array content to another array by maintaining count #3106

Demonstrating how to filter the array content and then copy it to another array by maintaining the filter size count.

#include <iostream>
using namespace std;
int main()
{
int ar[5]={32,10,19,44,99};
int arf[5];
int cnt=0;
for(int i=0,j=0;i<5;i++)
if(ar[i]>30)
{
arf[j]=ar[i]; j++; cnt++;
}

for(int i=0;i<cnt;i++)
if(arf[i]>=0) cout<<arf[i]<<" ";

return 0;
}

Linear search using array reference passing to function #1628

Search for presence of element in an integer array by using a function with a call by reference of array, size and search term.

#include <iostream>
using namespace std;
bool search(int [],int, int);
int main()
{
int st;
int na[]={25,10,32,44,99};
cout<<"Enter search term ";
cin>>st;
if(search(na,5,st))
cout<<"Element found"<<endl;
else
cout<<"Element not found"<<endl;
}

bool search(int AR[],int size,int item)
{
for(int i=0;i<size;i++)
{
if(AR[i]==item) return true;
}
return false;
}
Solved Problems

Linear search function for search term presence and location #1632

Write a function linsearch(int [],int, int) which searches for the term 23 in

Average of temperature values stored in array #2487

Write a program to store 10 temperature readings in an array and then find th

Proving palindrome array #2848

Write a program to prove that the give array [23,43,51,43,23] is a palindrome

Sum of integer array #3088

Write a program to collect 5 integer values from user in an integer array an

Average value of array and members more than average #3098

Prepare an array of temperature readings of an entire week with data as


Code Sheets:12  Solved Problems:5 
Back