All pastes #190606 Raw Edit

Lab 4.

public c v1 · immutable
·published 2006-10-04 04:39 UTC
rendered paste body
#include <stdio.h>void print_row (int,char);	 // prints a row of characters of specified length.//void print_2d (int,int,char);    // prints an mxn matrix of a specified character. //void print_triangle (int,char);  // prints a triangle of specified width.          //void print_tree (int,char);      // prints a triangle of specified width.          //void print_base (int,char);      // prints a nice looking tree of specified width. //int main(void){		int num ;	int rows;	char ch,sel,sch ;	printf("Lab 4. Functions , for loops , etc. \n");	printf("Select an option (1,2,3,4)\n");	printf("1. Print a row \n");	printf("2. Print a \"matrix\" \n");	printf("3. Print a triangle \n");	printf("4. Print a tree ;) \n \n");	printf("Selection:");		scanf( "%c" , &sel );// Selection 1. Print a row.													//		if (sel == '1') {			printf("Insert number of characters \n");			scanf("%d%c",&num);						printf("Insert the character you wish to print \n");			scanf("%c%c",&ch);			printf("\n-----------Result-----------\n");					print_row(num,ch);						printf("\n");						} else { 					// Selection 2. Print a matrix (with characters). 						//						if (sel == '2') {				printf("Insert the number for m \n");				scanf("%d%c",&rows);				printf("Insert the number for n \n");				scanf("%d%c",&num);				printf("Insert the character you wish to print \n");				scanf("%c%c",&ch);				printf("\n-----------Result-----------\n");				print_2d(rows,num,ch);				} else {// Selection 3. Print a triangle.				if (sel == '3') {					printf("Insert the width of the base \n");					scanf("%d%c",&num);					printf("Insert the character you wish to print \n");					scanf("%c%c",&ch);					printf("\n-----------Result-----------\n");								print_triangle(num,ch);					} else {							// Selection 4. Print a tree.													//										if (sel == '4') {					printf("Insert the width of the base \n");					scanf("%d%c",&num);					printf("Insert the character you wish to print \n");					scanf("%c%c",&ch);					printf("Do you also wish to print the tree's log? (Y/N) \n");					scanf("%c%c",&sch);												printf("\n-----------Result-----------\n");										print_tree(num,ch);												if ((sch=='Y')||(sch=='y')) { 					print_base(num,ch);					}				}			}		}	}}			// Functions //void print_row(int num,char ch){		int x;	for (x=0; x < num; x++){		printf("%c",ch);	}}void print_2d(int x,int y,char ch){		int row;	for (row=0; row < y; row++){		print_row(x,ch);		printf("\n");	}}void print_triangle(int x,char ch){	int col;	for (col=0; col < x; col++){		print_row(col,ch);		printf("\n");	}}void print_tree(int width,char ch){		int charnum;// Checking to see if the width of the tree is an odd number.      //// An odd width will result in a normal tree structure whereas an  //// even width will result in a tree structure without exact top.   //	if (width%2){			width = (width/2);		print_row((width+1),' ');		printf ("%c \n",ch);		charnum = 1;		// If the width is odd, every line will hold an even number of  	//// characters, the analogous happens with even numbers.           //// We set an incrementation by 2, and a starting value of 1 for  	//// odd numbers and of 0 for even numbers.								   //		}else{						width = ((width/2));		print_row (width,' ');		printf ("/\\ \n");		charnum = 0;	}	// We then print the rest of the tree's "branches". 				   //// Simply put, we increment the # of characters by 2 while        //// crossing down and decrement each preceeding spaces by 1.       //			for (;0<width;width--){		charnum=charnum+2;		print_row(width,' ');		print_row(charnum,ch);		printf("\n");		}		}void print_base(int width,char ch){		if (width%2){ 		print_row( (width/2) ,' ');		printf("%c %c \n ",ch,ch);				}else{		print_row((width/2),' ');		printf("%c%c \n ",ch,ch);		}		}