Array in C programming

 Array 
Array 

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.

Arrays are of two types:

        One-dimensional arrays 

        Multidimensional arrays

  • Two dimensional array
  • Three dimensional array
  • Four dimensional array
One Dimensional Array(1D)
            Arrays must be declared before they can be used in the program. The dimensionality of an array is determined by the number of subscript present in the given array. If there is only one subscript, then it is called a one-dimensional array.

 Syntax:

            type variable_name[lengthofarry];

Program: Write a program to create 1D array, input numbers and sort the elements.

Code:
#include<stdio.h>
void main()
{
  int i,j ,num,temp, a[100];
  printf("Enter a number :");
  scanf("%d",&num);
  for(i=0;i<num;i++)
  {
  printf("\n Enter the element at place %d is :",i);
  scanf("%d",&a[i]);
  }
 for(i=0;i<num;i++)
 {
  for(j=i+1;j<num;j++)
  {
  if(a[i]>a[j])
  {
  temp=a[i];
  a[i]=a[j];
  a[j]=temp;
}
}
   }
   printf("The arry in ascending order is ");
    for(i=0;i<num;i++)
   printf(" %d ",a[i])  ;
   }

Output:
Enter a number :8

 Enter the element at place 0 is :12

 Enter the element at place 1 is :15

 Enter the element at place 2 is :16

 Enter the element at place 3 is :78

 Enter the element at place 4 is :89

 Enter the element at place 5 is :65

 Enter the element at place 6 is :90

 Enter the element at place 7 is :45
The arry in ascending order is  12  15  16  45  65  78  89  90
--------------------------------



Program: Write a program to take input element for array and search a number are in  array or not.

Code:
#include<stdio.h>
void main()
 
{
int i,number=0,a[100],no,num;
printf("Enter the number (less than 100)");
scanf("%d",&no);
for(i=0;i<no;i++)
{
printf("Enter the element at %d place  in the array ",i);
scanf("%d",&a[i]);
}
printf("Enter the element which you want to seached ");
scanf("%d",&num);
for(i=0;i<no;i++)
{
if(num==a[i])
{
number=1;
break;
}
}
if(number==1)
printf("The %d is found at the place %d", num,i);
else 
printf("The number is not found");
}

Output:
Enter the number (less than 100)15
Enter the element at 0 place  in the array 126
Enter the element at 1 place  in the array 36
Enter the element at 2 place  in the array 96
Enter the element at 3 place  in the array 89
Enter the element at 4 place  in the array 45
Enter the element at 5 place  in the array 65
Enter the element at 6 place  in the array 75
Enter the element at 7 place  in the array 76
Enter the element at 8 place  in the array 45
Enter the element at 9 place  in the array 53
Enter the element at 10 place  in the array 48
Enter the element at 11 place  in the array 56
Enter the element at 12 place  in the array 23
Enter the element at 13 place  in the array 12
Enter the element at 14 place  in the array 1
Enter the element which you want to seached 48
The 48 is found at the place 10
---------------------------------------



Program : write a program to  find the average of number from user. using array

Code:
#include<stdio.h>

int  average(int no)
{       
int a[100],i,sum=0;
for(i=0;i<no;i++)
{
printf("Enter the %d element",i+1);
scanf("%d",&a[i]);
sum=sum+a[i];
}
return sum;
 } 
void main()
{
int no;
float avg;
printf("Enter the number of element: ");
scanf("%d",&no);
avg=average(no)/no;
printf("Average=%f ",avg);
}

Output:
Enter the number of element: 5
Enter the 1 element11
Enter the 2 element12
Enter the 3 element13
Enter the 4 element14
Enter the 5 element15
Average=13.000000
--------------------------------


Program: Write a program to  to count number of digit ,alphabets, vowels. 

Code:
#include<stdio.h>
void main()
{
char ch[20];
int i,j,vowel=0,digit=0,space=0,count=0;
printf("Enter a word ");
gets(ch);
for(i=0;ch[i]!='\0';i++)
{
count++;
}
for (i=0;i<count;i++)
{
if(ch[i]=='a'||ch[i]=='e'||ch[i]=='o'||ch[i]=='u')
vowel++;
if(ch[i]<='9' && ch[i]>='0')
{
digit++;
}
if(ch[i]==' ')
{
space++;
}
}
printf("Number of vowel is :%d \n",vowel);
printf("Number of digit is :%d \n",digit);
printf("Number of space is :%d \n",vowel);
}

Output:
Enter a word Rajan shukla 932679
Number of vowel is :4
Number of digit is :6
Number of space is :4

--------------------------------

Program: Write a program to check wehather string is  polidrome or not .

Code:
#include<stdio.h>
void main()
{
char a[100];
int i,no=0, len=0, k;
printf("Enter the  String ");
scanf("%s",&a);
for(i=0;a[i]!='\0';i++)
{
len++;
}
len--;
for(i=0;i<len;i++)
{
if(a[i]!=a[len])
{
no=1;
break;
}
len--;
}
if(no==1)
printf("Enter string is not palindrome");
else
printf("Enter string is palindrome");
}
Output:
Enter the  String 123321
Enter string is palindrome
--------------------------------





        

Comments

Popular posts from this blog

CYBER SECURITY & CRACKING PASSWORDS

"The Dark Side of Social Media: How It Fuels Digital Addiction and Impacts Mental Health"

Online Platforms to Learn Coding