Question

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

void insertion_sort(int array [], int length){
int i,j,a;
for(j =1; j<length;j++){
a = array[j];
for(i= j-1; i>=0 && array[i] > a;i--)
array[i+1]= array[i];
array[i+1]= a;
}
  
}

int factorial(int num){
if(num==0)
return 1;
else
return num* factorial(num-1);
}


int main()
{
int a[] = {2, 5, 7, 9, 12, 13, 15, 17, 19, 20};
int b[] = {18, 16, 19, -5, 3, 14, 6, 0};
int c[] = {4, 2, 5, 3, 1};

/* testing initialize_array */
print_array(a, sizeof(a)/sizeof(a[0])); /* print: 2, 5, 7, 9, 12, 13, 15, 17, 19, 20 */
initialize_array(a, sizeof(a)/sizeof(a[0]));
print_array(a, sizeof(a)/sizeof(a[0])); /* print: 5, 0, 5, 0, 5, 0, 5, 0, 5, 0 */

/* testing insertionSort */
print_array(b, sizeof(b)/sizeof(b[0])); /* print: 18, 16, 19, -5, 3, 14, 6, 0 */
insertion_sort (b, sizeof(b)/sizeof(b[0]));
print_array(b, sizeof(b)/sizeof(b[0])); /* print: 19, 18, 16, 14, 6, 3, 0, -5 */

/* testing factorial */
printf("%d\n", factorial(5)); /* print: 120 */

c[0] = factorial (c[0]);
c[1] = factorial (c[2]);
print_array(c, sizeof(c)/sizeof(c[0])); /* print: 24, 120, 5, 3, 1 */

return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#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;
    }
}

void insertion_sort(int array[], int length) {
    int temp;
    for (int i = 1; i < length; i++) {
        for(int j = i; j > 0; j--) {
            if(array[j] > array[j-1]) {
                temp = array[j];
                array[j] = array[j-1];
                array[j-1] = temp;
            }
        }
    }
}

int factorial(int num) {
    if (num == 0)
        return 1;
    else
        return num * factorial(num - 1);
}


int main() {
    int a[] = {2, 5, 7, 9, 12, 13, 15, 17, 19, 20};
    int b[] = {18, 16, 19, -5, 3, 14, 6, 0};
    int c[] = {4, 2, 5, 3, 1};

/* testing initialize_array */
    print_array(a, sizeof(a) / sizeof(a[0])); /* print: 2, 5, 7, 9, 12, 13, 15, 17, 19, 20 */
    initialize_array(a, sizeof(a) / sizeof(a[0]));
    print_array(a, sizeof(a) / sizeof(a[0])); /* print: 5, 0, 5, 0, 5, 0, 5, 0, 5, 0 */

/* testing insertionSort */
    print_array(b, sizeof(b) / sizeof(b[0])); /* print: 18, 16, 19, -5, 3, 14, 6, 0 */
    insertion_sort(b, sizeof(b) / sizeof(b[0]));
    print_array(b, sizeof(b) / sizeof(b[0])); /* print: 19, 18, 16, 14, 6, 3, 0, -5 */

/* testing factorial */
    printf("%d\n", factorial(5)); /* print: 120 */

    c[0] = factorial(c[0]);
    c[1] = factorial(c[2]);
    print_array(c, sizeof(c) / sizeof(c[0])); /* print: 24, 120, 5, 3, 1 */
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
I am having problems getting the insertion sort function to output in descending order. Also, the...
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 help in C: with the following code, i am getting a random 127 printed in...

    Please help in C: with the following code, i am getting a random 127 printed in front of my reverse display of the array. The file i am pulling (data.txt) is: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 when the reverse prints, it prints: 127 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 not sure why...please...

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

  • If void * is a pointer to void is a "generic" pointer type, and a void...

    If void * is a pointer to void is a "generic" pointer type, and a void * can be converted to any other pointer type * without an explicit cast, why in the ,myarrcopy function the malloc is created like char and not like void? if we are going to work with different type of arrays? Here is de program: *This is a function that make a copy of any given array. * We then use this function make a...

  • Consider the syntactically correct C code below, which is missing a function copy_positive. #include <stdio.h> /*...

    Consider the syntactically correct C code below, which is missing a function copy_positive. #include <stdio.h> /* copy_positive(A, Aout, size) * Given an array A, which will have the provided size, copy all positive * (non-negative/non-zero) elements of A into the provided output array Aout. * Return the size of the resulting array. */ /* (your code from below would be placed here) */ void print_array(int arr[], int n){ for( int i = 0; i < n; i++ ) printf("%d ",...

  • Must be written in C 89 Mode Array Insertion Your task is to complete the implementation...

    Must be written in C 89 Mode Array Insertion Your task is to complete the implementation of the insertion function, insert_into_array: int* insert_into_array(int arr[], size_t arr_len, int value, size_t pos); The insertion function inserts a value into an array at a specified position. All elements to the right of the inserted element are shifted over a position (upon inserting, all elements at the insertion position are shifted up an index, and the last element in the array is overwritten by...

  • I'm trying to code a C program so it sorts an array of integer numbers of...

    I'm trying to code a C program so it sorts an array of integer numbers of size n in ascending order. My code is written below but its not working properly, its giving me errors, I think my sort and swap functions aren't done right maybe, please help and fix. It says "undefined reference to "SelectionSort" as an error. But the question asks to not change the PrintArray and Main function. #include <stdio.h> void PrintArray(int size, int array[]) { for...

  • I am having problems with the following assignment. It is done in the c language. The...

    I am having problems with the following assignment. It is done in the c language. The code is not reading the a.txt file. The instructions are in the picture below and so is my code. It should read the a.txt file and print. The red car hit the blue car and name how many times those words appeared. Can i please get some help. Thank you. MY CODE: #include <stdio.h> #include <stdlib.h> #include <string.h> struct node { char *str; int...

  • Write a C Program that inputs an array of integers from the user along-with the length...

    Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...

  • c++. please show screenshots of output This project will continue to familiarize the student with using...

    c++. please show screenshots of output This project will continue to familiarize the student with using arrays. Store all the function proto-types in project11_funch and store all the functions in project11_func.cpp. Make sure you have proper header comments in each.h,.cpp file. When you run your program, your sample output should show your name. Your report should include the following: 1) project11_func.h 2) project11_func.cpp 3) project11.cpp 4) sample output Write the following functions: a) void fill_array_with_random_nums(int array(), int N): Fill array...

  • OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main()...

    OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main() int mptr, *cptr mptr = (int*)malloc(sizeof(int)); printf("%d", "mptr); cptr = (int)calloc(sizeof(int),1); printf("%d","cptr); garbage 0 000 O garbage segmentation fault Question 8 1 pts If this program "Hello" is run from the command line as "Hello 12 3" what is the output? (char '1'is ascii value 49. char '2' is ascii value 50 char'3' is ascii value 51): #include<stdio.h> int main (int argc, char*argv[]) int...

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