This program is to be done in C language Sorting Arrays You gradually modify you
ID: 3349159 • Letter: T
Question
This program is to be done in C language
Sorting Arrays
You gradually modify your program in the three parts below so that it becomes the required sort.c.
Always test each complemented function thoroughly before you write another function.
Part I
Write a program that asks the user to enter 10 integers and stores them in an array.
Write a function that takes as input an array and its length and prints the content of the array separated
by tabs. Modify your program so it prints the content of the array after initializing it from the user input
(use the function you wrote).
Write a function (largest) that takes as input an array of int, the length of the array, and an index of the
array and returns the largest element of the array between the given index and the end of the array.
Your program should then ask the user for a position and use the function largest to return the largest
element starting at that position.
Compile and test your program.
The following declaration is an example of passing an array and its length as the parameters to a
function:
void sampleFunction(int array[], int size)
Part II
Modify your function, change its name to largestPos and update its code so it returns the position of the
largest element found. Change your program so it prints the largest element in the array starting at the
user given position.
Part III
Write a program (sort.c) that asks the user to enter a number of integers, stores them in an array, and
then sorts the array in decreasing order. Your program should do the following:
- Ask the user to enter an integer between 2 and 15. This will represent the number of elements
in the array
- Check if the input is between 2 and 15. If not, your program should keep asking the user to enter
an integer between 2 and 15 until a valid input is entered
- Ask the user to enter the specified number of integers
- Read those integers into an array
- Print the array before sorting
- Sort the array in decreasing order using bubble sort (assume that the last index of the array is
LAST):
o Find the largest element of the array (use the function written in part II)
o Swap the largest element with the element at position 0
o Find the largest element between position 1 and LAST
o Swap the largest element with the element at position 1
o Find the largest element between position 2 and LAST
o Swap the largest element with the element at position 2
o Repeat until all elements are sorted
- Print the array after sorting
Explanation / Answer
part 1 :
#include <stdio.h>
void function(int ar[],int size);
int largest(int ar[],int size,int index);
int main()
{
int ar[10],i = 0,index = 0,x = 0;
printf("Enter 10 numbers : ");
for(i=0;i<10;i++)
{
scanf(" %d",&ar[i]);
}
function(ar,10);
printf("Enter index : ");
scanf(" %d",&index);
x = largest(ar,10,index);
return (0);
}
void function(int ar[],int size)
{
int i = 0;
for(i=0;i<size;i++)
{
printf("%d ",ar[i]);
}
printf(" ");
}
int largest(int ar[],int size,int index)
{
int largest_element = -9999,i = 0,j = 0;
for(i=index;i<size;i++)
{
if(ar[i]>largest_element)
{
largest_element = ar[i];
}
}
return largest_element;
}
part 2 :
#include <stdio.h>
void function(int ar[],int size);
int largestPos(int ar[],int size,int index);
int main()
{
int ar[10],i = 0,index = 0,x = 0;
printf("Enter 10 numbers : ");
for(i=0;i<10;i++)
{
scanf(" %d",&ar[i]);
}
function(ar,10);
printf("Enter index : ");
scanf(" %d",&index);
x = ar[largestPos(ar,10,index)];
printf("Largest element between index : %d and end of the array is : %d ",index,x);
return (0);
}
void function(int ar[],int size)
{
int i = 0;
for(i=0;i<size;i++)
{
printf("%d ",ar[i]);
}
printf(" ");
}
int largestPos(int ar[],int size,int index)
{
int largest_element = -9999,i = 0,j = 0;
for(i=index;i<size;i++)
{
if(ar[i]>largest_element)
{
largest_element = ar[i];
}
}
for(i=index;i<size;i++)
{
if(largest_element == ar[i])
{
return(i);
}
}
}
part 3 :
#include <stdio.h>
void function(int ar[],int size);
int largestPos(int ar[],int size,int index);
int main()
{
int n = 2,i = 0,j = 0,index = 0,x = 0,y=0;
start:
printf("Enter no.of integers(2 - 15) : ");
scanf(" %d",&n);
if(n < 2 || n >15)
{
goto start;
}
int ar[n];
printf("Enter %d numbers : ",n);
for(i=0;i<n;i++)
{
scanf(" %d",&ar[i]);
}
function(ar,n);
for(i=0;i<n;i++)
{
x = largestPos(ar,n,i);
for(j=i;j<i+1;j++)
{
y = ar[x];
ar[x] = ar[j];
ar[j] = y;
}
}
function(ar,n);
return (0);
}
void function(int ar[],int size)
{
int i = 0;
for(i=0;i<size;i++)
{
printf("%d ",ar[i]);
}
printf(" ");
}
int largestPos(int ar[],int size,int index)
{
int largest_element = -9999,i = 0,j = 0;
for(i=index;i<size;i++)
{
if(ar[i]>largest_element)
{
largest_element = ar[i];
}
}
for(i=index;i<size;i++)
{
if(largest_element == ar[i])
{
return(i);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.