Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "hilfe.h"
  4. #include <string.h>
  5. double average(double arr[][3], int länge);
  6. double minimum(double arr[][3], int länge);
  7. int main(){
  8.  
  9.     double length[MAX_DATA];
  10.     int n = 0;
  11.     double input;
  12.     char unit[2];
  13.  
  14.     //Intro
  15.     printf(" == Length Converter ==  ");
  16.     printf("\n\nPlease provide the number of records(max 100) you want to enter: ");
  17.     scanf("%d", &n);
  18.  
  19.  
  20.     //Fehlerhandling n<= 0
  21.     if(n > MAX_DATA || n <= 0){
  22.     printf("Error: Please check the number of records you want to enter\n");
  23.     return EXIT_FAILURE;
  24.     }
  25.  
  26.     //Dekleration von Double Array für Werte und für Meterwerte
  27.     double records[n][3];
  28.     double meterwerte[n];
  29.  
  30.     int i;
  31.     for(i = 0; i < n; i++){
  32.         printf("Entry %d : <length> <unit>\n" , i+1);
  33.         scanf("%lf %s",&input,&unit[0]);
  34.  
  35.         // Usereingabe
  36.  
  37.         // Usereingabe (Zahl) in Tabelle Spalte 1 schreiben
  38.         records[i][0] = input;
  39.  
  40.         // Usereingabe (String) in Tabelle Spalte 2 codiert als Double ( 0.00 = mm,..)
  41.         // Überprüfen der Einheit und schreiben in Spalte 3 (in m)
  42.         if(strcmp(unit, "mm") ==0){
  43.             records[i][1] = 0;
  44.             records[i][2] = input/1000;
  45.         }else if(strcmp(unit, "cm") ==0){
  46.             records[i][2] = input/100;
  47.             records[i][1] = 1;
  48.         }else if(strcmp(unit, "dm") ==0){
  49.             records[i][2] = input/10;
  50.             records[i][1] = 2;
  51.         }else if(strcmp(unit, "km") ==0){
  52.             records[i][2] = input * 1000;
  53.             records[i][1] = 3;
  54.         }
  55.     printf("%lf m\n", records[i][2]);
  56.     }
  57.  
  58.  
  59. //  for(int i = 0; i < n; i++){
  60. //      meterwerte[i] = records[i][2];
  61. //  }
  62.  
  63.     puts("\n--------");
  64.     printf("Average: %6.3lf m\n",average(records,n));
  65.     printf("Minimum: %6.3lf m\n", minimum(records,n));
  66.     puts("");
  67.  
  68.     return 1;
  69. }
  70.  
  71. double average(double arr[][3], int länge){
  72.  
  73.     double ret = 0;
  74.     int i;
  75.     for(i = 0; i < länge; i++){
  76.         ret += arr[i][2];
  77.     }
  78.     return ret /= länge;
  79. }
  80. double minimum(double arr[][3], int länge){
  81.  
  82.     double min = arr[0][2];
  83.  
  84.     for(int i = 1; i < länge; i++){
  85.         if(arr[i][2] < min){
  86.             min = arr[i][2];
  87.         }
  88.     }
  89.     return min;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement