Thread: Simple program on Arrays

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.

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, 10: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, 10: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, 09:55 PM

Tags for this Thread