Thread: creating dynamic arrays

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

    creating dynamic arrays

    I need help creating an array that will take the size of a an input value so far I've tried this

    Code:
    void custom (int amount)
    {
            int *custh, *custl, *delarray;
            int i;
            unsigned char pthold = inp(0x43), logical, sndhold = (0x61);
            custh = (int *) malloc( amount*sizeof( int ) );
            custl = (int *) malloc(amount*sizeof( int ) );
            delarray = (int *) malloc(amount*sizeof( int ) );
            
            printf("Please enter the high, low values for the notes and the delay:\n");
            
            for(i = 0; i < amount; i++)
            {
                    printf("High value #%d: ", i + 1);
                    scanf("%d", &custh[i]);
                    printf("Low value #%d: ", i + 1);
                    scanf("%d", &custl[i]);
                    printf("Delay of #%dst value: ", i + 1);
                    scanf("%d", &delarray[i]);
            }
            outp(0x43, pthold | 0xB6);
            
            for(i = 0; i < amount; i++)
            {
            timer(custh[i], custl[i]);
            speaker(sndhold, delarray[i]);
            }
    }
    But all it does is take one input value and then just gets stuck in a loop
    Last edited by Cnewb; 12-01-2002 at 12:11 AM.

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    6
    Oh yeah I'm using Turbo C++ 3 to compile it

  3. #3
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    to make an array with a size that you don't find out untill runtime you can do

    int *array;

    int size;

    //read in the size

    (*array) = new int[size];

    I think that should work.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    6
    I tried that and it wouldn't compile here's the complete code.

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    void timer ( int h,int l);
    void speaker (unsigned char sndh, int del);
    void custom (int amount);
    int main()
    {
            int high[8] = {0x98, 0x00, 0xCA, 0xE9, 0xDA, 0x1F, 0x3A, 0xE4};
            int low[8] = {0x0A, 0x0A, 0x11, 0x08, 0x0F, 0x0E, 0x0D, 0x0B};
            int sequence[25] = {2, 2, 4, 2, 6, 5, 2,2, 4, 2, 7, 6, 2, 2, 3, 0, 6, 5, 4, 1, 1, 0, 6, 7, 6};
            int delarray[25] = {500,500, 1000, 1000, 1000, 2000, 500, 500, 1000, 1000, 1000, 2000, 500, 500, 1000, 1000, 1000, 1000, 3000, 500, 500, 1000, 1000, 1000, 2000};
            unsigned char pthold = inp(0x43), logical, sndhold = inp(0x61);
            int delay = 0, select = 0, i, amt;
            
            printf("\n\nMusic Program\n");
            printf("--------------------------------------\n\n");
            printf("1.\t Play Happy Birthday\n");
            printf("2.\t Enter your own notes\n");
            printf("\n\n--------------------------------------\n\n");
            printf("Enter selection please: ");
            scanf("%d", &select);
            
            if(select == 2)
            {
            printf("Please enter amount of notes: ");
            scanf("%d", &amt);
            custom(amt);
            }
    
            else if(select == 1)
            {
            for(i = 0; i < 25; i++)
            {
            timer(high[sequence[i]], low[sequence[i]]);
            speaker(sndhold, delarray[i]);
            }
            }
    }
    
    void custom (int amount)
    {
            int *custh, *custl, *delarray;
            int i;
            unsigned char pthold = inp(0x43), logical, sndhold = (0x61);
            (*custh) = new int[amount];
            (*custl) = new int[amount];
            (*delarray) = new int[amount];
            
            printf("Please enter the high, low values for the notes and the delay:\n");
            
            for(i = 0; i < amount; i++)
            {
                    printf("High value #%d: ", i + 1);
                    scanf("%d", &custh[i]);
                    printf("Low value #%d: ", i + 1);
                    scanf("%d", &custl[i]);
                    printf("Delay of #%dst value: ", i + 1);
                    scanf("%d", &delarray[i]);
            }
            outp(0x43, pthold | 0xB6);
            
            for(i = 0; i < amount; i++)
            {
            timer(custh[i], custl[i]);
            speaker(sndhold, delarray[i]);
            }
    }
    
    void timer (int h, int l)
    {
           outp(0x42, h);
           outp(0x42, l);
    }
    
    void speaker (unsigned char sndh, int del)
    {
        outp(0x61, sndh | 0x23);
        delay(del);
        outp(0x61, sndh);
    }
    The program is supposed to play "happy birthday" over the pc speaker, what I'm attempting to do is
    allow the input of custom notes into an array that
    won't be defined until runtime and then have them play back in sequence to play other songs. I get these errors when I try to compile it:

    Turbo C++ Version 3.00 Copyright (c) 1992 Borland International
    snda2a.c:
    Warning snda2a.c 40: Function should return a value in function main
    Warning snda2a.c 40: 'delay' is assigned a value that is never used in function
    main
    Warning snda2a.c 40: 'pthold' is assigned a value that is never used in function
    main
    Error snda2a.c 47: Undefined symbol 'new' in function custom
    Error snda2a.c 47: Statement missing ; in function custom
    Error snda2a.c 48: Statement missing ; in function custom
    Error snda2a.c 49: Statement missing ; in function custom
    Warning snda2a.c 62: Code has no effect in function custom
    Warning snda2a.c 73: Code has no effect in function timer
    Warning snda2a.c 74: Code has no effect in function timer
    Warning snda2a.c 79: Code has no effect in function speaker
    Warning snda2a.c 81: Code has no effect in function speaker
    *** 4 errors in Compile ***
    Last edited by Cnewb; 12-01-2002 at 01:01 AM.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    6
    I tried this

    Code:
            (*custh) = custh[amount];
            (*custl) = custl[amount];
            (*delarray) = delarray[amount];
    it compiles but it does the same thing as my initial problem. After I input the first value for the custh array it gets stuck in a loop and begins playing the note of the value inputted.

  6. #6
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by beege31337
    to make an array with a size that you don't find out untill runtime you can do

    int *array;

    int size;

    //read in the size

    (*array) = new int[size];

    I think that should work.
    This is the C Programming section not the C++ section. In C there is no such as new and no such thing as delete/delete []

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    - delete new'd memory.
    - your compiler thinks you're using C, not C++
    - I'm not sure what (*custh) does, etc.

    Code:
    /*edit: decomment C code to quell the compiler */
    void custom (int amount)
    {
       int *custh, *custl, *delarray;
       int i;
       unsigned char pthold = inp(0x43), logical, sndhold = (0x61);
       custh = new int[amount]; // (int*)malloc(amount*sizeof(int));
       custl = new int[amount]; // (int*)malloc(amount*sizeof(int));
       delarray = new int[amount];// (int*)malloc(amount*sizeof(int));
            
            printf("Please enter the high, low values for the notes and the delay:\n");
            
            for(i = 0; i < amount; i++)
            {
                    printf("High value #%d: ", i+1 );
                    scanf("%d", &custh[i]);
                    printf("Low value #%d: ", i+1 );
                    scanf("%d", &custl[i]);
                    printf("Delay of #%dst value: ", i+1 );
                    scanf("%d", &delarray[i]);
            }
            outp(0x43, pthold | 0xB6);
            
            for(i = 0; i < amount; i++)
            {
            timer(custh[i], custl[i]);
            speaker(sndhold, delarray[i]);
            }
    delete custh; // free(custh);
    delete custl; // free(custl);
    delete delarray;// free(delarray);
    
    
     return;
    }
    Last edited by Sebastiani; 12-01-2002 at 01:53 AM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Sebastiani
    - your compiler thinks you're using C, not C++
    There is a reason he posted in the C Programming section, not the C++ section as I said earlier

  9. #9
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    I know the way I showed was wrong for C , but since he said he was using a C++ compiler I thought I would give him the C++ answer.

  10. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    6
    My instructor told us we could only use a 16-bit compiler to make our programs that's why I'm using such an old one. Also this is for a C programming course so yeah I wouldn't be allowed to use the C++ commands even though I didn't realize they were C++ until poly mentioned it. Thanks for the suggestions I think I'll give salem's suggestion a try tomorrow and see how it goes since I'm getting rather tired of looking at it.

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    6
    I tried this:

    Code:
    void custom (int amount)
    {
            int *custh, *custl, *delarray;
            int i;
            unsigned char pthold = inp(0x43), logical, sndhold = (0x61);
            
            custh =  malloc( amount*sizeof( int ) );
            if( custh == NULL ) 
            {
            printf("Can't allocate\n");
            }
            free(custh);
            
            custl =  malloc(amount*sizeof( int ) );
            if( custl == NULL )
            {
            printf("Can't allocate\n");
            }
            free(custl);
            
            delarray =  malloc(amount*sizeof( int ) );
            if( delarray == NULL )
            {
            printf("Can't allocate\n");
            }
            free(delarray);
            
            printf("Please enter the high, low values for the notes and the delay:\n");
            
            for(i = 0; i < amount; i++)
            {
                    printf("High value #%d: ", i + 1);
                    scanf("%d", &custh[i]);
                    printf("Low value #%d: ", i + 1);
                    scanf("%d", &custl[i]);
                    printf("Delay of #%dst value: ", i + 1);
                    scanf("%d", &delarray[i]);
            }
            outp(0x43, pthold | 0xB6);
            
            for(i = 0; i < amount; i++)
            {
            timer(custh[i], custl[i]);
            speaker(sndhold, delarray[i]);
            }
    }
    But it gave me the same problem I had before this is what it looks like when I run it:


    Music Program
    --------------------------------------

    1. Play Happy Birthday
    2. Enter your own notes


    --------------------------------------

    Enter selection please: 2
    Please enter amount of notes: 2
    Please enter the high, low values for the notes and the delay:
    High value #1: CA
    Low value #1: Delay of #1st value: High value #2: Low value #2: Delay of #2st va
    lue:


    As you can see after I enter the high value it just runs through the for loop without letting me enter values for the low and delay and then it either returns me to the command prompt

  12. #12
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Also, you're allocating memory and then deleting it before you use it!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic arrays
    By s.nelson64 in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2008, 06:27 AM
  2. Seg Fault AND 2d dynamic arrays
    By lord in forum C++ Programming
    Replies: 3
    Last Post: 08-04-2008, 01:07 PM
  3. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  4. dynamic arrays
    By sokermaniac in forum C++ Programming
    Replies: 34
    Last Post: 05-12-2008, 12:41 PM
  5. Replies: 6
    Last Post: 11-28-2004, 11:01 AM