Question

In: Computer Science

IN C LANGUAGE This program reads a threshold, a size, and an array of integers. The...

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

Solutions

Expert Solution

  • Below is the detailed explanation of the above mentioned problem in C with code and output.
  • Please read comments mentioned in the code for better understanding.
  • Also created the main function for testing a test case for correctness.
  • CODE:

#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;
}

  • INPUT/OUTPUT:

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

  • Below are the attached screenshot of the code and it's output for better clarity and understanding.

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.


Related Solutions

c++ language Create a file program that reads an int type Array size 10; the array...
c++ language Create a file program that reads an int type Array size 10; the array has already 10 numbers, but your job is to resize the array, copy old elements of array to the new one and make it user input and add an additional 5 slots in the array, and lastly do binary search based on user input. close the file.
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
use c++ 1 a)Write a console program that creates an array of size 100 integers. Then...
use c++ 1 a)Write a console program that creates an array of size 100 integers. Then use Fibonacci function Fib(n) to fill up the array with Fib(n) for n = 3 to n = 103: So this means the array looks like: { Fib(3), Fib(4), Fib(5), ...., Fib[102) }. For this part of the assignment, you should first write a recursive Fib(n) function. .For testing, print out the 100 integers. b) For the second part of this assignment, you must...
1. Write an assembly language program that prompts the user for and reads four integers (x1,...
1. Write an assembly language program that prompts the user for and reads four integers (x1, y1, x2, y2) which represent the coordinates of two points. Make sure you keep track of which number is which. 2. Treat the line between the points as the radius of a sphere and compute the surface area of the sphere. Print the output with a label, such as “The surface area of the sphere is: …”. Hint: The distance between the points is...
C++ Program: Write another program (in C++) that will allocate a local static array of integers...
C++ Program: Write another program (in C++) that will allocate a local static array of integers and then a dynamic array of integers. Are they stored next to each other? You can examine this by examining the memory addresses where they are located. As described in class, on some systems the size of a dynamic array is actually stored in the bytes previous to a dynamically allocated array. Through some experiments on your own, try to see if this is...
Use MIPS assembly language program to swap two of the integers in an integer array. The...
Use MIPS assembly language program to swap two of the integers in an integer array. The program should include the Swap function to swap the integers and the main function to call the Swap function. The main function should: • Pass the starting address of the array in $a0. • Pass the indices of the two elements to swap in $a1 and $a2. • Preserve (i.e. push onto the stack) any T registers that it uses. • Call the Swap...
C Language NO ARRAY UTILIZATION OR SORTING Create a .txt file with 20 integers in the...
C Language NO ARRAY UTILIZATION OR SORTING Create a .txt file with 20 integers in the range of 0 to 100. There may be repeats. The numbers must not be ordered/sorted. The task is to find and print the two smallest numbers. You must accomplish this task without sorting the file and without using arrays for any purpose. It is possible that the smallest numbers are repeated – you should print the number of occurrences of the two smallest numbers....
Complete the given C++ program (prob1.cpp) to read an array of integers, print the array, and...
Complete the given C++ program (prob1.cpp) to read an array of integers, print the array, and then find the index of the largest element in the array. You are to write two functions, printArray() and getIndexLargest(), which are called from the main function. printArray() outputs integers to std::cout with a space in between numbers and a newline at the end. getIndexLargest () returns the index of the largest element in the array. Recall that indexes start at 0. If there...
(C++) Write a program that reads a list of integers from the keyboard and print out...
(C++) Write a program that reads a list of integers from the keyboard and print out the smallest number entered. For example, if user enters 0 3 -2 5 8 1, it should print out -2. The reading stops when 999 is entered.
Code is in C Write a program that reads integers from stdin. Once it reaches the...
Code is in C Write a program that reads integers from stdin. Once it reaches the * end of the input, it prints the smallest absolute value among those * of the numbers it read. * * For example, if * 4, 6 -3, 3, -2, 13, -4 * are read from stdin, the program should print 2. * * If the end of file is reached before any integer is seen, the * number printed should be INT_MAX (defined...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT