Array in C++
Array:
Array in C++ |
Array is defined as an ordered list of homogeneous data element.
- May be of type int, float,char or double
- All these element are stored in consecutive memory locations
- An array is described by a single name or an identifier and each element in an array is referenced by a subscript enclosed in a pair of square brackets
Syntax:
type variable_name[lengthofarry];
Program: Write a program take input and display the element using array in cpp.
Code:
#include<
iostream>
iostream>
using namespace std;
int main()
{
int i,arr[100],no;
cout<<"Enter the number of element (less than 100)";
cin>>no;
for(i=0;i<no;i++)
{
cout<<"Enter the element at"<<i <<" place";
cin>>arr[i];
}
cout<<"Elemen in the arry is :";
i=0;
while(i<no)
{
cout<<"\n"<<arr[i];
i++;
}
}
Ouput:
Enter the number of element (less than 100)5
Enter the element at0 place15
Enter the element at1 place20
Enter the element at2 place25
Enter the element at3 place30
Enter the element at4 place35
Elemen in the arry is :
15
20
25
30
35
--------------------------------
Program: Write a program to count the occurrence of Lowercase characters, Uppercase characters, Special characters, and Numeric values.
Code:
#include<iostream>
using namespace std;
int main()
{
char str[100];
int i ,digit=0,Upper_case=0,special=0,Lower_case=0 ,count=0;
cout<<"Enter the string :";
gets(str);
cout<<str;
for(i=0;str[i]!='\0';i++)
{
count++;
}
i=0;
while(str[i]!='\0')
{ if(str[i]>='0' && str[i]<='9')
{
digit++;
}
if(str[i]=='@' ||str[i]=='#' ||str[i]=='$' ||str[i]=='%'||str[i]=='^'||str[i]=='&')
{
special++;
}
if((str[i]>='a'&&str[i]<='z'))
{
Lower_case++;
}
if(str[i]>='A' &&str[i]<='Z')
{
Upper_case++;
}
i++;
}
cout<<"\nThe number of uppper case is:"<<Upper_case;
cout<<"\nThe number of lower case is:"<<Lower_case;
cout<<"\nThe number of special symbol is :"<<special;
cout<<"\n The nuber of didgt is:"<<digit;
}
Output:
Enter the string :rajanshukla0402@gmail
rajanshukla0402@gmail
The number of uppper case is:0
The number of lower case is:16
The number of special symbol is :1
The nuber of didgt is:4
--------------------------------------
Program: Write a program to arrange the n. numbers stored in the array in ascending order in Cpp.
Code:
#include<iostream>
using namespace std;
int main()
{5
int i,j, no,temp, a[100];
cout<<"Enter the number of element";
cin>>no;
for(i=0;i<no;i++)
{
cout<<"Enter the element at "<<i<<" p ";
cin>>a[i];
}
for(i=0;i<no;i++)
{
for(j=i+1;j<no;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
cout<<"\n\n number are in ascending order";
for(i=0;i<no;i++)
{
cout<<"\n Enter the element at "<<i<<" place "<<a[i];
}
}
Output:
Enter the number of element5
Enter the element at 0 place 16
Enter the element at 1 place 14
Enter the element at 2 place 18
Enter the element at 3 place 13
Enter the element at 4 place 56
number are in ascending order
Enter the element at 0 place 13
Enter the element at 1 place 14
Enter the element at 2 place 16
Enter the element at 3 place 18
Enter the element at 4 place 56
---------------------------------------------
Program:
#include<iostream>
using namespace std;
int main()
{
int i,j, no,temp, a[100];
cout<<"Enter the number of element";
cin>>no;
for(i=0;i<no;i++)
{
cout<<"Enter the element at "<<i<<" place ";
cin>>a[i];
}
for(i=0;i<no;i++)
{
for(j=i+1;j<no;j++)
{
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
cout<<"\n\n number are in desending order";
for(i=0;i<no;i++)
{
cout<<"\n Enter the element at "<<i<<" place "<<a[i];
}
}
Output:
Enter the number of element5
Enter the element at 0 place 16
Enter the element at 1 place 19
Enter the element at 2 place 78
Enter the element at 3 place 98
Enter the element at 4 place 32
number are in desending order
Enter the element at 0 place 98
Enter the element at 1 place 78
Enter the element at 2 place 32
Enter the element at 3 place 19
Enter the element at 4 place 16
--------------------------------------------------------
Comments
Post a Comment