Buch Cover Buch Cover Buch Cover Buch Cover

Web-Code: - Webcode Help

Feld filtern (2) (Felder)

Füllen Sie ein Feld mit den Zahlen:

2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14.

Schreiben Sie nun ein Programm, das aus dem obigem Feld das größte und kleinste Element eliminiert und das Resultat  als neues Feld zurückgibt. Der Prototyp der Funktion soll wie folgt aussehen:

elim(original: integer[]): integer[]

Bemerkung: Das neue Feld enthält somit weniger Elemente und wird dadurch kürzer.

Zusatzaufgabe: Sollte das Maximum (bzw. Minimum) mehrfach auftreten, so sind alle Exemplare zu löschen. Im gegebenen Zahlenbeispiel würden die Zahl Eins und beide Siebzehner (17) gelöscht. Das Maximum bzw. das Minimum könnte hierbei in einer Subroutine ermittelt werden.

6 Kommentare

Bitte melde dich an um einen Kommentar abzugeben

Kommentare (6)

mrdotzko 10. April 2018 14:49   reply report
tmxmaster
Ich würde bei der Aufgabenstellung (zumindest im Zusatzaufgabenteil) noch ergänzen, dass das größte und das kleinste Element jeweils in Unterprogrammen zu ermitteln ist. Und dass die Unterprogramme dann in der Funktion elim verwendet werden sollen.

ich würde in die Aufgebenstelllung evtl. noch die Lösung schreiben, dann können sie alle lösen.
wwbaselmemes 10. April 2018 14:40   reply report
mrdotzko
sach ma des is mal ne schwierige Aufgabe ne

ja find isch auch ne
mrdotzko 10. April 2018 14:39   reply report
sach ma des is mal ne schwierige Aufgabe ne
wwbaselmemes 10. April 2018 14:33   reply report
tmxmaster
Ich würde bei der Aufgabenstellung (zumindest im Zusatzaufgabenteil) noch ergänzen, dass das größte und das kleinste Element jeweils in Unterprogrammen zu ermitteln ist. Und dass die Unterprogramme dann in der Funktion elim verwendet werden sollen.

ok cool
wwbaselmemes 10. April 2018 14:32   reply report
holla
tmxmaster 27. April 2011 00:34   reply report
Ich würde bei der Aufgabenstellung (zumindest im Zusatzaufgabenteil) noch ergänzen, dass das größte und das kleinste Element jeweils in Unterprogrammen zu ermitteln ist. Und dass die Unterprogramme dann in der Funktion elim verwendet werden sollen.

8 Lösung(en)

public class Elim {
  public static void main(String[] args) {
    new Elim().top();    }

  void top() {
    int[] arr = {2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14};
    int[] elim = elim(arr);
    ausgabe(elim); }

  
  int[] elim(int[] arr) {
    int min = findMin(arr);
    int max = findMax(arr);
    int count = countElesOhneMinMax(arr, min, max);
    int[] res = new int[count];
    ohneMinMaxUebertragen(arr, res, min, max);
    return res;  }  

  void ohneMinMaxUebertragen(int[] arr, int[] res,
                                   int min, int max) {
    int fromIdx = 0;
    int toIdx = 0;
    while(fromIdx < arr.length) {
        if(arr[fromIdx] != min && arr[fromIdx] != max) {
            res[toIdx] = arr[fromIdx];
            toIdx = toIdx + 1; }
        fromIdx = fromIdx + 1;  }  
  }

  int countElesOhneMinMax(int[] arr, int min, int max) {
    int count = 0;
    for(int x : arr) {
        if(x != min && x != max) {
            count = count + 1;  }
    }
    return count; }

  int findMin(int[] arr) {
    int min = arr[0];
    for(int m : arr) {
        if(m < min) {
            min = m;  }
    }
    return min; }

  int findMax(int[] arr) {
    int max = arr[0];
    for(int m : arr) {
        if(m > max) {
            max = m;  }
    }
    return max; }
  
  void ausgabe(int[] elim) {
    for(int x : elim) {
        System.out.print(x  + ", ");  }
  }
  
} // end of class Elim
                
def findminmax(a):
   a.sort()
   b=[]
   b.append(a[0])
   b.append(a[len(a)-1])
   return b

def elim(a):
   minmax=findminmax(a)
   for zahl in minmax:
      for i in range (a.count(zahl)):
         a.remove(zahl)
   return a

print elim([2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14])
                
  public int[] elim(int[] original){
  
    // Temporäres Integer-Feld zur Aufnahme der neu gereihten Elemente
    int[] f = new int[original.length];
    
    // Elementzaehler für die Elemente ohne Max und Min
    int countElem = 0;
    
    int max = this.getMaxValue(original);
    int min = this.getMinValue(original);
    
    for (int i = 0; i < original.length; i++){

      // Aktuelles Element bestimmen
      int elem = original[i];
      
      if (elem != max && elem != min){
        countElem = countElem + 1;
        f[countElem-1] = elem;
      }
    }
    int[]erg = new int[countElem]; // Rückgabefeld anlegen

    // Kopieren der nicht gefilterten Elemente in das Rückgabefeld.
    for (int i = 0; i < countElem; i++){
      erg[i] = f[i];
    }

    return erg;
  }

  public int getMaxValue(int[] feld){
    int max = feld[0];
    for (int i = 1; i < feld.length; i++){
      int elem = feld[i];
      if (elem > max){
        max = elem;
      }
    }
    return max;
  }
  
  public int getMinValue(int[] feld){
    int min = feld[0];
    for (int i = 1; i < feld.length; i++){
      int elem = feld[i];
      if (elem < min){
        min = elem;
      }
    }
    return min;
  }
                

