Showing posts with label Average. Show all posts
Showing posts with label Average. Show all posts

C Program Array Example: Average

How to write a C Program Array Example: Average in C Programming Language ?


Solution For C Program :

Write the procedure , which is one of a sum , a product and a geometric average in the panel for the NxM elements are located on opposite diagonal and main diagonal . Remind ! Counting only odd elements !

Write the procedure , which is one of a sum , a product and a geometric average in the panel for the NxM elements are located on opposite diagonal and main diagonal . Remind ! Counting only odd elements !


Schreib die Prozedur, die zählt eine Summe, einen Produkt und einen geometrischen Durchschnitt in der Tafel NxM fur die Elemente befinden sich über gegenläufigen Diagonale und unter hauptsächlichen Diagonale. Erinner! Zähl nur für ungerade Elementen!


Solution For C Program :

//Schreib die Prozedur, die zählt eine Summe, einen Produkt und einen geometrischen Durchschnitt in der Tafel NxM fur die Elemente befinden sich über gegenläufigen Diagonale und unter hauptsächlichen Diagonale. Erinner! Zähl nur für ungerade Elementen!

void prozedur (int tafel[N][M])
{
int i=0,j=0,Summe=0,Produkt=1;
float Durchschnitt;
for(i;i<N;i++)
   for(j;j<M;j++)
      if(i>j && i<(N-1)-j && tafel[i][j]!=0)
         Summe+=tafel[i][j];
         Produkt*=tafel[i][j];
Durchschnitt=Summe/(N*M);
printf("Summe ergibt: %d Produkt ergibt: %d geometrischer Durchschnitt ergibt: %d,Summe,Produkt,Durchschnitt);
}

C Program Array NxM Elements Geometric/Arithmetic

How to Write a C Program for numbering the product of all real numbers listed in the array NxM elements,  or function counting the arithmetic average of all the numbers listed in the array NxM Elemental, for counting the geometric mean NxM array elements and for counting the arithmetic average above the main diagonal in the table NxM elements in C Programming Language ?


//Napisać procedurę liczącą iloczyn wszystkich liczb rzeczywistych znajdujących się w tablicy NxM elementowej.

Write procedure for numbering the product of all real numbers listed in the array NxM elements.



void iloczyn (int tablica[N][M])
{
 int i,j,iloczyn=0;
for(i=0;i<N;i++)
    for(j=0;j<M;j++)
       iloczyn*=tablica[i][j];
printf("Iloczyn liczb w tablicy wynosi: ",iloczyn);
}

//Napisać procedurę lub funkcję liczącą średnią arytmetyczną wszystkich liczb znajdujących się w tablicy NxM elementowej.

Write a procedure or function counting the arithmetic average of all the numbers listed in the array NxM Elemental



float srednia_aryt(int tablica[N][M])
{
int i,j,suma=0;
for(i=0;i<N;i++)
    for(j=0;j<M;j++)
       suma+=tablica[i][j];
return suma/N*M;
}

 //Napisać procedurę liczącą średnią geometryczną w tablicy NxM elementowej.

//Write procedure for counting the geometric mean NxM array elements.



float srednia_geo(int tablica[N][M])
{
int i,j,iloczyn=0;
for(i=0;i<N;i++)
    for(j=0;j<M;j++)
       iloczyn*=tablica[i][j];
return pow(iloczyn,NxM);
}

   //Napisać procedurę liczącą średnią arytmetyczną ponad główną przekątna w tablicy NxM elementowej.

Write procedure for counting the arithmetic average above the main diagonal in the table NxM elements.



float srednia_aryt(int tablica[N][M])
{
int i,j,suma=0;
for(i=0;i<N;i++)
    for(j=0;j<M;j++)
       if(i<j)
         suma+=tablica[i][j];
return suma/N*M;
}

C Program To Find The Average Of n Numbers

How to write a C Program To Find The Average Of n Numbers in C Programming Language ?

Solution For C Program:

/*C Program To Find The Average Of n Numbers*/

#include<stdio.h>
void main()
{
int n, count;
float sum = 0, x, avg;
printf("\nEnter How Many Numbers : ");
scanf("%d", &n);
for(count = 1; count <= n; count++)
{
printf("x = ");
scanf("%f", &x);
sum += x;
}
avg = sum / n;
printf("\nThe Average of Numbers is : %0.2f", avg);
}

You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable

C Program to Average temperature in quarters

How to write a C Program to Average temperature in quarters in C Programming Language ?


This C Program to Average temperature in quarters.

]Solution:
  1.  printf("\n\n\nAverage temperature in quarters :\n\n ");
  2.     printf("     JAN-MAR   APR-JUN   JUL-SEP   OCT-DEC\n");
  3.     for(rok=0; rok<=15; rok++)
  4.     {
  5.         printf("%d ",(1995+rok));
  6.         for(mesic=0; mesic<=3; mesic++)
  7.         {
  8.             printf(" %4.1f     %",mean_row(temp, rok+1, 1+mesic*3, 3+mesic*3));
  9.         }
  10.         printf("\n");
  11.     }

