1. )Complete the function so that elements in the integer array values are sorte
ID: 3540496 • Letter: 1
Question
1. )Complete the function so that elements in the integer array values are sorted from lowest to highest. void sortArray(int values[], int size) { //your code here return; } 2.) Write the C++ code for the following: a. Declare an array of type int, length 5, called numbers b. Fill the array with the following data: 19, 56, 24, 0, 2 c. Sort the array in descending order
3. )Assuming the following array is declared: int numbers[] = {1, 2, 3, 4, 5}; What is the output of cout << numbers <<endl; why?
Explanation / Answer
1)void sortArray(int values[10],int size)
{
int temp,i,j;
for(i=0;i<size-1;i++)
{
for(j=i+1;j<size;j++)
if(values[i] > values[j])
{
temp = values[j];
values[j] = values[i];
values[i] = temp;
}
}
}
2) a)int numbers[5];
b)int numbers[ ] = {19,56,24,0,2};
c)#include<iostream.h>
#include<conio.h>
void main()
{
int i,j,temp;
int numbers[ ] = {19,56,24,0,2};
for(i=0;i<4;i++)
{
for(j=i+1;j<5;j++)
if(a[i] > a[j])
{
temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
cout<<"After sorting we get : "<<endl;
for(i=0;i<5;i++)
{
cout<<a[i]<<endl;
}
getch();
}
3) Output for this question is some garbage value, because we doesnot identify any array element , we declare an array of 5 elements but we try to display only "numbers" we must have to identify any array here.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.