Write a main program to declare an integer array of size 3 and initialize it wit
ID: 3812827 • Letter: W
Question
Write a main program to declare an integer array of size 3 and initialize it with 1, 2 and 3. Pass this array to a function called change. The function has two parameters one is the array and the other is the size of the array. The function should only use pointer notations and it should multiply each item of the array by 2 (using pointer notations). A second function called printArray should also accept two parameters one is the array and the other one is the size. This function should display the values of the array. Main program should test both functions. Also make sure to use the principle of least privilege for the printArray function. ( C Language )
Explanation / Answer
#include<stdio.h>
#include<string.h>
int change(int *a, const int size) {
int i;
for(i=0; i<size; i++){
*(a+i) = *(a+i) * 2;
}
}
void printArray (int *a,const int size) {
int i;
for(i=0; i<size; i++){
printf("%d ",*(a+i) );
}
}
int main() {
int a[3] = {1,2,3};
change(a, 3);
printArray (a, 3);
}
Output:
sh-4.2$ gcc -o main *.c
sh-4.2$ main
2
4
6
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.