Thread: Help With Pointers

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    36

    Help With Pointers

    I'm making a program that would do something like this:


    Enter the mark out of 10 for a set of 20 students:

    7 6 9 4 7 7 5 10 8 6 5

    Below is the chart of number of students vs mark out of 10


    Number Of
    Students
    10| *
    9| *
    8| *
    7| *
    6|
    5| *
    4| *
    3| *
    2| *
    1| *
    0|
    -------------------------------------------
    0 1 2 3 4 5 6 7 8 9 10
    (Marks out of 10)
    Please note that the stars will not line up properly in this post, but you all get the idea. The stars is suppose to go to the intersection of the Number of the Student, and his/her mark.

    I made an array of 10 ints. Now I'm guessing that the *'s in the chart will be entered by using pointers? Since I'm not too familiar with pointers, please someone give me an example of how to do this.

    And the with the chart itself, I just use printf() to enter them in a fixed location? ex:

    Code:
    printf("\n");
    printf("\n");
    printf("Number of\n");
    printf("Students\n");
    printf("\n");
    printf("10|\n");
    printf("\n");
    printf("9 |\n");
    printf("\n");
    printf("8 |\n");
    printf("\n");
    printf("7 |\n");
    printf("\n");
    printf("6 |\n");
    printf("\n");
    printf("5 |\n");
    printf("\n");
    printf("4 |\n");
    printf("\n");
    printf("3 |\n");
    printf("\n");
    printf("2 |\n");
    printf("\n");
    printf("1 |\n");
    printf("\n");
    printf("0 |\n");
    printf("   --------------------------------------------------------------\n");
    printf("   0     1     2     3     4     5     6     7     8     9     10\n");
    printf("                                   Mark (10)                     \n");
    I am very new at this so please bare with me. I do apologize that my questions may seem stupid since this work may seem very easy from your standards.
    Last edited by 98dodgeneondohc; 04-29-2005 at 07:45 AM.

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    I don't think you really need pointers for this.

    If you're trying to draw a little histogram in ASCII, just draw n asterisks across to get a bar that's n units long.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    Quote Originally Posted by joshdick
    I don't think you really need pointers for this.

    If you're trying to draw a little histogram in ASCII, just draw n asterisks across to get a bar that's n units long.
    I don't understand what u mean. Please give me an example.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    Guys, what I need to know is how I will take the marks (entered by the user), and enter it in the chart.

    I know how to read inputs from the user. I just don't know how to output them on the chart, in the proper spot.

    For example, say the user enters 9, 8, 10 etc... the they all go into the array that I made --> int marks[10]

    Now I need to take those marks from the array, then enter them into the chart as a --> * , in their proper location of coarse. So if the First student gets 9 (mark), the * will appear in the intersection of Number Of Students: 1 and Marks: 9
    Last edited by 98dodgeneondohc; 04-29-2005 at 07:56 AM.

  6. #6
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Just output 8 spaces before the asterisk to put it in the ninth position.

    Perhaps you could use a 2-D char array to hold the grid of asterisks and spaces. Initialise the array with all spaces. Then, when the first student gets a 9 mark, just array[9][1] = '*';

    When you're done with filling in those amounts, output the 2-d array using a nested for loop.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       int i, j, mark[] = {7,6,9,4,7,7,5,10,8,6,5};
       for ( i = 0; i < sizeof mark / sizeof *mark; ++i )
       {
          printf("%2d|", i + 1);
          for ( j = 0; j < mark[i]; ++j )
          {
             fputs("  ", stdout);
          }
          puts("*");
       }
       puts("  +----------------------");
       puts("   0 1 2 3 4 5 6 7 8 9 10");
       return 0;
    }
    
    /* my output
     1|              *
     2|            *
     3|                  *
     4|        *
     5|              *
     6|              *
     7|          *
     8|                    *
     9|                *
    10|            *
    11|          *
      +----------------------
       0 1 2 3 4 5 6 7 8 9 10
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Or you could just wait till someone codes it for you
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    Quote Originally Posted by Dave_Sinkula
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       int i, j, mark[] = {7,6,9,4,7,7,5,10,8,6,5};
       for ( i = 0; i < sizeof mark / sizeof *mark; ++i )
       {
          printf("%2d|", i + 1);
          for ( j = 0; j < mark[i]; ++j )
          {
             fputs("  ", stdout);
          }
          puts("*");
       }
       puts("  +----------------------");
       puts("   0 1 2 3 4 5 6 7 8 9 10");
       return 0;
    }
    
    /* my output
     1|              *
     2|            *
     3|                  *
     4|        *
     5|              *
     6|              *
     7|          *
     8|                    *
     9|                *
    10|            *
    11|          *
      +----------------------
       0 1 2 3 4 5 6 7 8 9 10
    */

    How would I modify this array so that it instead takes the values from the user input?

  10. #10
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    Quote Originally Posted by Dave_Sinkula
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       int i, j, mark[] = {7,6,9,4,7,7,5,10,8,6,5};
       for ( i = 0; i < sizeof mark / sizeof *mark; ++i )
       {
          printf("%2d|", i + 1);
          for ( j = 0; j < mark[i]; ++j )
          {
             fputs("  ", stdout);
          }
          puts("*");
       }
       puts("  +----------------------");
       puts("   0 1 2 3 4 5 6 7 8 9 10");
       return 0;
    }
    
    /* my output
     1|              *
     2|            *
     3|                  *
     4|        *
     5|              *
     6|              *
     7|          *
     8|                    *
     9|                *
    10|            *
    11|          *
      +----------------------
       0 1 2 3 4 5 6 7 8 9 10
    */

    mark[] = {7,6,9,4,7,7,5,10,8,6,5};

    How would I modify this array so that it instead takes the values from the user input?

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by joshdick
    Or you could just wait till someone codes it for you
    Is 3 lines of code really going overboard and giving away the farm? Or 11 lines if you consider the full context? Maybe there's 50 lines total if you count an example of how to preserve whitespace using code tags.
    Quote Originally Posted by 98dodgeneondohc
    I don't understand what u mean. Please give me an example.
    Quote Originally Posted by 98dodgeneondohc
    Guys, what I need to know is how I will take the marks (entered by the user), and enter it in the chart.

    I know how to read inputs from the user. I just don't know how to output them on the chart, in the proper spot.

    For example, say the user enters 9, 8, 10 etc... the they all go into the array that I made --> int marks[10]

    Now I need to take those marks from the array, then enter them into the chart as a --> * , in their proper location of coarse. So if the First student gets 9 (mark), the * will appear in the intersection of Number Of Students: 1 and Marks: 9
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    How would I modify this array so that it instead takes the values from the user input?
    Declare the array with some size, say mark[20], then loop through and obtain each.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by 98dodgeneondohc
    Code:
    mark[] = {7,6,9,4,7,7,5,10,8,6,5};


    How would I modify this array so that it instead takes the values from the user input?
    I know how to read inputs from the user.
    Then again, maybe not.

  14. #14
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    Quote Originally Posted by swoopy
    Then again, maybe not.

    I meant I know how to read inputs from users such as using scanf(), fgets() etc... then assigning those values to variables. I am not really familiar with assigning integer values into int arrays by using one of the functions i mentioned above.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    array[ something ] = somevalue;
    I'd suggest you get yourself a good C book and start from the beginning. It's not a race. Just pay attention and don't continue on to the next lesson until you fully understand the one you're on.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM