trouble printing a tower

hi there all,
i was relatively new to programming in C using structures and pointers.
I am trying to print this:

|_
|_ _
|_ _ _

now i tried to use a structure for the bars and same for the hash.
My code is listed below, could someone tell me where am i going wrong? And how do i set the hashe's "_" in the array?
#include<stdio.h>

typedef struct tower{
        char bar[3]; // 3 could be N as well
        char hsh[????]; //any number user supplies
}Tower;

Tower *t1;
int main()
{
        int i;
        for(i=0;i<3;i++)
{              t1->*arr[i]='|';
              // what i m trying to do is to set up an array hsh2 in which i have {'_','_ _','_ _ _''_ _ _ _' n so on and print it, but my problem is, arrays here could be static only, say if i want this for N number of '_' thats when i can't figure out what to do!}
                printf("%s %s\n",t1->*arr[i], t1->*hsh[i]);

}
return 0;

}


Can someone please give me suggestions on this?

Thank You
aik_21Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Kent OlsenConnect With a Mentor Data Warehouse Architect / DBACommented:


Easy enough.  :)

                printf("%s",BarArray[j]);

This format statement says that you're going to be passing a string that will be substituted into the "%s".  BarArray[j] is a character.  i.e. a short integer!  

What you really want to do is pass a string to the format specifier by putting a '&' in front of the variable BarArray[], or specify that you're going to be printing a character, not a string.  You do that with the "%c" specifier.  Try the following and see what happens.

                printf("%c",BarArray[j]);



Kent
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi  aik_21,

There are a couple of ways to do this.  Dynamic arrays are the easiest.

char *DashArray = NULL;

SetDashArraySize (int size)
{
  DashArray = (char *)malloc (size+1);
  memset (DashArray, '_', size);
  DashArray[size] = 0;
}


But you said that you must use static arrays.  That's only slighly tougher.


#define DASHARRAYSIZE 128

char DashArray[DASHARRAYSIZE+1];

InitializeDashArray ()
{
  memset (DashArray, '_', DASHARRAYSIZE);
  DashArray[DASHARRAYSIZE] = 0;
}

Now to print more than DASHARRAYSIZE characters, you'll simply loop.

  while (PrintCount > DASHARRAYSIZE)
  {
    printf (DashArray);
    PrintCount -= DASHARRAYSIZE;
  }

Then print whatever is left

  printf ("%*.*s", PrintCount, PrintCount, DashArray);


Good Luck,
Kent
0
 
brettmjohnsonCommented:
You don't really need structures or arrays, and in fact, it is quite an inappropriate application of each.

int i, j, ndashes;

for (i = 0; i < 3; i++) {
      putchar('|');
      for (j = 0; j < ndashes; j++)
            putchar('_');
      putchar('\n');
}

0
The eGuide to Automating Firewall Change Control

Today’s IT environment is constantly changing, which affects security policies and firewall rules. Discover tips to help you embrace this change through process improvement & identify areas where automation & actionable intelligence can enhance both security and business agility.

 
aik_21Author Commented:
Hello Kent,

thank you for your answer, i see getting somewhere.  I have some questions related to your answer, bear with me :).

char DashArray[DASHARRAYSIZE+1];
Why are you adding one making it size 129

DashArray[DASHARRAYSIZE] = 0;
Whats the reason of setting 129th element to 0.
printf ("%*.*s", PrintCount, PrintCount, DashArray);

And the above print statement has pointers embedded between the % and s?
I didn't quite get that part.

I know it must be something trivial but unfortunately i m just a beginner.

Thanks

Aik_in
0
 
aik_21Author Commented:
Hello brettmjohnson,

I tried your suggestion and got this as the result:
|_____
|_____
|_____

Now that looks good, but my problem was that i have to ensure that the hashes have to correspond to the bars in a way that bar one would have one hash, bar two would have two hashes and so on...so don't you think the best way to keep a track of the bar number and number of hashes would be in an array?

Let me play with this more and i'll get back to you again.

Thanks

Aik_in
0
 
ozoCommented:
for (j = 0; j <= i; j++)
0
 
Kent OlsenData Warehouse Architect / DBACommented:


:)

There's nothing wrong with being a beginner.  We all were at one time or another....

Strings are always 1 character longer than their "visible" size.  "ABC"  displays as three characters, but the string takes up 4.  That's because strings in C are terminated with a byte of 0.  Other languages do things things differently and keep the count of the string somewhere, but not C.  (C is pretty simple in most of its concepts.)

So to keep 128 '_' characters in a string, we need one extra byte for the string terminator (zero).

>>printf ("%*.*s", PrintCount, PrintCount, DashArray);

The '*' in the format specifier tells printf() that the value will be passed as a parameter.  When PrintCount is 3, the equivalent statement would be:

printf ("%3.3s" DashArray);

It's impractical to have 128 printf() statements and then use the one you want based on the size of PrintCount.  So the language lets you plug the number right into the format specifier this way.


Good Luck,
Kent
0
 
aik_21Author Commented:
Hello Kent,

I have applied your strategy(partly) i.e. used memset to set the arrays.  I have been able to get the algorithm right, but now i have problems with the syntax.  I know it's gotta do something with addressing and pointers for these arrays.  When i compile this program i get a segmentation fault/core dumped error.  I know its with the print statement, i am somehow pointing to the location in memory.
Can you point out, what could be the problem with this?

#include<stdio.h>
#define HASHES 128
#define BARS 5

void InitializeDashArray();
void InitializeBarArray();

char DashArray[HASHES+1];
char BarArray[BARS+1];

void InitializeArrays ()
{
        memset(DashArray, '_', HASHES);
        memset(BarArray,'|',BARS);
        DashArray[HASHES] = 0;
        BarArray[BARS]=0;
}

main()
{
        int i,j,ndashes;
        ndashes=5;
        InitializeArrays();

        for(j=0;j<5;j++) //i m trying to print five bars
        {
                printf("%s",BarArray[j]);
             i=0;

                while(i<=j) //print the corresponding hashes
                {
                        printf("%s",DashArray[i]);
                        i++;
                }
                        printf("\n");
        }
}

Thanks

Aik_21
0
 
aik_21Author Commented:
Kent,
TWO THUMBS UP :D

It worked, and i feel so silly that it didn't cross my mind!

Thank You very much

Aik_21
0
All Courses

From novice to tech pro — start learning today.