sort.c
#include <stdlib.h>
#include <stdio.h>
#include "libsort.h"
int main()
{
int* array;
int size, c;
float median;
printf("Enter the array size:\n");
scanf("%d", &size);
array = (int*) malloc(size * sizeof(int));
printf("Enter %d integers:\n", size);
for (c = 0; c < size; c++)
scanf("%d",
&array[c]);
sort(array, size);
printf("Array sorted in ascending
order:\n");
for (c = 0; c < size; c++)
printf("%d ",
array[c]);
printf("\n");
median = find_median(array, size);
printf("The median is: %.1f\n", median);
return 0;
}
Please find my answer.
########## libsort.h ###########
void sort(int array[], int size);
float find_median(int array[], int size);
########## libsort.c ###########
void sort(int array[], int size){
int i, j, min_idx;
// One by one move boundary of unsorted subarray
for (i = 0; i < size-1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < size; j++)
if (array[j] < array[min_idx])
min_idx = j;
// Swap the found minimum element with the first element
int t = array[min_idx];
array[min_idx] = array[i];
array[i] = t;
}
}
float find_median(int array[], int size) {
if(size %2 == 1)
return array[size/2];
else
return (array[size/2-1] + array[size/2]);
}
####### sort.c ######
#include <stdlib.h>
#include <stdio.h>
#include "libsort.h"
int main()
{
int* array;
int size, c;
float median;
printf("Enter the array size:\n");
scanf("%d", &size);
array = (int*) malloc(size * sizeof(int));
printf("Enter %d integers:\n", size);
for (c = 0; c < size; c++)
scanf("%d", &array[c]);
sort(array, size);
printf("Array sorted in ascending order:\n");
for (c = 0; c < size; c++)
printf("%d ", array[c]);
printf("\n");
median = find_median(array, size);
printf("The median is: %.1f\n", median);
return 0;
}
sort.c #include <stdlib.h> #include <stdio.h> #include "libsort.h" int main() { int* array; int size,...
#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...
#include<stdio.h> int main() { int data[10], i, j, temp; printf("Enter 10 random number to sort in ascending order:\n"); for(i = 0; i < 10; i++) scanf("%d", &data[i]); /* Sorting process start */ ****** INSERT YOUR CODE TO COMPLETE THE PROGRAM ****** printf("After sort\n"); for(i = 0; i < 10; i++) printf("%d\n",data[i]); return 0; } Looking for alternative solutions for the program code.
Translate the following C program to Pep/9 assembly language. #include <stdio.h> int main() { int number; scanf("%d", &number); if (number % 2 == 0) { printf("Even\n"); } else { printf("Odd\n"); } return 0; }
#include<stdio.h> #include<stdlib.h> #include <time.h> int main() { /* first you have to let the computer generate a random number. Then it has to declare how many tries the user has for the game loop. we then need the player to enter their guess. After every guess we have to give an output of how many numbers they have in the right location and how many they have the right number. The player will keep guessing until their 10 tries are...
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;...
Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int *); void replaceAt(int *, int, int); int isEmpty(int *, int); int isFull(int *, int); void removeAt(int *, int); void printList(int *, int); int main() { int *a; int arraySize=0,l=0,loc=0; int choice; while(1) { printf("\n Main Menu"); printf("\n 1.Create list\n 2.Insert element at particular position\n 3.Delete list.\n4. Remove an element at given position \n 5.Replace an element at given position\n 6. Check the size of...
#include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here. */ int GetNumOfNonWSCharacters(const char usrStr[]) { int length; int i; int count = 0; char c; length=strlen(usrStr); for (i = 0; i < length; i++) { c=usrStr[i]; if ( c!=' ' ) { count++; } } return count; } int GetNumOfWords(const char usrStr[]) { int counted = 0; // result // state: const char* it = usrStr; int inword = 0; do switch(*it)...
Solve using C programming 3. lf you are given this code. #include <stdio.h> int main() int var1, var2; int sum; printf("Enter number 1:\n "); scanf("%d",&var1); printf("Enter number 2:In ); scanf("%d",&var2); sum-var1+var2; printf ("Vnsum of two entered numbers : %d", //printf ("Output: %d", res); sum); return e; Modify this code by creating a function called "addition". Make the arguments of the functions numberl and number 2. Add the two values in the function. Return a value called "result". Print "result" in...
C program-- the output is not right please help me to correct it. #include <stdio.h> int main() { int arr[100]; int i,j,n,p,value,temp; printf("Enter the number of elements in the array: \n "); scanf("%d",&n); printf("Enter %d elements in the array: \n",n); for(i=0;i<n;i++) { printf("\nelement %d: ",i); scanf("\n%d",&arr[i]); } printf("\nEnter the value to be inserted: \n "); scanf("\n%d",&value); printf("The exist array is: \n"); for(i=0;i<n;i++) { printf("%d",arr[i]); } p=i; for(i=0;i<n;i++) if(value<arr[i] ) { p = i; break; } arr[p]=value; printf("\n"); for (i =...
what's wrong with my code?????? #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXBINS 99 #define MAXCORS 26 #define MAXCUS 100 #define MAXPUR 10 /* datatype for a list of orders ----------------- */ typedef struct { int bin_order_number; char bin_order_char; }bin_order; typedef struct { int orderNo; bin_order orderbin; }order; typedef struct { int cusnumber; int n; /* number of the orders what the txt include */ order oo[MAXPUR+1]; }customer; typedef struct{ int n; /*number of the customers */ customer cc[MAXCUS+1];...