Question

In: Computer Science

8.3) Design and implement an application that creates a histogram that allows you to visually inspect...

8.3)

Design and implement an application that creates a histogram that allows you to visually inspect the frequency distribution of a set of values. The program should read in an arbitrary number of integers that are in the range 1 to 100 inclusive; then produce a chart similar to the one below that indicates how many input values fell in the range 1 to 10, 11 to 20, and so on. Print one asterisk for each value entered.

1 - 10 | *****

11 - 20 | **

21 - 30 | *******************

31 - 40 |

41 - 50 | *** 5

1 - 60 | ********

61 - 70 | **

71 - 80 | *****

81 - 90 | *******

91 - 100 | *********

8.4)

The lines in the histogram in PP 8.3 will be too long if a large number of values is entered. Modify the program so that it prints an asterisk for every five values in each category. Ignore leftovers. For example, if a category had 17 values, print three asterisks in that row. If a category had 4 values, do not print any asterisks in that row.

Please write the code simply, without using data structures.

Solutions

Expert Solution

/**********************************************/
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
   // Declaringh constant
const int SIZE = 10;
  
// Creating an array
int freq[SIZE] = { 0 };

// Declaring variables
int n, num;

// Declaring variables
cout << "How many no of integers you want to enter ?";
cin >> n;

// Creating an array
int nos[n];
  
for(int i=0;i<n;i++)
{
   nos[i]=rand()%(100) + 1;
   }

for (int i = 0; i < n; i++)
{
switch (nos[i])
{
case 91 ... 100:

freq[9]++;
break;
case 81 ... 90:
freq[8]++;
break;
case 71 ... 80:
freq[7]++;
break;
case 61 ... 70:
freq[6]++;
break;
case 51 ... 60:
freq[5]++;
break;
case 41 ... 50:
freq[4]++;
break;
case 31 ... 40:
freq[3]++;
break;
case 21 ... 30:
freq[2]++;
break;
case 11 ... 20:
freq[1]++;
break;
case 1 ... 10:
freq[0]++;
break;
}
}

// Displaying the frequency distrubution
for (int i = 0; i < SIZE; i++)
{
cout << ((i * 10) + 1) << " - " << ((i * 10) + 10) << "|";
for (int j = 0; j < freq[i]; j++)
{
cout << "*";
}
cout << endl;
}
return 0;
}

/**********************************************/

/**********************************************/

Output:

/**********************************************/


#include <iostream>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
   // Declaringh constant
const int SIZE = 10;
  
// Creating an array
int freq[SIZE] = { 0 };

// Declaring variables
int n, num;

// Declaring variables
cout << "How many no of integers you want to enter ?";
cin >> n;

// Creating an array
int nos[n];
  
for(int i=0;i<n;i++)
{
   nos[i]=rand()%(100) + 1;
   }

for (int i = 0; i < n; i++)
{
switch (nos[i])
{
case 91 ... 100:

freq[9]++;
break;
case 81 ... 90:
freq[8]++;
break;
case 71 ... 80:
freq[7]++;
break;
case 61 ... 70:
freq[6]++;
break;
case 51 ... 60:
freq[5]++;
break;
case 41 ... 50:
freq[4]++;
break;
case 31 ... 40:
freq[3]++;
break;
case 21 ... 30:
freq[2]++;
break;
case 11 ... 20:
freq[1]++;
break;
case 1 ... 10:
freq[0]++;
break;
}
}

// Displaying the frequency distrubution
for (int i = 0; i < SIZE; i++)
{
cout << ((i * 10) + 1) << " - " << ((i * 10) + 10) << "|";
for (int j = 0; j < freq[i]/5; j++)
{
cout << "*";
}
cout << endl;
}
return 0;
}

/**********************************************/

/**********************************************/

Output:

/**********************************************/


Related Solutions

Java - Design and implement an application that creates a histogram that allows you to visually...
Java - Design and implement an application that creates a histogram that allows you to visually inspect the frequency distribution of a set of values. The program should read in an arbitrary number of integers that are in the range 1 to 100 inclusive; then produce a chart similar to the one below that indicates how many input values fell in the range 1 to 10, 11 to 20, and so on. Print one asterisk for each value entered. Sample...
Design and implement a Java program that creates a GUI that will allow a customer to...
Design and implement a Java program that creates a GUI that will allow a customer to order pizza and other items from a Pizza Paarlor. The customer should be able to order a variety of items which are listed below. The GUI should allow the customer (viaJavaFX UI Controls - text areas, buttons, checkbox, radio button, etc.) to input the following information: Name of the customer First Name Last Name Phone number of the customer Type of food being order...
Implement a Composite Design Pattern for the code below that creates a family tree MAIN: public...
Implement a Composite Design Pattern for the code below that creates a family tree MAIN: public class Main { public static void main(String[] args) { /* Let's create a family tree (for instance like one used on genealogy sites). For the sake of simplicity, assume an individual can have at most two children. If an individual has 1-2 children, they are considered a "tree". If an individual does not have children, they are considered a "person". With that in mind,...
Design and implement an application that reads an integer value and prints the sum of all...
Design and implement an application that reads an integer value and prints the sum of all even integers between 2 and the input value, inclusive. Print an error message if the input value is less than 2 and prompt accordingly so that the user can enter the right number. Your program file will be called YourLastNameExamQ2
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t use vectors), and provide an implementation for the following operations on books in the array 1)isEmpty() returns true if the array is empty, otherwise false 2)isFull() returns true if the array is full, otherwise false 3)listSize() prints the number of books in the array 4)print() prints the content of the array 5)insert(Book) asks the user to enter new book info, and it adds the...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t use vectors), and provide an implementation for the following operations on books in the array 1)isEmpty() returns true if the array is empty, otherwise false 2)isFull() returns true if the array is full, otherwise false 3)listSize() prints the number of books in the array 4)print() prints the content of the array 5)insert(Book) asks the user to enter new book info, and it adds the...
Design and implement an application that can compute the weekly pay for different students at a college.
In Java Design and implement an application that can compute the weekly pay for different students at a college. Students (all with a name, major, GPA) can be undergraduate or graduate students. Undergraduate students can be volunteers to be tuto rs or teaching assistants. Graduate students can be teaching assistants or research assistants. Volunteer tuto rs are not paid anything. Undergraduate teaching assistants are paid $15 per hour and can work a maximum of 20 hours per week. Graduate teaching assistants...
programing language JAVA: Design and implement an application that reads a sentence from the user, then...
programing language JAVA: Design and implement an application that reads a sentence from the user, then counts all the vowels(a, e, i, o, u) in the entire sentence, and prints the number of vowels in the sentence. vowels may be upercase
A, B:   Design and Implement a C# windows form application to ask the user for 10...
A, B:   Design and Implement a C# windows form application to ask the user for 10 integer numbers, sort them in ascending order and display the sorted list. Use bubble sort technique to sort the array elements and do not use any built-in sort method to sort the array elements.                                                        [02] C:    Test and evaluate your program by inputting variety of values.
A, B:    Design and Implement a C# windows form application to encrypt and decrypt text....
A, B:    Design and Implement a C# windows form application to encrypt and decrypt text. The application use to receive a string and display another encrypted string. The application also decrypt the encrypted string. The approach for encryption/decryption is simple one i.e. to encrypt we will add 1 to each character, so that "hello" would become "ifmmp", and to decrypt we would subtract 1 from each character.    C:   Test and evaluate application by applying different strings.      ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT