tags:

views:

232

answers:

3

i have written a program that prints out the integers in the same order as the input with 5 numbers per line. That is, the first 5 integers will be printed in the first line; the next 5 integers in the next line; andso on. and also i was trying to print out the numbers in a bar chart formatt:like

81-105 ( 1) x
56-80 ( 5) xxxxx
6-11(5) xxxxx
-1-5 (3) xxx

my program:

cntr=0;
 while (fscanf(in, "%d", &a[i]) != EOF)
   {i++;

 fprintf(out, "%d-%d (%d) %s\n", A, B, k, x, cntr);
 fprintf(out, "%d\n", k, cntr);

    fprintf(out, "x", a[i]);
    i++;
   }

   fprintf(out, "1864-2336 (%d)%s\n", k, x);
   fprintf(out, "1391-1863 (%d)%s\n", k, x);
   fprintf(out, "918-1390 (%d)%s\n", k, x);
   fprintf(out, "445-917 (%d)%s\n", k, x);
   fprintf(out,"-28-444 (%d)%s\n", k, x);
    fclose(in);
    fclose(out);
return 0;
}

enter code here
A: 

just do a loop to print the amount of x's you need

   fprintf(out, "1864-2336 (%d)%s\n", k, x);
   for(x=k;x>0;x--)
      fprint(out,"%s","x");
   fprintf(out,"\n");

Cheers!

/B2S

Born2Smile
i have tried by using a loop to print the numbers of x's that i want to print. however, it kept giving me an error saying core dumped. can anybody help me how to figure out how to print the intigers in a bar chart format: i have my input numbers in my input file and i want to print them in five range (intervals), and in that range i want to have counters to count the number of integers that falls into that range by assigning K, where k is the number of intigers that falls into that range; and also i want to print "x's" for K ineach interval.
in otherway: A-B(K)xxx , A and B are the range , and k is thenumbers of integrs that falls into that range.my program: fprintf(out,"\n"); fprintf(out, "%d", cntr); // fprintf(out, "1864-2336 (%d)%s\n", k, "x"); // A=1864; //B=2336; //k=5; for(x=k;x>0;x--) for(A = 2336; i <= 1864; i--) fprintf(out,"%d-%d (%d)%s", A, B, k, x); }
If you edit your question, when you add source code it is easier to read...In the code in your comment, you didn't initialize i, so the loop will go all wrong. Also you made the condition i<=number, that is bad when you are counting down. If you count down, you want i>=number. Otherwise you should count up (i++).
Born2Smile
A: 

There are many ways to do this

It seems you already have somewhere declared an array of the values that user enters. a[]

In order to calculate the bar chart you could after user has entered all values, sort that array (e.g. using qsort()) then traverse the array checking the number of same values that occur in the array - this would be the number of '*' to print out

I think you should first let user enter the values, then do the output.

The code you show looks wrong, e.g. you increment 'i' twice in the while loop. You use the a[i] after the increment so it will not have the read value.

Also its good to use fgets/atoi instead when reading numbers that way invalid numbers will not cause your program to potentially crash.

Anders K.
i wrote my code using an array for my interval range however; i am not getting the intervals, it is giving me the array it self: fprintf(out, "\n"); d = ((A)-(B))/C; previous = A; left[5]= B; for(i=0; i<5; i++){ right[i]= previous; if(i!=5) left[i]= previous-d; previous= left[i]-1; }
A: 

You don't have to use a loop to print out your x's. You can declare a single string of x's that's as long as the maximum you will need, and then use a size specification in the printf call to control how many are printed. Here's a simple example to illustrate what I mean:

#include <stdio.h>
#include <string.h>

int main(void)
{
  char maxBar[] = "xxxxxxxxxx";
  size_t i;

  for (i = 0; i < strlen(maxBar); i++)
  {
    printf("%lu: %-*.*s\n", (unsigned long) i, strlen(maxBar), i, maxBar); 
  }
  return 0;
}

The output should look as follows:

0: __________          
1: x_________
2: xx________
3: xxx_______
4: xxxx______
5: xxxxx_____

etc., where _ represents a space character.

A * in a printf conversion specifier indicates that a field width or precision specification is being passed as an argument to printf. Writing

printf("%-*.*s\n", 10, 2, "test");

is the same as writing

printf("%-10.2s\n", "test");

which, reading left to right, means

  1. -: Left-justify the output
  2. 10: Minimum field width is 10 characters
  3. .2: Precision (max number of chars printed) is 2

So the output looks like

te________

where _ represents a space character.

So, assuming you know how big your bar needs to be in advance, you can write something like

for (i = 0; i < k; i++)
{
  printf("%d-%d (%d) %*.*s\n", lo[k], hi[k], ct[k], strlen(maxBar), ct[k], maxBar);
}
John Bode