|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <iostream>
#include<ctime>
using namespace std;
int main(){
int *x = new int[6];
cout<<"Waehlen Sie 6 Zahlen von 1 bis 49: "<<endl;
for(int y = 0; y < 6; y++){
cout << y+1 << ". Zahl: ";
cin >> x[y];
}
int zufallsZahl = 0;
srand( (int) time(NULL) ); // ist einmalig notwendig damit der Zufallsgenerator funktioniert,
int *arr= new int[6];
for (int i=0; i<6; i++) { // i<20, wir wollen 20x wuerfeln
zufallsZahl = rand(); // "rand();" gibt eine zufällige Interger-Zahl zwischen 1 und 32768 zurück
zufallsZahl = zufallsZahlI + 1; // jetzt muessen wir von unserer Zufallszahl so lange 6 abziehen, bis sie kleiner als 6 ist
// Ausgabe auf dem Bildschirm
arr = zufallsZahl;
cout << zufallsZahl << " " ;
}
int treffer = 0;
for(int j = 0; j<6; j++)
for(int k = 0; k<6; k++)
if(x[j] == arr[k])
treffer++;
cout<<"Sie haben " << treffer <<" Treffer!"<<endl;
return 0;
}
|
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include<iostream>
using namespace std;
int main(){
int ar;
cout << "Array: ";
cin >> ar;
bool *arr = new bool[ar];
for(int i=0 ; i<ar ; i++)
arr=true;
arr[0]=false;
arr[1]=false;
for(int a=2 ; a<ar ; a++){
if(arr[a]==true){
for(int b=2 ; a*b<ar ; b++)
arr[a*b] = false;// Alle Geradezahlen nach dem kleinsten Index(=2) sind jetzt falsch
}
}
for(int i=0 ; i<ar ; i++)
if(arr==true)
cout<<i<<"\t";
cout<<endl;
return 0;
}
|
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#include<iostream>
using namespace std;
// Diese Funktion dient dem Ausgaben des Arrays
void print(int *arr, int laenge){
for(int i = 0; i < laenge; i++)
cout << arr[i] << "\t";
cout << endl;
}
// Diese Funktion potenziert jeden Arrayeintrag. Am Ende der Funktion steht an
// jeder Stelle die dritte Potenz des ursprünglichen Eintrags.
void potenzieren(int *arr, int laenge){
for(int i = 0; i < laenge; i++)
arr[i] = arr[i]*arr[i]*arr[i];
}
int main(){
unsigned int i;
cout << "Bitte die Laenge eingeben: ";
cin >> i;
// Erstellen eines dynamischen Arrays (wird nächste Woche ausführlicher besprochen)
int * arr = new int [i];
for (int ind = 0; ind < i; ind++)
arr[ind] = ind + 1;
print(arr, i);
potenzieren(arr, i);
print(arr, i);
return 0;
}
|
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#include <iostream>
#include<ctime>
using namespace std;
void zahlenAbfragen(){
int *x = new int[6];
cout<<"Waehlen Sie 6 Zahlen von 1 bis 49: "<<endl;
for(int y = 0; y < 6; y++){
cout << y+1 << ". Zahl: ";
cin >> x[y];
delete[] x}
}
void zufallszahlenErzeugen()
{int zufallsZahl = 0;
srand( (int) time(NULL) ); // ist einmalig notwendig damit der Zufallsgenerator funktioniert,
int *arr= new int[6];
for (int i=0; i<6; i++) { // i<20, wir wollen 20x wuerfeln
zufallsZahl = rand(); // "rand();" gibt eine zufällige Interger-Zahl zwischen 1 und 32768 zurück
zufallsZahl = zufallsZahl%49 + 1; // jetzt muessen wir von unserer Zufallszahl so lange 6 abziehen, bis sie kleiner als 6 ist
// Ausgabe auf dem Bildschirm
arr[i] = zufallsZahl;
cout << zufallsZahl << " " ;
}
}
void resultatVergleichen()
{int treffer = 0;
for(int j = 0; j<6; j++)
for(int k = 0; k<6; k++)
if(x[j] == arr[k])
treffer++;
cout<<"Sie haben " << treffer <<" Treffer!"<<endl;
}
int main(){
zahlenabfrage();
zufallszahlenErzeugen();
resultatVergleichen();
return 0;
}
|
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include<iostream>
using namespace std;
int main(){
int ar;
cout << "Array: ";
cin >> ar;
bool *arr = new bool[ar];
for(int i=0 ; i<ar ; i++)
arr[i]=true;
arr[0]=false;
arr[1]=false;
for(int a=2 ; a<ar ; a++){
if(arr[a]==true){
for(int b=2 ; a*b<ar ; b++)
arr[a*b] = false;// Alle Geradezahlen nach dem kleinsten Index(=2) sind jetzt falsch
}
}
for(int i=0 ; i<ar ; i++)
if(arr[i]==true)
cout<<i<<"\t";
cout<<endl;
return 0;
}
|
|
|
C/C++ Quellcode |
1 |
void zahlenAbfragen() |
|
|
C/C++ Quellcode |
1 |
zahlenAbfragen(); |
|
|
C/C++ Quellcode |
1 |
zahlenabfrage(); |
|
|
C/C++ Quellcode |
1 2 3 4 5 |
#include <iostream> #include<ctime> using namespace std; int *x = new int[6]; int *arr= new int[6]; |
|
|
C/C++ Quellcode |
1 |
resultatVergleichen() |
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
#include <iostream>
#include<ctime>
using namespace std;
int *x = new int[6];
int *arr= new int[6];
void zahlenAbfragen(){
int *x = new int[6];
cout<<"Waehlen Sie 6 Zahlen von 1 bis 49: "<<endl;
for(int y = 0; y < 6; y++){
cout << y+1 << ". Zahl: ";
cin >> x[y];
delete[] x;}
}
void zufallszahlenErzeugen()
{int zufallsZahl = 0;
srand( (int) time(NULL) ); // ist einmalig notwendig damit der Zufallsgenerator funktioniert,
int *arr= new int[6];
for (int i=0; i<6; i++) { // i<20, wir wollen 20x wuerfeln
zufallsZahl = rand(); // "rand();" gibt eine zufällige Interger-Zahl zwischen 1 und 32768 zurück
zufallsZahl = zufallsZahl%49 + 1; // jetzt muessen wir von unserer Zufallszahl so lange 6 abziehen, bis sie kleiner als 6 ist
// Ausgabe auf dem Bildschirm
arr[i] = zufallsZahl;
cout << zufallsZahl << " " ;
}
}
void resultatVergleichen()
{int treffer = 0;
for(int j = 0; j<6; j++)
for(int k = 0; k<6; k++)
if(x[j] == arr[k])
treffer++;
cout<<"Sie haben " << treffer <<" Treffer!"<<endl;
}
int main(){
zahlenAbfragen();
zufallszahlenErzeugen();
resultatVergleichen();
return 0;
}
|

