+ 1

star shapes in C

I have a question that if I want to make the shape below for odd numbers what should I do?(for example 3: * *** * 5: * *** ***** *** *

26th Oct 2018, 5:41 AM
Matin Zamani
Matin Zamani - avatar
7 Answers
+ 2
There are 4 functions in my attached code that print the required shape. They are different. The simplest one is diamond_4 which i think is easy to understand diamond_1,diamond_2 and diamond_3 are a bit difficult to understand. change the cout statements to printf as you require them in c. cout<<"*" to printf("*"); cout<<" " to printf(" "); https://code.sololearn.com/c0XR45S70aka/?ref=app
26th Oct 2018, 5:46 AM
Megatron
Megatron - avatar
+ 1
I meant that the program gets a number from the user then print it
26th Oct 2018, 5:54 AM
Matin Zamani
Matin Zamani - avatar
+ 1
😔😔😔Here it is again..... scanf("%d",&SIZE); for(int x=0;x<SIZE;++x) { for(int sp=0;sp<abs(SIZE/2-x);++sp) printf(" "); for(int y=0;y<2*(SIZE/2-abs(SIZE/2-x))+1;++y) printf("*"); printf("\n"); } EDIT: SORRY forgot that add this to beginning of code int abs(int val) { if(val<0) return val*-1; return val; }
26th Oct 2018, 6:08 AM
Megatron
Megatron - avatar
+ 1
I did that code on visual C but it gives me an error that the abs is not defined, how can I refer the absolute function in the code?
26th Oct 2018, 6:20 AM
Matin Zamani
Matin Zamani - avatar
+ 1
Tested this code this version works #include <stdio.h> int abs(int val) { if(val<0) return val*-1; return val; } int main() { int SIZE; scanf("%d",&SIZE); for(int x=0;x<SIZE;++x) { for(int sp=0;sp<abs(SIZE/2-x);++sp) printf(" "); for(int y=0;y<2*(SIZE/2-abs(SIZE/2-x))+1;++y) printf("*"); printf("\n"); } return 0; }
26th Oct 2018, 7:49 AM
Megatron
Megatron - avatar
0
I did the code, would you please check it cause it doesn't give any output. The code is in the link below: https://code.sololearn.com/c9MJQZEMMlwu/#
26th Oct 2018, 6:42 AM
Matin Zamani
Matin Zamani - avatar
0
somewhere in your should be a mistake cause when I run it, it shows infinite stars I mean that somewhere there's an infinite loop!
26th Oct 2018, 9:51 AM
Matin Zamani
Matin Zamani - avatar