Go to Post Wow, possibly a HPJIE*? How cool would that be? *High Paying Job In Electronics. - DonRotolo [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #13   Spotlight this post!  
Unread 22-03-2012, 15:41
ctccromer's Avatar
ctccromer ctccromer is offline
Animus
AKA: Tucker Campbell
FRC #3753 (Bulah Bots)
Team Role: Alumni
 
Join Date: Jan 2012
Rookie Year: 2012
Location: Baton Rouge, Louisiana
Posts: 190
ctccromer is a name known to allctccromer is a name known to allctccromer is a name known to allctccromer is a name known to allctccromer is a name known to allctccromer is a name known to all
Re: C++ help: Understanding pointers?

Okay so after reading all of this and getting (what I think is) a better understanding of pointers, I attempted my book's chapter's next practice problem:
Quote:
Write a function that takes 3 arguments, a length, width and height, dynamically allocates a 3-dimensional array with those values and fills the 3-dimensional array with multiplication tables. Make sure to free the array when you are done.
I'm just filling it with an incrementing value rather than multiplication tables

Here's the code I wrote. It has 2 main errors that repeat many times that the compiler picked up and I wouldn't be surprised if there were several more. It looks like a lot, but you'll quickly realize that it's very small, quick, and (hopefully) simple so please don't be put off by its size.
So what's wrong with this and how can I fix it?
Please ignore the fact that I made it a 3x? 2dimensional array instead of a 3dimensional array. I noticed that towards the end of writing this but that's not the main focus of the exercise so I'll fix that later.
Code:
//get 3 user-input values and make a 3dimensional array with those dimensions.  Allocate that much memory.

#include <iostream>
using namespace std;

void setValues(int *a, int *b, int *c, int *arrayOne[], int *arrayTwo[], int *arrayThree[]);
void showValues(int *a, int *b, int *c, int *arrayOne[], int *arrayTwo[], int *arrayThree[]);

int main()
{
    int firstNumber;
    int *p_firstNumber = & firstNumber;
    int secondNumber;
    int *p_secondNumber = & secondNumber;
    int thirdNumber;
    int *p_thirdNumber = & thirdNumber;

    //get the 3 sizes
    cout << "Give me a length, width, and height!\n";
    cout << "Length: "; cin >> firstNumber;
    cout << "Width: "; cin >> secondNumber;
    cout << "Height: "; cin >> thirdNumber;

    //create the pointer version of the 3dimensional array
    int **p_p_fullArray = new int[3];
    int *p_firstArray = new int[firstNumber];
    int *p_secondArray = new int[secondNumber];
    int *p_thirdArray = new int[thirdNumber];

    //set the 3 1dimensional arrays to the full array
    p_p_fullArray[0] = & p_firstArray;
    p_p_fullArray[1] = & p_secondArray;
    p_p_fullArray[2] = & p_thirdArray;

    //create normal variables for the 3dimensional array
    int firstArray[firstNumber];
    int secondArray[secondNumber];
    int thirdArray[thirdNumber];

    //set the 3 1dimensional pointers to the 3 1dimensional variables
    p_firstArray = & firstArray;
    p_secondArray = & secondArray;
    p_thirdArray = & thirdArray;

    //assign something to every cell in the array
    setValues(int *p_firstNumber, int *p_secondNumber, int *p_thirdNumber, int *p_firstArray[], int *p_secondArray[], int *p_thirdArray[]);

    //show the array (easy way to make sure the program worked)
    cout << "\nCool thanks.  Here's your array:" << endl;
    showValues(int *p_firstNumber, int *p_secondNumber, int *p_thirdNumber, int *p_firstArray[], int *p_secondArray[], int *p_thirdArray[]);

    //free the allocated memory and make sure the pointers don't grab on to anything random and cause issues
    delete p_p_fullArray;
    p_p_fullArray = NULL;
    delete p_firstArray;
    p_firstArray = NULL;
    delete p_secondArray;
    p_secondArray = NULL;
    delete p_thirdArray;
    p_thirdArray = NULL;
    delete p_firstNumber;
    p_firstNumber = NULL;
    delete p_secondNumber;
    p_secondNumber = NULL;
    delete p_thirdNumber;
    p_thirdNumber = NULL;
}

void setValues(int *a, int *b, int *c, int *arrayOne[], int *arrayTwo[], int *arrayThree[])
{
    int value = 0;

    for (int i = 0; i < a; i++)
    {
        *arrayOne[i] = value;
        ++value;
    }

    for (int j = 0; j < b; j++)
    {
        *arrayTwo[j] = value;
        ++value;
    }

    for (int k = 0; k < c; k++)
    {
        *arrayThree[k] = value;
        ++value;
    }
}

void showValues(int *a, int *b, int *c, int *arrayOne[], int *arrayTwo[], int *arrayThree[])
{
    cout << "first row: ";
    for (int i = 0; i < a; i++)
    {
        cout << arrayOne[i] << " ";
    }

    cout << "\nsecond row: ";
    for (int j = 0; j < b; j++)
    {
        cout << arrayTwo[j] << " ";
    }

    cout << "\nthird row: ";
    for (int k = 0; k < c; k++)
    {
        cout << arrayThree[k] << " ";
    }
}
__________________
Team 3753 BulahBots
 


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 01:46.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi