AD1

Monday, 16 January 2017

Increment all array values using pointers

Write C function INC(int array[5],int n) that accepts two parameters array of five integers and integer, and adds the integer n to all array elements , then it returns the new array , as example if array[5]={1,5,7,0,2} and n =5 after function call we have  array[]={6,10,12,5,7}
#include <stdio.h>
void INC(int *pointer,int n)
{
for(int x=0;x<=4;x++)
{
*(pointer+x)+=n;
}
return;
}
int main()
{
int N;printf("enter the number you want to add to all array elements\n");
scanf("%d",&N);
int array[5]={1,5,7,0,2};
INC(array,N);
printf("array after function call\n");
for(int i=0;i<=4;i++)
{
printf("%d\t",array[i]);
}
return 0;
}
view raw inc.c hosted with ❤ by GitHub






No comments:

Post a Comment