Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

How to print vertical lines in a graph in C++

Name: pieisgood 2007-08-16 2:42 ID:zUDRSrwb

ok... so I have been working on a stupid program that takes in characters one at a time and then counts then and then prints out how many characters there are and a graph.. but I don't know how to make my graph vertical... can any one /prog/ help a fool like me?

source:

#include <stdio.h>
#include <iostream>
#include <cctype>
using namespace std;



int main(int argc, char *argv[])
{
 int cols;
 int max = 80;
 int c;
 int y;
 int k;

 int count[26];

 for (int x = 0; x < 26; x++)
 count[x] = 0;

          while (! cin.eof())
          {
           if( isalpha ( c = tolower( cin.get() ) ) )


              count [ c - 'a']++ ;  // for everytime a certain value appears add 1 to that values number and when it shows up again add another 1 = 2





           }


                 for (int i = 0; i < 26; i++)
                 printf("[%c] = %d\n" , i+'a' , count[i]);



                   for (int x=0; x<26; x++)
             {

                if (count[x] > 1)
                {


                    cols =  max * count[x] / count[25] ;


                  for (int y=0; y < cols; y++)

                           cout.put('-') ;
                         cout.put ('\n');
                        }
                    }





  return 0;

}

Name: Anonymous 2007-08-16 3:02 ID:jf2er3YH

That's some expert spacing you've got there.

Name: Anonymous 2007-08-16 3:16 ID:zUDRSrwb

fuck you nigger

Name: Anonymous 2007-08-16 3:32 ID:jf2er3YH

Do you kiss your mother with that mouth? You probably do.

Name: Anonymous 2007-08-16 3:36 ID:Heaven

>>1
Your indent is broken.

Name: Anonymous 2007-08-16 4:01 ID:Heaven

>>3
Enjoy your shit

Name: Anonymous 2007-08-16 4:15 ID:zUDRSrwb

>>6
I think I will elitist faggot.

Name: Anonymous 2007-08-16 4:21 ID:Heaven

>>6
I'm not elitist, your indentation sucks cock.

Name: Anonymous 2007-08-16 5:21 ID:zUDRSrwb

>>8
and that's why you won't help me?

Name: Anonymous 2007-08-16 5:34 ID:qW9Z5YD6

>>9
It's ok to say that maybe programming isn't right for you.  Why not try building model aeroplanes instead?  That's challenging too.

Name: Anonymous 2007-08-16 5:41 ID:qW9Z5YD6

>>9
It is how you're not helping yourself.

Name: Anonymous 2007-08-16 6:48 ID:CoVYDg/2

Try implementing this algorithm:

let maxcount = the maximum value contained in your count[] array
let graphheight = the desired height of your graph
let blockratio = maxcount / graphheight
loop counter y from graphheight down to 1
   loop counter x from 0 to 25
      if count[x] / blockratio is less than or equal to y
         then print bar chart character
         else print a space
   end loop x
   start a new line
end loop y
start a new line
print all characters from a to z

Name: Anonymous 2007-08-16 7:01 ID:vBs7SIl9

>>1
man ncurses

Name: Anonymous 2007-08-16 7:21 ID:XncurV27

>>12
Add the forced indentation of code, and you almost have Python code.

Name: Anonymous 2007-08-16 7:58 ID:Heaven

/usr/dict/words produces a nice graph:

    #
    #
    #
    #
    #
    #
    #   #
#   #   #         #
#   #   #    #   ##
#   #   #    #   ###
#   #   #    ##  ###
#   #   #  # ##  ###
#   #   #  # ##  ###
# # #   #  # ##  ###
# ###   #  # ##  ###
# ###   #  # ##  ####
# ### # #  ##### ####
##### ###  ##### ####
#########  ##### ####   #
######### ###### ###### #
abcdefghijklmnopqrstuvwxyz

Name: Anonymous 2007-08-16 8:07 ID:CoVYDg/2

    █
    █
    █
    █
    █   █
█   █   █         █
█   █   █    █   ██
█   █   █    █   ███
█   █   █    ██  ███
█   █   █  █ ██  ███
█ █ █   █  █ ██  ███
█ ███   █  █ ██  ████
█ ███ █ █  █████ ████
█████ ███  █████ ████   █
█████████ ██████ ██████ █
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
abcdefghijklmnopqrstuvwxyz

Name: Anonymous 2007-08-16 8:08 ID:CoVYDg/2

    █
    █
    █
    █
    █   █
█   █   █         █
█   █   █    █   ███
█   █   █    ██  ███
█   █   █  █ ██  ███
█   █   █  █ ██  ███
█ ███   █  █ ██  ███
█ ███ █ █  █████ ████
█████ ███  █████ ████
█████████ ██████ ██████ █
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
abcdefghijklmnopqrstuvwxyz

Name: Anonymous 2007-08-16 8:30 ID:cNnPpPGu

Fuk yur stupid unreadable C++ shit

(let ((freq (make-array 256 :initial-element 0)))
  (with-open-file (s "/usr/share/dict/words")
    (loop (handler-case (map nil (lambda (c) (incf (aref freq (char-code c)))) (read-line s))
        (error (_) (return)))))
  ((lambda (mmx) (map-into freq (lambda (x) (floor (* x 15) mmx)) freq)) (loop for x across freq maximizing x))
  (loop for y from 0 to 15 do
    (loop for x from 0 to 26 do (write-char (if (< (aref freq (+ (char-code #\a) x)) (- 16 y)) #\  #\#))) (terpri)) (format t "abcdefghijklmopqrstuvwxyz~%"))


use lisp!

Name: Anonymous 2007-08-16 8:31 ID:cNnPpPGu

CL-USER> (let ((freq (make-array 256 :initial-element 0)))
              (with-open-file (s "/usr/share/dict/words")
            (loop (handler-case (map nil (lambda (c) (incf (aref freq (char-code c)))) (read-line s))
                (error (_) (return)))))
              ((lambda (mmx) (map-into freq (lambda (x) (floor (* x 15) mmx)) freq)) (loop for x across freq maximizing x))
              (loop for y from 0 to 15 do
               (loop for x from 0 to 26 do (write-char (if (< (aref freq (+ (char-code #\a) x)) (- 16 y)) #\  #\#))) (terpri)) (format t "abcdefghijklmopqrstuvwxyz~%"))
; in: LAMBDA NIL
;     HANDLER-CASE
; --> BLOCK COMMON-LISP:LET TAGBODY RETURN-FROM
; ==>
;   (COMMON-LISP:LET ((_ #:G12))
;     (RETURN))
;
; caught STYLE-WARNING:
;   The variable _ is defined but never used.
;
; compilation unit finished
;   caught 1 STYLE-WARNING condition
                          
    #                     
    #                     
    #                     
#   #   #                 
#   #   #                 
#   #   #    ##  #        
#   #   #    ##  # #      
#   #   #  # ##  ###      
#   #   #  # ##  ###      
# # #   #  # ##  ###      
# # #   #  # ##  ####     
# ###  ##  ##### ####     
# ###  ##  ##### ####   # 
##### ###  ##### ####   # 
#########  ##### #####  # 
abcdefghijklmopqrstuvwxyz
NIL

Name: Anonymous 2007-08-16 8:39 ID:Heaven


                  #
#                 #
#                 #
#            #    #
#            #    #
#   #   #  # #   ##
#   #   #  # #   ##
#   #   #  # #   ##
#   #   #  # ##  ##
#  ##   #  ####  ###
# ### ###  ####  ###
##### ###  ##### ####
abcdefghijklmopqrstuvwxyz
nil

FUCK YEAH CLISP

Name: Anonymous 2007-08-16 8:40 ID:Heaven

// FUCK YOU LISP CRAP

#include <iostream>
#include <algorithm>

using namespace std;

#define DEFAULT_GRAPH_HEIGHT 20

int main(int argc, char *argv[])
{
        int graph_height = 0;

        if (argc>1)
        graph_height = atoi(argv[1]);

        if (graph_height<=0)
                graph_height = DEFAULT_GRAPH_HEIGHT;

        int count[26] = {0};
        int c;

        while(!cin.eof())
                if (isalpha(c = tolower(cin.get())))
                        count[c-'a']++;

        int max_count = 0;

        for(int i=0;i<26;i++)
                max_count = max(max_count, count[i]);

        int bar_ratio = max_count / graph_height;

        for(int y=graph_height;y>0;y--)
        {
                for(int x=0;x<26;x++)
        {
                        if ((count[x]/bar_ratio)>=y)
                                cout.put('#');
                        else    cout.put(' ');
        }
                cout.put('\n');
        }

        for(c='a';c<='z';c++)
                cout.put(c);

        cout.put('\n');

        return 0;
}

Name: Anonymous 2007-08-16 8:49 ID:Heaven


        while(!cin.eof())
                if (isalpha(c = tolower(cin.get())))
                        count[c-'a']++;

god the stupidity.
SERIOUSLY FUCKING FIXED:

while(c = cin.get)
if(isalpha(c)) count[tolower(c)-'a']++;

Name: Anonymous 2007-08-16 8:50 ID:HW6/JAEU

>>22
WHAT?!
YOU FAGS CONFUSED ME FUCK
I MENT
while((c = cin.get()) != EOF)
FUCKING CHRIST

Name: Anonymous 2007-08-16 8:50 ID:Heaven

AND I FORGOT MY FUCKIGN SAGE

Name: Anonymous 2007-08-16 8:56 ID:CoVYDg/2

>>22
>>23
>>24

Chill out, moron

Name: Anonymous 2007-08-16 8:59 ID:Heaven

>>22
WHA THE FUCK IS GOING ON WITH ME TODAY


while((c = getc(stdin)) != EOF)
if(isalpha(c)) count[(c | 1 << 5) - 'a']++;

Name: Anonymous 2007-08-16 9:00 ID:Heaven

fAGGOTS

Name: Anonymous 2007-08-16 9:00 ID:Heaven

>>26

PROTIP: calling islower does not harm performance as much as you'd like to think

Name: Anonymous 2007-08-16 9:02 ID:PEqEhZL/

This thread is full of mental illness and failure.

Name: Anonymous 2007-08-16 9:05 ID:Heaven

>>28
i think you mean tolower.

Okay,
In earlier times, toupper was a function-like preprocessor macro and was defined to work only on lower-case letters; it misbehaved if applied to digits, punctuation, or letters which were already upper-case. Similarly, tolower worked only on upper-case letters. Therefore, old code (or code written for wide portability) tends to call islower before toupper, and isupper before tolower. The C Standard, however, says that toupper and tolower must work correctly on all characters, i.e. characters which don't need changing are left alone.

WHAT ABOUT COMPABILITY WITH OLDER SYSTEMS?!

and i prefer n | 1 << 5, im that hardcore.

Name: Anonymous 2007-08-16 9:14 ID:Heaven

>>30
Yes, that's what I meant. But you still fail it.

Name: Anonymous 2007-08-16 9:45 ID:Heaven

first of all look at the IMMENSE FAILURE of the C++ programmer
>>1,21,22,23,26

then look at the beautify, readability and expressiveness of the lisp solution
>>18

Conclusion: C++ IS WHAT BRAINDAMAGED PEOPLE USE TO PROGRAM

Name: Anonymous 2007-08-16 9:59 ID:Heaven

>>32
then look at the beautify, readability and expressiveness of the lisp solution
It looks like a trainwreck of parentheses and meaningless 3-letter lowercase tokens arranged in no particular order LOOP X CAR MAP % NIL FOR LAMBDA FOLDR LOOP CHAR FILE PENIS PENIS PENIS))))))))))))). I'm sure someone ELSE could have written it elegantly in lisp, but your solution looks like utter shit.

(the C++ one is worse though)

Name: Anonymous 2007-08-16 10:21 ID:cNnPpPGu

>>33
You're so fucking stupid it makes me almost want to explain the joke to you..

Name: Anonymous 2007-08-16 10:38 ID:Heaven

LISP is nothing to joke about

Name: Anonymous 2007-08-16 10:40 ID:cNnPpPGu

>>35
LISP is shit compared to Lisp.

Name: Anonymous 2007-08-16 10:45 ID:TERK82Hl

>>33
I was explaining Lisp to my friend the other day, and she got it. She actually thought it looked nice. I was flabbergasted.

Name: Anonymous 2007-08-16 11:14 ID:6LNm6CIN

<<37 'she', am i rite?

Name: Anonymous 2007-08-16 11:15 ID:Heaven

>>38
<<37
lern2quote. srly.

Name: Anonymous 2007-08-16 11:17 ID:Heaven

>>37
let me give a try

In Lisp, you wrap expressions with parentheses. Lisp is all about expressions, hence the parentheses. Expressions get evaluated. the end.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List