K&R xrcise 1-13

D

Davor Afrić

Hi guys, I'm a newb in C doing my lessons from the K&R book. I am
hoping somebody is willing to take a look at my code for 1-13.
It's a vertical histogram and code seems to work except that it seems
to run the y-axis
(numbering) all the way up in the sky which is of course completely
useless to me.
I would be gratefull if someone could point out the mistake I am not
seeing.
I'm sorry if it's a bother to you. Here is the code:

#include <stdio.h>
#define IN 1
#define OUT 0

int max_array(int array[], int num_elements); // prototype of a
function
int sum_array(int array[], int num_elements);

main()
{
int words[11];
int i, j, c=0, c_count=0, w_count=0, pos=OUT;
for(i = 0; i <= 10; ++i)
words = 0;

while ((c = getchar()) != EOF)
{
if(c==' ' || c=='\n' || c=='\t')
pos = OUT;
else if(pos == OUT)
pos = IN;
if(pos == IN)
++c_count;
else if(c_count <= 10)
{
++words[c_count];
++w_count;
c_count = 0;
}
else
{
++w_count;
c_count = 0;
}
}
w_count = w_count - (sum_array(words,11));

// draw the histogram
for(j = (max_array(words, 11)); j >= 1; --j)
{
printf(" %4d|", j);
for(i = 1; i < 11; ++i)
{
if(words >= j)
printf(" *");
else
printf(" ");
}
if(w_count >= j)
printf(" *");
else
printf(" ");
putchar ('\n');
}
for(i = 0; i < 51; ++i) printf("_");
putchar('\n');
printf(" ");
for(i = 1; i < 11; ++i) printf("%4d", i);
printf(" >10\n");
return 0;
}

// function to return the largest integer in an array
int max_array(int array[], int num_elements)
{
int i, max=-32000;
for(i = 0; i < num_elements; ++i)
{
if(array > max)
max = array;
}
return (max);
}

// function to return the sum of an array's elements
int sum_array(int array[], int num_elements)
{
int i, sum=0;
for(i = 0; i < num_elements; ++i)
sum = sum + array;
return (sum);
}
 
A

Alf P. Steinbach

Hi guys, I'm a newb in C doing my lessons from the K&R book. I am

C and C++ are two different languages.

You would be best helped by posting in a C language group.

Java programmers, C++ programmers and Python programmers may be able to
help you, but posting in a C group is superior for C language questions.


Cheers & hth.,

- Alf
 
D

dafrix

C and C++ are two different languages.

You would be best helped by posting in a C language group.

Java programmers, C++ programmers and Python programmers may be able to
help you, but posting in a C group is superior for C language questions.

Cheers & hth.,

- Alf

Thank you for pointing that out to me. I realised it just when I
clicked send :(
I'll repost there and see if I can delete this one :)
cheers!
 
J

Juha Nieminen

Davor Afri?? said:
Hi guys, I'm a newb in C doing my lessons from the K&R book.

No disrespect to K&R, but that book is a bit outdated (to say the least).
You might want to use a more modern book.

And, as someone said, C is not C++.
 
D

dafrix

  No disrespect to K&R, but that book is a bit outdated (to say the least).
You might want to use a more modern book.

  And, as someone said, C is not C++.

I've deleted the lenghty post with the code already. Can you, Juha,
suggest a more modern book as good as K&R's book. It does work for me
very well, you know. Thanks for the reply.

Davor
 
J

Jorgen Grahn

I've deleted the lenghty post with the code already.

Since you've mentioned it twice: cancelling postings on Usenet doesn't
work, and hasn't for at least a decade.
Can you, Juha,
suggest a more modern book as good as K&R's book. It does work for me
very well, you know. Thanks for the reply.

I think he was just referring to the fact that it (if you have the
latest edition) covers a more than twenty years old dialect of the
language. For book advice, ask on comp.lang.c.

/Jorgen
 
P

Peter Remmers

Am 11.02.2012 20:00, schrieb Jorgen Grahn:
I think he was just referring to the fact that it (if you have the
latest edition) covers a more than twenty years old dialect of the
language. For book advice, ask on comp.lang.c.

Well, at least it talks about function prototypes and doesn't use the
old syntax where funtion parameter types are declared between ) and {.
:)


Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top