Question
Modify the program in C that copies every third element in an input array to an output array and discards all the other values to
Modify the program in C that copies every third element in an input array to an output array and discards all the other values to use pointer arithmetic in the
decimate_by3 function.
Modification: The decimate_by3 function should use pointer arithmeticnot subscriptingto visit array elements. In other words, eliminate the loop index variables and all use of the []operator in the function.
Example input/output #1:
Enter
the length of the array: 8
Enter the elements of the array: 3 4 7 14 9 12 8 2
Output: 3 14 8
Example input/output #2:
Enter
the length of the array: 6
Enter the elements of the array: 3 4 7 14 9 12
Output: 3 14
program that needs to be modified
#include // the decimate by 3 void function void decimate_by3(int a1[], int n, int a2[]) { //sets the parameters as for loops int i;int k=0; for(i=0;i { a2[k]=a1[i]; k++; } for(i=0;i<=n/3;i++) { printf("%d",a2[i]); printf(" ");
} } //main body of the program int main() { //defines the array as ints and sets up the scanner int n; printf("enter the length of array:"); scanf("%d",&n); int a1[n]; int a2[n/3]; int i=0; //takes the nuimbers of the array printf("Enter the elements of the array:"); for(i=0;i scanf("%d", &a1[i]); //utilizes the decimate_by3 function decimate_by3(a1,n, a2); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started