Thread: Array Issues

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    28

    Array Issues

    Hi there. I am making a program that asks a user to input 10 elements of an array and then displays the sum of the elements along with the average and how much each element is as a percent of the total.

    My main issue is that I cannot get my mean and how much each element accounts for as a percent of the total. Here is a sample output followed by my code.

    enter the elements
    10
    9
    8
    7
    6
    5
    4
    3
    2
    1
    n percent of total
    ___________________
    10 %d
    9 %d
    8 %d
    7 %d
    6 %d
    5 %d
    4 %d
    3 %d
    2 %d
    1 %d
    The total value of the array is 55
    The mean value of the array is 0

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define THE_LENGTH  10
    void main()
    {
    int a[THE_LENGTH],i,sum = 0;
    double mean = 0;
    printf("enter the elements\n");
    for(i=0;i<THE_LENGTH;i++)
    scanf("%d",&a[i]);
    for(i=0;i<THE_LENGTH;i++)
    {
        sum += a[i];
    }
    printf("n       percent of total\n");
    printf("___________________\n");
    for(i=0;i<THE_LENGTH;i++)
    {
        printf("%d      %%d\n", a[i], a[i]/sum);
    }
    printf("The total value of the array is %d\n",sum);
    printf("The mean value of the array is %d\n", sum/10.0);
    }

  2. #2
    Registered User
    Join Date
    Mar 2013
    Posts
    10
    you should write it like so
    Code:
    printf("The mean value of the array is %.2f\n",(double) sum/10.0);
    and the percent of total
    Code:
        printf("%d      %d\n", a[i],(a[i]*100)/sum);
    Last edited by cena06; 03-10-2013 at 06:07 PM.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    I see now. Thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array issues
    By LearnOnTheFly in forum C Programming
    Replies: 7
    Last Post: 03-29-2012, 04:18 PM
  2. array issues
    By Rob123 in forum C Programming
    Replies: 9
    Last Post: 04-26-2009, 09:35 AM
  3. Array Issues
    By deviousdexter in forum C# Programming
    Replies: 9
    Last Post: 11-24-2008, 09:04 AM
  4. Array Issues
    By GCNDoug in forum C Programming
    Replies: 12
    Last Post: 04-16-2007, 12:27 PM