C Program Average Temperature in Quarters

How to write a C Program Average Temperature in Quarters in C Programming Language ?


This C Program Finds Average temperature in quarters.

Solution:

  1.  printf("\n\n\nAverage temperature in quarters :\n\n ");
  2.     printf("     JAN-MAR   APR-JUN   JUL-SEP   OCT-DEC\n");
  3.     for(rok=0; rok<=15; rok++)
  4.     {
  5.         printf("%d ",(1995+rok));
  6.         for(mesic=0; mesic<=3; mesic++)
  7.         {
  8.             printf(" %4.1f     %",mean_row(temp, rok+1, 1+mesic*3, 3+mesic*3));
  9.  
  10.         }
  11.         printf("\n");
  12.     }

Average function in C

C Program: Average Function




#include <limits.h>

int avg(int a, int b)
{
    int max = INT_MAX;
    int min = INT_MIN;
    int hmax = max / 2;
    int hmin = min / 2;

    if ((a == max) && (b == max)) {
        return max;
    }
    else if ((a == min) && (b == min)) {
        return min;
    }
    else if (a == b) {
        return a;
    }

    // 2 conditions of failure left to cover
    // 1 A == max , B > hmax
    // 2 B == max , A > hmax
    // Fuck
    if(a > hmax ) {
        if (b > hmax) {
            return hmax + (((a - hmax) + (b - hmax)) / 2);
        }
        else if (b > 0) {
            return (a / 2) + (b / 2);
        }
        else {
            return ((a + b) / 2);
        }
    }
    else if (a < hmin) {
        if (b < hmin) {
            return hmin + (((a - hmin) + (b - hmin)) / 2);
        }
        else if (b < 0) {
            return (a / 2) + (b / 2);
        }
        else {
            return ((a + b) / 2);
        }
    }
    else {
        return ((a + b) / 2);
    }
}

C Program Number of Judge and Score From Each Judge

How to Write a C Program reads in the number of judges and the score from each judge. Then it calculates the average score without regard to the lowest and highest judge score. Finally it prints the results (the highest, the lowest and the final average score).


Solution:

#include <stdio.h>
 
void printInfo(void) {
 printf("Program information\n");
 printf("The program reads in the number of judges and the score from each judge.\n");
 printf("Then it calculates the average score without regard to the lowest and\n");
 printf("highest judge score. Finally it prints the results (the highest, the\n");
 printf("lowest and the final average score).\n");
}
 
int readJudges(void) {
 int number;
 
 while (1) {
  printf("Number of judges (min 3 and max 10 judges)? ");
  scanf("%d", &number);
   if (number >= 3 && number <= 10) {
    return number;
   }
 }
 return 0;
}
 
/*Reads judge scores from user */
void readScore(int number, double judgeScore[]) {
    printf("\n");
 double points;
 
 for(int i = 0 ; i < number ; i++) {
  printf("Score from judge %d? ", i+1);
  scanf("%lf", &points);
  judgeScore[i] = points;
 }
}
 
void printScore(int number, double judgeScore[]) {
 printf("\n");
 printf("Loaded scores:\n");
 
 for(int i = 0 ; i < number ; i++) {
  printf("Judge %d: %.1f\n", i+1, judgeScore[i]);
 }
}
 
/* Returns highest value of 'score' as a pointer */
void highestValue(double* max, int number, double judgeScore[]) {
 max = judgeScore;
 for(int i = 0 ; i < number ; i++) {
  if (judgeScore[i] > *max) {
            max = judgeScore + i;
  }
 }
}
 
/* Returns lowest value of 'score' as a pointer */
void lowestValue(double* min, int number, double judgeScore[]) {
 min = judgeScore;
 for(int i = 0 ; i < number ; i++) {
  if (judgeScore[i] < *min) {
            min = judgeScore + i;
  }
 }
}
 
void average(int number, double judgeScore[], double* avg, double* min, double* max) {  
    double sum;
        for(int i = 0 ; i < number ; i++) {
            sum += judgeScore[i];
        }
        *avg = (sum - *max - *min)/(number - 2); /* Beräknar "medel" utan att ta
             hänsyn till minsta och största */;
}
 
void printResult(double max, double low, double avg) {
 printf("Final result:\n");
 printf("Highest judge score: %.1f\n", max);
 printf("Lowest judge score: %.1f\n", low);  
 printf("Final average score: %.1f\n", avg);
}
 
int main(void) {
 printInfo();
       
 printf("\n");
       
 int number;
 number = readJudges();
       
 double score[number]; /* Arrayens storlek beror på readJudges */
 readScore(number, score);
 printScore(number, score); /* Efter readScore exekverats innehåller score alla poäng */
       
 printf("\n");
       
 double avg, low, max;
 lowestValue(&low, number, score);
 highestValue(&max, number, score);
 average(number, score, &avg, &low, &max);

 printResult(max, low, avg);

 return 0;
}

C Program to accept n numbers from user store these numbers into an array & calculate average of n numbers

How to write a c program to accept n numbers from user store these numbers into an array & calculate average of n numbers in C Programming Language ?

Solution:
/*C Program to accept n numbers from user store these numbers into an array & calculate average of n numbers*/
#include<stdio.h>
#include<conio.h>
  void main()
{
  int a[20],n,i,t=0;
  float avg=0;
  clrscr();
  printf("\nEnter the n numbers: ");
  scanf("%d",&n);
  printf("\nEnter the numbers: ");
  for(i=0;i<n;i++)
{
  scanf("%d",&a[i]);
  t=t+a[i];
}
  printf("\nTotal is:%d",t);
  avg=(float)t/n;
  printf("\nAverage of n numbers is: %f",avg);
  getch();
}

//OUTPUT:
//Enter the n numbers: 10
//Enter the numbers: 1
//2
//3
//4
//5
//6
//7
//8
//9
//10

//Total is: 55
//Average of n numbers is: 5.5

Accept n number from user, store these number into an array and calculate the average of n number

How to write a C Program to accept n number from user, store these number into an array and calculate the average of n number in C Programming Language ?


Solution:
/*write a c progarm to accept n number from user, store
these number into an array and calculate the average of n
number*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,sum=0;
float avg;
clrscr();
printf("\nenter how many number :");
scanf("%d",&n);
printf("\nenter the element :\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
avg=sum/n;
printf("\nthe average is :%f",avg);
getch();
}
/*
enter how many number :5                                                    
                                                                           
enter the element :                                                        
9                                                                          
8                                                                          
7                                                                          
4                                                                          
5                                                                          
                                                                           
the average is :6.000000                                                    
*/