Thread: Arrays and stuff..

  1. #1
    ---
    Join Date
    May 2004
    Posts
    1,379

    Arrays and stuff..

    i know how good you guys are at helping people out here so i trust you will help me too.

    how can i reproduce this function....

    Code:
    void printDetail(void)
    {
            /*
             * Displays final result
             * Prints entire contents of array according to the co-ordinates given from determineCol and determineRow
             */
            fprintf(fpout,"\n");
            fprintf(fpout,"       REPEAT   %3d     %3d     %3d      %3d     %3d     %3d    %3d\n\n",coords[0][0],coords[0][1],coords[0][2],coords[0][3],coords[0][4],coords[0][5],coords[0][TOTALCOL]);
            fprintf(fpout,"TELEVISION AD   %3d     %3d     %3d      %3d     %3d     %3d    %3d\n\n",coords[1][0],coords[1][1],coords[1][2],coords[1][3],coords[1][4],coords[1][5],coords[1][TOTALCOL]);
            fprintf(fpout," NEWSPAPER AD   %3d     %3d     %3d      %3d     %3d     %3d    %3d\n\n",coords[2][0],coords[2][1],coords[2][2],coords[2][3],coords[2][4],coords[2][5],coords[2][TOTALCOL]);
            fprintf(fpout,"     RADIO AD   %3d     %3d     %3d      %3d     %3d     %3d    %3d\n\n",coords[3][0],coords[3][1],coords[3][2],coords[3][3],coords[3][4],coords[3][5],coords[3][TOTALCOL]);
            fprintf(fpout,"WORD OF MOUTH   %3d     %3d     %3d      %3d     %3d     %3d    %3d\n\n",coords[4][0],coords[4][1],coords[4][2],coords[4][3],coords[4][4],coords[4][5],coords[4][TOTALCOL]);
            fprintf(fpout,"        OTHER   %3d     %3d     %3d      %3d     %3d     %3d    %3d\n\n",coords[5][0],coords[5][1],coords[5][2],coords[5][3],coords[5][4],coords[5][5],coords[5][TOTALCOL]);
            fprintf(fpout,"        =====  ====    ====    ====     ====    ====    ====   ====\n");
            fprintf(fpout,"        TOTAL  %4d    %4d    %4d     %4d    %4d    %4d   %4d\n",coords[TOTALROW][0],coords[TOTALROW][1],coords[TOTALROW][2],coords[TOTALROW][3],coords[TOTALROW][4],coords[TOTALROW][5],coords[TOTALROW][TOTALCOL]);
            fprintf(fpout,"        =====  ====    ====    ====     ====    ====    ====   ====\n");
    
    }
    by using this method to show the values in the array.

    Code:
    //EXAMPLE ONLY
    void printDetail()
    {
            int i,j;                                
            for (i=0;i<=7;++i)
            {                                       
                    for(j=0;j<=7;++j)               
                    {                               
                            fprintf(fpout,"%d",coords[i][j]);
                    }
            }
    }

    What i mean is i want to use the above method but how do i get around the fact that i have the row headings on the same lines? as in the first example?
    The output format in the first example is how its supposed to look.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int i, j;
    char *headers[] =
    {
        "REPEAT", 
        "TELEVISION AD",
        "NEWSPAPER AD",
        "RADIO AD",
        "WORD OF MOUTH",
        "OTHER",
        "TOTAL"
    };
    
    for( i = 0; i < 7; i++ )
    {
        fprintf( file, "%13s\t", headers[i] );
        for( j = 0; j < 7; j++ )
        {
            fprintf( file "%3d\t", array[j] );
        }
        fprintf( file, "\n" );
        if( i == 4 || i == 6 )
            fprintf( file, "...your line of === and what not...");
    }
    I assume that's something like what you're talking about. If it's wrong, blame strong drink.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    i only half understand your concept here.

    even after fixing the syntax errors (strong booze will do that ) this code still doesnt format it the right way...

    let me attach my src code and the input file.

    once this source code is compiled the output is perfect i just want to change that function using the above method.
    Last edited by sand_man; 05-05-2004 at 08:53 AM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The only thing that's wrong with that code is it's missing a comma. Other than that, if you actually have array declared as an array of integers, it compiles and gives the exact output you want, execpt I didn't fill in the line of === as I stated. Here is my output once I add the missing comma:
    Code:
    :~/programming$ ./foof
           REPEAT     1       3       4      55      66      77      99
    TELEVISION AD     1       3       4      55      66      77      99
     NEWSPAPER AD     1       3       4      55      66      77      99
         RADIO AD     1       3       4      55      66      77      99
    WORD OF MOUTH     1       3       4      55      66      77      99
    ...your line of === and what not...
            OTHER     1       3       4      55      66      77      99
            TOTAL     1       3       4      55      66      77      99
    ...your line of === and what not...
    :~/programming$
    So perhaps you should either expalin better what it is you want to do, or relook at the code I wrote. Oh, and to be honest, I did replace file with stdout because I didn't feel like opening a file. However, the end result is the same if you fix the line where your === is supposed to go with however you want it to look. But then, if I did everything for you, you wouldn't learn anything.

    [edit]Ah. I see the difference. Change the 4 to a 5, lose the 6, and print your final === line after the end. Other than that', it's the same. Still, not too shabby for off the top of my head while addled.[/edit]

    Quzah.
    Last edited by quzah; 05-05-2004 at 05:31 PM.
    Hope is the first step on the road to disappointment.

  5. #5
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    quzah,

    I have a question about your code.

    Code:
    char *headers[] =
    {
    "REPEAT", 
    "TELEVISION AD",
    "NEWSPAPER AD",
    "RADIO AD",
    "WORD OF MOUTH",
    "OTHER",
    "TOTAL"
    };
    I keep seeing declarations that to my eyes (newbie) look invalid.
    I'm assuming you are making a multi-demensional array pointer with those values.
    Does it not need to have [][] to declare that? This is similar to seeing a string literal declared as.

    Code:
    char *a="hello world";
    None of my text say you can do either of these declarations.

    By the way happy Cinco everybody.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It is an array of character pointers, which are pointing to string literals. You don't have to specify the size of the array (elements), because it's a single dimension array. Remembering that you can initialize arrays when you create them, gives us the final step of assigning string literals to each pointer in the array.

    Just like you do:
    Code:
    char *pointer = "Hello World!";
    I'm doing the same thing with an array of pointers to characters.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    ---
    Join Date
    May 2004
    Posts
    1,379
    thanks for you help Quzah.
    you were right in the first place i just was too tired to understand.
    it works and it works well

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays
    By lilhawk2892 in forum C++ Programming
    Replies: 5
    Last Post: 07-04-2006, 12:04 AM
  2. Arrays stuff
    By incognito in forum C++ Programming
    Replies: 3
    Last Post: 03-08-2003, 08:56 AM
  3. Hello all and pointers to multi-dimensional arrays
    By Mario in forum C++ Programming
    Replies: 16
    Last Post: 05-17-2002, 08:05 AM
  4. Arrays as function parameters
    By Flake in forum C++ Programming
    Replies: 2
    Last Post: 03-07-2002, 02:05 AM
  5. arrays and validation (basic stuff)
    By Fountain in forum C++ Programming
    Replies: 2
    Last Post: 12-21-2001, 04:25 PM