Thread: whats wrong in this simple program

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    75

    whats wrong in this simple program

    i want to print total of 3 numbers using array. i trie but sum is print as zero. tell me whats wrong in the code
    Code:
    #include <stdio.h>main()
    {
    int n,i;
    float a[100],sum=0;
    for(i=0;i<3;i++)
    {
        printf("Enter 3 no to find total ");
    scanf("%d",&a[i]);
    sum=sum+a[i];
    }
    printf("total is %d",sum);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The array named a is an array of float. Hence, the format specifier for scanf should be "%f", not "%d". Your compiler should have warned you of this, so compile at a high warning level and pay attention to the warnings.
    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
    Aug 2015
    Posts
    75
    thx problem solved. so everytime i have to use float before i declare array ? why i cant use int a[100]

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by san12345
    so everytime i have to use float before i declare array ?
    No.

    Quote Originally Posted by san12345
    why i cant use int a[100]
    You can, if that satisfies your requirements.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Whats wrong with simple recursion printing program?
    By tmac619619 in forum C Programming
    Replies: 6
    Last Post: 09-02-2015, 02:41 PM
  2. whats wrong with this very simple code?
    By sway1 in forum C++ Programming
    Replies: 2
    Last Post: 07-06-2008, 12:18 PM
  3. Whats wrong with this simple code?
    By o14v in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2008, 01:46 PM
  4. Whats Wrong in My simple program?
    By MartinLiao in forum C Programming
    Replies: 1
    Last Post: 09-02-2004, 02:30 AM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM