-5

I am trying to write a program on generating a sinusoidal text in C. I tried the code below and got a vertical sine wave. Is it possible to make it horizontal? I am new to C programming, so if you could, please explain the code in simplest manner. Thank you!

#include <stdio.h>
#include <math.h>


int main()
{
    // declarations
    int x;
    char y[80];
    char str[] = "This is a test string";
    // setting entire array to spaces
    for(x = 0; x < 79; x++)
        y[x] = ' ';
    y[79] = '\0';

    // print 20 lines, for one period (2 pi radians)
    for(x = 0; str[x]!='\0'; x++)
    {
        y[40 + (int) (40 * sin(M_PI * (float) x /10))] = str[x];
        printf("%s\n", y);
        y[40 + (int) (40 * sin(M_PI * (float) x / 10))] = ' ';
    }

    // exit
    return 0;
} 

Output:

                                   T                                      
                                                    h                          
                                                               i               
                                                                        s      


                                                                              s

                                                               a               

                                        t                                      
                            e                                                  
                 s                                                             
        t                                                                      

s                                                                              
  t                                                                            
        r                                                                      
                 i                                                             
                            n                                                  
                                        g               

Is it possible to make the wave horizontal without changing the existing code? Thank you!

  • 2
    What do you mean “without changing the existing code”? If you want to change the program’s behaviour, you’re going to have to change its code. – Ry- Mar 10 '18 at 20:56
  • 1
    Right now, you output one non-space character per (consistently-spaced) line, at a sinusoidally oscillating offset from the left. The 90-degree rotation of that is one non-space character per (consistently-spaced) column, at a sinusoidally oscillating offset from the bottom (or top) line. In particular, you will probably have multiple non-space characters on some lines, so you'll need to build all the lines first before printing any of them. – John Bollinger Mar 10 '18 at 21:06
  • @Ryan♦ , I thought there might be a small change, that's why I said that. If there is a change,then could you please tell me that? Thank you! – Vijayachandran Chettiar Mar 10 '18 at 21:09
  • @JohnBollinger I didn't quite understand what you meant. Could you please explain with code? Thank you! – Vijayachandran Chettiar Mar 10 '18 at 21:11
  • 1
    You should build an array before printing, because it is not so easy to go back up in the console. – Weather Vane Mar 10 '18 at 21:11
  • 2
    No, @VijayachandranChettiar, I will not "explain with code", though it would indeed be clearer, because that would be doing your homework for you. – John Bollinger Mar 10 '18 at 21:13
  • This won't work very well in the other direction without a rethink. The [Compukit UK101](https://en.wikipedia.org/wiki/Compukit_UK101) (where my adventure began) had a set of eight characters each with a horizontal line at a different height. With no graphics mode it was ideal for this task, but nearly 40 years later . . . it's fun but if you are interested in graphics you could explore the [OpenGL Utility Toolkit](https://en.wikipedia.org/wiki/OpenGL_Utility_Toolkit). – Weather Vane Mar 10 '18 at 21:17
  • https://ericlippert.com/2014/03/21/find-a-simpler-problem/ For this case I recommend making a rectangle first. Then a circle. The solutions for these are quite obvious and then it will be the foundation to make your horizontal shape which looks like a sinus wave. Also first use a trivial texteditor, e.g. notepad, to make an example of what you need, while making it try to think how to tell the compulter to do it for you. – Yunnosch Mar 10 '18 at 21:18
  • *If* there were an easy way to print columns down the page, as easy as it is to print lines across the page, then yes, it would be simple to make the sine wave go across the screen. But since there isn't, the answer to your question is, no, there's no simple modification. You would have to either fill in and then print out a character array, as other comments have suggested, or use [curses](https://en.wikipedia.org/wiki/Curses_(programming_library)), or perhaps make direct use of [ANSI cursor-motion sequences](https://en.wikipedia.org/wiki/ANSI_escape_code). – Steve Summit Mar 10 '18 at 22:02
  • @JohnBollinger I am not doing homework, I am new to C and I am trying to learn the above code and how it works. That's all. – Vijayachandran Chettiar Mar 11 '18 at 04:34
  • @Weather Vane, I am using repl.it site, in that the graphics header file is not working. – Vijayachandran Chettiar Mar 11 '18 at 04:35
  • @Yunnosch I think I got an idea now, thank you for being helpful! – Vijayachandran Chettiar Mar 11 '18 at 04:36
  • @Steve Summit I will look into it. Thank you for your effort! – Vijayachandran Chettiar Mar 11 '18 at 04:37
  • @VijayachandranChettiar, even if I believe that the exercise you are working is not a formal assignment from an instructor-led course, the fact remains that if what you are trying to learn is how to program in C, then it would defeat that purpose for me to write a solution for you. On the other hand, if you are trying to learn something else, such as how to get other people to write code for you, then I'm afraid I'm simply not on board with that. – John Bollinger Mar 11 '18 at 19:03
  • @JohnBollinger Either you are lazy to help others or you have no clue about what you are talking about. I was able to understand something from other comments but your explanation was difficult for me to understand, that's why I had asked to explain further. – Vijayachandran Chettiar Mar 19 '18 at 16:59

1 Answers1

1

I wrote an answer to your question, which works, maybe not completly as you expect but it should give you enough to work with

note the following:

  • after you wrote to the console\file it is very difficult to return and change printed values
  • you must define your desiered output matrix and prepare the entire output before you print anything

the logic behine my example is this:

  1. create a matrix (80*22)
  2. fill the matrix with spaces
  3. fill the matrix by columns
  4. print the entire matrix by char;

#include <stdio.h>
#include <math.h>
#include <string.h>

int main()
{
    // declarations
    int col, row;
    char str[] = "This is a test string";

    /* define the screen for print (80 width * 22 length)*/
    char printout[80][22];

    // setting entire matrix to spaces
    for (row=0; row < 22 ; row++)
    {
        for(col = 0; col < 80; col++)
            printout[col][row] = ' ';
    }

    /* fill in the columns modulo the string to allow continuous output */
    for(col = 0; col < 80 ; col++)
    {
        printout[col][10 + (int) (10 * sin(M_PI * (float) col /10))] = str[( col % strlen(str) )];
    }

    /* printout the entire matrix formatted */
    for (row = 0 ; row < 22 ; row++) {
        for (col = 0 ; col < 80 ; col++) {
            printf("%c", printout[col][row]);
        }
        printf("\n");
    }
    // exit
    return 0;
} 

there are many thing to correct in this code - the rows should consider the size of the string, you should parse it as string not char etc. but again it does give you what you want and it might help you to continue...

               s                                       t                   s    
                t                 t s                 s                   e t   
             t   r               s   t               e   s               t      


            s     i             e     r             t     t                   s 

           e       n           t       i                   r           a       t


T         t         g                   n         a         i                   


 h                   T       a           g                   n       s          

  i     a             h                   T     s             g     i           


   s                   i   s               h   i               T                
      s                 s i                 i                   h s             
     i                                       s                   i              
Omer Dagan
  • 662
  • 5
  • 14