Printing an integer stored in an array

This is a discussion on Printing an integer stored in an array within the C Programming forums, part of the General Programming Boards category; How do I print an integer at a certain address in an array? (say I have an array a[4][5] {0,1,2,3,4...} ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    4

    Printing an integer stored in an array

    How do I print an integer at a certain address in an array?

    (say I have an array a[4][5] {0,1,2,3,4...} and I want to print the number 0 by referencing the address in the array.)
    I have tried a[0], a[0][0], a, *a, a+0 and *a+0.

    none of these work!!

    I thank everyone in advance for their help.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #include <stdio.h>
    int main( void )
    {
        int array[3] = { 1, 2, 3 };
        int mdarray[2][2] = { {1,2}, {3,4} };
        int x,y;
    
        for( x = 0; x < 3; x++ )
        {
             printf("array[%d] is %d\n", x,array[x]);
        }
        for( x = 0; x < 2; x++ )
            for( y = 0; y < 2; x++ )
            {
                 printf("array[%d][%d] is %d\n", x,y,array[x][y]);
            }
        return 0;
    }
    Vola.

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

  3. #3
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    Well if you want to print the first row first column a[0][0] is the correct location.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    4
    For some reason, SOMETHING I'm doing is wrong... Here's my program, so far.. It's not finished, ,please disregard the "sum" function.

    Code:
    #include<stdio.h>
    #define N 20
    int sum(int * , int);
    int main(void)
    {
            int i;
            int a[4][5];
    for (i=0; i<20; ++i);
    scanf("%d", &a[i]);
    
    printf("Precinct       Candidate A       Candidate B       Candidate C       Candidate D\n");
    printf("   1                %d                %d                %d                 %d   \n", a[0][0], a[0][1], a[0][2], a[0][3]);
    printf("   2                %d                %d                %d                 %d   \n", a[1][0], a[1][1], a[1][2], a[1][3]);
    printf("   3                %d                %d                %d                 %d   \n", a[2][0], a[2][1], a[2][2], a[2][3]);
    printf("   4                %d                %d                %d                 %d   \n", a[3][0], a[3][1], a[3][2], a[3][3]);
    printf("   5                %d                %d                %d                 %d   \n", a[4][0], a[4][1], a[4][2], a[4][3]);
    return 0;
    }
    
    int sum(int a[], int n)
    {
            int i;
            int s = 0;
    
            for (i=0; i < n; ++i)
                    s += a[i];
            return s;
    }

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Freez3L
    For some reason, SOMETHING I'm doing is wrong...

    for (i=0; i<20; ++i);
    scanf("%d", &a[i]);
    You have a ; at the end of your for loop. So unless the purpose of your loop is to preform a minute, undetectable, delay, I'd say it's best to leave that one out...

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 01:24 PM
  2. Reading an integer array from console??
    By pico in forum C Programming
    Replies: 16
    Last Post: 12-23-2008, 07:53 AM
  3. Small problem with printing elements of an array
    By DLR in forum C Programming
    Replies: 17
    Last Post: 03-09-2006, 05:57 PM
  4. Converting a "Char" string to an Integer Array
    By Jazz in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2001, 12:50 PM
  5. Replies: 5
    Last Post: 11-20-2001, 11:48 AM

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