adsense

Hii, welcome to my site. My name is Om prakash kartik. This blog helps you to learn programming languages concepts.

Program in C and C++ to accessing array elements in different ways

Program in C and C++ to accessing array elements in different ways



C
CPP
#include<stdio.h>
  int main()
   {
     int a[10]={1,2,3,4,5,6,7,8,9,10},i;
     printf("1st way a[i]  ");
     for(i=0;i<10;i++)
      printf("%4d",a[i]);
     printf("\n2nd way i[a]  ");
     for(i=0;i<10;i++)
      printf("%4d",i[a]);
 printf("\n3rd way *(a+i)");
     for(i=0;i<10;i++)
      printf("%4d",*(a+i));
 printf("\n4th way *(i+a)");
     for(i=0;i<10;i++)
      printf("%4d",*(i+a)); 
    return 0;
   }
#include<iostream>
 using namespace std;
  int main()
   {
     int a[10]={1,2,3,4,5,6,7,8,9,10},i;
     cout<<"1st way a[i]  ";
     for(i=0;i<10;i++)
      cout<<"  "<<a[i];
     cout<<"\n2nd way i[a]  ";
     for(i=0;i<10;i++)
      cout<<"  "<<i[a];
 cout<<"\n3rd way *(a+i)";
     for(i=0;i<10;i++)
      cout<<"  "<<*(a+i);
 cout<<"\n4th way *(i+a)";
     for(i=0;i<10;i++)
      cout<<"  "<<*(i+a); 
    return 0;
   }
Output:-








Post a Comment

0 Comments