Question

create a file homework_part_1.c a) Implement the function initialize_array that receives two parameters: an array of...

create a file homework_part_1.c

a) Implement the function initialize_array that receives two parameters: an array of integers and the array size. Use a for loop and an if statement to put 0s in the odd positions of the array and 5s in the even positions. You must use pointers to work with the array. Hint: review pointers as parameters.

b) Implement the function print_array that receives as parameters an array of integers and the array size. Use a for statements to print all the elements in the array. You must use pointers to work with the array. Hint: review pointers as parameters.

c) Implement the function insertion_sort that receives as parameters an array of integers and the array size, and order the array’s elements in descending order. Implement Insertion Sort algorithm. It should be Insertion Sort, not Selection Sort, not Quick Sort, etc.

d) Implement the recursive function that calculates and returns the factorial of a number. The function receives the number (integer number) as parameter

Compile and run the code for homework_part_1.c

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you


homework_part_1.c
----------

#include <stdio.h>
void initialize_array(int *arr, int size);
void print_array(int *arr, int size);
void insertion_sort(int *arr, int size);
long factorial(int n);
int main(){
int arr1[10];
int arr2[5] = {5, 9, 3, 1, 6};

initialize_array(arr1, 10);
printf("Array arr1 after initialization: ");
print_array(arr1, 10);

printf("\nArray arr2 before insertion sort: ");
print_array(arr2, 5);

insertion_sort(arr2, 5);
printf("Array arr2 after insertion sort: ");
print_array(arr2, 5);

printf("\nFactorial of 5 is %ld\n", factorial(5));
return 0;
}

void initialize_array(int *arr, int size){
int i;
for(i = 0; i < size; i++){
if(i % 2 == 1) //odd position
*arr = 0;
else //even position
*arr = 5;

arr++;
}
}
void print_array(int *arr, int size){
int i;
for(i = 0; i < size; i++){
printf("%d ", *arr);
arr++;
}
printf("\n");
}

void insertion_sort(int *arr, int size){
int i, j;
int val;
for(i = 0; i < size; i++){
val = arr[i];
for(j = i - 1; j >= 0 && arr[j] > val; j--)
arr[j+1] = arr[j];

arr[j+1] = val;
}
}

long factorial(int n){
if(n <= 1)
return 1;
else
return n * factorial(n-1);
}


output
---
Array arr1 after initialization: 5 0 5 0 5 0 5 0 5 0

Array arr2 before insertion sort: 5 9 3 1 6
Array arr2 after insertion sort: 1 3 5 6 9

Factorial of 5 is 120

Add a comment
Know the answer?
Add Answer to:
create a file homework_part_1.c a) Implement the function initialize_array that receives two parameters: an array of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C programming Strictly - Write a program to sort an array of integers via arrays of...

    C programming Strictly - Write a program to sort an array of integers via arrays of pointers to those integers as shown in the figure. Problem 1 (68 points): Write a program to sort an array of integers via arrays of pointers to those integers as shown in the figure. The code should contain the following functions: 1)to randomly generate an array of integer numbers; 2) to allocate memory and build the arrays of pointers as shown in the figure...

  • In below C++ sort.cpp 1- Implement the insertion_sort function. 2- Implement the compareSensorPtr function and the...

    In below C++ sort.cpp 1- Implement the insertion_sort function. 2- Implement the compareSensorPtr function and the code in main to create and sort the array of pointers. The places to make modifications are indicated by TODO: comments. You should not have to make modifications anywhere else. 3- what's big O and runtime for 100000 items. #include <iostream> #include <algorithm> #include <numeric> #include <vector> #include <string> #include <cstdlib> #include <cassert> using namespace std; // Set this to false to skip the...

  • Create a file called Sort.py (note the capitalization). Within that file, write two different Python functions....

    Create a file called Sort.py (note the capitalization). Within that file, write two different Python functions. Each function will take an array of integers as a parameter and sort those integers in increasing order. One will use insertion sort, and the other will use selection sort (described below). Simple functions will suffice here; do not create any classes. Your insertion sort function should be called insertion_sort(arr). Your selection sort function should be called selection_sort(arr). Selection sort must use a while...

  • Implement the sort procedure from Assignment 18 as a void function. Pass the address of the...

    Implement the sort procedure from Assignment 18 as a void function. Pass the address of the first array element to the function along with the number of elements in the array. Use pointers in your function to reference the array elements. The sort function should do nothing but sort the elements of the array. DO NOT pass the entire array as an argument to the function. As in Assignment 18, print out the array before and after sorting. Use the...

  • 1. Write code for a function that receives two parameters (a,and b) by value and two...

    1. Write code for a function that receives two parameters (a,and b) by value and two more parameters (c and d) by reference. All parameters are double. The function works by assigning c to (a/b) and assigning d to (a*b). The function has no return value. From main, use scanf to get two numbers, then call the function, and then display both outcome values to the output in a printf statement. 2. After part 1 is completed, write code to...

  • C++ Programme Write a function that receives, as arguments, a pointer to a double array, as...

    C++ Programme Write a function that receives, as arguments, a pointer to a double array, as well as the number of elements: in the array(int). The function must use pointer operations and calculate the standard deviation of the elements in the array. The function returns the standard deviation to the calling statementſ Use the function in main( to display its operation. | Example: array = (11.2, 2.4, 3.13, 16.4, 5.8, 9.22, 4.9, 10.5, 6.5, 2.99) std Dev(array) = 4.249367 ZWI...

  • (C++) Write a function, insertAt, that takes four parameters: an array of integers, the number of...

    (C++) Write a function, insertAt, that takes four parameters: an array of integers, the number of elements in the array, an integer (say, insertItem), and an integer (say, index). The function should insert insertItem in the array provided at the position specified by index. If index is out of range, output the following: Position of the item to be inserted is out of range or if the index is negative: Position of the item to be inserted must be nonnegative...

  • Write a program using C in Microsoft visual studios. Write a function that receives an array...

    Write a program using C in Microsoft visual studios. Write a function that receives an array of integers and the array length and prints one integer per line (Hint: Use \n for a line break). Left align all of the integers and use a field width of 10. Populate an array of 10 elements from the user and pass the array and its length to the function.

  • Program must be wriiten in c++ Write a function, removeAt, that takes three parameters: an array...

    Program must be wriiten in c++ Write a function, removeAt, that takes three parameters: an array of integers, the number of elements in the array, and an integer (say, index). The function should delete the array element indicated by index. If index is out of range or the array is empty, output an appropriate message. (Note that after deleting the element, the number of elements in the array is reduced by 1.) Assume that the array is unsorted.

  • (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element...

    (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to the element 1 of the new array. Element 1 of the argument array should be copied to element 2 of the new array, and so forth....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT