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

Write a program that reads in a list of POSITIVE integers in the range 1 to 9 fr

ID: 3625251 • Letter: W

Question

Write 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. You may 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)
Your 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.

Your 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..(have main 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)

You may incorporate more functions if you wish.

Sample run:

Enter at most 50 positive integers in the range 1..9, enter 0 when you are done.

1 4 2 3 6 7 4 6 2 3 7
2 3 4
3 7 2 3 0

Sorted array:
1 2 2 2 2 3 3 3 3 3
4 4 4 6 6 7 7 7


OUTPUT:
N Count
1 1
2 4
3 5
4 3
6 2
7 3

Explanation / Answer

please rate - thanks

#include <iostream>
using namespace std;
int GET_DATA(int[]);
void SORT(int,int[]);
void PRINT_SORTED_ARRAY(int,int[]);
void DISPLAY(int,int[]);
int GET_NUMBER();
int main()
{int n,data[50];
n=GET_DATA(data);
SORT(n,data);
PRINT_SORTED_ARRAY(n,data);
DISPLAY(n,data);
system("pause");
return 0;
}
int GET_DATA(int a[])
{int n=0,num;
num=GET_NUMBER();
while(num>0&&n<50)
   {
    a[n]=num;
    n++;
    if(n<50)
        num=GET_NUMBER();
    }
return n;

}
int GET_NUMBER()
{int n;
cout<<"Enter a number between 1 and 9 (0 to exit): ";
cin>>n;

while(n<0||n>9)
   {cout<<"Invalid entry ";
    cout<<"Enter a number between 1 and 9 (0 to exit): ";
    cin>>n;
    }
return n;  
}      
void SORT(int n,int a[])
{int i,j,t;
for(i=0;i<n-1;i++)
     for(j=i;j<n;j++)
        if(a[i]>a[j])
           {t=a[i];
           a[i]=a[j];
           a[j]=t;
           }
}
void PRINT_SORTED_ARRAY(int n,int a[])
{int i;
cout<<"Sorted array ";
for(i=0;i<n;i++)
    {cout<<a[i]<<" ";
     if((i+1)%10==0)
          cout<<endl;
     }
cout<<endl<<endl;;
}
void DISPLAY(int n,int a[])
{cout<<"OUTPUT N Count ";
int count,i,j;
for(i=1;i<=9;i++)
   {count=0;
   for(j=0;j<n;j++)
        if(a[j]==i)
            count++;
   if(count>0)
       cout<<i<<" "<<count<<endl;
}
}

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