New to C

This is a discussion on New to C within the C Programming forums, part of the General Programming Boards category; Hi all was hoping someone could help me out here. Im new to C so the problem might seem simple. ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    3

    New to C

    Hi all was hoping someone could help me out here.
    Im new to C so the problem might seem simple. I have an array that is [10][10] and i am attempting to access a specific int from the array and print it out. The how would i go about doin this in a function?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    By passing the array to the function and accessing the element(s) you want.

    Show us your code, and we will help you better.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    3
    Sorry I forgot the code might help this is all i've got so far its a tutorial i am trying to complete for my tutor


    Code:
    -  UW PICO(tm) 2.3                    File: part1.c-------------------Modified
    
    #include <stdio.h>
    #include <string.h>
    int menu_ops(void);
    void display(int[][]);
    int main(void)
    
    {
    
    
    int p1;
    int p2;
    
    
    
          int distance[10][10] =
          {
          { 0,23,12,89,456,123,46,732,345,123},
          { 23,0,46,234,123,46,89,234,567,90},
          { 12,46,0,767,456,46,234,123,732,35},
          { 89,234,767,0,732,32,48,67,98,100},
          { 456,123,456,732,0,234,46,89,89,732},
          { 123,46,46,32,234,0,123,46,123,234},
          { 46,89,234,48,46,123,0,46,89,19},
          { 732,234,123,67,89,46,46,0,123,732},
          { 345,567,732,98,89,123,89,123,0,78},
          { 123,90,35,100,732,234,19,78,732,0}
          };
    
    
    int option,rtn;
    option = menu_ops();
    while(option != 'Q' && option != 'q')
      {
      switch(option)
          {
            case 'a': case 'A': /*display distance between 2 points */
    
                                     break;
            case 'b': case 'B': /*display distance and cost */
                                     break;
            case 'c': case 'C': /*display milage chart */
                                     display(distance);
                                     break;
            default:  printf("Invalid choive try again");
          }
      option = menu_ops();
      }
       return 0;
    }
    
    
    int menu_ops(void)
    {
      char opt;
      char rtn;
      printf("\n A. Display distance between 2 points\n");
      printf("B.  Display distance between 2 points and the cost\n");
      printf("C.  Display the milage chart\n");
      printf("please enter your option (Q quits the sytem):  ");
      scanf("%c%c", &opt,&rtn);
      return opt;
    }
    
    
     void display(int chart[10][10])
     {
       int row, col;
       for (row=0; row<10;row++)
       {
          for (col=0; col<10;col++)
          printf("%4d", chart[row][col]);
          printf("\n");
       }
     }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by shadds View Post
    and i am attempting to access a specific int from the array and print it out. The how would i go about doin this in a function?
    Quote Originally Posted by shadds View Post

    Code:
     void display(int chart[10][10])
     {
       int row, col;
       for (row=0; row<10;row++)
       {
          for (col=0; col<10;col++)
          printf("%4d", chart[row][col]);  /* Printing out one specific value -- row # row, column # col */
          printf("\n");
       }
     }
    That would appear to be how you print out an array element (in this case, a lot of them) from a function.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Aside from the fact that your prototype declaration:
    Code:
    void display(int[][]);
    and defintion:
    Code:
    void display(int chart[10][10])
    are mismatched, I don't see anything directly wrong.

    What is not working?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    3
    yea that is my program that works. I am now looking at creating a function that can pick a point from the [10][10] array and print it out. for example the user will select from my menu to display the distance from two points. the thing is i cant find anything in my notes on how to achieve this i was thinking using a pointer but im a bit unsure how to go about this, or even if this is the best way to do it.

    p.s the [10][10] array is a milage chart so if i picked row 5 and col 5 the system would print the middle point
    Last edited by shadds; 04-08-2008 at 04:16 PM.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So how do you THINK you should solve it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

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