Wednesday, 23 August 2017

fill shape with C



i wrote a program to fill in closed figures with asterisks. for some reason, it isn't accepting the sentinel value EOF (Ctrl-D). why is this?





#include "usefunc.h"

#define height 100
#define width 100

void showRow(int numbers[], int size_numbers) {
int i;
printf("[ ");

for (i = 0; i < size_numbers-3; i++) {
printf("%c, ", numbers[i]);
}
printf("%c ]", numbers[size_numbers-3]);
printf("\n");
}

void showshape(int shape[][width], int lines, int max_buf) {
int i, j;
for (i = 0; i < lines; i++) {

for (j = 0; j < max_buf; j++) {
printf("%c", shape[i][j]);
}
printf("\n");
}
}

void fill(int row[][width], int rownum, int end) {
int i, c = 1, inside = 0;
for (i = 0; i < end; i++) {

if (row[rownum][i] == '*') {
c++;
}
if (!(c%2)) inside = 1;
else inside = 0;
if (inside) {
row[rownum][i] = '*';
}
}
}


int main () {
int shape[height][width], i = 0, j = 0, lines = 0;
int sentinel = 0;
int temp = 0;
while (sentinel != EOF) {
while ((temp = getchar()) != '\n') {
sentinel = temp;
shape[i][j] = temp;
j++;

}
i++;
lines++;
}
for (i = 0; i < lines; i++) {
fill(shape, i, width);
}
fill(shape, 0, j);
//for (i = 0; i < lines; i++)
showshape(shape, lines, j+2);

}


okay, just updated the code. it doesn't quite print the box. what's going on??



another update for the code. this time i am copying the value of temp. however, i get Bus error - what am i doing wrong?!


Answer



You want:



int temp;



EOF is an integer value, not a char.


No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...