0

I've spent the past 5 hours trying too write this program, got halfway and realised I cant use arrays or functions, strings etc.

The problem is i need too create a program that reads integer values terminated by a sentinel value and displays a bar chart of single digit numbers(0-9). The chart shows the total number of occurrences for each number in the sequence. Here is what i have so far

EDIT: THIS IS MY REVISED CODE

#include <iostream>
using namespace std;

int main()
{
   //initilise variables
    int num = 0, count0 = 0, count1 = 0,count2 = 0,count3 = 0,count4 = 0, 
count5 =0, count6 = 0,count7 = 0,count8 = 0,count9 = 0, value;
int SENTINEL = -1;

//get numbers till a -1 values has been entered
cout << "Please enter a number: ";
while(num != SENTINEL)
{
    cin >> num;

    //adds each occurrence of number entered
    switch(num)
{
    case 0:
        count0+= 1;
        break;
    case 1:
        count1+= 1;
        break;
    case 2: 
        count2+=1;
        break;
    case 3:
        count3+= 1;
        break;
    case 4:
        count4+= 1;
        break;
    case 5: 
        count5+= 1;
        break;
    case 6:
        count6+= 1;
        break;
    case 7:
        count7+= 1;
        break;
    case 8: 
        count8+= 1;
        break;
    case 9:
        count9+= 1;
        break;
    default:;
}
}

for(int rows = 0; rows < 10; rows++)
{
    cout << endl << rows << " | ";
    for(int c = 0; c < count0; c++)
    {
        {
            cout << "*" ;

        }
    }

}

EDIT:

Im having trouble with getting the for loop for my columns too output the * on just a single row. Any idea what i should change too allow that? So far, if i input 3 zero's(count0=3) it will display 3 "*", but on all 9 rows.

3
  • why shouldn't you use arrays or functions? If that's the case, you have a big problem there: #include <iostream> includes lots of functions, classes, tempaltes and other C++ stuff. Your question title reads liek "how do i make a table without saws, drills and wood planes?" Commented Apr 19, 2013 at 7:28
  • @ArneMertz yeah i know that, i think thats what makes this question hard. I asked my teacher, who said we can only use looping and conditionals, if arrays are used, he wont accept it :(. Commented Apr 19, 2013 at 7:36
  • Be careful, you use num before it it initialized in while(num != SENTINEL). Its value is random if you don't init it. Commented Apr 19, 2013 at 7:58

2 Answers 2

1

What you could do:

  1. Write the program with functions and arrays.
  2. Erase the functions by putting their code wherever they are called, remember to set the arguments correctly.
  3. Replace the arrays with N variables and their access with a huge switch/case:

     //arr[i] = 42;
     switch (i) {
     case 0: arr_0 = 42; break;
     case 1: arr_1 = 42; break;
     //...
     }
    

You could facilitate both by using macros, e.g. use Boost.Preprocessor to repeat the cases from 0 to N

What you should do: Say no. Tell your teacher that it's bullshit to not use the language's most basic features. But maybe wait with that until after he accepted your solution ;)

Sign up to request clarification or add additional context in comments.

2 Comments

do you know how too stop values being input into the array with a sentinel value, such as -1?
you have that part right with your while loop. (but read the comments on the question - you have to initialize num)
0

If you only need to count single digit numbers, then one way to go is to use 10 variables =) int count0, count1... etc... then you have almost array so, you can count input and then output counted values..

4 Comments

this wouldnt work if the numbers were not in an ordered sequence i dont think? say if the sequnce was 1 1 3 4 1 5 1 -1. would i test each varaible too see if it held a 1, then if it did say for each add them together too output 3 *?
why? you read a number from an input stream and then do something straight forward like switch(value) { case 0: count0++; break case 1: count1++; break; etc... . Looks ugly but if I understood task right will do the job.
so far i put the switch statement into the while loop, or should i place it outside?
you should put it in the loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.