creating pattern using c

A

anto_kamau

Question

write a c program that displays a diamond pattern, when a user enters
an odd number between 0 and 19, and displays on the screen
 
R

Roberto Waltman

Question
write a c program that displays a diamond pattern, when a user enters
an odd number between 0 and 19, and displays on the screen

Mathematicians answer: This problem has been solved already. Search
the archives of comp.lang.c, and/or do your own homework.
 
R

Robert Gamble

Question

write a c program that displays a diamond pattern, when a user enters
an odd number between 0 and 19, and displays on the screen

Most people here don't take well to "do my homework for me" requests.
Show us what you have so far and what you are having trouble with and
you will be much more likely to receive helpful replies.

Robert Gamble
 
J

Joe Smith

Robert Gamble said:
Most people here don't take well to "do my homework for me" requests.
Show us what you have so far and what you are having trouble with and
you will be much more likely to receive helpful replies.

I think Mr. Gamble places value on the OP's education that rightly belongs
in our collective fun. It's Sunday. Probably due tomorrow. The kid is
hungover from the kegger last night and his TA probably keeps track of clc.
There is the potential for a Faustian bargain here. joe
 
E

Eric Sosman

Question

write a c program that displays a diamond pattern, when a user enters
an odd number between 0 and 19, and displays on the screen

#include <stdio.h>
#include <stdlib.h>
int main(void) {
int number;
printf ("Enter an odd number between 0 and 19: ");
fflush (stdout);
if (scanf("%d", &number) != 1) {
printf ("That's not a number!\n");
abort();
}
if (number < 0 || number > 19) {
printf ("That's not between 0 and 19!\n");
abort();
}
if (number % 2 != 1) {
printf ("That's not an odd number!\n");
abort();
}
printf ("a diamond pattern\n");
return EXIT_SUCCESS;
}
 
R

Richard Heathfield

(e-mail address removed) said:
Question

write a c program that displays a diamond pattern, when a user enters
an odd number between 0 and 19, and displays on the screen

There is nothing new under the sun; this question has been asked, and
answered, before.

#include <stdlib.h>
#include <stdio.h>


#define O int
#define B main
#define F char
#define U if
#define S atoi
#define C for
#define A putchar
#define T '*'
#define E ' '
#define D '\n'
#define c ==
#define o =
#define d ++
#define e return
#define r ||

O
B (
O k
, F
* v
[ ]
) {
O i
, j = 9
; U ( k > 1
) { j = S
( v [ 1
] ) ; }
U ( ! (
j > 0 )
) { j =
5 ; } U ( (
j & 1 ) c 0 ) {
d j ; } C (
i = 0 ;
i < j * j
; A ( i / j
c ( 3 * j
) / 2 -
( i % j + 1
) r i / j c j /
2 - i % j r
i / j c
j / 2 +
i % j r
i / j c
i % j -
j / 2 ? T
: E ) , i d
, i % j
c 0
? A
( D
) :
0 )
; e
0 ;
}
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Question

write a c program that displays a diamond pattern, when a user enters
an odd number between 0 and 19, and displays on the screen

Ok, I'll give you the complete solution.

But you must promise me you pay me $50 for helping you out with this
difficult task.

Promise?

Ok, here's the solution:

#include <stdio.h>

