Thread: In need of help boys/girls.

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    15

    In need of help boys/girls.

    Write a program that defines an array of 25 right Triang pointers, and then use Malloc to allocate 25 rightTriangle objects who pointters are then assigned to each of the 25 array elements. NOw, make each triangle a mulitple of a 3-4-5 triangle. The first triangle should have l=3, w=4, h=5. Each succeeding triangle should have dimensions a factor of two bigger than the last one.. Set the colors all the same. Now write a loop that will print each Triangle's description to the screen.


    ok that is the problem i'm having. This is what i've came up with.. I'm having the most trouble with Malloc.

    Header file is this.

    Code:
    #include <iostream>
    
    using namespace std;
    
    typedef struct
    {
        int l, w, h;
        char color[10];
    } rightTriangle;
    and my main.cpp file

    Code:
    #include "prob2.h"
    
    void main()
    {
        int i;
        
        rightTriangle triangle[25];
    
    
        triangle[0].l = 3;
        triangle[0].w = 4;
        triangle[0].h = 5;
        for (i=1; i<25; i++)
        {
            triangle[i].l = triangle[i - 1].l * 2;
            triangle[i].w = triangle[i - 1].w * 2;
            triangle[i].h = triangle[i - 1].h * 2;
        }
    
        for (i=0; i<25; i++)
        {
            cout << i << " l: " << triangle[i].l << " w: " << triangle[i].w << " h: " << triangle[i].h << endl;
    
        }
    }
    Please help me

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    82
    When you say "Then use Malloc," is that strictly necessary for the code, or is it just the only way you know to dynamically allocate?

    In C++ 'new' is greatly preferred.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    15
    unfortunately i know that new is preferred.. Although this assignment would like me to really use malloc in this program. I just can't figure it out.

  4. #4
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Figure out what? Be specific, you barely have done anything with regards to your assignment and nobody will do your homework for you.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    15
    I have allocated the triangle 25 . I dont know how to use Malloc really at all. Doing the triangle section was an easy step. Malloc is what is getting mu stumped.

  6. #6
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    The prototype for malloc() is:
    Code:
    void* malloc(size_t size);
    All malloc() does is allocate size bytes and returns a pointer of type void to that memory, or NULL if an error occured. So you are probably asking, well that’s great but how do I use it? Well the standard convention for using malloc is:
    Code:
    pointer = malloc(n * sizeof(*pointer));
    where n is the number of elements you need and *pointer (the dereferenced pointer) is used to determine the size of the individual element. So if I wanted to create 1 new node I would do it like so:
    Code:
    //allocate space for our node
    newNode = malloc(1 * sizeof(*newNode));
    //check for error
    if(newNode == NULL){
         printf("Error allocating memory");
         return;
    }
    *Note, the ISO standard dictates there is no need to cast the return from malloc(), however some compilers may complain about being unable to convert the types. If this happens you can still use malloc, simply cast the return*


    Here is a FAQ casting malloc

    Let me know if you need more info or explaination.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > rightTriangle triangle[25];

    All you need to do now is
    Code:
        rightTriangle *triangle = (rightTriangle)malloc( 25 * sizeof *triangle );
    Yes, you need the cast if you insist on using malloc in C++.

    This is better, tell your tutor.
    Code:
        rightTriangle *triangle = new rightTriangle[25];
    Also tell your tutor that main returns an int.
    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.

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    15
    Quote Originally Posted by Salem
    > rightTriangle triangle[25];

    All you need to do now is
    Code:
        rightTriangle *triangle = (rightTriangle)malloc( 25 * sizeof *triangle );
    you forgot (rightTriangle*)malloc =).. figured that out right after this.

    thank you very much for helping me with this. Everytime that I come to these boards, I get the help that I need to advance my study. Thank you again for your help. Until we visit again.

Popular pages Recent additions subscribe to a feed