Solved

trouble printing a tower

Posted on 2004-09-28
9
329 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 45

Expert Comment

by:Kent Olsen
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
LVL 84

Expert Comment

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

Expert Comment

by:Kent Olsen
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:
Kent Olsen 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

688 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