Hello.
I'm having a difficult time trying to solve a problem I have.
This program randomises radius and x-coordinate for balls in an array of structs. What I want to do is find balls within an intervall specified by user input.
For example, lets say the user only wants to see balls that have radius between 1 and 4, the program should then only show all the balls that have the radius 1,2,3,4.
Can someone please nudge me in the right direction on how to solve this? Thank you.
My code so far:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NBALL 100
typedef struct{
float radius;
float x;
}Ball;
int addBalls(Ball ABALL[], int *nrofballs);
void viewBalls(Ball ABALL[], int *nrofballs);
void findBalls(Ball ABALL[], int *nrofballs);
int main(void){
srand(time(NULL));
char mainmenu;
Ball ABALL[NBALL]={0};
int *nrofballs, i;
nrofballs=&i;
do{
printf("vaf");
scanf(" %c", &mainmenu);
switch(mainmenu){
case 'q': printf("Program exit success."); break;
case 'v': viewBalls(ABALL, &i); break;
case 'a': addBalls(ABALL, &i); break;
case 'f': findBalls(ABALL, &i); break;
default : printf("Error.\n"); break;
}
}while (mainmenu!='q');
return 0;
}
void viewBalls(Ball ABALL[], int *nrofballs){
for(int i=0; i < *nrofballs; i++){
printf("\n%d\t", i);
printf("Radius: %f\t", ABALL[i].radius);
printf("X: %f\n", ABALL[i].x);
}
}
int addBalls(Ball ABALL[], int *nrofballs){
int i;
i=*nrofballs;
printf("How many: ");
scanf("%d", nrofballs);
for (i=0; i < *nrofballs ; i++){
ABALL[i].radius = rand() %9+1;
}
for (i=0; i < *nrofballs; i++){
ABALL[i].x = rand() %100+0;
}
*nrofballs=i;
return *nrofballs;
}
void findBalls(Ball ABALL[], int *nrofballs){
float max, min;
int i, j, swap;
printf("Set min and max values of the radius: ");
scanf("%f %f", &min, &max);
/* Sort balls first */
for (i=0; i < (*nrofballs-1); i++){
for (j=0; j < *nrofballs-i-1; j++){
if (ABALL[j].radius > ABALL[j+1].radius){
swap = ABALL[j].radius;
ABALL[j].radius = ABALL[j+1].radius;
ABALL[j+1].radius = swap;
}
}
}
}