Tuesday, May 30, 2017

t9.c (done)

#include<stdio.h>
#include<math.h>

int main (void){
        int i, diff, xd, yd, hd, slope; //yd is useless
        int atZero = 0;
    int start = 0;
        scanf("%d %d %d", &xd, &yd, &hd);
        // calculates slope
        float halfxd = xd/2;
    float raw_slope = (halfxd/hd);
        if (raw_slope != floor(raw_slope)){
                if (slope <1){
                        // number of #s that start at 0
                        atZero = hd * (1 - raw_slope);
                        slope = 1;
                } else {
                       // new slope
                        slope = (int)floor(raw_slope);
                }
        } else {
        slope = raw_slope;
    }

    //calculates number of white spaces at top
    diff = hd - 1 - atZero;
    //printf("diff = %d; atZero = %d", diff, atZero);
    for (i = 0; i<diff; i++){
        start += slope;
    }

    //prints top row
    if (hd>1){
    for ( i=0; i<start; i++){
        printf(" ");
    }
    for (i=0; i<(xd-start*2); i++){
        printf("#");
    }
    printf("\n");
    start -= slope;
    }

        // prints each line
    if (hd>2){
        for (i=0; i<hd-2-atZero; i++){
                // prints the spaces at the beginning
                for (int s =0; s<(start); s++){
                        printf(" ");
                }
        printf("#");
                for (int s=0; s<(xd-2-(start)*2); s++){
                        printf(".");
                }
        printf("#\n");
                start-= slope;
    }
        if (atZero != 0){
                for( int k=0; k< atZero; k++ ){
                        printf("#");
                        // to print the "."
                        for (int j =0; j < xd-2; j++){
                 printf(".");
             }
                        printf("#\n");
                        i++;
                        }
                }
    }
        // prints last line
        for (i=0; i<xd; i++){
                printf("#");
        }
        printf("\n");
        return 0;
}

No comments:

Post a Comment