Thread: help with this code please

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    90

    help with this code please

    i wrote this code it prints average ,min,max in array but the problem is that the average doesn't print correctly . help plz
    Code:
    #include <stdio.h>
    #include <conio.h>
    void func(int A[],int size,int *Max,int *Min,int *Sum,int *Avg)
    {
    for(int i=0;i<size;i++)
    {
    if(*Max<A[i])
    *Max=A[i];
    if(*Min>A[i])
    *Min=A[i];
    }
    for(int i=0;i<size;i++)
    {
    *Sum=*Sum+A[i];
    *Avg=*Sum/10;
    }
    }
    
    main()
    {
    int A[10],Max,Min,Sum;
    int Avg;
    for(int i=0;i<10;i++)
    {
    printf("plz enter the element # ",i+1);
    scanf("%d",&A[i]);
    }
    func(A,10,&Max,&Min,&Sum,&Avg);
    printf("The average of the elements in the array is %d",Avg);
    printf("The max is %d and the min is %d",Max,Min);
    getch();
    }

  2. #2
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    ops forgot to initialize sum=0

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You also need to initialise Max and Min the main function. You may find certain values available from <limits.h> useful in doing this.

    By the way, please indent your code properly.
    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

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    ok thanks for the help , will do

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Sheesh - 70 posts and no indentation.

    Mmm, conio.h AND C99 for loops.
    Lemme guess, you're compiling using C++ in TurboC.
    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. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM