Hi,
I have made an attempt at exercise 1.14 in K&R2: namely;
"Write a program to print a histogram of the frequencies of different
characters in its input."
Here is my program and the output.
#include <stdio.h>
#define HISTHEIGHT 400
int main()
{
int c, i, j, tot;
int nchars[26];
for (i = 0; i < 26; i++)
nchars[i] = 0;
tot = 0;
while ((c = getchar()) != EOF) {
if ((c - 'A') >= 0 && (c-'A') <= 26) {
nchars[c - 'A']++;
tot++;
}
else if ((c - 'a') >= 0 && (c - 'a' <=26)) {
nchars[c-'a']++;
tot++;
}
}
/* Turn into a value out of HISTHEIGHT so it will fit screen */
/* when printing character frequencies as a histogram */
for (i = 0; i < 26; i++)
nchars[i] = ( nchars[i] * HISTHEIGHT ) / tot;
for (i = HISTHEIGHT; i 0; i--) {
for (j = 0; j < 26; j++)
if (nchars[j] >= i)
printf("***");
else
printf(" ");
printf("\n");
}
printf(" A B C D E F G H I J K L M N O P Q R S T U V W X Y Z\n");
return 0;
}
*************** ************OUT PUT************ *********
[ilan@pillemer Ch1]$ ./a.out < /usr/share/dict/words
***
***
***
***
***
***
***
***
***
*** *** ***
*** *** ***
*** *** ***
*** *** ***
*** *** ***
*** *** ***
*** *** *** ****** ***
*** *** *** ****** ******
*** *** *** ****** ******
*** *** *** ****** *********
*** *** *** ****** *********
*** *** *** ****** *********
*** *** *** *** ****** *********
*** *** *** *** ****** *********
*** *** *** *** ****** *********
*** *** *** *** ****** *********
*** *** *** *** ****** *********
*** *** *** *** ****** *********
*** *** *** *** *** ****** *********
*** *** *** *** *** ****** *********
*** ********* *** *** ****** ************
*** ********* *** *** ****** ************
*** ********* *** *** ********* ************
*** ********* ****** *************** ************
*** ********* ****** *************** ************
*** ********* ********* *************** ************
*** ********* ********* *************** ************
*************** ********* *************** ************ ***
*************** ********* *************** ************ ***
*************** ********* *************** ************ ***
*************** ************ *************** ************ ***
*************** ************ *************** *** *************** *** ***
*************** ************ *************** *** *************** *** ***
*************** ************ *************** *** *************** ************
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
[ilan@pillemer Ch1]$ 3 1259
On 01 Apr 2007 08:03:04 +0200, ilan pillemer <il**@pillemer. net>
wrote:
>Hi,
I have made an attempt at exercise 1.14 in K&R2: namely;
"Write a program to print a histogram of the frequencies of different characters in its input."
Here is my program and the output.
#include <stdio.h> #define HISTHEIGHT 400
int main() {
int c, i, j, tot;
int nchars[26];
for (i = 0; i < 26; i++)
nchars[i] = 0;
tot = 0;
while ((c = getchar()) != EOF) {
if ((c - 'A') >= 0 && (c-'A') <= 26) {
A couple of issues:
Not all character sets have the alphabet in consecutive
characters. For example, EBCDIC used on IBM mainframes has seven
non-alphabetic characters between I and J.
When c-'A' is equal to 26, you attempt to increment nchars[26].
There is no such element of the array. The elements run from [0] to
[25] inclusive.
nchars[c - 'A']++;
tot++;
}
else if ((c - 'a') >= 0 && (c - 'a' <=26)) { nchars[c-'a']++; tot++;
}
}
/* Turn into a value out of HISTHEIGHT so it will fit screen */
/* when printing character frequencies as a histogram */
for (i = 0; i < 26; i++)
nchars[i] = ( nchars[i] * HISTHEIGHT ) / tot;
An interesting interpretation of the requirement. Every other attempt
I have seen produced actual counts, not relative frequencies.
>
for (i = HISTHEIGHT; i 0; i--) {
for (j = 0; j < 26; j++)
if (nchars[j] >= i) printf("***");
else printf(" ");
printf("\n");
}
printf(" A B C D E F G H I J K L M N O P Q R S T U V W X Y Z\n");
return 0; }
Remove del for email
ilan pillemer said:
Hi,
I have made an attempt at exercise 1.14 in K&R2: namely;
<snip>
while ((c = getchar()) != EOF) {
if ((c - 'A') >= 0 && (c-'A') <= 26) {
How does this program cope on IBM mainframes? Badly, I suspect.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at the above domain, - www.
"ilan pillemer" <il**@pillemer. netwrote in message
Hi,
I have made an attempt at exercise 1.14 in K&R2: namely;
"Write a program to print a histogram of the frequencies of different
characters in its input."
Here is my program and the output.
#include <stdio.h>
#define HISTHEIGHT 400
int main()
{
int c, i, j, tot;
int nchars[26];
for (i = 0; i < 26; i++)
nchars[i] = 0;
tot = 0;
while ((c = getchar()) != EOF) {
if ((c - 'A') >= 0 && (c-'A') <= 26) {
nchars[c - 'A']++;
tot++;
}
else if ((c - 'a') >= 0 && (c - 'a' <=26)) {
nchars[c-'a']++;
tot++;
}
}
/* Turn into a value out of HISTHEIGHT so it will fit screen */
/* when printing character frequencies as a histogram */
for (i = 0; i < 26; i++)
nchars[i] = ( nchars[i] * HISTHEIGHT ) / tot;
for (i = HISTHEIGHT; i 0; i--) {
for (j = 0; j < 26; j++)
if (nchars[j] >= i)
printf("***");
else
printf(" ");
printf("\n");
}
printf(" A B C D E F G H I J K L M N O P Q R S T U V
W X Y Z\n");
return 0;
}
*************** ************OUT PUT************ *********
[ilan@pillemer Ch1]$ ./a.out < /usr/share/dict/words
***
***
***
***
***
***
***
***
***
*** *** ***
*** *** ***
*** *** ***
*** *** ***
*** *** ***
*** *** ***
*** *** *** ****** ***
*** *** *** ****** ******
*** *** *** ****** ******
*** *** *** ****** *********
*** *** *** ****** *********
*** *** *** ****** *********
*** *** *** *** ****** *********
*** *** *** *** ****** *********
*** *** *** *** ****** *********
*** *** *** *** ****** *********
*** *** *** *** ****** *********
*** *** *** *** ****** *********
*** *** *** *** *** ****** *********
*** *** *** *** *** ****** *********
*** ********* *** *** ****** ************
*** ********* *** *** ****** ************
*** ********* *** *** ********* ************
*** ********* ****** *************** ************
*** ********* ****** *************** ************
*** ********* ********* *************** ************
*** ********* ********* *************** ************
*************** ********* *************** ************
***
*************** ********* *************** ************
***
*************** ********* *************** ************
***
*************** ************ *************** ************
***
*************** ************ *************** *** *************** ***
***
*************** ************ *************** *** *************** ***
***
*************** ************ *************** ***
*************** ************
A B C D E F G H I J K L M N O P Q R S T U V W X Y
Z
[ilan@pillemer Ch1]$
Good first attempt.
However as Richard Heathfield pointed out, this program assumes that A to Z
are contiguous in memory. On some machines that don't use ASCII this is not
the case.
So why not write a function
/*
Convert to character to index 0 - 25
Params: ch - character to convert
Returns: index, or -1 if not an upper case character.
*/
int chtoindex(char ch)
{
char *upper = "ABCDEFGHIJKLMN OPQRSTUVWYZ";
/* code here */
}
--
Free games and programming goodies. http://www.personal.leeds.ac.uk/~bgy1mm This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: David |
last post by:
this phrase has been mentioned many times in this NG and it seems like
everyone know what it's. is it a book? a standard? or a guide of some
sort? where can i read more about K&R2? thanks!
|
by: Chris Readle |
last post by:
Ok, I've just recently finished a beginning C class and now I'm working
through K&R2 (alongside the C99 standard) to *really* learn C.
So anyway, I'm working on an exercise in chapter one which give me
strange behavior. Here is the code I've written:
/******************************************************************************
* K&R2 Exercise 1-9
* Write a program to copy its input to its output, replacing strings
of blanks
* with a...
|
by: TTroy |
last post by:
I FOUND MAJOR ERRORS in K&R2 (making it almost useless for the herein
mentioned topics).
K&R2 Section 5.9 Pointers vs. Multidimension Arrays starts of like
this...
"Newcomers to C are somtimes confused about the difference between a
two-dimensional array and an array of pointers..."
then continues to explain int *b; to be...
|
by: Sunny |
last post by:
Hi,
Well this might look like a dumb question to 99.99% of u but what
exactly is K&R2 and where can I get it?
Secondly alot of people say that you can learn by reading good books on
C. I know the one by Dennis Ritchie is good but can any one mention
some other books as well
Thanx.
|
by: Josh Zenker |
last post by:
This is my attempt at exercise 1-10 in K&R2. The code looks sloppy to
me. Is there a more elegant way to do this?
#include <stdio.h>
/* copies input to output, printing */
/* series of blanks as a single one */
int main() {
int c;
| |
by: arnuld |
last post by:
there is a solution on "clc-wiki" for exercise 1.17 of K&R2:
http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_1:Exercise_17
i see this uses pointers whereas K&R2 have not discussed pointers yet.
i have created a solution myself by modifying the example programme of
section 1.19.
i tried to find the source-code of K&R2 using Google. i found the home
|
by: arnuld |
last post by:
i have created a solutions myself. it compiles without any trouble
and runs but it prints some strange characters. i am not able to find
where is the trouble.
--------------------------------- PROGRAMME
--------------------------------
/* K&R2 section 1.9
exercise 1.19
|
by: arnuld |
last post by:
this programme runs without any error but it does not do what i want
it to do:
------------- PROGRAMME --------------
/* K&R2, section 1.6 Arrays; Exercise 1-13.
STATEMENT:
Write a program to print a histogram of the lengths of words in its
input.
It is easy to draw the histogram with the bars horizontal; a vertical
|
by: Anthony Irwin |
last post by:
Hi all,
I have been going through the k&r2 book and all the examples are done
with main() instead of int main(void) and k&r2 in the start of chapter
1 does not return 0 so I haven't yet either.
I did two compiles shown below.
$ gcc -std=c89 -Wall exercise_1-9.c -o exercise_1-9
exercise_1-9.c:5: warning: return type defaults to `int'
|
by: arnuld |
last post by:
STATEMENT: Write the function strindex(s, t), which returns the position of the
rightmost occurence of t in s, or -1 if there is none.
PURPOSE: this program prints the index of the rightmost match on the line.
The match we have to find is a char array named pattern. We also print out
the number of matches we have found. We will take the input from
command-line.
PROBLEM: Segmentation Fault
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |