// A simple program that uses loops to output a grid
// Marc Chee, February 2019

#include <stdio.h>

int main (void) {
    int gridSize;
    int x = 0;
    int y = 0;
    int exit = 0;
    
    // Start the loop that lets the program run multiple times
    while(exit == 0) {
        // let the user choose our grid size
        printf("Please enter the size of the grid or 0 to exit: ");
        scanf("%d", &gridSize);
        
        // User has chosen to exit
        if (gridSize == 0) {
            printf("Thank you for using Grid Drawer. Exiting now.");
            // setting the sentinel to leave the loop
            exit = 1;
            // another option would be to "return 0" here to exit immediately
        }
        
        // loop through and print multiple rows
        while (y < gridSize) {
            // print a single row
            while (x < gridSize) {
                printf("*");
                x = x + 1;
            }
            // end the row
            printf("\n");
            y = y + 1;
            // reset x for the next row
            x = 0;
        }
        // make a gap between different runs of the program
        printf("\n");
        // reset variables for the next run
        gridSize = 0;
        x = 0;
        y = 0;
    }    
    return 0;
}

Resource created Wednesday 27 February 2019, 11:30:39 PM, last modified Sunday 03 March 2019, 02:08:12 AM.

file: theGrid.c


Back to top

COMP1511 19T1 (Programming Fundamentals) is powered by WebCMS3
CRICOS Provider No. 00098G