Tuesday, October 25, 2011

Tuesday 10.25.11

#include
void display(char cr, int lines, int width);
int main(void)
{
int ch;
int rows, cols;

printf("Enter a character and two integers:\n");
while ((ch = getchar()) != '\n')
{
if (scanf("%d %d", &rows, &cols) != 2)
break;
display(ch, rows, cols);
while (getchar() != '\n')
continue;
printf("Enter another character and two integers;\n");
printf("Enter a newline to quit.\n");
}
printf("Bye.\n");

return 0;
}

void display(char cr, int lines, int width)
{
int row, col;

for (row = 1; row <= lines; row++)
{
for (col = 1; col <= width; col++)
putchar(cr);
putchar('\n');

}


#include

//validate that input is an integer
int get_int(void);
//validate that range limits are valid
int bad_limits(int begin, int end, int low, int high);
//calculate the sum of the squares of teh two integers
//a through b
double sum_squares(int a, int b);
int main(void)
{
const int MIN = -1000; //lower limit to range
const int MAX = +1000; //upper limit to range
int start; //start of range
int stop; //end of range
double answer;

printf("This program computes the sum of the squares of integerrs in a range.\n");
printf("The lower bound should not be less than -1000 and \n the upper bound should not be more than +1000.\n");
printf("Enter the limits (enter 0 for both limits to quit):\nlower limit: ");
start = get_int();
printf("upper limit: ");
stop = get_int();
while (start != 0 || stop != 0)
{
if (bad_limits(start, stop, MIN, MAX))
printf("Please try again.\n");
else
{
answer = sum_squares(start, stop);
printf("The sum of the squares of the integers ");
printf("from %d to %d is %g\n", start, stop, answer);
}
printf("Enter the limits (enter 0 for both limits to quit):\n");
printf("lower lmit: ");
start = get_int();
printf("upper limit: ");
stop = get_int();
}
printf("Done.\n");
return 0;
}

int get_int(void)
{
int input;
char ch;

while (scanf("%d", &input) != 1)
{
while ((ch = getchar()) != 1)
putchar(ch); //dispose of bad input
printf(" is not an integer.\n Please enter an ");
printf("integer value, such as 25, -178, or 3: ");
}
return input;
}

double sum_squares(int a, int b)
{
double total = 0;
int i;

for (i = a; i <= b; i++)
total += i * i;
return total;
}

int bad_limits(int begin, int end, int low, int high)
{
int not_good = 0;
if (begin > end)
{
printf("%d isn't smaller than %d.\n", begin, end);
not_good = 1;
}
if (begin < low || end < low)
{
printf("Values must be %d or greater.\n", low);
not_good = 1;
}
if (begin > high || end > high)
{
printf("Values must be %d or less.\n", high);
not_good = 1;
}

return not_good;
}


#include
char get_choice(void);
char get_first(void);
int get_int(void);
void count(void);
int main(void)
{
int choice;
void count(void);

while ((choice = get_choice()) != 'q')
{
switch (choice)
{
case 'a' : printf("Buy low, sell high.\n");
break;
case 'b' : putchar('\a'); //ansi
break;
case 'c' : count();
break;
default: printf("Program error!\n");
break;
}
}
printf("Bye.\n");

return 0;
}

void count(void)
{
int n, i;
printf("Count how far? Enter an integer:\n");
n = get_int();
for (i = 1; i <= n; i++)
{
printf("%d\n", i);
}
while (getchar() != '\n')
continue;
}

char get_choice(void)
{
int ch;

printf("Enter the letter of your choice:\n");
printf("a. advice b. bell\n");
printf("c. count q.quit\n");
ch = get_first();
while ((ch < 'a' || ch > 'c') && ch!= 'q')
{
printf("Please respond with a, b, c or q.\n");
ch = get_first();
}
return ch;
}

char get_first(void)
{
int ch;
ch = getchar();
while (getchar() != '\n')
continue;

return ch;
}

int get_int(void)
{
int input;
char ch;

while (scanf("%d", &input) != 1)
{
putchar(ch);
}
printf(" is not an integer.\nPlease enter an ");
printf("integer value; such as 25, -178, of 3:");
return input;
}


#include
#define NAME "GIGATHINK, INC."
#define ADDRESS "101 Megabuck Plaza"
#define PLACE "Megapolis, CA 94904"
#define WIDTH 40

void starbar(void);

int main(void)
{
starbar();
printf("%s\n", NAME);
printf("%s\n", ADDRESS);
printf("%s\n", PLACE);
starbar();

return 0;
}

void starbar(void)
{
int count;

for (count = 1; count <= WIDTH; count++)
putchar('*');
putchar('\n');
}



#include
#include
#define NAME "GIGATHINK, INC."
#define ADDRESS "101 Megabuck Plaza"
#define PLACE "Megapolis, CA 94904"
#define WIDTH 40
#define SPACE ' '

void show_n_char(char ch, int num);

int main(void)
{
int spaces;
show_n_char('*', WIDTH);
putchar('\n');
show_n_char(SPACE, 12);
printf("%s\n", NAME);

spaces = (WIDTH - strlen(ADDRESS)) / 2;
//let the program calculate how many spaces to skip
show_n_char(SPACE, spaces);
printf("%s\n", ADDRESS);
show_n_char(SPACE, (WIDTH - strlen(PLACE)) / 2);
printf("%s\n", PLACE);
show_n_char('*', WIDTH);
putchar('\n');

return 0;
}

//show_n_char() definition
void show_n_char(char ch, int num)
{
int count;

for (count = 1; count <= num; count++)
putchar(ch);
}

No comments: