Hallo zusammen,
ich hab ein programm geschrieben das mit rand() und %5 einen Array zahle zuweist.
Nun möchte ich die 0er, 1er, 2er...und 4er zaehlen.
Das zaehlen mache ich mit 5 If-Anweisungen.
Gibt es statt den 5 Ifs auch was einfacheres?
Hier das Programm:
Alles anzeigen
MfG C_Boy
ich hab ein programm geschrieben das mit rand() und %5 einen Array zahle zuweist.
Nun möchte ich die 0er, 1er, 2er...und 4er zaehlen.
Das zaehlen mache ich mit 5 If-Anweisungen.
Gibt es statt den 5 Ifs auch was einfacheres?
Hier das Programm:
C-Quellcode
- #include <stdio.h>
- #include <stdlib.h>
- void erzeugen(float a[],int length,int x);
- int zaehlen(float a[]);
- void erzeugen(float a[],int length,int x)
- {
- a[length]=rand()%5;
- }
- int zaehlen(float a[])
- {
- int x=0;
- int rot=0;
- int gelb=0;
- int lila=0;
- int rosa=0;
- int schwarz=0;
- if(a[x] == '0')
- {
- rot++;
- }
- if(a[x] == '1')
- {
- gelb++;
- }
- if(a[x] == '2')
- {
- lila++;
- }
- if(a[x] == '3')
- {
- rosa++;
- }
- if(a[x] == '4')
- {
- schwarz++;
- }
- return(rot,gelb,lila,rosa,schwarz);
- }
- int main()
- {
- float a[300];
- int length=0;
- int x=0;
- int rot=0;
- int gelb=0;
- int lila=0;
- int rosa=0;
- int schwarz=0;
- for(x=0; x<=20; x++)
- {
- erzeugen(a,length,x);
- printf("a[%d] = %f\n",length,a[length]);
- length++;
- }
- rot,gelb,lila,rosa,schwarz=zaehlen(a);
- printf("%d",rot,gelb,lila,rosa,schwarz);
- return(0);
- }
MfG C_Boy