Simple program on Arrays

This is a discussion on Simple program on Arrays within the C Programming forums, part of the General Programming Boards category; PHP Code: // testing arrays  # include <stdio.h> void main () { int a [ 2 ], i ; printf ( "\nenter elements of array A" ); for ( i = ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    113

    Exclamation Simple program on Arrays

    PHP Code:
    // testing arrays 

    # include <stdio.h>
    void main()
    {
    int a[2],i;
    printf("\nenter elements of array A");

    for (
    i=0;i<=1;i++)

    printf("\nenter element =");
    scanf("%d",&a[i]);    
    }

    printf("\nthe entered array is %d ",a[2]);

    This is the basic array program which takes 2 array elements as input and gives that elements as output.
    I don't understand why my compiler is showing output as 2 no matter what you enter.
    I use gcc in Ubuntu OS

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Your array named a only has 2 elements, so a[2] does not exist. You should be printing the elements of a one by one.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    1.can you give me the program code of it.
    2. When I declared array for only 2 elements, It should print 2 elements if I asked it, why It should matter.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by suryak
    1.can you give me the program code of it.
    Yes, but no, I won't. Considering what you have already done, this should be a piece of cake.

    Quote Originally Posted by suryak
    2. When I declared array for only 2 elements, It should print 2 elements if I asked it, why It should matter.
    Indeed, you are able to print 2 elements. The problem is that you are not printing two elements. You print a single element a[2], but a[2] does not exist. a[0] and a[1] exist.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    Thanks !
    I got it.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    > I use gcc in Ubuntu OS
    And did it complain about void main?
    Or do you just ignore all warnings?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    > I use gcc in Ubuntu OS
    And did it complain about void main?
    Or do you just ignore all warnings?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple arrays
    By DerrickakaDRoC in forum C Programming
    Replies: 12
    Last Post: 12-12-2010, 09:57 PM
  2. A simple program using ARRAYS, STRUCTS, and CLASSES
    By CStudentHelp in forum C++ Programming
    Replies: 8
    Last Post: 04-17-2008, 09:57 PM
  3. Simple program, not so simple problem
    By nolsen in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2008, 09:28 AM
  4. Simple question about arrays
    By tima in forum C Programming
    Replies: 3
    Last Post: 05-21-2007, 09:24 AM
  5. arrays - simple problem
    By ademkiv in forum C Programming
    Replies: 2
    Last Post: 03-02-2006, 08:55 PM

Tags for this Thread


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