Need help with the following code: 1)Have user enter 0 to end the array, also do
ID: 3621916 • Letter: N
Question
Need help with the following code:
1)Have user enter 0 to end the array, also do not store 0 in the array
2)need to limit the POSITIVE integers in the range 1 to 9 from the keyboard into an array
3)program should then print out the sorted array with say 10 entries per line. (use a separate function here, called PRINT_SORTED_ARRAY)
4)also program is not returning the highest value in the table for counted numbers
#include
using namespace std;
void get_data(int a[], int size, int& number_used);
void sort(int a[], int number_used);
void swap_values(int& v1, int& v2);
int index_of_smallest(const int a[], int start_index, int number_used);
void display_count(const int a[], int number_used);
int main()
{
using namespace std;
int sample_array[10];
int number_used;
cout<<"Enter size of array";
cin>>number_used;
get_data(sample_array, 10, number_used);
sort(sample_array, number_used);
cout<<"Sorted Array"<
for (int index=0; index
cout<<< " ";
cout<<" ";
display_count(sample_array, number_used);
return 0;
}
void get_data(int a[], int size, int& number_used)
{
using namespace std;
cout<<"Enter at most 50 positive integers ranging from 1 to 9, when you are done enter 0:"<
int next;
int index = 0;
cin>>next;
while((next>=0)&&(index
{
a[index]=next;
index++;
cin>>next;
}
number_used = index;
}
void sort(int a[], int number_used)
{
int index_of_next_smallest;
for(int index = 0; index
{
index_of_next_smallest = index_of_smallest(a, index, number_used);
swap_values(a[index], a[index_of_next_smallest]);
}
}
void swap_values(int& v1, int& v2)
{
int temp;
temp = v1;
v1=v2;
v2=temp;
}
int index_of_smallest(const int a[], int start_index, int number_used)
{
int min = a[start_index];
int index_of_min = start_index;
for(int index= start_index+1; index
if(a[index]
{
min = a[index];
index_of_min = index;
}
return index_of_min;
}
void display_count(const int a[], int number_used)
{
cout<<"N Count"<
int x=0;
for(int i=0;i
{
int count;
if(i==0)
count=0;
else
count=1;
while(a[x]==a[i])
{
count++;
i++;
}
cout<<<" "<<
x=i;
}
}
Explanation / Answer
#include <iostream>
using namespace std;
void get_data(int a[], int size, int& number_used);
void sort(int a[], int number_used);
void swap_values(int& v1, int& v2);
int index_of_smallest(const int a[], int start_index, int number_used);
void print_array(int a[], int number_used);
void display_count(const int a[], int number_used);
int main()
{
using namespace std;
int sample_array[10];
int number_used;
cout<<"Enter size of array";
cin>>number_used;
get_data(sample_array, 10, number_used);
sort(sample_array, number_used);
print_array(sample_array, number_used);
/*cout<<"Sorted Array"<<endl;
for (int index=0; index<number_used; index++)
cout<<sample_array[index]<< " ";
cout<<" ";*/
display_count(sample_array, number_used);
return 0;
}
void get_data(int a[], int size, int& number_used)
{
using namespace std;
cout<<"Enter at most 50 positive integers ranging from 1 to 9, when you are done enter 0:"<<endl;
int next;
int index = 0;
cin>>next;
while(next!=0 && next<=9)
{
a[index]=next;
index++;
cin>>next;
}
number_used = index;
}
void sort(int a[], int number_used)
{
int index_of_next_smallest;
for(int index = 0; index<number_used-1;index++)
{
index_of_next_smallest = index_of_smallest(a, index, number_used);
swap_values(a[index], a[index_of_next_smallest]);
}
}
void print_array(int a[], int size)
{
cout<<" Sorted Array"<<endl;
for (int index=0; index<size; index++)
cout<<a[index]<< " ";
cout<<" ";
}
void swap_values(int& v1, int& v2)
{
int temp;
temp = v1;
v1=v2;
v2=temp;
}
int index_of_smallest(const int a[], int start_index, int number_used)
{
int min = a[start_index];
int index_of_min = start_index;
for(int index= start_index+1; index<number_used; index++)
if(a[index]<min)
{
min = a[index];
index_of_min = index;
}
return index_of_min;
}
void display_count(const int a[], int number_used)
{
cout<<"N Count"<<endl;
int x=0;
for(int i=0;i<number_used+1;i++)
{
int count;
if(i==0)
count=0;
else
count=1;
while(a[x]==a[i])
{
count++;
i++;
}
cout<<a[x]<<" "<<count<<endl;
x=i;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.