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
  • Please complete the following programming in C++ with clear explanations. Thanks! Primitive Types, Searching and Recursion...

    Please complete the following programming in C++ with clear explanations. Thanks! Primitive Types, Searching and Recursion a) Create a class Homework (in a file homework.h and homework.cpp). b) Create a 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 1s in the odd positions of the array and 0s in the even positions. (Use pointers to pass an array of integers as parameter) c) Create...

  • Part 1. Primitive Types, Sorting, Recursion for Homework.java a) Implement the static method initializeArray that receives...

    Part 1. Primitive Types, Sorting, Recursion for Homework.java a) Implement the static method initializeArray that receives as a parameter an array of integers. Use a for loop and an if statement to put 0s in the odd positions of the array and 5s in the even positions. b) Implement the static method printArray that receives as a parameter an array of integers. Use a for statements to print all the elements in the array. c) Implement the static method insertionSort...

  • 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...

  • 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...

  • I am having problems getting the insertion sort function to output in descending order. Also, the...

    I am having problems getting the insertion sort function to output in descending order. Also, the initialize array function is not being called when I test it in the main function. I am wondering why it prints out the original array instead of initializing. Thank you. #include <stdio.h> void print_array(int *array, int length){ int i; for(i=0; i<length;i++) printf("%d"",",array[i]); printf("\n"); } void initialize_array(int *array, int length){ int i; for(i=0, i<length; i++;){ if (i%2 ==0) array[i]= 5; else array[i]= 0; } }...

  • 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...

  • 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...

  • 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...

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