Lösung von: Jürgen Mang (Heinrich-Emanuel-Merck-Schule Darmstadt)

package ch.santis.programmierenlernen.kapitel6;

public class Aufgabe_6_4 {
	
	public static void main(String[] args) {
		new Aufgabe_6_4().top();
	}
	
	// Aufgabe: Die grössten und kleinsten Elemente eliminieren
	void top() {
		
		int [] array1 = {2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14};
		
		// Grösste und Kleinste Zahl im Array1 bestimmen
		int max = Integer.MIN_VALUE;
		int min = Integer.MAX_VALUE;
		for(int index = 0; index < array1.length; index++) {
			if(max < array1[index]) {
				max = array1[index];
			}
			if(min > array1[index]) { // Bewusst kein else if verwendet(!)
				min = array1[index];
			}	
		}
		// Array2 Länge bestimmen
		int array2length = 0;
		for(int indexwert : array1) {
			if(indexwert < max && indexwert > min) {
				array2length++;
			}
		}
		// Array2 deklarieren und initialisieren (ohne max und min Zahlen von Array1)
		int [] array2;
		array2 = new int [array2length];
		int index2 = 0;
		for(int indexwert : array1) {
			if(indexwert < max && indexwert > min) {
				array2[index2] = indexwert;
				index2++;
			}
		}
		
		// Printen
		System.out.println("Kleinste Zahl: " + min + " || Grösste Zahl: " + max);
		for(int index : array1) {
			System.out.print(index + " ");
		}
		System.out.println("\n=============================");
		for(int index : array2) {
			System.out.print(index + " ");
		}
		
	}
	
}
                

Lösung von: Jan Roth (Santis Training AG)

let theArr = [2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14],
    elim = theArr.slice(),   // dies fertigt eine kopie eines arrays an
    min = Math.min.apply(null, theArr),
    max = Math.max.apply(null, theArr);

for (i = 0; i <= elim.length; i++)
  if (elim[i] == min || elim[i] == max) {
    elim.splice(i, 1);
    i--;
  }

console.log(elim);
                

Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)

// NET Core 3.x; C# 8.x

using System;
using System.Collections.Generic;
using System.Linq;

namespace CS_Aufgabe_Feld_Filtern
{
    class Program
    {
        static void Main(string[] args)
        {
            var oList = new List<int>() { 2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14 };
            Print(oList, "Ausgangsliste: ");

            var eList = oList.Where(x => x == oList.Min() || x == oList.Max()).ToList();
            Print(eList, "Entfernte Elemente: ");

            var nList = oList.Where(x => x != oList.Min() && x != oList.Max()).ToList();
            Print(nList, "Neue Liste: ");

            static void Print(List<int> lst, string def = "") => Console.WriteLine($"{def}{string.Join(", ", lst.ToArray())}");
        }
    }
}
                

Lösung von: Jens Kelm (@JKooP)

// C++ 14 | VS-2022

#include <iostream>
#include <vector>
#include <algorithm>

std::vector<int> remove_min_max_element(const std::vector<int>& v_in) {
    if (v_in.empty()) return v_in;
    std::vector<int> v{ v_in };
    auto max{ *std::max_element(v.begin(), v.end()) };
    auto min{ *std::min_element(v.begin(), v.end()) };
    auto it{ std::remove_if(v.begin(), v.end(), [&max, &min] (int i) { return i == max || i == min; }) };
    v.erase(it, v.end());
    return v;
}

void print(const std::vector<int>& v) {
    if (v.size() == 0) return;
    auto it{ --v.end() };
    for (auto i{ v.begin() }; i != it; i++)
        std::cout << *i << ", ";
    std::cout << v.back() << " ";
}

int main() {
    const std::vector<int> v{ 2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14 };
    print(remove_min_max_element(v));
}
                

Lösung von: Jens Kelm (@JKooP)

// C++20 | VS-2022

#include <iostream>
#include <vector>
#include <algorithm>
#include <ranges>


inline constexpr auto remove_min_max_element(const auto& con_) {
    if (con_.empty()) return con_;
    std::vector<int> out{ con_ };
    const auto min_max{ std::ranges::minmax(out) };
    const auto rem{ std::ranges::remove_if(out, [&min_max](const auto& i) { return i == min_max.min || i == min_max.max; }) };
    out.erase(rem.begin(), rem.end());
    return out;
}

inline constexpr auto print(auto&& con_) {
    if (con_.size() == 0) return;
    const auto it{ --con_.end() };
    for (auto i{ con_.begin() }; i != it; i++)
        std::cout << *i << ", ";
    std::cout << con_.back() << " ";
}

int main() {
    const std::vector<int> v{ 2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14 };
    print(remove_min_max_element(v));
}
                

Lösung von: Jens Kelm (@JKooP)

Verifikation/Checksumme:

2, 10, 9, 16, 3, 9, 16, 5, 14.

Aktionen

Bewertung

Durchschnittliche Bewertung:

Eigene Bewertung:
Bitte zuerst anmelden

Meta

Zeit: 1
Schwierigkeit: Mittel
Webcode: 3hiq-vtfi
Autor: Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch)

Download PDF

Download ZIP

Zu Aufgabenblatt hinzufügen