Thread: Assignment Problem (Structures)

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    1

    Assignment Problem (Structures)

    Hi there! Basically, I have an issue understanding Question 1.

    I need assistance with my functions "runElevators" and "goToFloor" as I'm not sure what is required in them. I think I did something wrong in "runElevators", but I'm not sure what exactly.

    Any help would be greatly appreciated. Thanks!

    Lab #5 (CS1010)

    Code:
    #include <stdio.h>#include <string.h>
    #define CAPACITY 15
    #define MAX_LEN 21
    #define MAX_ELEVATORS 5
    
    
    typedef struct { 
        int floor, passenger, usage;
    } elevator_t;
    
    
    void setupElevators(elevator_t [], int);
    void readSequences(char [][MAX_LEN], int);
    void runElevators(elevator_t [], char [][MAX_LEN], int);
    void goToFloor(elevator_t *, int);
    void printElevators(elevator_t [], int size);
    int mostUsedElevator(elevator_t [], int);
    
    
    // This function is given. You are NOT to modify it.
    int main(void){
        int size;
        elevator_t elevators[MAX_ELEVATORS];
        char sequences[MAX_ELEVATORS][MAX_LEN];
        
        printf("Enter number of elevators: ");
        scanf("%d", &size);
        
        setupElevators(elevators, size);
        readSequences(sequences, size);
        
        runElevators(elevators, sequences, size);
        
        printElevators(elevators, size);
        printf("Most used elevator: %d\n", mostUsedElevator(elevators, size));
        
        return 0;
    }
    
    
    void setupElevators(elevator_t elevators[], int size){
        
        int i;
    
    
        for(i = 0; i < size; i++)
        {
            elevators[i].floor = 1;
            elevators[i].passenger = 0;
            elevators[i].usage = 0;
        }
    }    
    
    
    // Read in the sequences of floors the elevators go to.
    // This function is given. You are NOT to modify it.
    void readSequences(char sequences[][MAX_LEN], int size){
        int i;
        
        for (i = 0; i<size; i++){
            printf("Enter sequence for elevator %d: ", i+1);
            scanf("%s", sequences[i]);
        }
    }
    
    
    void runElevators(elevator_t elevators[], char sequences[][MAX_LEN], int size){
        
        
        int i, c;
    
    
        for(i = 0; i < size; i++)
            for(c = 0; c < strlen(sequences[i]); c++)
            {    
                elevators[i].floor = sequences[i][strlen(sequences[i]) - 1];
                
                if((elevators[i].passenger < 15) && (sequences[i][c+1] > sequences[i][c]))    
                    elevators[i].passenger += sequences[i][c];
    
    
                if(elevators[i].passenger > 15)
                    elevators[i].passenger = 15;
                    
                if((elevators[i].passenger > 0) && (sequences[i][c] > sequences[i][c+1]))
                    elevators[i].passenger -= sequences[i][c+1];
                
                if(sequences[i][c] > sequences[i][c+1])
                    elevators[i].usage += sequences[i][c+1];
            }
    
    
    }
    
    
    
    
    /*void goToFloor(elevator_t *elevator, int floor){
        
         
    
    
    
           
    void printElevators(elevator_t elevators[], int size){
        
        int i;
    
    
        for(i = 0; i < size; i++)
        {    
            printf("Elevator : %d\n", i+1); 
            printf("Floor: %d\n", elevators[i].floor);
            printf("Number of passengers: %d\n", elevators[i].passenger) ;
            printf("Usage: %d\n", elevators[i].usage);
        }
    }
    
    
    int mostUsedElevator(elevator_t elevators[], int size){
        // Incomplete
        return 1;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > elevators[i].floor = sequences[i][strlen(sequences[i]) - 1];
    At some point, runElevators needs to call gotoFloor.

    Also, note that '2' (the character) does not equal 2 (the integer).

    '2' - '0' is 2
    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
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    No wonder things are so terrible...

    Code:
    // This function is given. You are NOT to modify it.
    int main(void){
    
    // Read in the sequences of floors the elevators go to.
    // This function is given. You are NOT to modify it.
    void readSequences(char sequences[][MAX_LEN], int size){
    And yet there is clearly a very high potential for a buffer overflow (two actually). But you're not allowed to fix either of them. Who wrote the skeleton program and the parts you're not allowed to change?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Hodor
    And yet there is clearly a very high potential for a buffer overflow (two actually). But you're not allowed to fix either of them.
    The functions have access to the MAX_ELEVATORS macro, so they can check if size > MAX_ELEVATORS, and if so ignore the invalid input by using MAX_ELEVATORS as the size, or maybe even terminate the program with an error message.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by laserlight View Post
    The functions have access to the MAX_ELEVATORS macro, so they can check if size > MAX_ELEVATORS, and if so ignore the invalid input by using MAX_ELEVATORS as the size, or maybe even terminate the program with an error message.
    Of course they do. But the student is not allowed to modify the functions already implemented, or main().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-22-2015, 02:44 PM
  2. Assignment problem
    By SterlingM in forum C++ Programming
    Replies: 5
    Last Post: 12-05-2009, 09:43 AM
  3. problem with assignment
    By roaan in forum C Programming
    Replies: 3
    Last Post: 09-08-2009, 10:20 AM
  4. An assignment problem
    By EeeK in forum C Programming
    Replies: 1
    Last Post: 10-03-2003, 07:51 PM
  5. problem of assignment.....
    By skwei81 in forum C++ Programming
    Replies: 1
    Last Post: 03-27-2003, 12:28 PM