Results 1 to 3 of 3
  1. #1

    Lightbulb Problem for average in array


    The problem is :

    Create an array of N integers. Ask from the user how many element he/she wants to input in the array. Determine the sum of all numbers, the average of all even numbers and count how many are odd numbers and how many zero's were inputed ? at the end, print the accumulated and counted values of the array.

    Karon akong problem man gud kay kung mu input ko ug 6 elements din mo-input ko ug numbers nga 0,1,2,3,4,5 kay ang iyahang average sa even numbers kay 2.5 instead of 2 kay ang zero kay even mana diba? so dapat jud ana kay (0+2+4)/3 kay 3 man ka even numbers ang na input. Confused au ko ngano 2.5 jud iyahang output sa average sa even.

    Here is my code :

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    main()
    {
          int num[]={};
          int max=0,i,sumofzero=0,zero=0,sumodd=0,sumeven=0;
          float sum=0;
          float ave=0;
          
          
          printf("How many elements you want? : ");
          scanf("%d", &max);
          
          for(i=0;i<max;i++)
          {
                            printf("Enter number  %d : \n",i );
                            scanf("%d", &num[i]);
                             
                               sum+=num[i];  
                             
          }
                            for(i=0;i<max;i++)//for even average numbers
                            {
                                if(num[i]%2==0)
                                           
                                             
                                           ave=(sum/max);
                                             
                            }
                            
                                  for(i=0;i<max;i++)//for odd numbers
                                      {
                                       if(num[i]%2!=0) 
                                                   
                                           sumodd+=1;         
                                       } 
                                                    
                                   for(i=0;i<max;i++)//for zero numbers
                                      {
                                      if(num[i]==0)
                                      
                                      sumofzero+=num[i]+1;
                                      } 
                                     
                                           
                                           printf("The sum of all numbers is : %.2f \n", sum);
                                           printf("The average of all even numbers is %.2f \n", ave);
                                           printf("The total odd numbers were inputed is : %d \n", sumodd); 
                                           printf("The sum of all zero numbers is : %d \n", sumofzero); 
                                           
                                           
                                           getch();
                                           
                                           }

    pero if mu input ko ug 5 ra ka elements din ang numbers kay 0,1,2,3,4 kay ang output sa average is 2 w/c is sakto. Unsa diay kulang or sayop sa akong code? Hope maka tabang mo, thank you!

  2. #2

    Default Re: Problem for average in array

    murag diri ang sayop---> ave=(sum/max);

    'sum' should be 'sumeven', di ba?
    also, don't divide it by 'max'. ang 'max' is ALL the number inputted. if you are getting the average of EVEN numbers, then you should get the COUNT of EVEN numbers and use that to calculate your average.

    pero, if you meant the average number of EVEN numbers in the array, then count pila ka even numbers ang nitunga then divide that by 'max'

  3. #3

    Default Re: Problem for average in array

    Just a logic error. Problem solved above my post.

  4.    Advertisement

Similar Threads

 
  1. Best Places for Romance in Cebu?
    By wAn_eyE in forum Destinations
    Replies: 559
    Last Post: 05-08-2018, 08:00 AM
  2. Skin and Fur Problems for Dogs~
    By chanbri in forum Pet Discussions
    Replies: 228
    Last Post: 07-31-2011, 07:00 AM
  3. Color properties in JQuery Problems for IE6
    By janmuy in forum Programming
    Replies: 6
    Last Post: 06-21-2011, 05:00 PM
  4. blue screen problem for windows 7 in laptop
    By andriy in forum Gizmos & Gadgets (Old)
    Replies: 3
    Last Post: 12-16-2010, 02:41 PM
  5. wanted to watch: PUBLIC DEBATES or FORUM for ISTORYANS in person
    By la_gnostic in forum Politics & Current Events
    Replies: 54
    Last Post: 02-12-2007, 04:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top