displaying a array to output

This is a discussion on displaying a array to output within the C Programming forums, part of the General Programming Boards category; Hello to all, I am trying to display the contents of an array to screen. My problem is with what ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    77

    displaying a array to output

    Hello to all,

    I am trying to display the contents of an array to screen. My problem is with what is going into my array. I am trying to put a sequence of characters into it. I am able to get characters to go into it but, for some reason there not the correct ones??? Below is my source code and the sequence I am trying to put in. I only want the letters to be added.

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

    main()
    {
    char test[] = "ORIGIN" ;
    char header[100] ;
    char sequence[100000] ;
    char buffer[10000] ;
    char i, c, n, w ;

    /* for(n=0 ; n < 999 ; ++n)
    {
    sequence[n] = 0 ;
    {
    */

    while(fgets(header, 100, stdin))
    {
    if(strstr(header, "LOCUS"))
    {
    int w = 12 ;
    while(isblank(header[w]))
    {
    ++w ;
    }
    printf(">%s", &header[w] ) ;
    break ;
    }
    }



    while(fgets(buffer, 10000, stdin))
    {
    if(strstr(buffer, test)) // start obtaining bases after ORIGIN
    {
    n = 0 ;
    while((c=getchar()) != '/')
    switch(c)
    {
    case 'a': case 'c': case 't': case 'g': case 'u': case 'm': case 'r': case 'w':
    case 's': case 'y': case 'k': case 'v': case 'h': case 'd': case 'b': case 'n':
    sequence[n] = c ; // put any base character into sequence array
    ++n ;
    break ;
    case ' ':
    case '\n':
    case '\t':
    break ; // ignore white space
    default:
    break ;
    }
    }


    }

    printf("%s\n", sequence) ;

    return 0 ;
    }


    SEQUENCE:

    1 cctcagatca ctctttggca acgacccctc gycacmataa agataggggg gcaactaaag
    61 gaagctctat tagahacagg agcagatgat accatattma aagaaataaa tttgccagga
    121 agatggaarc caaaaatgat agggggaatt ggaggtttta tcaaagtaag acagtatgat
    181 cagatactca tagaaatctg tggacataaa gttataggta cagtattagt aggacctaca
    241 cctgtcaacg taattggaag aaatctgttr actcagattg gttgcacttt aaatttt
    //

    Any help would be appreciated,
    Thank you

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum!

    The biggest help right now is to highlight your whole program, after opening your original post for editing, and click on the pound sysmbol #.

    That will put your program inside code tags, and then if you can indent it properly, that will also help a great deal.

    Right off, I wouldn't use a case statement like you have. A simple

    Code:
    int i = 0;
    while (etc.) ... {
    
    if (my_char >= 'a' && my_char <= 'z')
       array[i++] = my_char;
    
    }
    
    /* now print out your array */
    Looks far cleaner to my eye, but I'll take a longer look at it when it's looking like a real program.
    Last edited by Adak; 02-10-2008 at 08:34 AM.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    Main also returns int.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    77
    Thanks for the advice. Sorry for the ugly code, I am new to programming.

    Your loop helped condense everything but, I am still not getting the correct output. And # doesn't seem to put my code in tags.

    Heres the code, input and output. The output should be only the letters from the input. I am getting a totally different array of letters then are in the input.

    Also, how can I structure my output so that I only print 60 characters per line at a time.

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

    main()
    {
    char test[] = "ORIGIN" ;
    char header[100] ;
    char sequence[100000] ;
    char buffer[10000] ;
    char i, c, n, w ;

    /* for(n=0 ; n < 999 ; ++n)
    {
    sequence[n] = 0 ;
    {
    */

    while(fgets(header, 100, stdin))
    {
    if(strstr(header, "LOCUS"))
    {
    int w = 12 ;
    while(isblank(header[w]))
    {
    ++w ;
    }
    printf(">%s", &header[w] ) ;
    break ;
    }
    }



    while(fgets(buffer, 10000, stdin))
    {
    if(strstr(buffer, test)) // start obtaining bases after ORIGIN
    {
    int n = 0 ;
    while((c=getchar()) != '/')
    {
    if(c >= 'a' && c <= 'z')
    {
    sequence[i++] = c ;
    }
    }
    }
    }
    printf("%s\n", sequence) ;

    return 0 ;
    }

    INPUT (its an entire file but i am only concerned the following argument of it

    1 cctcagatca ctctttggca acgacccctc gycacmataa agataggggg gcaactaaag
    61 gaagctctat tagahacagg agcagatgat accatattma aagaaataaa tttgccagga
    121 agatggaarc caaaaatgat agggggaatt ggaggtttta tcaaagtaag acagtatgat
    181 cagatactca tagaaatctg tggacataaa gttataggta cagtattagt aggacctaca
    241 cctgtcaacg taattggaag aaatctgttr actcagattg gttgcacttt aaatttt
    //

    WRONG OUTPUT

    gaagaaatctgttractcagattggttgcactttaaattttgataggggg gcaactaaaggaagctctattagahacaggagcagatgataccatattma aagaaataaatttgccaggaagatggaa

    The output should be:

    >AJ002507 297 bp DNA linear VRL 14-NOV-2006
    cctcagatca ctctttggca acgacccctc gycacmataa agataggggg gcaactaaag
    gaagctctat tagahacagg agcagatgat accatattma aagaaataaa tttgccagga
    agatggaarc caaaaatgat agggggaatt ggaggtttta tcaaagtaag acagtatgat
    cctgtcaacg taattggaag aaatctgttr actcagattg gttgcacttt aaatttt

    If anyone has any advice that would be great.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    Use code tags.
    Main returns int.
    Try to listen.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    77
    How do I use tags. I selected my source code and hit #. All it did was erase my code.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by gkoenig View Post
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    main()
        {
            char test[] = "ORIGIN" ;
            char header[100] ;
            char sequence[100000] ;
            char buffer[10000] ;
            char i, c, n, w ;
            
         /*   for(n=0 ; n < 999 ; ++n)
                { 
                    sequence[n] = 0 ;
                {
        */
                    
            while(fgets(header, 100, stdin))
                {
                    if(strstr(header, "LOCUS"))
                        {
                            int w = 12 ;
                                while(isblank(header[w]))
                                    {
                                        ++w ;
                                    }
                                    printf(">&#37;s", &header[w] ) ;
                                    break ;
                        }
                 }
            
          
                                           
            while(fgets(buffer, 10000, stdin))
                    {
                    if(strstr(buffer, test)) // start obtaining bases after ORIGIN
                        { 
                           int n = 0 ;
                           while((c=getchar()) != '/')
                                {
                                if(c >= 'a' && c <= 'z')
                                    {
                                    sequence[i++] = c ;
                                    }
                                }                            
                        }           
                    }
            printf("%s\n", sequence) ; 
              
            return 0 ;
        }
    INPUT (its an entire file but i am only concerned the following argument of it

    1 cctcagatca ctctttggca acgacccctc gycacmataa agataggggg gcaactaaag
    61 gaagctctat tagahacagg agcagatgat accatattma aagaaataaa tttgccagga
    121 agatggaarc caaaaatgat agggggaatt ggaggtttta tcaaagtaag acagtatgat
    181 cagatactca tagaaatctg tggacataaa gttataggta cagtattagt aggacctaca
    241 cctgtcaacg taattggaag aaatctgttr actcagattg gttgcacttt aaatttt
    //

    WRONG OUTPUT

    gaagaaatctgttractcagattggttgcactttaaattttgataggggg gcaactaaaggaagctctattagahacaggagcagatgataccatattma aagaaataaatttgccaggaagatggaa

    The output should be:

    >AJ002507 297 bp DNA linear VRL 14-NOV-2006
    cctcagatca ctctttggca acgacccctc gycacmataa agataggggg gcaactaaag
    gaagctctat tagahacagg agcagatgat accatattma aagaaataaa tttgccagga
    agatggaarc caaaaatgat agggggaatt ggaggtttta tcaaagtaag acagtatgat
    cctgtcaacg taattggaag aaatctgttr actcagattg gttgcacttt aaatttt
    I don't know whether it's a browser issue, but clicking on # (between the quote bubble and the php, not the key "#") did the above.

    Are you sure you want that header in a loop? I'm not sure whether you're redirecting everything from a file or typing it in at the keyboard, but I can't see why you would want that in a loop.

    And where do you initialize i? Edit to add: and i really really really needs to be an int, not a char, else you're only going to be able to access (probably) 255 elements of your array.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    No, it doesn't. Either you use code tags, aka [code] and [/ code] (except the space),
    or you just select all your code and hit the "#". It will wrap the code in code tags.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    77
    thanks for the advice, I declared my counter variables as chars. I changed them to int and it works fine. Also I found the hash icon.

    My next problem is with my output display. I want to display my array sequence so that only 60 letters are printed per line.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by gkoenig View Post
    thanks for the advice, I declared my counter variables as chars. I changed them to int and it works fine. Also I found the hash icon.

    My next problem is with my output display. I want to display my array sequence so that only 60 letters are printed per line.
    So print it out one character at a time; when you get to sixty, print a \n.

    Note that for-loops will handle both the repetitive printing part and the counting to sixty part; or you can use the &#37; (remainder) operator to see whether you just printed a multiple of sixty.

  11. #11
    Registered User
    Join Date
    Feb 2008
    Posts
    77
    I am having trouble with my loop. I am receiving a "bus error"

    here is my source for the loop:

    f
    Code:
    or(x = 0; x<60 ; ++x)
                {
                printf("%s\n", sequence[x]) ;     
                     if(x==60)
                        {
                            printf("\n");
                        }
                }

    Sorry for incompetence when it comes to coding, I am a not a programmer.
    Any help would be greatly appreciated.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You print a character with %c, not with %s. The for-loop as you have it will only print the first sixty characters, you need to go to strlen(sequence). Something like:
    Code:
    for (x=0; x < strlen(sequence); ++x)
        {
        printf("%c\n", sequence[x]);
        if (x%60 == 0)
            printf("\n");
        }

  13. #13
    Registered User
    Join Date
    Feb 2008
    Posts
    77
    again, thanks for the help. you guys and gals are life-savers

    I have one last issue with my code. My loop that prints my array out is putting 61 letters on the first line and the correct amount of 60 on the rest. How do I get the first line to print 60 also.

    heres the loop:

    Code:
    for(x = 0; x<strlen(sequence) ; ++x)
                {
                printf("%c", sequence[x]) ;                            
                     if((x%60 == 0) && (x!=0))
                        {
                            printf("\n");
                        }
                }

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Do the test before you print the character.

  15. #15
    Registered User
    Join Date
    Feb 2008
    Posts
    77
    Still having a problem. Heres how I tried to change the code:

    Code:
            printf("%c", sequence[0]) ;
            for(x = 1; x<strlen(sequence) ; ++x) // print 60 letters per line.
                {                                        
                     if((x%60 == 0) && (x != 0))
                        {
                            printf("\n") ;
                        }
                     else 
                        {
                        printf("%c", sequence[x]) ;
                        }
                }
    I even tried starting a x=0 in the for loop with the same conditional for the if loop

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Output an array in a textbox
    By Diablo02 in forum C# Programming
    Replies: 5
    Last Post: 10-18-2007, 03:56 AM
  2. Displaying my glut output in my own class window.
    By Queatrix in forum Windows Programming
    Replies: 0
    Last Post: 10-19-2005, 10:09 AM
  3. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21