Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need a program that reads in a list of positive integers in the range 1 to 9 fro

ID: 3621884 • Letter: N

Question

Need a program that reads in a list of positive integers in the range 1 to 9 from the keyboard into an array with base type int. Assume that not more than 50 integers will be read in, but you don’t know how many there will be.. (Have the user enter a 0 when he is done; do not store the 0 in the array)

Program should determine how many entries are actually entered (not counting the 0) as it reads them in, Use a function called GET_DATA that is passed the array to read in the numbers and return the number of nonzero entries that were read in. (Have it either return the number of nonzero values that were read in directly or via a call by reference parameter- your choice) .

DO NOT use global variables…

Main should be actually declaring the array and calling GET_DATA.

Program should then sort the array in ascending order. Use a function to do this, call the function SORT…have main call this function

(Use functions to accomplish this - hint use the code in the class notes or textbook, but make sure you understand how it works). 

Your program should then print out the sorted array with say 10 entries per line. (Again use a separate  function here, called PRINT_SORTED_ARRAY..(havemain call this function)

 Finally your program should print out a table consisting of two columns.

The first is a list of distinct numbers found and the second is how many times each number occurred in the array. (Write a  function called DISPLAY to accomplish this..again have main call this function)

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 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"<<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)&&(index<size))

{

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 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;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;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote