Showing posts with label Lab 1. Show all posts
Showing posts with label Lab 1. Show all posts

Wednesday, May 31, 2017

t6.c (done)

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

int main (void)
{
    int result = 0;
    float i;
    float smallest =0, largest=0, total=0;
    int eachNewline = 0;
    while (result != EOF)
    {
        result = scanf ("%f",&i);
        if(smallest ==0 && largest == 0 && total == 0){
                smallest = i;
                largest = i;
        }
        if (result == EOF){ break;}
        if (i<smallest){
            smallest = i;
        } else if (i>largest){
            largest = i;
        }
        total = i + total;
        eachNewline++;
    }
    printf("%.2f %.2f %.2f\n", smallest, largest, (total/eachNewline));
    return 0;
}





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;
}

Saturday, May 27, 2017

Lab 1 Task 8 done

#include<stdio.h>

int main (void)
{
    int a[4], xd, yd, height, i, x;

    do
    {
        scanf("%d %d %d", &xd, &yd, &height);
        // prints out first line
        for(i=0; i<xd; i++){
            putchar('#');
        }
        printf("%s", "\n");

        // prints middle stuff
        if (yd >2){
            for(i=0; i<yd-2; i++){
                putchar('#');
                for (x=0; x<xd-2; x++){
                    putchar('.');
                }
                putchar('#');
                printf("%s", "\n");
            }
        }

        // prints end line
        if (yd > 1){
            for(i=0; i<xd; i++){
                putchar('#');
            }
        printf("%s", "\n");
        }
    }while (1);
    return 0;
}

Lab 1 Task 7 done

#include <stdio.h>

int main (void)
{
    int a[100];
    int i; int x = 0; int y = 0;
    do
    {
    for (i=0; a[i] != 0; i++){
        scanf("%d", &a[i]);
        for (y=0; y< a[i]; y++){
            putchar('#');
        }
        printf("%s", "\n");
    }
    }while (1);
        return 0;
}