Skip to content
Snippets Groups Projects
Commit 3b703fb8 authored by dario.genga's avatar dario.genga
Browse files

Add method to multiply an array with a value

The value is given by the user.
parent 6163ed7f
No related branches found
No related tags found
No related merge requests found
...@@ -9,6 +9,7 @@ int main() { ...@@ -9,6 +9,7 @@ int main() {
srand(time(NULL)); srand(time(NULL));
size_t cycle_number = 3; size_t cycle_number = 3;
size_t value = 0; size_t value = 0;
size_t multiply_value = 0;
// Ask the user the size of the array // Ask the user the size of the array
size_t array_size = ask_array_size(); size_t array_size = ask_array_size();
int *array = malloc(array_size * sizeof(int)); int *array = malloc(array_size * sizeof(int));
...@@ -56,10 +57,19 @@ int main() { ...@@ -56,10 +57,19 @@ int main() {
printf("Result array :\n"); printf("Result array :\n");
print_array(result_sum_array, array_size); print_array(result_sum_array, array_size);
// Create a fourth array that will stock the multiplication between the first array and a value from the user
int *result_mul_array = malloc(array_size * sizeof(int));
printf("Multiply the first array with the following value : \n");
scanf("%ld", &multiply_value);
multiply_array_with_value(array, array_size, result_mul_array, multiply_value);
printf("Result array after multiplication :\n");
print_array(result_mul_array, array_size);
// Free the memory // Free the memory
free(array); free(array);
free(second_array); free(second_array);
free(result_array); free(result_sum_array);
free(result_mul_array);
return 0; return 0;
} }
\ No newline at end of file
...@@ -116,6 +116,12 @@ void compute_two_array(int* first_array, int* second_array, int* result_array, s ...@@ -116,6 +116,12 @@ void compute_two_array(int* first_array, int* second_array, int* result_array, s
} }
} }
void multiply_array_with_value(int* array, size_t array_size, int* result_array, int value) {
for (size_t i = 0; i < array_size; i++) {
result_array[i] = array[i] * value;
}
}
void swap(int *x, int *y) void swap(int *x, int *y)
{ {
int tmp = *x; int tmp = *x;
......
...@@ -25,6 +25,8 @@ size_t count_elements_in_array_lower_than_value(int* array, size_t array_size, i ...@@ -25,6 +25,8 @@ size_t count_elements_in_array_lower_than_value(int* array, size_t array_size, i
void compute_two_array(int* first_array, int* second_array, int* result_array, size_t array_size); void compute_two_array(int* first_array, int* second_array, int* result_array, size_t array_size);
void multiply_array_with_value(int* array, size_t array_size, int* result_array, int value);
void print_array(int* array, size_t array_size); void print_array(int* array, size_t array_size);
void swap(int *x, int *y); void swap(int *x, int *y);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment