Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- int max(int values[], int N) {
- int i, max = values[0];
- for(i=1; i<N; i++) {
- if (max < values[i]) max = values[i];
- }
- return max;
- }
- void histogram(char * result,int * values, int numValues) {
- int i, j, h;
- h = max(values,numValues);
- strcpy(result,"");
- for (i=0; i<(h+2); i++) {
- if (i==0 || i==h+1) {
- for (j=0; j<(numValues+2); j++){
- strcat(result,"*");
- }
- strcat(result,"\n");
- }
- else
- for (j=0; j<(numValues+3); j++){
- if (j==0 || j==numValues+1) strcat(result,"*");
- else if (j==numValues+2) strcat(result,"\n");
- else {
- if (values[j-1] >= h-(i-1)) strcat(result,"X");
- else strcat(result," ");
- }
- }
- }
- }
- int main()
- {
- int values1[10] = {1,0,3,1,2,4,5,6,2,2};
- int values2[3] = {1,0,1};
- int values3[6] = {3,4,1,0,2,3};
- char * formatted;
- char * example = "*****\n*X X*\n*****";
- histogram(formatted,values1, 10);
- printf("%s\n\n",formatted);
- histogram(formatted,values2,3);
- printf("%s\n",formatted);
- if (strcmp(example,formatted) == 0) printf("This matches EXACTLY and is correct");
- histogram(formatted,values3,6);
- printf("%s\n",formatted);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement