In: Computer Science
IN C LANGUAGE
This program reads a threshold, a size, and an array of integers. The program then calls the foo function. The function will modify the array x of size n by doubling any values in x that are less than the threshold. The function will count how many values were changed and how many were not changed via two reference parameters. The function signature is:
void foo(int n, int x[], int threshold, int *pChanged, int *pUnchanged);
The function will be unit tested, so do not change the rest of the program or the function signature.
Remember that variables are not initialized to 0 in ZyBooks.
Increase the changed count whenever you double a number.
Example
What is the threshold? 10 How many integers? 5 Enter 5 integers: 9 10 11 3 20 The array is now: [ 18 10 11 6 20 ] 2 were changed 3 were unchanged
Explanation:
Of the 5 integers, 9 and 3 are less than the threshold 10: 9 doubles to 18 3 doubles to 6 2 were changed, 3 were unchanged
#include<stdio.h>
#include<stdlib.h>
//function that will update the array x based on threshold and
update pChanged and pUnchanged
void foo(int n, int x[], int threshold, int *pChanged, int
*pUnchanged){
//loop through array
for(int i=0;i<n;i++){
//check if current element is less than threshold then update
pChanged and array element
if(x[i]<threshold){
(*pChanged)++;
x[i]=x[i]*2;
}
//otherwise only update pUnchanged
else{
(*pUnchanged)++;
}
}
return;
}
//driver function
int main(){
//user input threshold
printf("What is the threshold?\n");
int threshold;
scanf("%d",&threshold);
//user enter number of integers in array
printf("How many integers?\n");
int n;
scanf("%d",&n);
//user enters array
printf("Enter %d integers?\n",n);
int *x=(int*)malloc(n * sizeof(int));
for(int i=0;i<n;i++){
scanf("%d",&x[i]);
}
//create variables for keep track of changed and unchanged
element
int pChanged=0,pUnchanged=0;
//call foo() with all mentioned attributes
foo(n,x,threshold,&pChanged,&pUnchanged);
//print updated array
printf("The array is now:\n");
printf("[ ");
for(int i=0;i<n;i++){
printf("%d ",x[i]);
}
printf("]\n");
//print count of changed and unchanged elements
printf("%d were changed\n",pChanged);
printf("%d were unchanged\n",pUnchanged);
return 0;
}
What is the threshold?
10
How many integers?
5
Enter 5 integers?
9 10 11 3 20
The array is now:
[ 18 10 11 6 20 ]
2 were changed
3 were unchanged
CODE
INPUT/OUTPUT
So if you have any doubt regarding this explanation please feel free to ask in the comment section below and if it is helpful then please upvote this solution, THANK YOU.