Thread: Searching array of structs within interval

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    3

    Searching array of structs within interval

    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;
          }
        }
      }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    I'd start by removing the sorting from findBalls, and just do a linear search based on your input values.

    Also, your indentation needs work.
    Indent style - Wikipedia, the free encyclopedia
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2016
    Posts
    3
    Thanks for the reply. Just started with C, so I appreciate all the tips!

    I added this to my function:

    Code:
    void findBalls(Ball ABALL[], int *nrofballs){
    float max, min;
    int i;
            printf("Set min and max values of the radius: ");
            scanf("%f %f", &min, &max);
    
            for(int i=0; i < *nrofballs; i++){
            if (ABALL[i].radius == min && ABALL[i].radius == max){
            printf("\n%d\t", i);
            printf("Radius: %f\t", ABALL[i].radius);
            printf("X: %f\n", ABALL[i].x);
            }
        }
    }
    But it seems when using && the compiler just ignores the whole for loop. Why is that? Do i need to make two for loop for the linear search to work for two values?

    Thank you.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Maybe you want something other than == as the comparison for a range of values.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2016
    Posts
    3
    Oh, did not know that at all. Now it's working. Thank you very much.
    Instead of starting another topic I will ask in this thread.

    My function for adding balls seems to overwrite all the other elements in the array. Code is as follows

    Code:
    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;
    }
    Is it not possible to make the scanf add balls after last known position? Should it not just be to make the pointer 'go' to the next element - like - (*nrofballs)++?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-20-2011, 09:43 PM
  2. Passing Structs Into An Array Of Structs.
    By TheTaoOfBill in forum C Programming
    Replies: 3
    Last Post: 10-07-2010, 09:38 AM
  3. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  4. Replies: 3
    Last Post: 03-31-2009, 12:34 PM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM

Tags for this Thread