Write a C Program: This program on the will be a program that has a few function
ID: 3830383 • Letter: W
Question
Write a C Program:
This program on the will be a program that has a few functions that manipulate arrays (without yet using DMA). Remember to use const when appropriate.
Here are the sorts of array-manipulating functions you should be able to write:
Output the elements of the array, with the values separated by commas
(no leading or trailing comma)
Search for a value in the array, returning its first or last position (or UINT_MAX of it's not there), or the number of times it occurs.
Return the minimum, sum, product, or maximum of some of the elements in the array
Reverse the order of elements in the array
Change each of the value in the array according to some mathematical rule (e.g. square each element).
Sort the elements, according to some sorting rule, using qsort
Sort the elements, according to some sorting rule, without using qsort
This program has you work with arrays of ints
Write a function named show that
has 2 args: an int array (that show doesn't change) and the # of elements in the array
may assume without checking that the array has at least one element.
outputs all the array values on a single line, separated by commas, no leading or trailing comma
Write a function named sort that
has 2 args: an int array and the # of elements in the array
may assume without checking that the array has only positive elements
uses qsort to sort the array so that the numbers are in increasing order of the last digit. If two numbers have the same last digit, it doesn't matter which one comes first
uses a helper function (that you will also write) that does the necessary comparisons for qsort
Write a main that
creates an int array and (somehow) puts values into it
calls show (to show the unsorted array)
calls sort
calls show again (to show the sorted array)
For example, your program might output
9, 22, 37, 14
22, 14, 37, 9
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
void show(int number[], int size);
void sort(int number[], int size);
int cmpfunc (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
void main()
{
int i, j, a;
int number[4] = {9,22,37,14};
printf("Numbers in array: ");
show(number, 4);
printf(" ");
sort(number, 4);
printf("The numbers arranged in ascending order are given below ");
show(number, 4);
printf(" ");
}
void show(int number[], int size){
int i;
for (i = 0; i < 4; ++i){
printf("%d", number[i]);
if(i<3)
printf(",");
}
}
void sort(int number[], int size){
qsort(number, size, sizeof(int), cmpfunc);
int i,j,a;
for (i = 0; i < size; ++i)
{
for (j = i + 1; j < size; ++j)
{
if (number[i] > number[j])
{
a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}
}
OUTPUT:
Numbers in array:
9,22,37,14
The numbers arranged in ascending order are given below
9,14,22,37
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.