Question: Write a function, called constantMultiple , that multiplies positive values in an array by a constant and multiplies negative values in an array by a
Write a function, called constantMultiple, that multiplies positive values in an array by a constant and multiplies negative values in an array by a different constant. Print out the resulting array after your function is called.
Function outputs: the modified array (arrays are automatically passed by pointer so your function should return void)
Function inputs: an array, the size of the array, an integer to multiply the positive values by, an integer to multiply the negative values by
Example output with a positive multiplier of 2 and a negative multiplier of 4 entered in as [2, 4]:
Positive multiplier and negative multiplier [P, N]: Seed: Original array: 6 -9 -7 2 -5 9 -10 9 8 -3 Modified array: 12 -36 -28 4 -20 18 -40 18 16 -12 USE THE TEMPLATE PROVIDED BELOW
#include
#include
// WRITE FUNCTION HERE. Your function must be called constantMultiple.
int main( void )
{
// declare 2D array
const int SIZE = 10;
int sig[SIZE];
// read in positive and negative multipliers
int pos, neg;
printf("Positive multiplier and negative multiplier [P, N]: ");
scanf("[%d,%d]", &pos, &neg);
// read in seed point
int seed;
printf("Seed: ");
scanf("%d", &seed);
srand( seed );
// fill array with random values between -10 and 10
printf("Original array: ");
for( int i = 0; i < SIZE; i++) {
sig[i] = rand()%20-10; // put random value into array
printf("%3d ", sig[i]); // print our array
}
printf(" ");
// CALL FUNCTION HERE
constantMultiple( sig, SIZE, pos, neg );
// PRINT OUT ARRAY RESULTS HERE
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
