D
dafrix
Hello there! I'm a newb to C and teaching myself from the K&R book,
going thru exercises. I was hoping somebody here would help me see the
mistake I'm missing in my code.
It seems to work, except that it runs the y-axis way up in the sky
for no reason, beyond the largest number being used in the program.
Sorry if this is an inapropriate post for usenet, I'm a newb here
also
Here's 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); // prototype
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);
}
going thru exercises. I was hoping somebody here would help me see the
mistake I'm missing in my code.
It seems to work, except that it runs the y-axis way up in the sky
for no reason, beyond the largest number being used in the program.
Sorry if this is an inapropriate post for usenet, I'm a newb here
also
#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); // prototype
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);
}