Solved

trouble printing a tower

Posted on 2004-09-28
9
297 Views
Last Modified: 2011-09-20
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
0
Comment
Question by:aik_21
9 Comments
 
LVL 45

Expert Comment

by:Kdo
ID: 12176220
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
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 12176481
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
 

Author Comment

by:aik_21
ID: 12176590
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
 

Author Comment

by:aik_21
ID: 12176620
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
Do email signature updates give you a headache?

Are you constantly making changes to email signatures? Are the images not formatting how you want them to? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today.

 
LVL 84

Expert Comment

by:ozo
ID: 12176639
for (j = 0; j <= i; j++)
0
 
LVL 45

Expert Comment

by:Kdo
ID: 12178622


:)

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
 

Author Comment

by:aik_21
ID: 12183260
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
 
LVL 45

Accepted Solution

by:
Kdo earned 20 total points
ID: 12183318


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
 

Author Comment

by:aik_21
ID: 12183492
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Dll Dynamic way c/c++ 9 139
C dll call freezes 5 93
why "." vs "->" 23 119
change colour of repeater control in asp.net c# 7 72
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now