Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) Create a program that divides each element of an array by 2 using only pointe

ID: 3531181 • Letter: 1

Question

1) Create a program that divides each element of an array by 2 using only pointers to access the array. No user input is required. For example, an array declared as

int x[3] = {4,6,2}

would be converted to {2,3,1}

2) Write a function that reverses an array, i.e. {1,2,3,4} becomes {4,3,2,1}. Use pointers for all access to the array.

3) Given three arrays, add the arrays together using pointer notation.
Example: {1,2,3} + {1,2,3} + {1,2,3} => {3,6,9}

4) Using an array, replace every letter between 'a' and 'd' with the letter 'X'

Explanation / Answer

#include<stdio.h>
int main(){
int x[3]={2,3,4};
int *p=x;
for(int i=0;i<3;i++){
*p = *p/2;
p++;
}
for(int i=0;i<3;i++){
printf("%d",x[i]);
}
return 0 ;
}

2.

#include<stdio.h>
int main(){
int x[4]={1,2,3,4}
int *p=x,t;
for(int i=0;i<2;i++){
t=*p;
*p=*(p+3-i);
*(p+3-i)=t;
p++
}
return 0 ;
}

3.

#include<stdio.h>
int main(){
int x[3]={1,2,3},y[3]={1,2,3},z[3]={1,2,3},a[3];
int a[3] ;
int *p=x, *q=y ,*r = z ;

for(int i=0;i<3;i++){

a[0]=*p+*q+*r;
p++;
q++;
r++;
}
for(int i=0;i<3;i++){
printf("%d",a[i]);
}
return 0 ;
}

4.

#include<stdio.h>
int main(){
char x[5]={'a','k','d','h','e'};
int count,j ;
for(int i=0;i<5;i++){
if(x[i]=='a' )
count = i;
if(x[i]=='d')
j = i ;
}
for(int i=count-1;i<j;i++){
x[i]='X' ;
}