int main(void)
{
printf("a diamond pattern, when a user enters an odd number between
0 and 19, and displays on the screen\n");
return 0;
}

You'll get an A+ grade if you hand that in. Or you could give us your
teacher's email address and we'll mail it directly to him from you, so
we'll know he got it.


Best regards / Med venlig hilsen
Martin Jørgensen
 
P

pete

Question

write a c program that displays a diamond pattern, when a user enters
an odd number between 0 and 19, and displays on the screen

/* BEGIN new.c */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void function(char *array, int max, int limit);
char *str_rev(char *s);

int main(void)
{
char *array;
int counter, rc, input, im1;

puts("enter an odd number between 0 and 19");
rc = scanf("%d", &input);
if (rc == 1 && input > 0 && 20 > input && input % 2 == 1) {
counter = input;
array = calloc(1, input);
if (array != NULL) {
im1 = input - 1;
while (counter-- != 0) {
function(array, im1, counter);
}
for (counter = 0; counter != input; ++counter) {
function(array, im1, counter);
}
} else {
puts("array == NULL");
}
free(array);
} else {
puts("bad input");
}
return 0;
}

void function(char *array, int max, int limit)
{
int count;

for (count = 0; count != limit; ++count) {
array[count] = ' ';
}
while (count != max) {
array[count] = '*';
++count;
}
fputs(array, stdout);
putchar('*');
str_rev(array);
puts(array);
}

char *str_rev(char *s)
{
char *t, swap;
char *const p = s;

if (s[0] != '\0' && s[1] != '\0') {
t = s + 1 + strlen(s + 2);
do {
swap = *t;
*t-- = *s;
*s++ = swap;
} while (t > s);
}
return p;
}

/* END new.c */
 
C

CBFalconer

Question

write a c program that displays a diamond pattern, when a user
enters an odd number between 0 and 19, and displays on the screen

Here you are. No charge this time. Tested code.

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) { int n; char *err; if (argc > 1)
{ n = strtol(argv[1], &err, 10); if ((err > argv[1]) && (n > 0)
&& (n < 20) && (n & 1)) { puts(" *"); puts("***"); puts(" *"); }
} return 0; }

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
M

Minlar Ginger

pete said:
Question

write a c program that displays a diamond pattern, when a user enters
an odd number between 0 and 19, and displays on the screen

/* BEGIN new.c */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void function(char *array, int max, int limit);
char *str_rev(char *s);

int main(void)
{
char *array;
int counter, rc, input, im1;

puts("enter an odd number between 0 and 19");
rc = scanf("%d", &input);
if (rc == 1 && input > 0 && 20 > input && input % 2 == 1) {
counter = input;
array = calloc(1, input);
if (array != NULL) {
im1 = input - 1;
while (counter-- != 0) {
function(array, im1, counter);
}
for (counter = 0; counter != input; ++counter) {
function(array, im1, counter);
}
} else {
puts("array == NULL");
}
free(array);
} else {
puts("bad input");
}
return 0;
}

void function(char *array, int max, int limit)
{
int count;

for (count = 0; count != limit; ++count) {
array[count] = ' ';
}
while (count != max) {
array[count] = '*';
++count;
}
fputs(array, stdout);
putchar('*');
str_rev(array);
puts(array);
}

char *str_rev(char *s)
{
char *t, swap;
char *const p = s;

if (s[0] != '\0' && s[1] != '\0') {
t = s + 1 + strlen(s + 2);
do {
swap = *t;
*t-- = *s;
*s++ = swap;
} while (t > s);
}
return p;
}

/* END new.c */
Good answer.
but there is an error in "array = calloc(1, input);".
"array=(char *)calloc(1, input);" is ok.

Minlar Ginger
 
R

Richard Heathfield

Minlar Ginger said:
Good answer.
but there is an error in "array = calloc(1, input);".

so this line should said:
"array=(char *)calloc(1, input);" is ok.

Wrong. If the code didn't compile without the cast, the C compiler is
broken. Or, more likely, you're not using a C compiler at all! (In C++, the
cast would be required - but then in C++, you wouldn't use calloc.)
 
M

Minlar Ginger

Richard said:
Minlar Ginger said:




Wrong. If the code didn't compile without the cast, the C compiler is
broken. Or, more likely, you're not using a C compiler at all! (In C++, the
cast would be required - but then in C++, you wouldn't use calloc.)

Thanks for your help. You are right . I use a C++ compiler ,and it
warning:
error C2440: '=' : cannot convert from 'void *' to 'char *'
Conversion from 'void*' to pointer to non-'void' requires an
explicit cast
but when I use a C compiler ,it compiles successfully. And I should
know more about cast,
Thank you again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,518
Latest member
RomanGratt

Latest Threads

Top