Hello! Im having some problems with my program. I need to display a graph, showing how many times a digit appears in the numbers of the sequence, for each possible digit.
This is my code so far:
#include <iostream>
#include <iomanip>
using namespace std;
//Adding function needed
//Read length of sequence
int read_length (int k)
{
do
{
cout << "Enter length of sequence: ";
cin >> k;
if (k < 0 || k > 300)
{
cout << "Invalid input, please try again!" << endl;
}
}
while (0 > k > 300);
return k;
}
//Read sequence of integers
int storeSeq(int seq[], int howMany)
{
cout << "Enter sequence: ";
for(int i = 0; i < howMany; i++)
{
cin >> seq[i]; //read and store value in the array
}
}
//Display sequence
void display_seq(int seq[], int howmany)
{
for(int i = 0; i < howmany; i++)
{
cout << seq[i] << " ";
int COL = 6;
if ( (i+1) % COL == 0 ) //is the 6th value on the line?
cout << endl; //change line
}
}
//Count the number of occurrences of each digit d stored in V
//V stores n digits
//C[d] stores the number of occurrences of digit d in array V
void count_occurences(int V[], int n, int C[])
{
for(int i = 0; i < n; i++)
{
C[V[i]]++; //count
}
}
//Display table
//C is an array with the frequency of each digit so, the number of slots of C is 10
//n is the size of the sequence of digits
void display_table(int C[], int n)
{
cout << "DIGITS:" << endl;
cout << "=======" << endl;//Display table header
for(int i = 0; i < n; i++)
{
cout << setw(4) << i << ":"; //Displaying digits 0-9
for(int k = 0; k < C[i]; k++)
{
cout << setw(8) << '*';
}
cout << endl;
}
}
int main ()
{
//Declare variables
int howmany = 0;
int k;
//1. Read length of sequence
howmany = read_length(k);
//2. Read & Display the sequence
int const SIZE = 300;
int const COLS = 6;
int seq[SIZE];
int sequence = storeSeq(seq, howmany);
//3. Display sequence
cout << "The sequence is: " << endl;
display_seq (seq, howmany);
cout << endl;
//Count the number of occurrences of each value in the sequence
int const SIZE2 = 10;
int counter[SIZE2] = {0};
count_occurences(seq, howmany, counter);
//Display table of probabilities
display_table(counter, howmany);
return 0;
}
Does anyone know what I can add to my code to display the graph correct?
#include <iostream>
#include <ctime>
constint SIZE = 10;
int main(){
srand(unsigned(time(NULL)));
int cnt[SIZE] = { 0 };
for (int i = 0; i < SIZE; i++){
//Fill array random values
cnt[i] = rand() % 10;
//Output line number
std::cout << std::endl << i << " ";
for (int k = 0; k < cnt[i]; k++){
//Output number stored in array as a number of *
std::cout << "*";
}
}
std::cout << std::endl;
return 0;
}