modaslam 0 Newbie Poster

Can any one help me to optimize this code. Or possible to alter and make it more simple
The out put i need to get is

1
 212
32123
.......

this is the code

main()
{
      int i,j,k,n=4,val,flag=0;
      for(i=0;i<n;i++)
      {
                      for(j=1;j<n-i;j++)
                         putchar(' ');
                      val=i+1;
                      flag=0;
                      for(k=0;k<2*i+1;k++)
                      {
                          
                          printf("%d",val);
                          if(val == 1)
                            flag = 1;
                           if(flag)
                             val++;
                            else
                            val--;  
                       } 
                       putchar('\n');
       }
                       getch();
}

Thanks

modaslam 0 Newbie Poster

I am wondering if we can build a calendar in C language...

...as in Gregorian Calendar format and design

Is there any code that could help me to print or to display a Gregorian Calendar???

pls. reply... thanks... God bless...


my yahoo email add is: [email]email snipped[/email]

The code i have given in this thread accept a normal date of format dd mm yy format and convert to Gregorian format well basically the this snippet is function used to convert the date.
okay

modaslam 0 Newbie Poster

> Is struct sfoo aligned? if yes/no why?
structs are always aligned. They're usually aligned to match the alignment of the member with the most restrictive alignment.
By removing the int, you removed the need to make the struct 4-byte aligned, so it just dropped back to the alignment for a char.

> 3. Is to possible to store any value in the padded bits?
Not in any portable manner it isn't.

> 1. what value shld i pass to size_t?
You use offsetof like posOfMember = offsetof( struct foo, aMember ); I mean not the offsetof function but to the macro which i have mentioned (size_t)&(....
> 2. when i remove '&' from the macro i got a segfault, why?
Because you really do end up dereferencing a NULL pointer!

> Is there any macro to disable structure padding?
That depends on your compiler, so you need to read the manual for your compiler to find out.
Some compilers use #pragma to achieve this.

Thank you for ur information

modaslam 0 Newbie Poster

I have some question on structure padding
Can any one help me!
Structure padding is done for alignment(is it right)
1. Why is it to be aligned?

2. struct sfoo
{
       int a;
       char i;
}

sizeof(struct sfoo) = 8 // 4+1+3(padding) 

struct sfoo
{
       //int a;
       char i;
}
sizeof(struct sfoo) = 1 // no padding

Is struct sfoo aligned? if yes/no why?

for a structure to be aligned, is it required that the toatal size must be a multiple of 2/4/8

3. Is to possible to store any value in the padded bits?
----End---0f---section--one

this macro usedto find the offset of a varable in stucture

#define offsetof(s,m) (size_t)&(((s *)0)->m)

((s *)0): takes the integer zero and casts it as a pointer to s. 
((s *)0)->m: dereferences that pointer to point to structure member m. 
&(((s *)0)->m): computes the address of m. 
(size_t)&(((s *)0)->m): casts the result to an appropriate data type.

1. what value shld i pass to size_t?
I know size_t is an unsigned integer returned by sizeof operator so what is the parameter passed to sizeof()
2. when i remove '&' from the macro i got a segfault, why?
Is it because of '0'. but even thought if we use '&' its must give a segfault because of '0'
but its not.
3. how come this macro give the result in nuber even though it is converted to *s


modaslam 0 Newbie Poster

What is C89?

modaslam 0 Newbie Poster

I have no idea why the original coder wrote that stuff, except it appears the s/he did not know about the standard C library date/time functions.

date arithmetic is best done with that library - convert a date/time to a struct tm, convert the struct tm to epoch seconds. Do the same thing with another time or date. Now you can add subtract or whatever - like what date & time was it 48 days ago? You don't have to worry about leap years, for example.

Work backwards to get a new date.

The functions you need are
gmtime or localtime
mktime
strftime

You also have to learn what goes into struct tm, so you can build one if your system does not have strptime.
here is some help:
http://publications.gbdirect.co.uk/c_book/chapter9/date_and_time.html

Its not like that. we are not supposed to use any library functions.. for finding the date.
Actually the program is to find the days elapsed between to given days.

The code snippet i have given is to convert the normal date to Gregorian type.

what they have explained is first they take two days and convert to Gregorian type.
and subtract second from first. so that they can get the difference in days elapsed

To convert a normal date to Gregorian type they used this function.

modaslam 0 Newbie Poster

hello Can anybody tell me how to Write a C program to print "Hello World" with out using ';'

modaslam 0 Newbie Poster

Hello Geeks,
I am working on a program for date,month calculation.
I found this code used to convert to Gregorian format for calculation.
It would be great full for me if anybody can explain this ... like 0.75 yy/4 .4*mm what are these constants why m-1, yy-1 etc..

long xx;
int yy,dd,m;
xx = 365 + yy +dd+31*(m-1)
if(m<3)
xx= xx+((yy-1)/4)-(0.75*(yy-1)/100 + 1);
else
xx = xx-(0.4*mm+2.3)+(yy/4)-(0.75*(yy/100)+1));

Thankyou

modaslam 0 Newbie Poster

>but how do i convert this code in to C code??

print ' ' ---> How would you display a space in C?
print n '*' ---> How would you display a * as many times as n is equals to?

for i = 0 to n do
print n + 2 '*' ---> How would you display in C a * n times plus 2 more?
loop ---> How would you loop in C from 0 to n?

print ' ' ---> How would you display a space in C?
print n '*' ---> How would you display a * as many times as n is equals to?

Okay I apologize .....

modaslam 0 Newbie Poster

You're over-engineering the solution. It's really nothing more than this:

print ' '
print n '*'

for i = 0 to n do
  print n + 2 '*'
loop

print ' '
print n '*'

but how do i convert this code in to C code??

modaslam 0 Newbie Poster

hello,
Can any one tell me how to print the patter

*
***
 *
for n =1
 **
****
****
 **
for n = 2
 ***
*****
*****
*****
 ***
for n = 3

the code i am having is

for(i=0;i<=n;i++)
{
   for(j=1;j<n-i;j++)
     putchar(' ');
   for(k=0;k<=i;k++)
    printf('* ');
  putchar('\n');
}

what will be the condition so that i will get that pattern
thanking you