Question

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 (int i = 0; i < size; i++)
    {
      printf ("array[%d] = %d \n", i, array[i]);
    }
}

void swap(int *var1, int *var2)
{
// swap function to swap var1 and var2 using a temp variable

int temp = *var1;
    *var1 = *var2;
    *var2 = temp;

}

void Sort(int size, int array[])
{
  
// sort function to sort an array

int i, j;
   for (i = 0; i < size-1; i++)     
         
       for (j = 0; j < size-i-1; j++)
           if (array[j] > array[j+1])
              swap(&array[j], &array[j+1]);


}


int main ()
{

int data[] = {2, 7, 8, -8, 1, 9, 3, 5};

SelectionSort(sizeof(data)/sizeof (int), data);
PrintArray(sizeof(data)/sizeof(int), data);

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>

void PrintArray(int size, int array[]) {
    for (int i = 0; i < size; i++) {
        printf("array[%d] = %d \n", i, array[i]);
    }
}

void swap(int *var1, int *var2) {
// swap function to swap var1 and var2 using a temp variable
    int temp = *var1;
    *var1 = *var2;
    *var2 = temp;
}

void SelectionSort(int size, int array[]) {
// sort function to sort an array
    int i, j;
    for (i = 0; i < size - 1; i++)
        for (j = 0; j < size - i - 1; j++)
            if (array[j] > array[j + 1])
                swap(&array[j], &array[j + 1]);
}


int main() {

    int data[] = {2, 7, 8, -8, 1, 9, 3, 5};

    SelectionSort(sizeof(data) / sizeof(int), data);
    PrintArray(sizeof(data) / sizeof(int), data);
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
I'm trying to code a C program so it sorts an array of integer numbers 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
  • I need the pseudocode for this c program source code. #include<stdio.h> void printarray(int array[], int asize){...

    I need the pseudocode for this c program source code. #include<stdio.h> void printarray(int array[], int asize){ int i; for(i = 0; i < asize;i++) printf("%d", array[i]); printf("\n"); } int sum(int array[], int asize){ int result = 0; int i = 0; for(i=0;i < asize;i++){ result = result + array[i]; } return result; } int swap( int* pA,int*pB){ int result = 0; if(*pA > pB){ int tamp = *pA; *pA = *pB; *pB = tamp; result = 1; } return result;...

  • I need help of how to include the merge sort code into this counter code found...

    I need help of how to include the merge sort code into this counter code found below: #include<iostream> using namespace std; int bubble_counter=0,selection_counter=0; // variables global void bubble_sort(int [], int); void show_array(int [],int); void selectionsort(int [], int ); int main() { int a[7]={4,1,7,2,9,0,3}; show_array(a,7); //bubble_sort(a,7); selectionsort(a,7); show_array(a,7); cout<<"Bubble counter = "<<bubble_counter<<endl; cout<<"Selection Counter = "<<selection_counter<<endl; return 0; } void show_array(int array[],int size) { for( int i=0 ; i<size; i++) { cout<<array[i]<< " "; } cout<<endl; } void bubble_sort( int array[],...

  • c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each...

    c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each size Im trying to do time analysis for Quick sort but i keep getting time = 0 also i want edit the program to test different random sizes of the array and give me the time in a...

  • I need to program 3 and add to program 2 bellows: Add the merge sort and...

    I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....

  • JAVA I got a function that sorts an array of cars (carLot) by MPG. Can someone...

    JAVA I got a function that sorts an array of cars (carLot) by MPG. Can someone modify so that it can sort the array of cars by MPG from lowest to highest instead. private void selectionSort(ArrayList<Car> list) { for (int i = 0; i < list.size() - 1; i++) { // Find the minimum in the list[i..list.length-1] Car currentMax = list.get(i); int currentMaxIndex = i; for (int j = i + 1; j < list.size(); j++) { if (currentMax.getMPG() <...

  • #include<stdio.h> #include<stdio.h> int main(){ int i; //initialize array char array[10] = {“Smith”, “Owen”, “Kowalczyk”, “Glass”, “Bierling”,...

    #include<stdio.h> #include<stdio.h> int main(){ int i; //initialize array char array[10] = {“Smith”, “Owen”, “Kowalczyk”, “Glass”, “Bierling”, “Hanenburg”, “Rhoderick”, “Pearce”, “Raymond”, “Kamphuis”}; for(int i=0; i<8;i++){ for(int j=0; j<9; j++){ if(strcmp(array[j],array[j+1])>0){ char temp[20]; strcpy(temp,array[j]); strcpy(array[j],array[j+1]); strcpy(array[j+1],temp); } } } printf(“---------File Names---------\n”); for(inti=0; i<9; i++){ printf(“\t%s\n”,array[i]); } printf(-------5 Largest Files according to sorting----\n”); for(int i=0;i>=5;i--) { printf(“\t%s\n”,array[i]); } return0; } Consider the "sort" program (using with void* parameters in the bubblesort function) from the week 10 "sort void" lecture. Modify it as follows...

  • Selection sort is often the first sorting algorithm covered in introductory computer science courses. Java code...

    Selection sort is often the first sorting algorithm covered in introductory computer science courses. Java code that uses selection sort to place the elements of an integer array into non-decreasing order is shown here: public void swapNumbers(int i, int j) { ​int temp = numbers[i];​ /* put numbers[i] somewhere to keep it safe */ ​numbers[i] = numbers[j]; /* put numbers[j] at index i */ ​numbers[j] = temp;​ /* put numbers[i] at index j */ } public void selectionSort(int[] numbers) {...

  • Hello this is my java sorting algorithm program i need all of my errors corrected so...

    Hello this is my java sorting algorithm program i need all of my errors corrected so I can run it. thank you!! import java.util.Scanner;    public class SortingAlogs { public static void main(String[]args){ int array [] = {9,11,15,34,1}; Scanner KB = new Scanner(System.in); int ch; while (true) { System.out.println("1 Bubble sort\n2 Insertion sort\n3 Selection sort\n"); ch = KB.nextInt(); if (ch==1)    bubbleSort(array); if (ch==2)    insertion(array); if (ch==3)    Selection(array); if (ch==4) break;    print(array);    System.out.println(); }    }...

  • How can i make a counter for the number of exchanges made in the linear algorithm?? The binary counter works but the lin...

    How can i make a counter for the number of exchanges made in the linear algorithm?? The binary counter works but the linear doesn't. Here's my code. #include <iostream> using namespace std; void selectionSort(int[], int, int& ); void showSelection(int[], int); void sortArray(int[], int, int&); void showArray(const int[], int); int main() {    int values[25] = { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24...

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