![]()
C/C++ Quellcode
1 2 3 4 5 #include <iostream> #include<ctime> using namespace std; int *x = new int[6]; int *arr= new int[6];
Deklarier sie Global, denn in
![]()
C/C++ Quellcode
1 resultatVergleichen()
gibt es Sie nicht, deshalb kann er Sie auch nicht finden.
Jetzt müsste es eigentlich gehen, hab bei mir noch nicht alle Fehler entfernt...
![]()
den fehler bekomm ich nach der abfrage.
Quoted
Der Fehler zeigt meistens an dass du entweder über einen Speicherbereich hinaus und hinein in einen andereren Block schreibst oder versuchst etwas freizugeben was bereits freigegeben wurde. Er tritt jedenfalls leider immer erst beim Versuch den korrupten Speicher freizugeben auf und nicht schon dort wo der eigentliche Fehler passiert.
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
void zahlenAbfragen(){
int *x = new int[6];
cout<<"Waehlen Sie 6 Zahlen von 1 bis 49: "<<endl;
for(int y = 0; y < 6; y++){
cout << y+1 << ". Zahl: ";
cin >> x[y];
delete[] x;}
}
|
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
void zufallszahlenErzeugen()
{int zufallsZahl = 0;
srand( (int) time(NULL) ); // ist einmalig notwendig damit der Zufallsgenerator funktioniert,
int *arr= new int[6];
for (int i=0; i<6; i++) { // i<20, wir wollen 20x wuerfeln
zufallsZahl = rand(); // "rand();" gibt eine zufällige Interger-Zahl zwischen 1 und 32768 zurück
zufallsZahl = zufallsZahlI + 1; // jetzt muessen wir von unserer Zufallszahl so lange 6 abziehen, bis sie kleiner als 6 ist
// Ausgabe auf dem Bildschirm
arr[i] = zufallsZahl;
cout << zufallsZahl << " " ;
}
}
|
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
void resultatVergleichen()
{int treffer = 0;
for(int j = 0; j<6; j++)
for(int k = 0; k<6; k++)
if(x[j] == arr[k])
treffer++;
cout<<"Sie haben " << treffer <<" Treffer!"<<endl;
}
|
|
|
C/C++ Quellcode |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
void zahlenAbfragen(){ int *x = new int[6]; cout<<"Waehlen Sie 6 Zahlen von 1 bis 49: "<<endl; for(int y = 0; y < 6; y++){ cout << y+1 << ". Zahl: "; cin >> x[y]; delete[] x;} } |
|
|
C/C++ Quellcode |
1 2 3 4 5 6 7 8 9 10 |
int addieren(int zahl1, int zahl2) // 2 Parameter { int zahl1; int zahl2; summe = zahl1 + zahl2; return int summe; } |