Thread: need help

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    40

    need help

    i need help with starting a problem that i have to do for class, you dont have to give me any code just could anyone help me on how to outline how to do it, possibly? I'm just confused i'm new with arrays and i know some about them but i dont know how to tackle this problem, any help would be appreciated thanks,

    here's the assignment: http://userpages.umbc.edu/~macneil/cmsc104/lab8.txt

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    I woudl suggest going to talk to your teacher first if your clueless as where to start.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    40
    ok here's what i've done:

    Code:
    #include <stdio.h>
    #define SIZE 8
    
    void printMenu();
    
    int main()
    {
        int i=0, j=0;
        char choice[80];
        int array[SIZE];
    
        for(i=0;i<SIZE;i++)
          {
              printf( "%d Enter a number: ", i );
              scanf( "%d", &array[i] );
          }
    
        printf( "\nThe Contents of the Array are:\n " );
    
        for(j=0;j<SIZE;j++)
          {
            printf( "%4d", array[j] );
          }
    
        printMenu();
    
        printf( "Enter a Choice: " );
        scanf( "%s", choice );
    
    
    
    
    }
    
    
    void printMenu()
    {
        printf( "\n\n ** Menu **\n\n" );
        printf( "1. * \n" );
        printf( "2. / \n" );
        printf( "3. + \n" );
        printf( "4. - \n" );
        printf( "5. = \n" );
        printf( "6. % \n" );
        printf( "7. Swap \n" );
        printf( "8. Drop \n" );
        printf( "9. Clear \n" );
        printf( "10. Quit \n\n" );
    }
    i need to know how to go to different functions whether the user enters one of the numbers, but more important if they enter a choice like "Swap", what would the syntax look like for a switch statement for something like this or if statements, just so whatever they enter will take them to the appropriate function that i'll write later. Thanks any help in advance.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    40

    some help with a problem please

    here's the assignment: http://userpages.umbc.edu/~macneil/cmsc104/lab8.txt


    and here's what i've done:


    Code:
    #include <stdio.h>
    #define SIZE 8
    
    void printMenu();
    
    int main()
    {
        int i=0, j=0;
        char choice[80];
        int array[SIZE];
    
        for(i=0;i<SIZE;i++)
          {
              printf( "%d Enter a number: ", i );
              scanf( "%d", &array[i] );
          }
    
        printf( "\nThe Contents of the Array are:\n " );
    
        for(j=0;j<SIZE;j++)
          {
            printf( "%4d", array[j] );
          }
    
        printMenu();
    
        printf( "Enter a Choice: " );
        scanf( "%s", choice );
    
    
    
    
    }
    
    
    void printMenu()
    {
        printf( "\n\n ** Menu **\n\n" );
        printf( "1. * \n" );
        printf( "2. / \n" );
        printf( "3. + \n" );
        printf( "4. - \n" );
        printf( "5. = \n" );
        printf( "6. % \n" );
        printf( "7. Swap \n" );
        printf( "8. Drop \n" );
        printf( "9. Clear \n" );
        printf( "10. Quit \n\n" );
    }

    i need to know how to go to different functions whether the user enters one of the numbers, but more important if they enter a choice like "Swap", what would the syntax look like for a switch statement for something like this or if statements, just so whatever they enter will take them to the appropriate function that i'll write later. Thanks any help in advance.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>scanf( "%s", choice );
    Use fgets(), it'll be safer:
    >fgets(choice, sizeof(choice), stdin);

    Then convert the users input to a number using strtol(), then you can use a simple switch statement:
    Code:
    switch (choice_num)
    {
        case 1: func1(); break;
        case 2: func2(); break;
        case 3: func3(); break;
        case 4: func4(); break;
        default: printf("Invalid\n"); break;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    36

    Smile swap

    A part of the solution

    You can write a separte swap function:

    Code:
    void swap(int &a, int &b)
    {
    int temp = a;
    a = b;
    b =  temp;
    }
    something like that...
    then you can call this function
    when
    Code:
    case '7': 
    swap(array[secondlast], array[last]);

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Please don't start new threads on the same topic.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    40
    ok now i'm here can anyone help me by showing me maybe what i should put into the functions themselves? i'm kinda stuck, help would be appreciated thanks,

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define SIZE 8
    
    void printMenu();
    void multiply(int [], int);
    void divide(int [], int);
    void add(int [], int);
    void sub(int [], int);
    void copy(int [], int);
    void remainder(int [], int);
    void swap(int [], int);
    void drop(int [], int);
    void clear(int [], int);
    
    int main()
    {
        int i=0, j=0;
        char choice[80];
        int array[SIZE];
    
        for(i=0;i<SIZE;i++)
          {
              printf( "%d Enter a number: ", i );
              scanf( "%d", &array[i] );
          }
    
        printf( "\nThe Contents of the Array are:\n " );
    
        for(j=0;j<SIZE;j++)
          {
            printf( "%4d", array[j] );
          }
    
        printMenu();
    
        printf( "Enter a Choice: " );
        scanf( "%s", choice );
    
        if((choice[0] == '1') || (choice[0] == '*'))
          {
              multiply( array, SIZE );
          }
        if((choice[0] == '2') || (choice[0] == '/'))
          {
              divide( array, SIZE );
          }
        if((choice[0] == '3') || (choice[0] == '+'))
          {
              add( array, SIZE );
          }
        if((choice[0] == '4') || (choice[0] == '-'))
          {
              sub( array, SIZE );
          }
        if((choice[0] == '5') || (choice[0] == '='))
          {
              copy( array, SIZE );
          }
        if((choice[0] == '6') || (choice[0] == '%'))
          {
              remainder( array, SIZE );
          }
        if((strcmp(choice, "7") == 0) || (strcmp(choice, "Swap") == 0))
          {
              swap( array, SIZE );
          }
        if((strcmp(choice, "8") == 0) || (strcmp(choice, "Drop") == 0))
          {
              drop( array, SIZE );
          }
        if((strcmp(choice, "9") == 0) || (strcmp(choice, "Clear") == 0))
          {
              clear( array, SIZE );
          }
        if((strcmp(choice, "10") == 0) || (strcmp(choice, "Quit") == 0))
          {
              exit(0);
          }
    }
    
    
    void printMenu()
    {
        printf( "\n\n ** Menu **\n\n" );
        printf( "1. * \n" );
        printf( "2. / \n" );
        printf( "3. + \n" );
        printf( "4. - \n" );
        printf( "5. = \n" );
        printf( "6. % \n" );
        printf( "7. Swap \n" );
        printf( "8. Drop \n" );
        printf( "9. Clear \n" );
        printf( "10. Quit \n\n" );
    }
    
    
    void multiply( int b[], int size )
    {
        int n;
    
        for(n=0; n < size; n++)
          {
            if((b[n] == '0') || (n == 7)
            {
    
            }
          }
    }
    
    void divide( int b[], int size )
    {
    
    }
    
    void add( int b[], int size )
    {
    
    }
    
    void sub( int b[], int size )
    {
    
    }
    
    void copy( int b[], int size )
    {
    
    }
    
    void remainder( int b[], int size )
    {
    
    }
    
    void swap( int b[], int size )
    {
    
    }
    
    void drop( int b[], int size )
    {
    
    }
    
    void clear( int b[], int size )
    {
    
    }

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    36

    Talking sorry!

    I hurriedly answered your question just to help you get started(also din't have much time before the class...
    The swap function I wrote before works only for C++. In C everything is pass by value..
    So, please bear me with that if I tried to mislead you, though i didn't mean to...
    The swap function in C looks something like:

    Code:
    void swap(int *x, int *y)
    {
        int temp = *x;
        *x = *y;
        *y = temp;
    }
    then you can call the function as

    Code:
    case 7: swap(&x, &y); break;
    This is final time so don't have much time to go through the question and help you more....

    good luck!
    will be there one day!

    I usually use VStudio 6!

Popular pages Recent additions subscribe to a feed